Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 2.48 KB | None | 0 0
  1. # Ugh, this is needed by some rules below.
  2. .SECONDEXPANSION:
  3.  
  4. # All source files.
  5. SOURCES = $(foreach dir,$(SOURCE_DIRS),$(call rwildcard, $(dir), *.c)$(call rwildcard, $(dir), *.cpp))
  6.  
  7. # All object files.
  8. OBJECTS = $(patsubst %,$(OBJECT_DIR)/%,$(patsubst %.c,%.c.o,$(patsubst %.cpp,%.cpp.o,$(SOURCES))))
  9.  
  10. # Dependency lists
  11. DEP_FILES = $(patsubst %.o,%.d,$(OBJECTS))
  12.  
  13. .PHONY: foo
  14. foo:
  15.     @echo $(DEP_FILES)
  16.  
  17. # Fancy alias for the primary target.
  18. .PHONY: build
  19. build: $(OUTPUT_FILE)
  20.        
  21. # The target that actually builds the executable.
  22. # I'm not sure why we need `.SECONDEXPANSION:` and `$$` before `(OBJECTS)`, but it's definitely necessary.
  23. # Note that object files come before linker flags.
  24. $(OUTPUT_FILE): $$(OBJECTS)
  25.     @echo [Linking] $(OUTPUT_FILE)
  26.     @$(CXX) $(OBJECTS) $(LDFLAGS) -o $@
  27.     @echo [Done]
  28.  
  29. # Targets that compile source files.   
  30. # Note that flags come before the source file.
  31. $(OBJECT_DIR)/%.c.o: %.c $(dir $@)
  32.     @echo [C] $<
  33.     $(call mkdir,$(dir $@))
  34.     @$(CC) -MMD -MP $(CFLAGS) $< -c -o $@
  35. $(OBJECT_DIR)/%.cpp.o: %.cpp $(dir $@)
  36.     @echo [C++] $<
  37.     $(call mkdir,$(dir $@))
  38.     @$(CXX) -MMD -MP $(CXXFLAGS) $< -c -o $@
  39.    
  40. # Target that cleans the build.
  41. .PHONY: clean
  42. clean:
  43.     $(call rmdir,$(OBJECT_DIR))
  44.     $(call rmfile,$(OUTPUT_FILE))
  45.  
  46. # FUNCTIONS
  47.    
  48. # Recursive wildcard function.
  49. # Source: https://stackoverflow.com/a/18258352/2752075
  50. # Recursively searches a directory for all files matching a pattern.
  51. # The first parameter is a directory, the second is a pattern.
  52. # Don't use `.` as a directory, or else it'll search your entire drive! (Huh.) Instead use an empty parameter.
  53. # Example usage: SOURCES = $(call rwildcard, src, *.cpp)
  54. rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)$(filter $(subst *,%,$2),$d))
  55.  
  56. # OS-specific functions
  57. # `-` at the beginning makes `make` ignore errors when executing shell commands.
  58. # `|| cd .` forces error code to be 0, silencing errors. Unlike `-` it prints no warnings.
  59. # Example usage: $(call rmfile, bin/out.exe)
  60. HOST_OS = linux
  61. ifneq ($(OS), Windows_NT)
  62. HOST_OS = linux
  63. else ifndef MSYSTEM
  64. HOST_OS = windows
  65. else
  66. HOST_OS = linux
  67. endif
  68.  
  69. ifeq ($(HOST_OS), windows)
  70. SILENT = >NUL 2>NUL || cd .
  71. rmfile = @del /F /Q $(subst /,\,$1) $(SILENT)
  72. rmdir = @rd /S /Q $(subst /,\,$1) $(SILENT)
  73. mkdir = @mkdir >NUL 2>NUL $(subst /,\,$1) $(SILENT)
  74. else
  75. SILENT = >/dev/null 2>/dev/null || cd .
  76. rmfile = @del /F /Q $1 $(SILENT)
  77. rmdir = @rm -rf $1 $(SILENT)
  78. mkdir = @mkdir -p $1 $(SILENT)
  79. endif
  80.  
  81. -include $(DEP_FILES)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement