component_wrapper.mk 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. # Name of the component
  28. COMPONENT_NAME := $(lastword $(subst /, ,$(realpath $(COMPONENT_PATH))))
  29. # Absolute path of the .a file
  30. COMPONENT_LIBRARY = lib$(COMPONENT_NAME).a
  31. # Source dirs a component has. Default to root directory of component.
  32. COMPONENT_SRCDIRS = .
  33. #Names of binary & text files to embed as raw content in the component library
  34. COMPONENT_EMBED_FILES ?=
  35. COMPONENT_EMBED_TXTFILES ?=
  36. # By default, include only the include/ dir.
  37. COMPONENT_ADD_INCLUDEDIRS = include
  38. COMPONENT_ADD_LDFLAGS = -l$(COMPONENT_NAME)
  39. ################################################################################
  40. # 2) Include the component.mk for the specific component (COMPONENT_MAKEFILE) to
  41. # override variables & optionally define custom targets.
  42. ################################################################################
  43. include $(COMPONENT_MAKEFILE)
  44. ################################################################################
  45. # 3) Set variables that depend on values that may changed by component.mk
  46. ################################################################################
  47. # Object files which need to be linked into the library
  48. # By default we take all .c, .cpp & .S files in COMPONENT_SRCDIRS.
  49. ifndef COMPONENT_OBJS
  50. # Find all source files in all COMPONENT_SRCDIRS
  51. COMPONENT_OBJS := $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.c,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.c)))
  52. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cpp,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cpp)))
  53. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.S,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.S)))
  54. # Make relative by removing COMPONENT_PATH from all found object paths
  55. COMPONENT_OBJS := $(patsubst $(COMPONENT_PATH)/%,%,$(COMPONENT_OBJS))
  56. endif
  57. # Object files with embedded binaries to add to the component library
  58. # Correspond to the files named in COMPONENT_EMBED_FILES & COMPONENT_EMBED_TXTFILES
  59. COMPONENT_EMBED_OBJS ?= $(addsuffix .bin.o,$(COMPONENT_EMBED_FILES)) $(addsuffix .txt.o,$(COMPONENT_EMBED_TXTFILES))
  60. # If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
  61. # variable with all the include dirs from all the components in random order. This
  62. # means we can accidentally grab a header from another component before grabbing our own.
  63. # To make sure that does not happen, re-order the includes so ours come first.
  64. OWN_INCLUDES:=$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS) $(COMPONENT_PRIV_INCLUDEDIRS)))
  65. COMPONENT_INCLUDES := $(OWN_INCLUDES) $(filter-out $(OWN_INCLUDES),$(COMPONENT_INCLUDES))
  66. ################################################################################
  67. # 4) Define a target to generate component_project_vars.mk Makefile which
  68. # contains common per-component settings which are included directly in the
  69. # top-level project make
  70. ################################################################################
  71. # macro to generate variable-relative paths inside component_project_vars.mk, whenever possible
  72. # ie put literal $(IDF_PATH), $(PROJECT_PATH) and $(BUILD_DIR_BASE) into the generated
  73. # makefiles where possible.
  74. #
  75. # This means if directories move (breaking absolute paths), don't need to 'make clean'
  76. define MakeVariablePath
  77. $(subst $(IDF_PATH),$$(IDF_PATH),$(subst $(PROJECT_PATH),$$(PROJECT_PATH),$(subst $(BUILD_DIR_BASE),\$$(BUILD_DIR_BASE),$(1))))
  78. endef
  79. # component_project_vars.mk target for the component. This is used to
  80. # take component.mk variables COMPONENT_ADD_INCLUDEDIRS,
  81. # COMPONENT_ADD_LDFLAGS and COMPONENT_DEPENDS and inject those into
  82. # the project make pass.
  83. #
  84. # The target here has no dependencies, as the parent target in
  85. # project.mk evaluates dependencies before calling down to here. See
  86. # GenerateComponentTargets macro in project.mk.
  87. #
  88. # If you are thinking of editing the output of this target for a
  89. # component-specific feature, please don't! What you want is a
  90. # Makefile.projbuild for your component (see docs/build-system.rst for
  91. # more.)
  92. component_project_vars.mk::
  93. $(details) "Building component project variables list $(abspath $@)"
  94. @echo '# Automatically generated build file. Do not edit.' > $@
  95. @echo 'COMPONENT_INCLUDES += $(call MakeVariablePath,$(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS)))' >> $@
  96. @echo 'COMPONENT_LDFLAGS += $(call MakeVariablePath,$(COMPONENT_ADD_LDFLAGS))' >> $@
  97. @echo '$(COMPONENT_NAME)-build: $(addsuffix -build,$(COMPONENT_DEPENDS))' >> $@
  98. ################################################################################
  99. # 5) If COMPONENT_OWNBUILDTARGET / COMPONENT_OWNCLEANTARGET is not set by component.mk,
  100. # define default build, clean, etc. targets
  101. ################################################################################
  102. # If COMPONENT_OWNBUILDTARGET is not set, define a phony build target and
  103. # a COMPONENT_LIBRARY link target.
  104. ifndef COMPONENT_OWNBUILDTARGET
  105. .PHONY: build
  106. build: $(COMPONENT_LIBRARY)
  107. @mkdir -p $(COMPONENT_SRCDIRS)
  108. # Build the archive. We remove the archive first, otherwise ar will get confused if we update
  109. # an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
  110. $(COMPONENT_LIBRARY): $(COMPONENT_OBJS) $(COMPONENT_EMBED_OBJS)
  111. $(summary) AR $@
  112. rm -f $@
  113. $(AR) cru $@ $^
  114. endif
  115. # If COMPONENT_OWNCLEANTARGET is not set, define a phony clean target
  116. ifndef COMPONENT_OWNCLEANTARGET
  117. CLEAN_FILES = $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EMBED_OBJS) $(COMPONENT_EXTRA_CLEAN) component_project_vars.mk
  118. .PHONY: clean
  119. clean:
  120. $(summary) RM $(CLEAN_FILES)
  121. rm -f $(CLEAN_FILES)
  122. endif
  123. # Include all dependency files already generated
  124. -include $(COMPONENT_OBJS:.o=.d)
  125. # This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
  126. define GenerateCompileTargets
  127. # $(1) - directory containing source files, relative to $(COMPONENT_PATH)
  128. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c | $(1)
  129. $$(summary) CC $$@
  130. $$(CC) $$(CFLAGS) $$(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  131. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp | $(1)
  132. $$(summary) CXX $$@
  133. $$(CXX) $$(CXXFLAGS) $$(CPPFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  134. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S | $(1)
  135. $$(summary) AS $$@
  136. $$(CC) $$(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  137. # CWD is build dir, create the build subdirectory if it doesn't exist
  138. $(1):
  139. @mkdir -p $(1)
  140. endef
  141. # Generate all the compile target patterns
  142. $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir))))
  143. ## Support for embedding binary files into the ELF as symbols
  144. OBJCOPY_EMBED_ARGS := --input binary --output elf32-xtensa-le --binary-architecture xtensa --rename-section .data=.rodata.embedded
  145. # Generate pattern for embedding text or binary files into the app
  146. # $(1) is name of file (as relative path inside component)
  147. # $(2) is txt or bin depending on file contents
  148. #
  149. # txt files are null-terminated before being embedded (otherwise
  150. # identical behaviour.)
  151. #
  152. # Files are temporarily copied to the build directory before objcopy,
  153. # because objcopy generates the symbol name from the full command line
  154. # path to the input file.
  155. define GenerateEmbedTarget
  156. $(1).$(2).o: $(call resolvepath,$(1),$(COMPONENT_PATH)) | $$(dir $(1))
  157. $(summary) EMBED $$@
  158. $$(if $$(filter-out $$(notdir $$(abspath $$<)),$$(abspath $$(notdir $$<))), cp $$< $$(notdir $$<) ) # copy input file to build dir, unless already in build dir
  159. $$(if $$(subst bin,,$(2)),echo -ne '\0' >> $$(notdir $$<) ) # trailing NUL byte on text output
  160. $(OBJCOPY) $(OBJCOPY_EMBED_ARGS) $$(notdir $$<) $$@
  161. rm $$(notdir $$<)
  162. endef
  163. # generate targets to embed binary & text files
  164. $(foreach binfile,$(COMPONENT_EMBED_FILES), $(eval $(call GenerateEmbedTarget,$(binfile),bin)))
  165. $(foreach txtfile,$(COMPONENT_EMBED_TXTFILES), $(eval $(call GenerateEmbedTarget,$(txtfile),txt)))
  166. # generate targets to create binary embed directories
  167. $(foreach bindir,$(sort $(dir $(COMPONENT_EMBED_FILES))), $(eval $(call GenerateBuildDirTarget,$(bindir))))