Makefile 6.6 KB

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