component_wrapper.mk 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. # Define optional compiling macros
  38. define compile_exclude
  39. COMPONENT_OBJEXCLUDE += $(1)
  40. endef
  41. define compile_include
  42. COMPONENT_OBJINCLUDE += $(1)
  43. endef
  44. define compile_only_if
  45. $(eval $(if $(1), $(call compile_include, $(2)), $(call compile_exclude, $(2))))
  46. endef
  47. define compile_only_if_not
  48. $(eval $(if $(1), $(call compile_exclude, $(2)), $(call compile_include, $(2))))
  49. endef
  50. COMPONENT_ADD_LINKER_DEPS ?=
  51. COMPONENT_DEPENDS ?=
  52. COMPONENT_EXTRA_CLEAN ?=
  53. COMPONENT_EXTRA_INCLUDES ?=
  54. COMPONENT_OBJEXCLUDE ?=
  55. COMPONENT_OBJINCLUDE ?=
  56. COMPONENT_SUBMODULES ?=
  57. ################################################################################
  58. # 2) Include the component.mk for the specific component (COMPONENT_MAKEFILE) to
  59. # override variables & optionally define custom targets.
  60. ################################################################################
  61. include $(COMPONENT_MAKEFILE)
  62. ################################################################################
  63. # 3) Set variables that depend on values that may changed by component.mk
  64. ################################################################################
  65. ifndef COMPONENT_CONFIG_ONLY # Skip steps 3-5 if COMPONENT_CONFIG_ONLY is set
  66. # Object files which need to be linked into the library
  67. # By default we take all .c, .cpp & .S files in COMPONENT_SRCDIRS.
  68. ifndef COMPONENT_OBJS
  69. # Find all source files in all COMPONENT_SRCDIRS
  70. COMPONENT_OBJS := $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.c,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.c)))
  71. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cpp,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cpp)))
  72. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.S,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.S)))
  73. # Make relative by removing COMPONENT_PATH from all found object paths
  74. COMPONENT_OBJS := $(patsubst $(COMPONENT_PATH)/%,%,$(COMPONENT_OBJS))
  75. else
  76. # Add in components defined by conditional compiling macros
  77. COMPONENT_OBJS += $(COMPONENT_OBJINCLUDE)
  78. endif
  79. # Remove items disabled by optional compilation
  80. COMPONENT_OBJS := $(foreach obj,$(COMPONENT_OBJS),$(if $(filter $(realpath $(obj)),$(realpath $(COMPONENT_OBJEXCLUDE))), ,$(obj)))
  81. # Remove duplicates
  82. COMPONENT_OBJS := $(call uniq,$(COMPONENT_OBJS))
  83. # Object files with embedded binaries to add to the component library
  84. # Correspond to the files named in COMPONENT_EMBED_FILES & COMPONENT_EMBED_TXTFILES
  85. COMPONENT_EMBED_OBJS ?= $(addsuffix .bin.o,$(notdir $(COMPONENT_EMBED_FILES))) $(addsuffix .txt.o,$(notdir $(COMPONENT_EMBED_TXTFILES)))
  86. # If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
  87. # variable with all the include dirs from all the components in random order. This
  88. # means we can accidentally grab a header from another component before grabbing our own.
  89. # To make sure that does not happen, re-order the includes so ours come first.
  90. COMPONENT_PRIV_INCLUDEDIRS ?=
  91. OWN_INCLUDES:=$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_PRIV_INCLUDEDIRS) $(COMPONENT_ADD_INCLUDEDIRS)))
  92. COMPONENT_INCLUDES := $(OWN_INCLUDES) $(filter-out $(OWN_INCLUDES),$(COMPONENT_INCLUDES))
  93. ################################################################################
  94. # 4) Define a target to generate component_project_vars.mk Makefile which
  95. # contains common per-component settings which are included directly in the
  96. # top-level project make
  97. #
  98. # (Skipped if COMPONENT_CONFIG_ONLY is set.)
  99. ################################################################################
  100. # macro to generate variable-relative paths inside component_project_vars.mk, whenever possible
  101. # ie put literal $(IDF_PATH), $(PROJECT_PATH) and $(BUILD_DIR_BASE) into the generated
  102. # makefiles where possible.
  103. #
  104. # This means if directories move (breaking absolute paths), don't need to 'make clean'
  105. define MakeVariablePath
  106. $(subst $(IDF_PATH),$$(IDF_PATH),$(subst $(PROJECT_PATH),$$(PROJECT_PATH),$(subst $(BUILD_DIR_BASE),$$(BUILD_DIR_BASE),$(1))))
  107. endef
  108. # component_project_vars.mk target for the component. This is used to
  109. # take component.mk variables COMPONENT_ADD_INCLUDEDIRS,
  110. # COMPONENT_ADD_LDFLAGS, COMPONENT_DEPENDS and COMPONENT_SUBMODULES
  111. # and inject those into the project make pass.
  112. #
  113. # The target here has no dependencies, as the parent target in
  114. # project.mk evaluates dependencies before calling down to here. See
  115. # GenerateComponentTargets macro in project.mk.
  116. #
  117. # If you are thinking of editing the output of this target for a
  118. # component-specific feature, please don't! What you want is a
  119. # Makefile.projbuild for your component (see docs/build-system.rst for
  120. # more.)
  121. #
  122. # Note: The :: target here is not a mistake. This target should always be
  123. # executed, as dependencies are checked by the parent project-level make target.
  124. # See https://www.gnu.org/software/make/manual/make.html#index-_003a_003a-rules-_0028double_002dcolon_0029
  125. component_project_vars.mk::
  126. $(details) "Building component project variables list $(abspath $@)"
  127. @echo '# Automatically generated build file. Do not edit.' > $@
  128. @echo 'COMPONENT_INCLUDES += $(call MakeVariablePath,$(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS)))' >> $@
  129. @echo 'COMPONENT_LDFLAGS += $(call MakeVariablePath,-L$(COMPONENT_BUILD_DIR) $(COMPONENT_ADD_LDFLAGS))' >> $@
  130. @echo 'COMPONENT_LINKER_DEPS += $(call MakeVariablePath,$(call resolvepath,$(COMPONENT_ADD_LINKER_DEPS),$(COMPONENT_PATH)))' >> $@
  131. @echo 'COMPONENT_SUBMODULES += $(call MakeVariablePath,$(addprefix $(COMPONENT_PATH)/,$(COMPONENT_SUBMODULES)))' >> $@
  132. @echo 'COMPONENT_LIBRARIES += $(COMPONENT_NAME)' >> $@
  133. @echo '$(COMPONENT_NAME)-build: $(addsuffix -build,$(COMPONENT_DEPENDS))' >> $@
  134. ################################################################################
  135. # 5) Where COMPONENT_OWNBUILDTARGET / COMPONENT_OWNCLEANTARGET
  136. # is not set by component.mk, define default build, clean, etc. targets
  137. #
  138. # (Skipped if COMPONENT_CONFIG_ONLY is set.)
  139. ################################################################################
  140. # Default build behaviour: define a phony build target and a COMPONENT_LIBRARY link target.
  141. ifndef COMPONENT_OWNBUILDTARGET
  142. .PHONY: build
  143. build: $(COMPONENT_LIBRARY)
  144. # Build the archive. We remove the archive first, otherwise ar will get confused if we update
  145. # an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
  146. $(COMPONENT_LIBRARY): $(COMPONENT_OBJS) $(COMPONENT_EMBED_OBJS)
  147. $(summary) AR $(patsubst $(PWD)/%,%,$(CURDIR))/$@
  148. rm -f $@
  149. $(AR) cru $@ $^
  150. endif
  151. # If COMPONENT_OWNCLEANTARGET is not set, define a phony clean target
  152. ifndef COMPONENT_OWNCLEANTARGET
  153. 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
  154. .PHONY: clean
  155. clean:
  156. $(summary) RM $(CLEAN_FILES)
  157. rm -f $(CLEAN_FILES)
  158. endif
  159. DEBUG_FLAGS ?= -ggdb
  160. # Include all dependency files already generated
  161. -include $(COMPONENT_OBJS:.o=.d)
  162. # This is a fix for situation where the project or IDF dir moves, and instead
  163. # of rebuilding the target the build fails until make clean is run
  164. #
  165. # It adds an empty dependency rule for the (possibly non-existent) source file itself,
  166. # which prevents it not being found from failing the build
  167. #
  168. # $1 == Source File, $2 == .o file used for .d file name
  169. define AppendSourceToDependencies
  170. echo "$1:" >> $$(patsubst %.o,%.d,$2)
  171. endef
  172. # This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
  173. define GenerateCompileTargets
  174. # $(1) - directory containing source files, relative to $(COMPONENT_PATH) - one of $(COMPONENT_SRCDIRS)
  175. #
  176. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_SRCDIRS)
  177. $$(summary) CC $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  178. $$(CC) $$(CFLAGS) $$(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  179. $(call AppendSourceToDependencies,$$<,$$@)
  180. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_SRCDIRS)
  181. $$(summary) CXX $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  182. $$(CXX) $$(CXXFLAGS) $$(CPPFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  183. $(call AppendSourceToDependencies,$$<,$$@)
  184. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S $(COMMON_MAKEFILES) $(COMPONENT_MAKEFILE) | $(COMPONENT_SRCDIRS)
  185. $$(summary) AS $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  186. $$(CC) $$(CPPFLAGS) $$(DEBUG_FLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  187. $(call AppendSourceToDependencies,$$<,$$@)
  188. # CWD is build dir, create the build subdirectory if it doesn't exist
  189. #
  190. # (NB: Each .o file depends on all relative component build dirs $(COMPONENT_SRCDIRS), rather than just $(1), to work
  191. # around a behaviour make 3.81 where the first pattern (randomly) seems to be matched rather than the best fit. ie if
  192. # 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
  193. # created but wouldn't be created if $(1)=a. Make 4.x doesn't have this problem, it seems to preferentially
  194. # choose the better match ie $(1)=a/b and %=c )
  195. #
  196. $(1):
  197. mkdir -p $(1)
  198. endef
  199. # Generate all the compile target patterns
  200. $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir))))
  201. ## Support for embedding binary files into the ELF as symbols
  202. OBJCOPY_EMBED_ARGS := --input-target binary --output-target elf32-xtensa-le --binary-architecture xtensa --rename-section .data=.rodata.embedded
  203. # Generate pattern for embedding text or binary files into the app
  204. # $(1) is name of file (as relative path inside component)
  205. # $(2) is txt or bin depending on file contents
  206. #
  207. # txt files are null-terminated before being embedded (otherwise
  208. # identical behaviour.)
  209. #
  210. define GenerateEmbedTarget
  211. # copy the input file into the build dir (using a subdirectory
  212. # in case the file already exists elsewhere in the build dir)
  213. embed_bin/$$(notdir $(1)): $(call resolvepath,$(1),$(COMPONENT_PATH)) | embed_bin
  214. cp $$< $$@
  215. embed_txt/$$(notdir $(1)): $(call resolvepath,$(1),$(COMPONENT_PATH)) | embed_txt
  216. cp $$< $$@
  217. printf '\0' >> $$@ # null-terminate text files
  218. # messing about with the embed_X subdirectory then using 'cd' for objcopy is because the
  219. # full path passed to OBJCOPY makes it into the name of the symbols in the .o file
  220. $$(notdir $(1)).$(2).o: embed_$(2)/$$(notdir $(1))
  221. $(summary) EMBED $$(patsubst $$(PWD)/%,%,$$(CURDIR))/$$@
  222. cd embed_$(2); $(OBJCOPY) $(OBJCOPY_EMBED_ARGS) $$(notdir $$<) ../$$@
  223. CLEAN_FILES += embed_$(2)/$$(notdir $(1))
  224. endef
  225. embed_txt embed_bin:
  226. mkdir -p $@
  227. # generate targets to embed binary & text files
  228. $(foreach binfile,$(COMPONENT_EMBED_FILES), $(eval $(call GenerateEmbedTarget,$(binfile),bin)))
  229. $(foreach txtfile,$(COMPONENT_EMBED_TXTFILES), $(eval $(call GenerateEmbedTarget,$(txtfile),txt)))
  230. else # COMPONENT_CONFIG_ONLY is set
  231. build:
  232. $(details) "No build needed for $(COMPONENT_NAME) (COMPONENT_CONFIG_ONLY)"
  233. clean:
  234. $(summary) RM component_project_vars.mk
  235. rm -f component_project_vars.mk
  236. component_project_vars.mk:: # no need to add variables via component.mk
  237. @echo '# COMPONENT_CONFIG_ONLY target sets no variables here' > $@
  238. endif # COMPONENT_CONFIG_ONLY