Makefile 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. ifneq (,$(wildcard ./Makefile.conf))
  113. .DEFAULT_GOAL := $(1)
  114. endif
  115. endif
  116. ifneq (,$$(findstring .a,$(1)))
  117. $$(eval $$(call add_target_library_arch, $(1)))
  118. else
  119. ifeq ($(1),test)
  120. $$(eval $$(call add_target_test_arch, $(1)))
  121. else
  122. $$(eval $$(call add_target_arch, $(1)))
  123. endif
  124. endif
  125. $(1):
  126. $$(call build_target,$$@)
  127. endef
  128. $(foreach target,$(TARGETS),$(eval $(call add_target,$(target))))
  129. ifneq ($(MAKECMDGOALS),clean)
  130. -include $(LOCALDEPENDS)
  131. endif
  132. # ---------- Object Rules ----------
  133. %.d.$(ARCH): %.cpp
  134. @if [ "$(ARCH)" != "any" ]; then \
  135. depth=$(MAKELEVEL); \
  136. while [ $${depth} -gt 0 ] ; do \
  137. echo -n " "; \
  138. depth=`expr $$depth - 1`; \
  139. done; \
  140. true; \
  141. echo -n " [MM] $@\n"; \
  142. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  143. fi
  144. %.o.$(ARCH): %.cpp
  145. @depth=$(MAKELEVEL); \
  146. while [ $${depth} -gt 0 ] ; do \
  147. echo -n " "; \
  148. depth=`expr $$depth - 1`; \
  149. done; \
  150. true
  151. @echo -n " [CC] $@\n"
  152. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  153. %.d.$(ARCH): %.cc
  154. @if [ "$(ARCH)" != "any" ]; then \
  155. depth=$(MAKELEVEL); \
  156. while [ $${depth} -gt 0 ] ; do \
  157. echo -n " "; \
  158. depth=`expr $$depth - 1`; \
  159. done; \
  160. true; \
  161. echo -n " [MM] $@\n"; \
  162. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  163. fi
  164. %.o.$(ARCH): %.cc
  165. @depth=$(MAKELEVEL); \
  166. while [ $${depth} -gt 0 ] ; do \
  167. echo -n " "; \
  168. depth=`expr $$depth - 1`; \
  169. done; \
  170. true
  171. @echo -n " [CC] $@\n"
  172. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  173. $(ARNAME): $(SUBDIRS) $(LOCALOBJECTS)
  174. @depth=$(MAKELEVEL); \
  175. while [ $${depth} -gt 0 ] ; do \
  176. echo -n " "; \
  177. depth=`expr $$depth - 1`; \
  178. done; \
  179. true
  180. @echo -n " [AR] $@\n"
  181. @$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX)
  182. # ---------- Recursive Rules ----------
  183. .PHONY: $(SUBDIRS)
  184. $(SUBDIRS):
  185. $(eval DIR := $(subst _DIRECTORY,,$@))
  186. @if [ -e "$(DIR)/Makefile" ]; then \
  187. depth=$(MAKELEVEL); \
  188. while [ $${depth} -gt 0 ] ; do \
  189. echo -n " "; \
  190. depth=`expr $$depth - 1`; \
  191. done; \
  192. true; \
  193. echo " <$(DIR)>"; \
  194. if ($(MAKE) -s -S -C $(DIR) artifacts $(ARCH)); then \
  195. :; \
  196. else \
  197. exit $$?; \
  198. fi; \
  199. depth=$(MAKELEVEL); \
  200. while [ $${depth} -gt 0 ] ; do \
  201. echo -n " "; \
  202. depth=`expr $$depth - 1`; \
  203. done; \
  204. true; \
  205. echo " </$(DIR)>"; \
  206. fi
  207. # ---------- Clean Rules ----------
  208. .PHONY: clean
  209. clean:
  210. @if [ "$(ARCH)" = "any" ]; then \
  211. for arch in $(ARCHS); do \
  212. echo "ARCH: $$arch"; \
  213. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  214. done; \
  215. true; \
  216. else \
  217. for dir in $(SUBDIRS); do \
  218. dir=$${dir%_DIRECTORY}; \
  219. if [ -e "$$dir/Makefile" ]; then \
  220. depth=$(MAKELEVEL); \
  221. while [ $${depth} -gt 0 ] ; do \
  222. echo -n " "; \
  223. depth=`expr $$depth - 1`; \
  224. done; \
  225. echo " <$$dir>"; \
  226. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  227. while [ $${depth} -gt 0 ] ; do \
  228. echo -n " "; \
  229. depth=`expr $$depth - 1`; \
  230. done; \
  231. echo " </$$dir>"; \
  232. fi \
  233. done; \
  234. for target in $(TARGETS); do \
  235. rm -f $$target.$(ARCH); \
  236. done; \
  237. rm -f lib/$(ARCH)/*; \
  238. true; \
  239. fi
  240. @rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) || true
  241. .PHONY: cleandeps
  242. cleandeps:
  243. @if [ "$(ARCH)" = "any" ]; then \
  244. for arch in $(ARCHS); do \
  245. echo "ARCH: $$arch"; \
  246. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  247. done; \
  248. true; \
  249. else \
  250. for dir in $(SUBDIRS); do \
  251. dir=$${dir%_DIRECTORY}; \
  252. if [ -e "$$dir/Makefile" ]; then \
  253. depth=$(MAKELEVEL); \
  254. while [ $${depth} -gt 0 ] ; do \
  255. echo -n " "; \
  256. depth=`expr $$depth - 1`; \
  257. done; \
  258. echo " <$$dir>"; \
  259. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  260. while [ $${depth} -gt 0 ] ; do \
  261. echo -n " "; \
  262. depth=`expr $$depth - 1`; \
  263. done; \
  264. echo " </$$dir>"; \
  265. fi \
  266. done; \
  267. for target in $(TARGETS); do \
  268. rm -f $$target.$(ARCH); \
  269. done; \
  270. true; \
  271. fi
  272. @rm -f $(LOCALDEPENDS) || true
  273. # ---------- Architecture Rules ----------
  274. .PHONY: $(ARCH)
  275. $(ARCH):
  276. @if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \
  277. if [ "$(ARCH)" = "any" ]; then \
  278. for arch in $(ARCHS); do \
  279. echo "ARCH: $$arch"; \
  280. $(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \
  281. done; \
  282. true; \
  283. else \
  284. $(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \
  285. fi \
  286. fi
  287. # ---------- All Rule ----------
  288. .PHONY: all
  289. all: $(TARGETS)