1 # Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
2 # Copyright 2018 apeos contributors
4 # Component wrapper makefile
6 # This makefile gets called recursively from the project make, once for each component.
7 # COMPONENT_MAKEFILE is set to point at the component.mk file for the component itself,
8 # which is included as part of this process (after default variables are defined).
10 # This makefile comprises multiple stages, marked in blocked comments below.
12 # CWD is the build directory of the component.
14 # Licensed under the Apache License, Version 2.0 (the "License");
15 # you may not use this file except in compliance with the License.
16 # You may obtain a copy of the License at
18 # http://www.apache.org/licenses/LICENSE-2.0
20 # Unless required by applicable law or agreed to in writing, software
21 # distributed under the License is distributed on an "AS IS" BASIS,
22 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 # See the License for the specific language governing permissions and
24 # limitations under the License.
27 $(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.
)
31 ################################################################################
32 # 1) Set default variables for the component build (including configuration
33 # loaded from sdkconfig.)
34 ################################################################################
36 # Find the path to the component
37 COMPONENT_PATH
:= $(abspath
$(dir $(COMPONENT_MAKEFILE
)))
40 # COMPONENT_BUILD_DIR is otherwise known as CWD for the build
41 COMPONENT_BUILD_DIR
:= $(abspath .
)
43 # include elements common to both project & component makefiles
44 # (includes project configuration set via menuconfig)
45 $(if
$(V
),$(info including
$(TOP_DIR
)/make
/common.mk...
))
46 include $(TOP_DIR
)/make
/common.mk
48 # Some of the following defaults may be overriden by the component's component.mk makefile,
49 # during the next step:
51 # Absolute path of the .a file
52 COMPONENT_LIBRARY
= lib
$(COMPONENT_NAME
).a
54 # Source dirs a component has. Default to root directory of component.
57 #Names of binary & text files to embed as raw content in the component library
58 COMPONENT_EMBED_FILES ?
=
59 COMPONENT_EMBED_TXTFILES ?
=
61 # By default, include only the include/ dir.
62 COMPONENT_ADD_INCLUDEDIRS
= include
63 COMPONENT_ADD_LDFLAGS
= -l
$(COMPONENT_NAME
)
65 # Define optional compiling macros
66 define compile_exclude
67 COMPONENT_OBJEXCLUDE
+= $(1)
70 define compile_include
71 COMPONENT_OBJINCLUDE
+= $(1)
74 define compile_only_if
75 $(eval
$(if
$(1), $(call compile_include
, $(2)), $(call compile_exclude
, $(2))))
78 define compile_only_if_not
79 $(eval
$(if
$(1), $(call compile_exclude
, $(2)), $(call compile_include
, $(2))))
82 COMPONENT_ADD_LINKER_DEPS ?
=
84 COMPONENT_EXTRA_CLEAN ?
=
85 COMPONENT_EXTRA_INCLUDES ?
=
86 COMPONENT_OBJEXCLUDE ?
=
87 COMPONENT_OBJINCLUDE ?
=
88 COMPONENT_SUBMODULES ?
=
90 ################################################################################
91 # 2) Include the component.mk for the specific component (COMPONENT_MAKEFILE) to
92 # override variables & optionally define custom targets. Also include global
93 # component makefiles.
94 ################################################################################
97 # Include any Makefile.componentbuild file letting components add
98 # configuration at the global component level
100 # Save component_path; we pass it to the called Makefile.componentbuild
101 # as COMPILING_COMPONENT_PATH, and we use it to restore the current
102 # COMPONENT_PATH later.
103 COMPILING_COMPONENT_PATH
:= $(COMPONENT_PATH
)
105 define includeCompBuildMakefile
106 $(if
$(V
),$(info including
$(1)/Makefile.componentbuild...
))
107 COMPONENT_PATH
:= $(1)
108 include $(1)/Makefile.componentbuild
110 $(foreach componentpath
,$(COMPONENT_PATHS
), \
111 $(if
$(wildcard $(componentpath
)/Makefile.componentbuild
), \
112 $(eval
$(call includeCompBuildMakefile
,$(componentpath
)))))
114 #Restore COMPONENT_PATH to what it was
115 COMPONENT_PATH
:= $(COMPILING_COMPONENT_PATH
)
118 # Include component.mk for this component.
119 $(if
$(V
),$(info including
$(COMPONENT_MAKEFILE
)...
))
120 include $(COMPONENT_MAKEFILE
)
123 ################################################################################
124 # 3) Set variables that depend on values that may changed by component.mk
125 ################################################################################
127 ifndef COMPONENT_CONFIG_ONLY
# Skip steps 3-5 if COMPONENT_CONFIG_ONLY is set
129 # Object files which need to be linked into the library
130 # By default we take all .c, .cpp, .cc & .S files in COMPONENT_SRCDIRS.
131 ifndef COMPONENT_OBJS
132 # Find all source files in all COMPONENT_SRCDIRS
133 COMPONENT_OBJS
:= $(foreach compsrcdir
,$(COMPONENT_SRCDIRS
),$(patsubst %.c
,%.o
,$(wildcard $(COMPONENT_PATH
)/$(compsrcdir
)/*.c
)))
134 COMPONENT_OBJS
+= $(foreach compsrcdir
,$(COMPONENT_SRCDIRS
),$(patsubst %.
cpp,%.o
,$(wildcard $(COMPONENT_PATH
)/$(compsrcdir
)/*.
cpp)))
135 COMPONENT_OBJS
+= $(foreach compsrcdir
,$(COMPONENT_SRCDIRS
),$(patsubst %.
cc,%.o
,$(wildcard $(COMPONENT_PATH
)/$(compsrcdir
)/*.
cc)))
136 COMPONENT_OBJS
+= $(foreach compsrcdir
,$(COMPONENT_SRCDIRS
),$(patsubst %.S
,%.o
,$(wildcard $(COMPONENT_PATH
)/$(compsrcdir
)/*.S
)))
137 # Make relative by removing COMPONENT_PATH from all found object paths
138 COMPONENT_OBJS
:= $(patsubst $(COMPONENT_PATH
)/%,%,$(COMPONENT_OBJS
))
140 # Add in components defined by conditional compiling macros
141 COMPONENT_OBJS
+= $(COMPONENT_OBJINCLUDE
)
143 # Remove items disabled by optional compilation
144 COMPONENT_OBJS
:= $(foreach obj
,$(COMPONENT_OBJS
),$(if
$(filter $(realpath
$(obj
)),$(realpath
$(COMPONENT_OBJEXCLUDE
))), ,$(obj
)))
147 COMPONENT_OBJS
:= $(call uniq
,$(COMPONENT_OBJS
))
150 # Object files with embedded binaries to add to the component library
151 # Correspond to the files named in COMPONENT_EMBED_FILES & COMPONENT_EMBED_TXTFILES
152 COMPONENT_EMBED_OBJS ?
= $(addsuffix .bin.o
,$(notdir $(COMPONENT_EMBED_FILES
))) $(addsuffix .txt.o
,$(notdir $(COMPONENT_EMBED_TXTFILES
)))
154 # If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
155 # variable with all the include dirs from all the components in random order. This
156 # means we can accidentally grab a header from another component before grabbing our own.
157 # To make sure that does not happen, re-order the includes so ours come first.
158 COMPONENT_PRIV_INCLUDEDIRS ?
=
159 OWN_INCLUDES
:=$(abspath
$(addprefix $(COMPONENT_PATH
)/,$(COMPONENT_PRIV_INCLUDEDIRS
) $(COMPONENT_ADD_INCLUDEDIRS
)))
160 COMPONENT_INCLUDES
:= $(OWN_INCLUDES
) $(filter-out $(OWN_INCLUDES
),$(COMPONENT_INCLUDES
))
163 ################################################################################
164 # 4) Define a target to generate component_project_vars.mk Makefile which
165 # contains common per-component settings which are included directly in the
166 # top-level project make
168 # (Skipped if COMPONENT_CONFIG_ONLY is set.)
169 ################################################################################
171 # macro to generate variable-relative paths inside component_project_vars.mk, whenever possible
172 # ie put literal $(IDF_PATH), $(PROJECT_PATH) and $(BUILD_DIR_BASE) into the generated
173 # makefiles where possible.
175 # This means if directories move (breaking absolute paths), don't need to 'make clean'
176 define MakeVariablePath
177 $(subst $(IDF_PATH
),$$(IDF_PATH
),$(subst $(PROJECT_PATH
),$$(PROJECT_PATH
),$(subst $(BUILD_DIR_BASE
),$$(BUILD_DIR_BASE
),$(1))))
180 # component_project_vars.mk target for the component. This is used to
181 # take component.mk variables COMPONENT_ADD_INCLUDEDIRS,
182 # COMPONENT_ADD_LDFLAGS, COMPONENT_DEPENDS and COMPONENT_SUBMODULES
183 # and inject those into the project make pass.
185 # The target here has no dependencies, as the parent target in
186 # project.mk evaluates dependencies before calling down to here. See
187 # GenerateComponentTargets macro in project.mk.
189 # If you are thinking of editing the output of this target for a
190 # component-specific feature, please don't! What you want is a
191 # Makefile.projbuild for your component (see docs/build-system.rst for
194 # Note: The :: target here is not a mistake. This target should always be
195 # executed, as dependencies are checked by the parent project-level make target.
196 # See https://www.gnu.org/software/make/manual/make.html#index-_003a_003a-rules-_0028double_002dcolon_0029
197 component_project_vars.mk
::
198 $(details
) "Building component project variables list $(abspath $@)"
199 @echo
'# Automatically generated build file. Do not edit.' > $@
200 @echo
'COMPONENT_INCLUDES += $(call MakeVariablePath,$(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS)))' >> $@
201 @echo
'COMPONENT_LDFLAGS += $(call MakeVariablePath,-L$(COMPONENT_BUILD_DIR) $(COMPONENT_ADD_LDFLAGS))' >> $@
202 @echo
'COMPONENT_LINKER_DEPS += $(call MakeVariablePath,$(call resolvepath,$(COMPONENT_ADD_LINKER_DEPS),$(COMPONENT_PATH)))' >> $@
203 @echo
'COMPONENT_SUBMODULES += $(call MakeVariablePath,$(addprefix $(COMPONENT_PATH)/,$(COMPONENT_SUBMODULES)))' >> $@
204 @echo
'COMPONENT_LIBRARIES += $(COMPONENT_NAME)' >> $@
205 @echo
'component-$(COMPONENT_NAME)-build: $(addprefix component-,$(addsuffix -build,$(COMPONENT_DEPENDS)))' >> $@
207 ################################################################################
208 # 5) Where COMPONENT_OWNBUILDTARGET / COMPONENT_OWNCLEANTARGET
209 # is not set by component.mk, define default build, clean, etc. targets
211 # (Skipped if COMPONENT_CONFIG_ONLY is set.)
212 ################################################################################
214 # Default build behaviour: define a phony build target and a COMPONENT_LIBRARY link target.
215 ifndef COMPONENT_OWNBUILDTARGET
217 build
: $(COMPONENT_LIBRARY
)
219 # Build the archive. We remove the archive first, otherwise ar will get confused if we update
220 # an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
221 $(COMPONENT_LIBRARY
): $(COMPONENT_OBJS
) $(COMPONENT_EMBED_OBJS
)
222 $(summary
) AR
$(patsubst $(PWD
)/%,%,$(CURDIR
))/$@
227 # If COMPONENT_OWNCLEANTARGET is not set, define a phony clean target
228 ifndef COMPONENT_OWNCLEANTARGET
229 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
232 $(summary
) RM
$(CLEAN_FILES
)
238 # Include all dependency files already generated
239 -include $(COMPONENT_OBJS
:.o
=.d
)
241 # This is a fix for situation where the project or IDF dir moves, and instead
242 # of rebuilding the target the build fails until make clean is run
244 # It adds an empty dependency rule for the (possibly non-existent) source file itself,
245 # which prevents it not being found from failing the build
247 # $1 == Source File, $2 == .o file used for .d file name
248 define AppendSourceToDependencies
249 echo
"$1:" >> $$(patsubst %.o
,%.d
,$2)
253 # This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
254 define GenerateCompileTargets
255 # $(1) - directory containing source files, relative to $(COMPONENT_PATH) - one of $(COMPONENT_SRCDIRS)
257 $(1)/%.o
: $$(COMPONENT_PATH
)/$(1)/%.c
$(COMMON_MAKEFILES
) $(COMPONENT_MAKEFILE
) |
$(COMPONENT_SRCDIRS
)
258 $$(summary
) CC
$$(patsubst $$(PWD
)/%,%,$$(CURDIR
))/$$@
259 $$(CC
) $$(CFLAGS
) $$(CPPFLAGS
) $$(addprefix -I
,$$(COMPONENT_INCLUDES
)) $$(addprefix -I
,$$(COMPONENT_EXTRA_INCLUDES
)) -I
$(1) -c
$$< -o
$$@
260 $(call AppendSourceToDependencies
,$$<,$$@
)
262 $(1)/%.o
: $$(COMPONENT_PATH
)/$(1)/%.
cpp $(COMMON_MAKEFILES
) $(COMPONENT_MAKEFILE
) |
$(COMPONENT_SRCDIRS
)
263 $$(summary
) CXX
$$(patsubst $$(PWD
)/%,%,$$(CURDIR
))/$$@
264 $$(CXX
) $$(CXXFLAGS
) $$(CPPFLAGS
) $$(addprefix -I
,$$(COMPONENT_INCLUDES
)) $$(addprefix -I
,$$(COMPONENT_EXTRA_INCLUDES
)) -I
$(1) -c
$$< -o
$$@
265 $(call AppendSourceToDependencies
,$$<,$$@
)
267 $(1)/%.o
: $$(COMPONENT_PATH
)/$(1)/%.
cc $(COMMON_MAKEFILES
) $(COMPONENT_MAKEFILE
) |
$(COMPONENT_SRCDIRS
)
268 $$(summary
) CXX
$$(patsubst $$(PWD
)/%,%,$$(CURDIR
))/$$@
269 $$(CXX
) $$(CXXFLAGS
) $$(CPPFLAGS
) $$(addprefix -I
,$$(COMPONENT_INCLUDES
)) $$(addprefix -I
,$$(COMPONENT_EXTRA_INCLUDES
)) -I
$(1) -c
$$< -o
$$@
270 $(call AppendSourceToDependencies
,$$<,$$@
)
272 $(1)/%.o
: $$(COMPONENT_PATH
)/$(1)/%.S
$(COMMON_MAKEFILES
) $(COMPONENT_MAKEFILE
) |
$(COMPONENT_SRCDIRS
)
273 $$(summary
) AS
$$(patsubst $$(PWD
)/%,%,$$(CURDIR
))/$$@
274 $$(CC
) $$(CPPFLAGS
) $$(DEBUG_FLAGS
) $$(addprefix -I
,$$(COMPONENT_INCLUDES
)) $$(addprefix -I
,$$(COMPONENT_EXTRA_INCLUDES
)) -I
$(1) -c
$$< -o
$$@
275 $(call AppendSourceToDependencies
,$$<,$$@
)
277 # CWD is build dir, create the build subdirectory if it doesn't exist
279 # (NB: Each .o file depends on all relative component build dirs $(COMPONENT_SRCDIRS), rather than just $(1), to work
280 # around a behaviour make 3.81 where the first pattern (randomly) seems to be matched rather than the best fit. ie if
281 # 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
282 # created but wouldn't be created if $(1)=a. Make 4.x doesn't have this problem, it seems to preferentially
283 # choose the better match ie $(1)=a/b and %=c )
289 # Generate all the compile target patterns
290 $(foreach srcdir,$(COMPONENT_SRCDIRS
), $(eval
$(call GenerateCompileTargets
,$(srcdir))))
292 ## Support for embedding binary files into the ELF as symbols
294 OBJCOPY_EMBED_ARGS
:= --input-target binary
--output-target elf32-xtensa-le
--binary-architecture xtensa
--rename-section .data
=.rodata.embedded
296 # Generate pattern for embedding text or binary files into the app
297 # $(1) is name of file (as relative path inside component)
298 # $(2) is txt or bin depending on file contents
300 # txt files are null-terminated before being embedded (otherwise
301 # identical behaviour.)
303 define GenerateEmbedTarget
305 # copy the input file into the build dir (using a subdirectory
306 # in case the file already exists elsewhere in the build dir)
307 embed_bin
/$$(notdir $(1)): $(call resolvepath
,$(1),$(COMPONENT_PATH
)) | embed_bin
310 embed_txt
/$$(notdir $(1)): $(call resolvepath
,$(1),$(COMPONENT_PATH
)) | embed_txt
312 printf
'\0' >> $$@
# null-terminate text files
314 # messing about with the embed_X subdirectory then using 'cd' for objcopy is because the
315 # full path passed to OBJCOPY makes it into the name of the symbols in the .o file
316 $$(notdir $(1)).
$(2).o
: embed_
$(2)/$$(notdir $(1))
317 $(summary
) EMBED
$$(patsubst $$(PWD
)/%,%,$$(CURDIR
))/$$@
318 cd embed_
$(2); $(OBJCOPY
) $(OBJCOPY_EMBED_ARGS
) $$(notdir $$<) ..
/$$@
320 CLEAN_FILES
+= embed_
$(2)/$$(notdir $(1))
326 # generate targets to embed binary & text files
327 $(foreach binfile
,$(COMPONENT_EMBED_FILES
), $(eval
$(call GenerateEmbedTarget
,$(binfile
),bin
)))
329 $(foreach txtfile
,$(COMPONENT_EMBED_TXTFILES
), $(eval
$(call GenerateEmbedTarget
,$(txtfile
),txt
)))
331 else # COMPONENT_CONFIG_ONLY is set
334 $(details
) "No build needed for $(COMPONENT_NAME) (COMPONENT_CONFIG_ONLY)"
337 $(summary
) RM component_project_vars.mk
338 rm -f component_project_vars.mk
340 component_project_vars.mk
:: # no need to add variables via component.mk
341 @echo
'# COMPONENT_CONFIG_ONLY target sets no variables here' > $@
343 endif # COMPONENT_CONFIG_ONLY