component_wrapper.mk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # Component wrapper makefile
  2. #
  3. # This makefile gets called recursively from the project make, once for each component.
  4. # COMPONENT_MAKEFILE is set to point at the component.mk file for the component itself,
  5. # which is included as part of this process (after default variables are defined).
  6. #
  7. # This makefile comprises multiple stages, marked in blocked comments below.
  8. #
  9. # CWD is the build directory of the component.
  10. ifndef PROJECT_PATH
  11. $(error Make was invoked from $(CURDIR). However please do not run make from the sdk or a component directory; invoke make from the project directory. See the ESP-IDF README for details.)
  12. endif
  13. ################################################################################
  14. # 1) Set default variables for the component build (including configuration
  15. # loaded from sdkconfig.)
  16. ################################################################################
  17. # Find the path to the component
  18. COMPONENT_PATH := $(abspath $(dir $(COMPONENT_MAKEFILE)))
  19. export COMPONENT_PATH
  20. # COMPONENT_BUILD_DIR is otherwise known as CWD for the build
  21. COMPONENT_BUILD_DIR := $(abspath .)
  22. # include elements common to both project & component makefiles
  23. # (includes project configuration set via menuconfig)
  24. include $(IDF_PATH)/make/common.mk
  25. # Some of the following defaults may be overriden by the component's component.mk makefile,
  26. # during the next step:
  27. # Absolute path of the .a file
  28. COMPONENT_LIBRARY = lib$(COMPONENT_NAME).a
  29. # Source dirs a component has. Default to root directory of component.
  30. COMPONENT_SRCDIRS = .
  31. #Names of binary & text files to embed as raw content in the component library
  32. COMPONENT_EMBED_FILES ?=
  33. COMPONENT_EMBED_TXTFILES ?=
  34. # By default, include only the include/ dir.
  35. COMPONENT_ADD_INCLUDEDIRS = include
  36. COMPONENT_ADD_LDFLAGS = -l$(COMPONENT_NAME)
  37. # Name of the linker fragment files this component presents to the Linker
  38. # script generator
  39. COMPONENT_ADD_LDFRAGMENTS ?=
  40. # Define optional compiling macros
  41. define compile_exclude
  42. COMPONENT_OBJEXCLUDE += $(1)
  43. endef
  44. define compile_include
  45. COMPONENT_OBJINCLUDE += $(1)
  46. endef
  47. define compile_only_if
  48. $(eval $(if $(1), $(call compile_include, $(2)), $(call compile_exclude, $(2))))
  49. endef
  50. define compile_only_if_not
  51. $(eval $(if $(1), $(call compile_exclude, $(2)), $(call compile_include, $(2))))
  52. endef
  53. COMPONENT_ADD_LINKER_DEPS ?=
  54. COMPONENT_DEPENDS ?=
  55. COMPONENT_EXTRA_CLEAN ?=
  56. COMPONENT_EXTRA_INCLUDES ?=
  57. COMPONENT_OBJEXCLUDE ?=
  58. COMPONENT_OBJINCLUDE ?=
  59. COMPONENT_SUBMODULES ?=
  60. ################################################################################
  61. # 2) Include the component.mk for the specific component (COMPONENT_MAKEFILE) to
  62. # override variables & optionally define custom targets. Also include global
  63. # component makefiles.
  64. ################################################################################
  65. # Include any Makefile.componentbuild file letting components add
  66. # configuration at the global component level
  67. # Save component_path; we pass it to the called Makefile.componentbuild
  68. # as COMPILING_COMPONENT_PATH, and we use it to restore the current
  69. # COMPONENT_PATH later.
  70. COMPILING_COMPONENT_PATH := $(COMPONENT_PATH)
  71. define includeCompBuildMakefile
  72. ifeq ("$(V)","1")
  73. $$(info including $(1)/Makefile.componentbuild...)
  74. endif
  75. COMPONENT_PATH := $(1)
  76. include $(1)/Makefile.componentbuild
  77. endef
  78. $(foreach componentpath,$(COMPONENT_PATHS), \
  79. $(if $(wildcard $(componentpath)/Makefile.componentbuild), \
  80. $(eval $(call includeCompBuildMakefile,$(componentpath)))))
  81. #Restore COMPONENT_PATH to what it was
  82. COMPONENT_PATH := $(COMPILING_COMPONENT_PATH)
  83. # Include component.mk for this component.
  84. include $(COMPONENT_MAKEFILE)
  85. ################################################################################
  86. # 3) Set variables that depend on values that may changed by component.mk
  87. ################################################################################
  88. ifndef COMPONENT_CONFIG_ONLY # Skip steps 3-5 if COMPONENT_CONFIG_ONLY is set
  89. # Object files which need to be linked into the library
  90. # By default we take all .c, .cpp, .cc & .S files in COMPONENT_SRCDIRS.
  91. ifndef COMPONENT_OBJS
  92. # Find all source files in all COMPONENT_SRCDIRS
  93. COMPONENT_OBJS := $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.c,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.c)))
  94. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cpp,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cpp)))
  95. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cc,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cc)))
  96. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.S,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.S)))
  97. # Make relative by removing COMPONENT_PATH from all found object paths
  98. COMPONENT_OBJS := $(patsubst $(COMPONENT_PATH)/%,%,$(COMPONENT_OBJS))
  99. else
  100. # Add in components defined by conditional compiling macros
  101. COMPONENT_OBJS += $(COMPONENT_OBJINCLUDE)
  102. endif
  103. # Remove any leading ../ from paths, so everything builds inside build dir
  104. COMPONENT_OBJS := $(call stripLeadingParentDirs,$(COMPONENT_OBJS))
  105. # Do the same for COMPONENT_OBJEXCLUDE (used below)
  106. COMPONENT_OBJEXCLUDE := $(call stripLeadingParentDirs,$(COMPONENT_OBJEXCLUDE))
  107. # COMPONENT_OBJDIRS is COMPONENT_SRCDIRS with the same transform applied
  108. COMPONENT_OBJDIRS := $(call stripLeadingParentDirs,$(COMPONENT_SRCDIRS))
  109. # Remove items disabled by optional compilation
  110. COMPONENT_OBJS := $(foreach obj,$(COMPONENT_OBJS),$(if $(filter $(abspath $(obj)),$(abspath $(COMPONENT_OBJEXCLUDE))), ,$(obj)))
  111. # Remove duplicates
  112. COMPONENT_OBJS := $(call uniq,$(COMPONENT_OBJS))
  113. # Object files with embedded binaries to add to the component library
  114. # Correspond to the files named in COMPONENT_EMBED_FILES & COMPONENT_EMBED_TXTFILES
  115. COMPONENT_EMBED_OBJS ?= $(addsuffix .bin.o,$(notdir $(COMPONENT_EMBED_FILES))) $(addsuffix .txt.o,$(notdir $(COMPONENT_EMBED_TXTFILES)))
  116. # If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
  117. # variable with all the include dirs from all the components in random order. This
  118. # means we can accidentally grab a header from another component before grabbing our own.
  119. # To make sure that does not happen, re-order the includes so ours come first.
  120. COMPONENT_PRIV_INCLUDEDIRS ?=
  121. OWN_INCLUDES:=$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_PRIV_INCLUDEDIRS) $(COMPONENT_ADD_INCLUDEDIRS)))
  122. COMPONENT_INCLUDES := $(OWN_INCLUDES) $(filter-out $(OWN_INCLUDES),$(COMPONENT_INCLUDES))
  123. ################################################################################
  124. # 4) Define a target to generate component_project_vars.mk Makefile which
  125. # contains common per-component settings which are included directly in the
  126. # top-level project make
  127. #
  128. # (Skipped if COMPONENT_CONFIG_ONLY is set.)
  129. ################################################################################
  130. # macro to generate variable-relative paths inside component_project_vars.mk, whenever possible
  131. # ie put literal $(IDF_PATH), $(PROJECT_PATH) and $(BUILD_DIR_BASE) into the generated
  132. # makefiles where possible.
  133. #
  134. # This means if directories move (breaking absolute paths), don't need to 'make clean'
  135. define MakeVariablePath
  136. $(subst $(IDF_PATH),$$(IDF_PATH),$(subst $(PROJECT_PATH),$$(PROJECT_PATH),$(subst $(BUILD_DIR_BASE),$$(BUILD_DIR_BASE),$(1))))
  137. endef
  138. # component_project_vars.mk target for the component. This is used to
  139. # take component.mk variables COMPONENT_ADD_INCLUDEDIRS,
  140. # COMPONENT_ADD_LDFLAGS, COMPONENT_DEPENDS and COMPONENT_SUBMODULES
  141. # and inject those into the project make pass.
  142. #
  143. # The target here has no dependencies, as the parent target in
  144. # project.mk evaluates dependencies before calling down to here. See
  145. # GenerateComponentTargets macro in project.mk.
  146. #
  147. # If you are thinking of editing the output of this target for a
  148. # component-specific feature, please don't! What you want is a
  149. # Makefile.projbuild for your component (see docs/build-system.rst for
  150. # more.)
  151. #
  152. # Note: The :: target here is not a mistake. This target should always be
  153. # executed, as dependencies are checked by the parent project-level make target.
  154. # See https://www.gnu.org/software/make/manual/make.html#index-_003a_003a-rules-_0028double_002dcolon_0029
  155. component_project_vars.mk::
  156. $(details) "Building component project variables list $(abspath $@)"
  157. @echo '# Automatically generated build file. Do not edit.' > $@
  158. @echo 'COMPONENT_INCLUDES += $(call MakeVariablePath,$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS))))' >> $@
  159. @echo 'COMPONENT_LDFLAGS += $(call MakeVariablePath,-L$(COMPONENT_BUILD_DIR) $(COMPONENT_ADD_LDFLAGS))' >> $@
  160. @echo 'COMPONENT_LINKER_DEPS += $(call MakeVariablePath,$(call resolvepath,$(COMPONENT_ADD_LINKER_DEPS),$(COMPONENT_PATH)))' >> $@
  161. @echo 'COMPONENT_SUBMODULES += $(call MakeVariablePath,$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_SUBMODULES))))' >> $@
  162. @echo 'COMPONENT_LIBRARIES += $(COMPONENT_NAME)' >> $@
  163. @echo 'COMPONENT_LDFRAGMENTS += $(call MakeVariablePath,$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_LDFRAGMENTS))))' >> $@
  164. @echo 'component-$(COMPONENT_NAME)-build: $(addprefix component-,$(addsuffix -build,$(COMPONENT_DEPENDS)))' >> $@
  165. ifdef COMPONENT_OWNBUILDTARGET
  166. @echo 'COMPONENT_$(COMPONENT_NAME)_BUILDTARGET := $(COMPONENT_OWNBUILDTARGET)' >> $@
  167. endif
  168. ifdef COMPONENT_OWNCLEANTARGET
  169. @echo 'COMPONENT_$(COMPONENT_NAME)_CLEANTARGET := $(COMPONENT_OWNCLEANTARGET)' >> $@
  170. endif
  171. ################################################################################
  172. # 5) Where COMPONENT_OWNBUILDTARGET / COMPONENT_OWNCLEANTARGET
  173. # is not set by component.mk, define default build, clean, etc. targets
  174. #
  175. # (Skipped if COMPONENT_CONFIG_ONLY is set.)
  176. ################################################################################
  177. # Default build behaviour: define a phony build target and a COMPONENT_LIBRARY link target.
  178. ifndef COMPONENT_OWNBUILDTARGET
  179. .PHONY: build
  180. build: $(COMPONENT_LIBRARY)
  181. # Build the archive. We remove the archive first, otherwise ar will get confused if we update
  182. # an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
  183. $(COMPONENT_LIBRARY): $(COMPONENT_OBJS) $(COMPONENT_EMBED_OBJS)
  184. $(summary) AR $(patsubst $(PWD)/%,%,$(CURDIR))/$@
  185. rm -f $@
  186. $(AR) $(ARFLAGS) $@ $(COMPONENT_OBJS) $(COMPONENT_EMBED_OBJS)
  187. endif
  188. # If COMPONENT_OWNCLEANTARGET is not set, define a phony clean target
  189. ifndef COMPONENT_OWNCLEANTARGET
  190. CLEAN_FILES := $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_OBJEXCLUDE) $(COMPONENT_OBJEXCLUDE:.o=.d) $(COMPONENT_EMBED_OBJS) $(COMPONENT_EXTRA_CLEAN) component_project_vars.mk
  191. .PHONY: clean
  192. clean:
  193. $(summary) RM $(CLEAN_FILES)
  194. rm -f $(CLEAN_FILES)
  195. endif
  196. DEBUG_FLAGS ?= -ggdb
  197. # Include all dependency files already generated
  198. -include $(COMPONENT_OBJS:.o=.d)
  199. # This is a fix for situation where the project or IDF dir moves, and instead
  200. # of rebuilding the target the build fails until make clean is run
  201. #
  202. # It adds an empty dependency rule for the (possibly non-existent) source file itself,
  203. # which prevents it not being found from failing the build
  204. #
  205. # $1 == Source File, $2 == .o file used for .d file name
  206. define AppendSourceToDependencies
  207. echo "$1:" >> $$(patsubst %.o,%.d,$2)
  208. endef
  209. # This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
  210. define GenerateCompileTargets
  211. # $(1) - directory containing source files, relative to $(COMPONENT_PATH) - one of $(COMPONENT_SRCDIRS)
  212. # $(2) - output build directory, which is $(1) with any leading ".."s converted to "."s to ensure output is always under build/
  213. #
  214. $(2)/%.o: $$(COMPONENT_PATH)/$(1)/%.c $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_OBJDIRS)
  215. $$(summary) CC $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  216. $$(CC) $$(CFLAGS) $$(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I $(1) -c $$(abspath $$<) -o $$@
  217. $(call AppendSourceToDependencies,$$<,$$@)
  218. $(2)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_OBJDIRS)
  219. $$(summary) CXX $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  220. $$(CXX) $$(CXXFLAGS) $$(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I $(1) -c $$(abspath $$<) -o $$@
  221. $(call AppendSourceToDependencies,$$<,$$@)
  222. $(2)/%.o: $$(COMPONENT_PATH)/$(1)/%.cc $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_OBJDIRS)
  223. $$(summary) CXX $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  224. $$(CXX) $$(CXXFLAGS) $$(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I $(1) -c $$(abspath $$<) -o $$@
  225. $(call AppendSourceToDependencies,$$<,$$@)
  226. $(2)/%.o: $$(COMPONENT_PATH)/$(1)/%.S $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_OBJDIRS)
  227. $$(summary) AS $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  228. $$(CC) $$(CPPFLAGS) $$(DEBUG_FLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I $(1) -c $$(abspath $$<) -o $$@
  229. $(call AppendSourceToDependencies,$$<,$$@)
  230. # CWD is build dir, create the build subdirectory if it doesn't exist
  231. #
  232. # (NB: Each .o file depends on all relative component build dirs $(COMPONENT_OBJDIRS), including $(2), to work
  233. # around a behaviour make 3.81 where the first pattern (randomly) seems to be matched rather than the best fit. ie if
  234. # you have objects a/y.o and a/b/c.o then c.o can be matched with $(1)=a & %=b/c, meaning that subdir 'a/b' needs to be
  235. # created but wouldn't be created if $(2)=a. Make 4.x doesn't have this problem, it seems to preferentially
  236. # choose the better match ie $(2)=a/b and %=c )
  237. #
  238. # Note: This may cause some issues for out-of-tree source files and make 3.81 :/
  239. #
  240. $(2):
  241. mkdir -p $(2)
  242. endef
  243. # Generate all the compile target patterns
  244. $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir),$(call stripLeadingParentDirs,$(srcdir)))))
  245. ## Support for embedding binary files into the ELF as symbols
  246. OBJCOPY_EMBED_ARGS := --input-target binary --output-target elf32-xtensa-le --binary-architecture xtensa --rename-section .data=.rodata.embedded
  247. # Generate pattern for embedding text or binary files into the app
  248. # $(1) is name of file (as relative path inside component)
  249. # $(2) is txt or bin depending on file contents
  250. #
  251. # txt files are null-terminated before being embedded (otherwise
  252. # identical behaviour.)
  253. #
  254. define GenerateEmbedTarget
  255. # copy the input file into the build dir (using a subdirectory
  256. # in case the file already exists elsewhere in the build dir)
  257. embed_bin/$$(notdir $(1)): $(call resolvepath,$(1),$(COMPONENT_PATH)) | embed_bin
  258. cp $$< $$@
  259. embed_txt/$$(notdir $(1)): $(call resolvepath,$(1),$(COMPONENT_PATH)) | embed_txt
  260. cp $$< $$@
  261. printf '\0' >> $$@ # null-terminate text files
  262. # messing about with the embed_X subdirectory then using 'cd' for objcopy is because the
  263. # full path passed to OBJCOPY makes it into the name of the symbols in the .o file
  264. $$(notdir $(1)).$(2).o: embed_$(2)/$$(notdir $(1))
  265. $(summary) EMBED $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  266. cd embed_$(2); $(OBJCOPY) $(OBJCOPY_EMBED_ARGS) $$(notdir $$<) ../$$@
  267. CLEAN_FILES += embed_$(2)/$$(notdir $(1))
  268. endef
  269. embed_txt embed_bin:
  270. mkdir -p $@
  271. # generate targets to embed binary & text files
  272. $(foreach binfile,$(COMPONENT_EMBED_FILES), $(eval $(call GenerateEmbedTarget,$(binfile),bin)))
  273. $(foreach txtfile,$(COMPONENT_EMBED_TXTFILES), $(eval $(call GenerateEmbedTarget,$(txtfile),txt)))
  274. else # COMPONENT_CONFIG_ONLY is set
  275. build:
  276. $(details) "No build needed for $(COMPONENT_NAME) (COMPONENT_CONFIG_ONLY)"
  277. clean:
  278. $(summary) RM component_project_vars.mk
  279. rm -f component_project_vars.mk
  280. component_project_vars.mk:: # no need to add variables via component.mk
  281. @echo '# COMPONENT_CONFIG_ONLY target sets no variables here' > $@
  282. endif # COMPONENT_CONFIG_ONLY