Makefile 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. REALOBJECTS := $(SOURCES:.cpp=.o.$(ARCH))
  38. DEPENDS = $(REALOBJECTS:.o.$(ARCH)=.d.$(ARCH))
  39. OBJECTS := RECURSIVE_PHONY
  40. OBJECTS += $(REALOBJECTS)
  41. LIBRARIES =
  42. LIBFILES =
  43. TARGETS =
  44. # ---------- Recursive Targets ----------
  45. .PHONY: RECURSIVE_PHONY
  46. $(LOCALOBJECTS): | $(SUBDIRS)
  47. $(OBJECTS): | $(SUBDIRS)
  48. artifacts: $(LOCALOBJECTS)
  49. .DEFAULT_GOAL := artifacts
  50. # ---------- Includes ----------
  51. -include $(ROOTPATH)/Makefiles/Makefile.conf.$(ARCH)
  52. -include $(ROOTPATH)/Makefiles/Makefile.conf.base
  53. -include $(ROOTPATH)/Makefile.conf
  54. # ---------- Libraries ----------
  55. define build_target_library_arch
  56. $(eval $@_DATETIME := $(DATETIME))
  57. @echo -n " [AR] $(1)\n"
  58. @$(PREFIX) $(AR) -crs $(1) $(2) $(POSTFIX)
  59. @mkdir -p $(ROOTPATH)/lib/$(ARCH)
  60. @mv $(1) $$(echo $(ROOTPATH)/lib/$(ARCH)/lib$(1) | sed 's/\.$(ARCH)//g')
  61. endef
  62. define add_target_library_arch
  63. $(1).$(ARCH): $$(OBJECTS) $$(LIBFILES)
  64. $$(call build_target_library_arch,$$@,$$(REALOBJECTS) $$(LIBFILES))
  65. endef
  66. define add_library
  67. LIBFILES += $(ROOTPATH)/Libraries/$(1)/lib/$(ARCH)/lib$(1).a
  68. LFLAGS += -l$(1)
  69. LFLAGS += -L$(ROOTPATH)/Libraries/$(1)/lib/$(ARCH)
  70. CFLAGS += -I$(ROOTPATH)/Libraries/$(1)/include
  71. endef
  72. $(foreach library,$(LIBRARIES),$(eval $(call add_library,$(library))))
  73. # ---------- Targets ----------
  74. define build_target_arch
  75. $(eval $@_DATETIME := $(DATETIME))
  76. @echo -n " [LD] $(1)\n"
  77. @$(PREFIX) $(CC) -o $(1) $(2) $(LFLAGS) $(CFLAGS) $(POSTFIX)
  78. @if [ -d "$(DEBUGDIR)" ]; then \
  79. $(OBJCOPY) --only-keep-debug $(1) $(DEBUGDIR)/$(1)-$($@_DATETIME).debug; \
  80. $(STRIP) $(SFLAGS) $(1); \
  81. $(OBJCOPY) --add-gnu-debuglink="$(DEBUGDIR)/$(1)-$($@_DATETIME).debug" $(1); \
  82. chmod -x $(DEBUGDIR)/$(1)-$($@_DATETIME).debug; \
  83. fi
  84. endef
  85. define build_target
  86. @if [ "$(ARCH)" = "any" ]; then \
  87. for arch in $(ARCHS); do \
  88. echo "ARCH: $$arch"; \
  89. $(MAKE) -s -S $(1) $$arch; \
  90. done; \
  91. true; \
  92. else \
  93. $(MAKE) -s -S $(1).$(ARCH) $(ARCH); \
  94. fi
  95. endef
  96. define add_target_test_arch
  97. $(1).$(ARCH): $$(OBJECTS) $$(LIBFILES) Test/Test.o.$(ARCH)
  98. $$(call build_target_arch,$$@,$$(REALOBJECTS) $$(LIBFILES) Test/Test.o.$(ARCH))
  99. endef
  100. define add_target_arch
  101. $(1).$(ARCH): $$(OBJECTS) $$(LIBFILES) Application/$(strip $(1)).o.$(ARCH)
  102. $$(call build_target_arch,$$@,$$(REALOBJECTS) $$(LIBFILES) Application/$(strip $(1)).o.$(ARCH))
  103. endef
  104. define add_target
  105. ifeq ($(.DEFAULT_GOAL),artifacts)
  106. .DEFAULT_GOAL := $(1)
  107. endif
  108. ifneq (,$$(findstring .a,$(1)))
  109. $$(eval $$(call add_target_library_arch, $(1)))
  110. else
  111. ifeq ($(1),test)
  112. $$(eval $$(call add_target_test_arch, $(1)))
  113. else
  114. $$(eval $$(call add_target_arch, $(1)))
  115. endif
  116. endif
  117. $(1):
  118. $$(call build_target,$$@)
  119. endef
  120. $(foreach target,$(TARGETS),$(eval $(call add_target,$(target))))
  121. ifneq ($(MAKECMDGOALS),clean)
  122. -include $(LOCALDEPENDS)
  123. endif
  124. # ---------- Object Rules ----------
  125. %.d.$(ARCH): %.cpp
  126. @if [ "$(ARCH)" != "any" ]; then \
  127. depth=$(MAKELEVEL); \
  128. while [ $${depth} -gt 0 ] ; do \
  129. echo -n " "; \
  130. depth=`expr $$depth - 1`; \
  131. done; \
  132. true; \
  133. echo -n " [MM] $@\n"; \
  134. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  135. fi
  136. %.o.$(ARCH): %.cpp
  137. @depth=$(MAKELEVEL); \
  138. while [ $${depth} -gt 0 ] ; do \
  139. echo -n " "; \
  140. depth=`expr $$depth - 1`; \
  141. done; \
  142. true
  143. @echo -n " [CC] $@\n"
  144. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  145. %.d.$(ARCH): %.cc
  146. @if [ "$(ARCH)" != "any" ]; then \
  147. depth=$(MAKELEVEL); \
  148. while [ $${depth} -gt 0 ] ; do \
  149. echo -n " "; \
  150. depth=`expr $$depth - 1`; \
  151. done; \
  152. true; \
  153. echo -n " [MM] $@\n"; \
  154. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  155. fi
  156. %.o.$(ARCH): %.cc
  157. @depth=$(MAKELEVEL); \
  158. while [ $${depth} -gt 0 ] ; do \
  159. echo -n " "; \
  160. depth=`expr $$depth - 1`; \
  161. done; \
  162. true
  163. @echo -n " [CC] $@\n"
  164. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  165. $(ARNAME): $(SUBDIRS) $(LOCALOBJECTS)
  166. @depth=$(MAKELEVEL); \
  167. while [ $${depth} -gt 0 ] ; do \
  168. echo -n " "; \
  169. depth=`expr $$depth - 1`; \
  170. done; \
  171. true
  172. @echo -n " [AR] $@\n"
  173. @$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX)
  174. # ---------- Recursive Rules ----------
  175. .PHONY: $(SUBDIRS)
  176. $(SUBDIRS):
  177. $(eval DIR := $(subst _DIRECTORY,,$@))
  178. @if [ -e "$(DIR)/Makefile" ]; then \
  179. depth=$(MAKELEVEL); \
  180. while [ $${depth} -gt 0 ] ; do \
  181. echo -n " "; \
  182. depth=`expr $$depth - 1`; \
  183. done; \
  184. true; \
  185. echo " <$(DIR)>"; \
  186. if ($(MAKE) -s -S -C $(DIR) artifacts $(ARCH)); then \
  187. :; \
  188. else \
  189. exit $$?; \
  190. fi; \
  191. depth=$(MAKELEVEL); \
  192. while [ $${depth} -gt 0 ] ; do \
  193. echo -n " "; \
  194. depth=`expr $$depth - 1`; \
  195. done; \
  196. true; \
  197. echo " </$(DIR)>"; \
  198. fi
  199. # ---------- Clean Rules ----------
  200. .PHONY: clean
  201. clean:
  202. @if [ "$(ARCH)" = "any" ]; then \
  203. for arch in $(ARCHS); do \
  204. echo "ARCH: $$arch"; \
  205. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  206. done; \
  207. true; \
  208. else \
  209. for dir in $(SUBDIRS); do \
  210. dir=$${dir%_DIRECTORY}; \
  211. if [ -e "$$dir/Makefile" ]; then \
  212. depth=$(MAKELEVEL); \
  213. while [ $${depth} -gt 0 ] ; do \
  214. echo -n " "; \
  215. depth=`expr $$depth - 1`; \
  216. done; \
  217. echo " <$$dir>"; \
  218. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  219. while [ $${depth} -gt 0 ] ; do \
  220. echo -n " "; \
  221. depth=`expr $$depth - 1`; \
  222. done; \
  223. echo " </$$dir>"; \
  224. fi \
  225. done; \
  226. for target in $(TARGETS); do \
  227. rm -f $$target.$(ARCH); \
  228. done; \
  229. rm -f lib/$(ARCH)/*; \
  230. true; \
  231. fi
  232. @rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) || true
  233. .PHONY: cleandeps
  234. cleandeps:
  235. @if [ "$(ARCH)" = "any" ]; then \
  236. for arch in $(ARCHS); do \
  237. echo "ARCH: $$arch"; \
  238. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  239. done; \
  240. true; \
  241. else \
  242. for dir in $(SUBDIRS); do \
  243. dir=$${dir%_DIRECTORY}; \
  244. if [ -e "$$dir/Makefile" ]; then \
  245. depth=$(MAKELEVEL); \
  246. while [ $${depth} -gt 0 ] ; do \
  247. echo -n " "; \
  248. depth=`expr $$depth - 1`; \
  249. done; \
  250. echo " <$$dir>"; \
  251. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  252. while [ $${depth} -gt 0 ] ; do \
  253. echo -n " "; \
  254. depth=`expr $$depth - 1`; \
  255. done; \
  256. echo " </$$dir>"; \
  257. fi \
  258. done; \
  259. for target in $(TARGETS); do \
  260. rm -f $$target.$(ARCH); \
  261. done; \
  262. true; \
  263. fi
  264. @rm -f $(LOCALDEPENDS) || true
  265. # ---------- Architecture Rules ----------
  266. .PHONY: $(ARCH)
  267. $(ARCH):
  268. @if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \
  269. if [ "$(ARCH)" = "any" ]; then \
  270. for arch in $(ARCHS); do \
  271. echo "ARCH: $$arch"; \
  272. $(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \
  273. done; \
  274. true; \
  275. else \
  276. $(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \
  277. fi \
  278. fi
  279. # ---------- All Rule ----------
  280. .PHONY: all
  281. all: $(TARGETS)