Makefile 7.3 KB

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