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