Makefile.common 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
  2. #
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without modification,
  6. # are permitted provided that the following conditions are met:
  7. #
  8. # 1. Redistributions of source code must retain the above copyright notice, this
  9. # list of conditions and the following disclaimer.
  10. #
  11. # 2. Redistributions in binary form, except as embedded into a Nordic
  12. # Semiconductor ASA integrated circuit in a product or a software update for
  13. # such product, must reproduce the above copyright notice, this list of
  14. # conditions and the following disclaimer in the documentation and/or other
  15. # materials provided with the distribution.
  16. #
  17. # 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. # contributors may be used to endorse or promote products derived from this
  19. # software without specific prior written permission.
  20. #
  21. # 4. This software, with or without modification, must only be used with a
  22. # Nordic Semiconductor ASA integrated circuit.
  23. #
  24. # 5. Any software provided in binary form under this license must not be reverse
  25. # engineered, decompiled, modified and/or disassembled.
  26. #
  27. # THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. # OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. # DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. # Options:
  38. # VERBOSE=1 (default is 0) - print each executed command
  39. # PRETTY=1 (default is 0) - show progress, in percentage
  40. # ABSOLUTE_PATHS=1 (default is 0) - convert all include folders and source
  41. # file paths to their absolute forms
  42. # PASS_INCLUDE_PATHS_VIA_FILE=1 (default is 0) - use <target>.inc file
  43. # to pass include paths to gcc
  44. # PASS_LINKER_INPUT_VIA_FILE=0 (default is 1) - don't use <target>.in file
  45. # to pass the list of linker input files
  46. VERBOSE ?= 0
  47. PRETTY ?= 0
  48. ABSOLUTE_PATHS ?= 0
  49. PASS_INCLUDE_PATHS_VIA_FILE ?= 0
  50. PASS_LINKER_INPUT_VIA_FILE ?= 1
  51. .SUFFIXES: # ignore built-in rules
  52. %.d: # don't try to make .d files
  53. .PRECIOUS: %.d %.o
  54. MK := mkdir
  55. RM := rm -rf
  56. # echo suspend
  57. ifeq ($(VERBOSE),1)
  58. NO_ECHO :=
  59. else
  60. NO_ECHO := @
  61. endif
  62. ifneq (,$(filter clean, $(MAKECMDGOALS)))
  63. OTHER_GOALS := $(filter-out clean, $(MAKECMDGOALS))
  64. ifneq (, $(OTHER_GOALS))
  65. $(info Cannot make anything in parallel with "clean".)
  66. $(info Execute "$(MAKE) clean \
  67. $(foreach goal, $(OTHER_GOALS),&& $(MAKE) $(goal))" instead.)
  68. $(error Cannot continue)
  69. else
  70. .PHONY: clean
  71. clean:
  72. $(RM) $(OUTPUT_DIRECTORY)
  73. endif # ifneq(, $(OTHER_GOALS))
  74. else # ifneq (,$(filter clean, $(MAKECMDGOALS)))
  75. ifndef PROGRESS
  76. ifeq ($(PRETTY),1)
  77. X := @
  78. EMPTY :=
  79. SPACE := $(EMPTY) $(EMPTY)
  80. TOTAL := $(subst $(SPACE),,$(filter $(X), \
  81. $(shell "$(MAKE)" $(MAKECMDGOALS) --dry-run \
  82. --no-print-directory PROGRESS=$(X))))
  83. 5 := $(X)$(X)$(X)$(X)$(X)
  84. 25 := $(5)$(5)$(5)$(5)$(5)
  85. 100 := $(25)$(25)$(25)$(25)
  86. C :=
  87. COUNTER = $(eval C := $(C)$(100))$(C)
  88. P :=
  89. count = $(if $(filter $1%,$2),$(eval \
  90. P += 1)$(call count,$1,$(2:$1%=%)),$(eval \
  91. C := $2))
  92. print = [$(if $(word 99,$1),99,$(if $(word 10,$1),, )$(words $1))%]
  93. PROGRESS = $(call count,$(TOTAL),$(COUNTER))$(call print,$(P)) $1
  94. else
  95. PROGRESS = $1
  96. endif # ifeq ($(PRETTY),1)
  97. PLATFORM_SUFFIX := $(if $(filter Windows%,$(OS)),windows,posix)
  98. TOOLCHAIN_CONFIG_FILE := $(TEMPLATE_PATH)/Makefile.$(PLATFORM_SUFFIX)
  99. include $(TOOLCHAIN_CONFIG_FILE)
  100. # $1 path
  101. define quote
  102. '$(subst ','\'',$(1))'
  103. endef
  104. # Toolchain commands
  105. CC := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-gcc)
  106. CXX := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-c++)
  107. AS := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-as)
  108. AR := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-ar) -r
  109. LD := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-ld)
  110. NM := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-nm)
  111. OBJDUMP := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-objdump)
  112. OBJCOPY := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-objcopy)
  113. SIZE := $(call quote,$(GNU_INSTALL_ROOT)$(GNU_PREFIX)-size)
  114. $(if $(shell $(CC) --version),,$(info Cannot find: $(CC).) \
  115. $(info Please set values in: "$(abspath $(TOOLCHAIN_CONFIG_FILE))") \
  116. $(info according to the actual configuration of your system.) \
  117. $(error Cannot continue))
  118. # Use ccache on linux if available
  119. CCACHE := $(if $(filter Windows%,$(OS)),, \
  120. $(if $(wildcard /usr/bin/ccache),ccache))
  121. CC := $(CCACHE) $(CC)
  122. endif # ifndef PROGRESS
  123. # $1 type of item
  124. # $2 items paths to check
  125. define ensure_exists_each
  126. $(foreach item, $(2), \
  127. $(if $(wildcard $(item)),, $(warning Cannot find $(1): $(item))))
  128. endef
  129. ifeq ($(PASS_INCLUDE_PATHS_VIA_FILE),1)
  130. INC_PATHS = @$($@_INC)
  131. GENERATE_INC_FILE := 1
  132. else
  133. INC_PATHS = $(call target_specific, INC_PATHS, $($@_TGT))
  134. GENERATE_INC_FILE :=
  135. endif
  136. # $1 object file
  137. # $2 source file
  138. # $3 include paths container file
  139. # $4 target name
  140. define bind_obj_with_src
  141. $(eval $(1) := $(2)) \
  142. $(eval $(1)_INC := $(3)) \
  143. $(eval $(1)_TGT := $(4)) \
  144. $(eval $(1): Makefile | $(dir $(1)).) \
  145. $(if $(GENERATE_INC_FILE), $(eval $(1): $(3)))
  146. endef
  147. # $1 target name
  148. # $2 source file name
  149. # Note: this additional .o for .s files is a workaround for issues with make 4.1
  150. # from MinGW (it does nothing to remake .s.o files when a rule for .S.o
  151. # files is defined as well).
  152. define get_object_file_name
  153. $(OUTPUT_DIRECTORY)/$(strip $(1))/$(notdir $(2:%.s=%.s.o)).o
  154. endef
  155. # $1 target name
  156. # $2 include paths container file
  157. # $3 list of source files
  158. define get_object_files
  159. $(call ensure_exists_each,source file, $(3)) \
  160. $(foreach src_file, $(3), \
  161. $(eval obj_file := $(call get_object_file_name, $(1), $(src_file))) \
  162. $(eval DEPENDENCIES += $(obj_file:.o=.d)) \
  163. $(call bind_obj_with_src, $(obj_file), $(src_file), $(2), $(1)) \
  164. $(obj_file))
  165. endef
  166. # $1 variable name
  167. # $2 target name
  168. define target_specific
  169. $($(addsuffix _$(strip $(2)), $(1)))
  170. endef
  171. ifeq ($(ABSOLUTE_PATHS),1)
  172. get_path = $(call quote,$(abspath $1))
  173. else
  174. get_path = $1
  175. endif
  176. # $1 list of include folders
  177. define get_inc_paths
  178. $(call ensure_exists_each,include folder,$(1)) \
  179. $(foreach folder,$(1),-I$(call get_path,$(folder)))
  180. endef
  181. # $1 target name
  182. # $2 include paths container file
  183. # $3 build goal name
  184. define prepare_build
  185. $(eval DEPENDENCIES :=) \
  186. $(eval $(3): \
  187. $(call get_object_files, $(1), $(2), \
  188. $(SRC_FILES) $(call target_specific, SRC_FILES, $(1)))) \
  189. $(eval -include $(DEPENDENCIES)) \
  190. $(eval INC_PATHS_$(strip $(1)) := \
  191. $(call get_inc_paths, \
  192. $(INC_FOLDERS) $(call target_specific, INC_FOLDERS, $(1))))
  193. endef
  194. # $1 target name
  195. define define_target
  196. $(eval OUTPUT_FILE := $(OUTPUT_DIRECTORY)/$(strip $(1))) \
  197. $(eval $(1): $(OUTPUT_FILE).out $(OUTPUT_FILE).hex $(OUTPUT_FILE).bin \
  198. ; @echo DONE $(strip $(1))) \
  199. $(call prepare_build, $(1), $(OUTPUT_FILE).inc, $(OUTPUT_FILE).out)
  200. endef
  201. # $1 target name
  202. # $2 library file name
  203. define define_library
  204. $(eval OUTPUT_FILE := $(OUTPUT_DIRECTORY)/$(strip $(1))) \
  205. $(eval $(1) := $(2)) \
  206. $(call prepare_build, $(1), $(OUTPUT_FILE).inc, $(1))
  207. endef
  208. # $1 content to be dumped
  209. # Invokes another instance of MAKE to dump the specified content to stdout,
  210. # which may be then redirected in shell to a file and this way stored there.
  211. # MAKE in version prior to 4.0 does not provide the $(file ...) function.
  212. define dump
  213. $(eval CONTENT_TO_DUMP := $(1)) \
  214. "$(MAKE)" -s --no-print-directory \
  215. -f "$(TEMPLATE_PATH)/dump.mk" VARIABLE=CONTENT_TO_DUMP
  216. endef
  217. export CONTENT_TO_DUMP
  218. .PHONY: $(TARGETS) all
  219. all: $(TARGETS)
  220. # Create build directories
  221. $(OUTPUT_DIRECTORY):
  222. $(MK) $@
  223. $(OUTPUT_DIRECTORY)/%/.: | $(OUTPUT_DIRECTORY)
  224. cd $(OUTPUT_DIRECTORY) && $(MK) $*
  225. $(OUTPUT_DIRECTORY)/%.inc: Makefile | $(OUTPUT_DIRECTORY)
  226. $(info Generating $@)
  227. $(NO_ECHO)$(call dump, $(call target_specific, INC_PATHS, $*)) > $@
  228. # $1 command
  229. # $2 flags
  230. # $3 message
  231. define run
  232. $(info $(call PROGRESS,$(3) file: $(notdir $($@)))) \
  233. $(NO_ECHO)$(1) -MP -MD -c -o $@ $(call get_path,$($@)) $(2) $(INC_PATHS)
  234. endef
  235. # Create object files from C source files
  236. %.c.o:
  237. $(call run,$(CC) -std=c99,$(CFLAGS),Compiling)
  238. # Create object files from C++ source files
  239. %.cpp.o:
  240. $(call run,$(CXX),$(CFLAGS) $(CXXFLAGS),Compiling)
  241. # Create object files from assembly source files
  242. %.S.o %.s.o.o:
  243. $(call run,$(CC) -x assembler-with-cpp,$(ASMFLAGS),Assembling)
  244. ifeq ($(PASS_LINKER_INPUT_VIA_FILE),1)
  245. GENERATE_LD_INPUT_FILE = $(call dump, $^ $(LIB_FILES)) > $(@:.out=.in)
  246. LD_INPUT = @$(@:.out=.in)
  247. else
  248. GENERATE_LD_INPUT_FILE =
  249. LD_INPUT = $^ $(LIB_FILES)
  250. endif
  251. # Link object files
  252. %.out:
  253. $(info $(call PROGRESS,Linking target: $@))
  254. $(NO_ECHO)$(GENERATE_LD_INPUT_FILE)
  255. $(NO_ECHO)$(CC) $(LDFLAGS) $(LD_INPUT) -Wl,-Map=$(@:.out=.map) -o $@
  256. $(NO_ECHO)$(SIZE) $@
  257. # Create binary .bin file from the .out file
  258. %.bin: %.out
  259. $(info Preparing: $@)
  260. $(NO_ECHO)$(OBJCOPY) -O binary $< $@
  261. # Create binary .hex file from the .out file
  262. %.hex: %.out
  263. $(info Preparing: $@)
  264. $(NO_ECHO)$(OBJCOPY) -O ihex $< $@
  265. endif # ifneq (,$(filter clean, $(MAKECMDGOALS)))