Makefile 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. @$(OBJCOPY) --only-keep-debug $(1) $(DEBUGDIR)/$(1)-$($@_DATETIME).debug
  50. @$(STRIP) $(SFLAGS) $(1)
  51. @$(OBJCOPY) --add-gnu-debuglink="$(DEBUGDIR)/$(1)-$($@_DATETIME).debug" $(1)
  52. @chmod -x $(DEBUGDIR)/$(1)-$($@_DATETIME).debug
  53. endef
  54. define build_target
  55. @if [ "$(ARCH)" = "any" ]; then \
  56. for arch in $(ARCHS); do \
  57. echo "ARCH: $$arch"; \
  58. $(MAKE) -s -S $(1) $$arch; \
  59. done; \
  60. true; \
  61. else \
  62. $(MAKE) -s -S $(1).$(ARCH) $(ARCH); \
  63. fi
  64. endef
  65. $(LOCALOBJECTS): | $(SUBDIRS)
  66. $(OBJECTS): | $(SUBDIRS)
  67. artifacts: $(LOCALOBJECTS)
  68. .DEFAULT_GOAL := artifacts
  69. -include Makefile.target
  70. ifneq ($(MAKECMDGOALS),clean)
  71. -include $(LOCALDEPENDS)
  72. endif
  73. %.d.$(ARCH): %.cpp
  74. @if [ "$(ARCH)" != "any" ]; then \
  75. depth=$(MAKELEVEL); \
  76. while [ $${depth} -gt 0 ] ; do \
  77. echo -n " "; \
  78. depth=`expr $$depth - 1`; \
  79. done; \
  80. true; \
  81. echo -n " [MM] $@\n"; \
  82. $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
  83. fi
  84. %.o.$(ARCH): %.cpp
  85. @depth=$(MAKELEVEL); \
  86. while [ $${depth} -gt 0 ] ; do \
  87. echo -n " "; \
  88. depth=`expr $$depth - 1`; \
  89. done; \
  90. true
  91. @echo -n " [CC] $@\n"
  92. @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)
  93. %.d.$(ARCH): %.cc
  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): %.cc
  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. $(ARNAME): $(SUBDIRS) $(LOCALOBJECTS)
  114. @depth=$(MAKELEVEL); \
  115. while [ $${depth} -gt 0 ] ; do \
  116. echo -n " "; \
  117. depth=`expr $$depth - 1`; \
  118. done; \
  119. true
  120. @echo -n " [AR] $@\n"
  121. @$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX)
  122. .PHONY: $(SUBDIRS)
  123. $(SUBDIRS):
  124. @if [ -e "$@/Makefile" ]; then \
  125. depth=$(MAKELEVEL); \
  126. while [ $${depth} -gt 0 ] ; do \
  127. echo -n " "; \
  128. depth=`expr $$depth - 1`; \
  129. done; \
  130. true; \
  131. echo " <$@>"; \
  132. $(MAKE) -s -S -C $@ artifacts $(ARCH); \
  133. depth=$(MAKELEVEL); \
  134. while [ $${depth} -gt 0 ] ; do \
  135. echo -n " "; \
  136. depth=`expr $$depth - 1`; \
  137. done; \
  138. true; \
  139. echo " </$@>"; \
  140. fi
  141. .PHONY: all
  142. all: $(TARGETS)
  143. .PHONY: clean
  144. clean:
  145. @if [ "$(ARCH)" = "any" ]; then \
  146. for arch in $(ARCHS); do \
  147. echo "ARCH: $$arch"; \
  148. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  149. done; \
  150. true; \
  151. else \
  152. for dir in $(SUBDIRS); do \
  153. if [ -e "$$dir/Makefile" ]; then \
  154. depth=$(MAKELEVEL); \
  155. while [ $${depth} -gt 0 ] ; do \
  156. echo -n " "; \
  157. depth=`expr $$depth - 1`; \
  158. done; \
  159. echo " <$$dir>"; \
  160. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  161. while [ $${depth} -gt 0 ] ; do \
  162. echo -n " "; \
  163. depth=`expr $$depth - 1`; \
  164. done; \
  165. echo " </$$dir>"; \
  166. fi \
  167. done; \
  168. for target in $(TARGETS); do \
  169. rm -f $$target.$(ARCH); \
  170. done; \
  171. rm -f lib/$(ARCH)/*; \
  172. true; \
  173. fi
  174. @rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) || true
  175. .PHONY: cleandeps
  176. cleandeps:
  177. @if [ "$(ARCH)" = "any" ]; then \
  178. for arch in $(ARCHS); do \
  179. echo "ARCH: $$arch"; \
  180. $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
  181. done; \
  182. true; \
  183. else \
  184. for dir in $(SUBDIRS); do \
  185. if [ -e "$$dir/Makefile" ]; then \
  186. depth=$(MAKELEVEL); \
  187. while [ $${depth} -gt 0 ] ; do \
  188. echo -n " "; \
  189. depth=`expr $$depth - 1`; \
  190. done; \
  191. echo " <$$dir>"; \
  192. $(MAKE) -s -S -C $$dir $@ $(ARCH); \
  193. while [ $${depth} -gt 0 ] ; do \
  194. echo -n " "; \
  195. depth=`expr $$depth - 1`; \
  196. done; \
  197. echo " </$$dir>"; \
  198. fi \
  199. done; \
  200. for target in $(TARGETS); do \
  201. rm -f $$target.$(ARCH); \
  202. done; \
  203. true; \
  204. fi
  205. @rm -f $(LOCALDEPENDS) || true
  206. .PHONY: $(ARCH)
  207. $(ARCH):
  208. @if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \
  209. if [ "$(ARCH)" = "any" ]; then \
  210. for arch in $(ARCHS); do \
  211. echo "ARCH: $$arch"; \
  212. $(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \
  213. done; \
  214. true; \
  215. else \
  216. $(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \
  217. fi \
  218. fi