Makefile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. -include Makefile.target
  89. ifneq ($(MAKECMDGOALS),clean)
  90. -include $(LOCALDEPENDS)
  91. endif
  92. # ---------- Object Rules ----------
  93. %.d.$(ARCH): %.cpp
  94. @if [ "$(ARCH)" != "any" ]; then \
  95. depth=$(MAKELEVEL); \
  96. while [ $${depth} -gt 0 ] ; do \
  97. echo -n " "; \
  98. depth=`expr $$depth - 1`; \
  99. done; \
  100. true; \
  101. echo -n " [MM] $@\n"; \
  102. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  103. fi
  104. %.o.$(ARCH): %.cpp
  105. @depth=$(MAKELEVEL); \
  106. while [ $${depth} -gt 0 ] ; do \
  107. echo -n " "; \
  108. depth=`expr $$depth - 1`; \
  109. done; \
  110. true
  111. @echo -n " [CC] $@\n"
  112. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  113. %.d.$(ARCH): %.cc
  114. @if [ "$(ARCH)" != "any" ]; then \
  115. depth=$(MAKELEVEL); \
  116. while [ $${depth} -gt 0 ] ; do \
  117. echo -n " "; \
  118. depth=`expr $$depth - 1`; \
  119. done; \
  120. true; \
  121. echo -n " [MM] $@\n"; \
  122. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  123. fi
  124. %.o.$(ARCH): %.cc
  125. @depth=$(MAKELEVEL); \
  126. while [ $${depth} -gt 0 ] ; do \
  127. echo -n " "; \
  128. depth=`expr $$depth - 1`; \
  129. done; \
  130. true
  131. @echo -n " [CC] $@\n"
  132. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  133. $(ARNAME): $(SUBDIRS) $(LOCALOBJECTS)
  134. @depth=$(MAKELEVEL); \
  135. while [ $${depth} -gt 0 ] ; do \
  136. echo -n " "; \
  137. depth=`expr $$depth - 1`; \
  138. done; \
  139. true
  140. @echo -n " [AR] $@\n"
  141. @$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX)
  142. # ---------- Recursive Rules ----------
  143. .PHONY: $(SUBDIRS)
  144. $(SUBDIRS):
  145. @if [ -e "$@/Makefile" ]; then \
  146. depth=$(MAKELEVEL); \
  147. while [ $${depth} -gt 0 ] ; do \
  148. echo -n " "; \
  149. depth=`expr $$depth - 1`; \
  150. done; \
  151. true; \
  152. echo " <$@>"; \
  153. if ($(MAKE) -s -S -C $@ artifacts $(ARCH)); then \
  154. :; \
  155. else \
  156. exit $$?; \
  157. fi; \
  158. depth=$(MAKELEVEL); \
  159. while [ $${depth} -gt 0 ] ; do \
  160. echo -n " "; \
  161. depth=`expr $$depth - 1`; \
  162. done; \
  163. true; \
  164. echo " </$@>"; \
  165. fi
  166. # ---------- Clean Rules ----------
  167. .PHONY: clean
  168. clean:
  169. @if [ "$(ARCH)" = "any" ]; then \
  170. for arch in $(ARCHS); do \
  171. echo "ARCH: $$arch"; \
  172. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  173. done; \
  174. true; \
  175. else \
  176. for dir in $(SUBDIRS); do \
  177. if [ -e "$$dir/Makefile" ]; then \
  178. depth=$(MAKELEVEL); \
  179. while [ $${depth} -gt 0 ] ; do \
  180. echo -n " "; \
  181. depth=`expr $$depth - 1`; \
  182. done; \
  183. echo " <$$dir>"; \
  184. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  185. while [ $${depth} -gt 0 ] ; do \
  186. echo -n " "; \
  187. depth=`expr $$depth - 1`; \
  188. done; \
  189. echo " </$$dir>"; \
  190. fi \
  191. done; \
  192. for target in $(TARGETS); do \
  193. rm -f $$target.$(ARCH); \
  194. done; \
  195. rm -f lib/$(ARCH)/*; \
  196. true; \
  197. fi
  198. @rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) || true
  199. .PHONY: cleandeps
  200. cleandeps:
  201. @if [ "$(ARCH)" = "any" ]; then \
  202. for arch in $(ARCHS); do \
  203. echo "ARCH: $$arch"; \
  204. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  205. done; \
  206. true; \
  207. else \
  208. for dir in $(SUBDIRS); do \
  209. if [ -e "$$dir/Makefile" ]; then \
  210. depth=$(MAKELEVEL); \
  211. while [ $${depth} -gt 0 ] ; do \
  212. echo -n " "; \
  213. depth=`expr $$depth - 1`; \
  214. done; \
  215. echo " <$$dir>"; \
  216. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  217. while [ $${depth} -gt 0 ] ; do \
  218. echo -n " "; \
  219. depth=`expr $$depth - 1`; \
  220. done; \
  221. echo " </$$dir>"; \
  222. fi \
  223. done; \
  224. for target in $(TARGETS); do \
  225. rm -f $$target.$(ARCH); \
  226. done; \
  227. true; \
  228. fi
  229. @rm -f $(LOCALDEPENDS) || true
  230. # ---------- Architecture Rules ----------
  231. .PHONY: $(ARCH)
  232. $(ARCH):
  233. @if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \
  234. if [ "$(ARCH)" = "any" ]; then \
  235. for arch in $(ARCHS); do \
  236. echo "ARCH: $$arch"; \
  237. $(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \
  238. done; \
  239. true; \
  240. else \
  241. $(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \
  242. fi \
  243. fi
  244. # ---------- All Rule ----------
  245. .PHONY: all
  246. all: $(TARGETS)