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