Makefile 8.2 KB

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