Makefile 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #
  2. # Makefile
  3. #
  4. # ---------- Base Defines ----------
  5. ROOTPATH := $(shell git rev-parse --show-toplevel)
  6. COMMANDLINE := $(shell ps -o args= $$PPID)
  7. # ---------- Architectures ----------
  8. ARCHS := $(shell ls $(ROOTPATH)/Makefiles/Makefile.conf.* | grep -v base | sed 's/.*Makefile\.conf\.//g')
  9. ARCHS += any
  10. -include $(ROOTPATH)/Makefiles/Makefile.aliases
  11. ARCHS := $(filter-out any,$(ARCHS))
  12. ifeq (,$(findstring -S,$(COMMANDLINE)))
  13. ifneq ($(ARCH),)
  14. $(info ARCH: $(ARCH))
  15. endif
  16. endif
  17. # ---------- Defines ----------
  18. DATETIME := `date +'%y%m%d-%H%M'`
  19. MAKEFILEPATH := $(strip $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))))
  20. PID := $(shell echo $$$$)
  21. PREFIX = nice -n 19 /usr/bin/time --format='%E (%U)' -o /dev/shm/t-$(PID)
  22. POSTFIX = && cat /dev/shm/t-$(PID) | xargs -I "%" echo -en '\033[1A\033[100D\033[60C' "%\n" && rm -f /dev/shm/t-$(PID)
  23. ARNAME = $(notdir $(CURDIR)).a.$(ARCH)_
  24. SUBDIRS := $(wildcard */.)
  25. SUBDIRS := $(foreach dir,$(SUBDIRS),$(subst /.,,$(dir)))
  26. FILTER = crash% doc% include% lib% Libraries% Makefiles%
  27. SUBDIRS := $(filter-out $(FILTER),$(SUBDIRS))
  28. SUBDIRARS = $(foreach dir,$(SUBDIRS),$(dir)/$(dir).a)
  29. SUBDIRS := $(foreach dir,$(SUBDIRS),$(dir)_DIRECTORY)
  30. LOCALSOURCES = $(wildcard *.cpp *.cc)
  31. LOCALOBJECTS := $(LOCALSOURCES:.cpp=.o.$(ARCH))
  32. LOCALOBJECTS := $(LOCALOBJECTS:.cc=.o.$(ARCH))
  33. LOCALDEPENDS = $(LOCALOBJECTS:.o.$(ARCH)=.d.$(ARCH))
  34. SOURCES = $(wildcard *.cpp */*.cpp */*/*.cpp */*/*/*.cpp)
  35. FILTER = Libraries%
  36. SOURCES := $(filter-out $(FILTER),$(SOURCES))
  37. OBJECTS = $(SOURCES:.cpp=.o.$(ARCH))
  38. DEPENDS = $(OBJECTS:.o.$(ARCH)=.d.$(ARCH))
  39. LIBRARIES =
  40. LIBFILES =
  41. TARGETS =
  42. # ---------- Recursive Targets ----------
  43. $(LOCALOBJECTS): | $(SUBDIRS)
  44. $(OBJECTS): | $(SUBDIRS)
  45. artifacts: $(LOCALOBJECTS)
  46. .DEFAULT_GOAL := artifacts
  47. # ---------- Includes ----------
  48. -include $(ROOTPATH)/Makefiles/Makefile.conf.$(ARCH)
  49. -include $(ROOTPATH)/Makefiles/Makefile.conf.base
  50. -include $(ROOTPATH)/Makefile.conf
  51. # ---------- Libraries ----------
  52. define build_target_library_arch
  53. $(eval $@_DATETIME := $(DATETIME))
  54. @echo -n " [AR] $(1)\n"
  55. @$(PREFIX) $(AR) -crs $(1) $(2) $(POSTFIX)
  56. @mkdir -p $(ROOTPATH)/lib/$(ARCH)
  57. @mv $(1) $$(echo $(ROOTPATH)/lib/$(ARCH)/lib$(1) | sed 's/\.$(ARCH)//g')
  58. endef
  59. define add_library
  60. LIBFILES += $(ROOTPATH)/Libraries/$(1)/lib/$(ARCH)/lib$(1).a
  61. LFLAGS += -l$(1)
  62. LFLAGS += -L$(ROOTPATH)/Libraries/$(1)/lib/$(ARCH)
  63. CFLAGS += -I$(ROOTPATH)/Libraries/$(1)/include
  64. endef
  65. $(foreach library,$(LIBRARIES),$(eval $(call add_library,$(library))))
  66. # ---------- Targets ----------
  67. define build_target_arch
  68. $(eval $@_DATETIME := $(DATETIME))
  69. @echo -n " [LD] $(1)\n"
  70. @$(PREFIX) $(CC) -o $(1) $(2) $(LFLAGS) $(CFLAGS) $(POSTFIX)
  71. @if [ -d "$(DEBUGDIR)" ]; then \
  72. $(OBJCOPY) --only-keep-debug $(1) $(DEBUGDIR)/$(1)-$($@_DATETIME).debug; \
  73. $(STRIP) $(SFLAGS) $(1); \
  74. $(OBJCOPY) --add-gnu-debuglink="$(DEBUGDIR)/$(1)-$($@_DATETIME).debug" $(1); \
  75. chmod -x $(DEBUGDIR)/$(1)-$($@_DATETIME).debug; \
  76. fi
  77. endef
  78. define build_target
  79. @if [ "$(ARCH)" = "any" ]; then \
  80. for arch in $(ARCHS); do \
  81. echo "ARCH: $$arch"; \
  82. $(MAKE) -s -S $(1) $$arch; \
  83. done; \
  84. true; \
  85. else \
  86. $(MAKE) -s -S $(1).$(ARCH) $(ARCH); \
  87. fi
  88. endef
  89. define add_target_arch
  90. $(1).$(ARCH): $$(OBJECTS) $$(LIBFILES) Application/$(strip $(1)).o.$(ARCH)
  91. $$(call build_target_arch,$$@,$$^)
  92. endef
  93. define add_target
  94. ifeq ($(.DEFAULT_GOAL),artifacts)
  95. .DEFAULT_GOAL := $(1)
  96. endif
  97. $(eval $(call add_target_arch, $(1)))
  98. $(1):
  99. $$(call build_target,$$@)
  100. endef
  101. $(foreach target,$(TARGETS),$(eval $(call add_target,$(target))))
  102. ifneq ($(MAKECMDGOALS),clean)
  103. -include $(LOCALDEPENDS)
  104. endif
  105. # ---------- Object Rules ----------
  106. %.d.$(ARCH): %.cpp
  107. @if [ "$(ARCH)" != "any" ]; then \
  108. depth=$(MAKELEVEL); \
  109. while [ $${depth} -gt 0 ] ; do \
  110. echo -n " "; \
  111. depth=`expr $$depth - 1`; \
  112. done; \
  113. true; \
  114. echo -n " [MM] $@\n"; \
  115. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  116. fi
  117. %.o.$(ARCH): %.cpp
  118. @depth=$(MAKELEVEL); \
  119. while [ $${depth} -gt 0 ] ; do \
  120. echo -n " "; \
  121. depth=`expr $$depth - 1`; \
  122. done; \
  123. true
  124. @echo -n " [CC] $@\n"
  125. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  126. %.d.$(ARCH): %.cc
  127. @if [ "$(ARCH)" != "any" ]; then \
  128. depth=$(MAKELEVEL); \
  129. while [ $${depth} -gt 0 ] ; do \
  130. echo -n " "; \
  131. depth=`expr $$depth - 1`; \
  132. done; \
  133. true; \
  134. echo -n " [MM] $@\n"; \
  135. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  136. fi
  137. %.o.$(ARCH): %.cc
  138. @depth=$(MAKELEVEL); \
  139. while [ $${depth} -gt 0 ] ; do \
  140. echo -n " "; \
  141. depth=`expr $$depth - 1`; \
  142. done; \
  143. true
  144. @echo -n " [CC] $@\n"
  145. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  146. $(ARNAME): $(SUBDIRS) $(LOCALOBJECTS)
  147. @depth=$(MAKELEVEL); \
  148. while [ $${depth} -gt 0 ] ; do \
  149. echo -n " "; \
  150. depth=`expr $$depth - 1`; \
  151. done; \
  152. true
  153. @echo -n " [AR] $@\n"
  154. @$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX)
  155. # ---------- Recursive Rules ----------
  156. .PHONY: $(SUBDIRS)
  157. $(SUBDIRS):
  158. $(eval DIR := $(subst _DIRECTORY,,$@))
  159. @if [ -e "$(DIR)/Makefile" ]; then \
  160. depth=$(MAKELEVEL); \
  161. while [ $${depth} -gt 0 ] ; do \
  162. echo -n " "; \
  163. depth=`expr $$depth - 1`; \
  164. done; \
  165. true; \
  166. echo " <$(DIR)>"; \
  167. if ($(MAKE) -s -S -C $(DIR) artifacts $(ARCH)); then \
  168. :; \
  169. else \
  170. exit $$?; \
  171. fi; \
  172. depth=$(MAKELEVEL); \
  173. while [ $${depth} -gt 0 ] ; do \
  174. echo -n " "; \
  175. depth=`expr $$depth - 1`; \
  176. done; \
  177. true; \
  178. echo " </$(DIR)>"; \
  179. fi
  180. # ---------- Clean Rules ----------
  181. .PHONY: clean
  182. clean:
  183. @if [ "$(ARCH)" = "any" ]; then \
  184. for arch in $(ARCHS); do \
  185. echo "ARCH: $$arch"; \
  186. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  187. done; \
  188. true; \
  189. else \
  190. for dir in $(SUBDIRS); do \
  191. dir=$${dir%_DIRECTORY}; \
  192. if [ -e "$$dir/Makefile" ]; then \
  193. depth=$(MAKELEVEL); \
  194. while [ $${depth} -gt 0 ] ; do \
  195. echo -n " "; \
  196. depth=`expr $$depth - 1`; \
  197. done; \
  198. echo " <$$dir>"; \
  199. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  200. while [ $${depth} -gt 0 ] ; do \
  201. echo -n " "; \
  202. depth=`expr $$depth - 1`; \
  203. done; \
  204. echo " </$$dir>"; \
  205. fi \
  206. done; \
  207. for target in $(TARGETS); do \
  208. rm -f $$target.$(ARCH); \
  209. done; \
  210. rm -f lib/$(ARCH)/*; \
  211. true; \
  212. fi
  213. @rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) || true
  214. .PHONY: cleandeps
  215. cleandeps:
  216. @if [ "$(ARCH)" = "any" ]; then \
  217. for arch in $(ARCHS); do \
  218. echo "ARCH: $$arch"; \
  219. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  220. done; \
  221. true; \
  222. else \
  223. for dir in $(SUBDIRS); do \
  224. dir=$${dir%_DIRECTORY}; \
  225. if [ -e "$$dir/Makefile" ]; then \
  226. depth=$(MAKELEVEL); \
  227. while [ $${depth} -gt 0 ] ; do \
  228. echo -n " "; \
  229. depth=`expr $$depth - 1`; \
  230. done; \
  231. echo " <$$dir>"; \
  232. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  233. while [ $${depth} -gt 0 ] ; do \
  234. echo -n " "; \
  235. depth=`expr $$depth - 1`; \
  236. done; \
  237. echo " </$$dir>"; \
  238. fi \
  239. done; \
  240. for target in $(TARGETS); do \
  241. rm -f $$target.$(ARCH); \
  242. done; \
  243. true; \
  244. fi
  245. @rm -f $(LOCALDEPENDS) || true
  246. # ---------- Architecture Rules ----------
  247. .PHONY: $(ARCH)
  248. $(ARCH):
  249. @if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \
  250. if [ "$(ARCH)" = "any" ]; then \
  251. for arch in $(ARCHS); do \
  252. echo "ARCH: $$arch"; \
  253. $(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \
  254. done; \
  255. true; \
  256. else \
  257. $(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \
  258. fi \
  259. fi
  260. # ---------- All Rule ----------
  261. .PHONY: all
  262. all: $(TARGETS)