Makefile 8.4 KB

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