Makefile 6.2 KB

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