Makefile 8.5 KB

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