Advertisement
Guest User

Awesome Generic Makefile

a guest
May 14th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 4.50 KB | None | 0 0
  1.  
  2.  
  3. CFLAGS =
  4. CXXFLAGS = -Wpedantic -Wall -Wextra -g -std=c++11
  5. CPPFLAGS = -DSFML
  6. LDFLAGS = -static-libstdc++
  7. LDLIBS =
  8.  
  9. # source directory
  10. SRC := src
  11.  
  12. # Where to find additional c++ source and header files in subdirectories
  13. # of SRC. Make sure to use the $(SLASH) variable for the directory seperator if
  14. # a subdirectory is included.
  15. # example: cppdirs = dir$(SLASH)nextdir
  16. cppdirs = entities util entities$(SLASH)weapons
  17.  
  18. # a list of headers to pre-compile
  19. # example pchfiles = pch.h stdpch.h
  20. pchfiles =
  21.  
  22. # output directory
  23. BUILD := build
  24.  
  25. # name of output program
  26. program = a
  27.  
  28.  
  29. # autodetect os
  30. OSTARGET ?= UNKNOWN
  31.  
  32. ifeq ($(OSTARGET),UNKNOWN)
  33.     ifeq ($(OS),Windows_NT)
  34.         OSTARGET := WINDOWS
  35.     else
  36.         UNAME := $(shell uname -s)
  37.         ifeq ($(UNAME),Linux)
  38.             OSTARGET := LINUX
  39.         endif
  40.         ifeq ($(UNAME),Darwin)
  41.             OSTARGET := MACOSX
  42.         endif
  43.     endif
  44. endif
  45.  
  46. ifeq ($(OSTARGET),LINUX)
  47.     # OS specific options
  48.     CFLAGS +=
  49.     CXXFLAGS += -I/usr/include $(addprefix -I,$(INCDIRS))
  50.     CPPFLAGS +=
  51.     LDFLAGS += -L/usr/lib
  52.     LDLIBS +=
  53.     RM := rm -f
  54.     RMDIR := rm -rf
  55.     MKDIR := mkdir -p
  56.     SLASH = /
  57.     CP := cp
  58.     PREFIX ?= /usr/local
  59.  
  60.     # compiler/linker programs
  61.     CC := gcc
  62.     CXX := g++
  63.     CPP := g++
  64.     LD := g++
  65.  
  66.     # extension of output program
  67.     #program +=
  68. endif
  69. ifeq ($(OSTARGET),MACOSX)
  70.     # OS specific options
  71.     CFLAGS +=
  72.     CXXFLAGS += -I/usr/include $(addprefix -I,$(INCDIRS))
  73.     CPPFLAGS +=
  74.     LDFLAGS += -L/usr/lib
  75.     LDLIBS +=
  76.     RM := rm -f
  77.     RMDIR := rm -rf
  78.     MKDIR := mkdir -p
  79.     CP := cp
  80.     SLASH = /
  81.     PREFIX ?= /usr/local
  82.  
  83.     # compiler/linker programs
  84.     CC := gcc
  85.     CXX := g++
  86.     CPP := g++
  87.     LD := g++
  88.  
  89.     # extension of output program
  90.     #program +=
  91. endif
  92. ifeq ($(OSTARGET),WINDOWS)
  93.     # OS specific options
  94.     CFLAGS +=
  95.     CXXFLAGS += -Iinclude $(addprefix -I,$(INCDIRS))
  96.     CPPFLAGS +=
  97.     LDFLAGS += -Llib
  98.     LDLIBS += -lmingw32
  99.     RM := del /F/Q
  100.     RMDIR := rmdir /S/Q
  101.     MKDIR := mkdir
  102.     CP := robocopy
  103.     SLASH = \\
  104.     PREFIX ?=
  105.  
  106.     # compiler/linker programs
  107.     CC := gcc
  108.     CXX := g++
  109.     CPP := g++
  110.     LD := g++
  111.  
  112.     # extension of output program
  113.     program := $(addsuffix .exe,$(program))
  114. endif
  115.  
  116. INCDIRS = $(SRC) $(addprefix $(SRC)/,$(cppdirs))
  117. VPATH = $(INCDIRS)
  118. cppsrc = $(wildcard $(SRC)/*.cpp $(addsuffix /*.cpp,$(INCDIRS)))
  119. objects = $(patsubst $(SRC)/%.o,$(BUILD)/%.o,$(cppsrc:.cpp=.o))
  120. depends = $(objects:.o=.d)
  121. gchfiles = $(addsuffix .gch,$(pchfiles))
  122. DESTDIR =
  123.  
  124. all: $(BUILD)/$(program)
  125.     @echo build complete!
  126.  
  127. # build single file
  128. single: $(BUILD)/$(in).o
  129.  
  130. $(BUILD)/$(program): $(objects)
  131.     @$(LD) -o $@ $^ $(LDFLAGS) $(LDLIBS)
  132.     @echo linking $^ into $@ using these libraries $(LDLIBS)
  133.  
  134. $(objects): $(gchfiles)
  135.  
  136. $(BUILD)/%.o: %.cpp
  137.     @$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
  138.     @echo compiling $< to $@ with $(CXX)
  139.  
  140. $(gchfiles): $(pchfiles)
  141.     @$(CXX) $(CPPFLAGS) $(CXXFLAGS) $<
  142.     @echo precompiling header $< to $@ with $(CXX)
  143.  
  144. $(objects) $(depends) $(gchfiles): | $(BUILD)
  145.  
  146. $(BUILD):
  147.     @$(MKDIR) $(BUILD) $(addprefix $(BUILD)$(SLASH),$(cppdirs))
  148.     @echo creating directories
  149.  
  150. # rule to generate a dependency file
  151. $(BUILD)/%.d: %.cpp
  152.     @$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
  153.     @echo generating dependencies for $<
  154.  
  155. # include all dependency files in the makefile
  156. -include $(depends)
  157.  
  158. .PHONY: clean install uninstall
  159. clean:
  160. #   $(RM) $(subst /,$(SLASH),$(objects)) $(subst /,$(SLASH),$(depends)) $(subst /,$(SLASH),$(BUILD)/$(program))
  161.     @echo cleaning...
  162.     @$(RMDIR) $(BUILD)
  163.     @echo done.
  164.  
  165. install: $(BUILD)/$(program)
  166. ifeq ($(OSTARGET),WINDOWS)
  167.     -$(CP) /S $(BUILD) $(DESTDIR)$(PREFIX)bin $(program)
  168.     -$(CP) /S data $(DESTDIR)$(PREFIX)bin$(SLASH)data
  169.     -$(CP) ./ $(DESTDIR)$(PREFIX)bin *.dll
  170. else
  171.     $(MKDIR) $(DESTDIR)$(PREFIX)/bin/$(program)
  172.     $(CP) $< $(DESTDIR)$(PREFIX)/bin/$(program)
  173.     $(CP) -r ./data $(DESTDIR)$(PREFIX)/bin/$(program)
  174. endif
  175.  
  176. uninstall:
  177. ifeq ($(OSTARGET), WINDOWS)
  178.     $(RMDIR) $(DESTDIR)$(PREFIX)/bin
  179. else
  180.     $(RMDIR) $(DESTDIR)$(PREFIX)$(SLASH)bin$(SLASH)$(program)
  181. endif
  182.  
  183. help:
  184.     @echo Commands:
  185.     @echo make all ---------------------------------- builds the program and puts all the output files in a directory called build
  186.     @echo make single in="<path-to-file/file>" ------ builds only a single file (omit the .cpp or .c extension and ignore the quotes as well)
  187.     @echo make install ------------------------------ builds the program just like make all and installs the final files to a directory
  188.     @echo make uninstall ---------------------------- will remove all the files from the install directory
  189.     @echo make clean -------------------------------- will remove the build directory
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement