]> granicus.if.org Git - esp-idf/blob - make/component_common.mk
build system: Replace get_variable target w/ component_project_vars.mk generated...
[esp-idf] / make / component_common.mk
1 # Component common makefile
2 #
3 # This Makefile gets included in the Makefile of all the components to set the correct include paths etc.
4 # PWD is the build directory of the component and the top Makefile is the one in the
5 # component source dir.
6 #
7 # The way the Makefile differentiates between those two is by looking at the environment
8 # variable PROJECT_PATH. If this is set (to the basepath of the project), we're building a
9 # component and its Makefile has included this makefile. If not, we're building the entire project.
10 #
11
12 #
13 # This Makefile requires the environment variable IDF_PATH to be set
14 # to the top-level directory where ESP-IDF is located (the directory
15 # containing this 'make' directory).
16 #
17
18 ifeq ("$(PROJECT_PATH)","")
19 $(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.)
20 endif
21
22 # Find the path to the component
23 COMPONENT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
24 export COMPONENT_PATH
25
26 include $(IDF_PATH)/make/common.mk
27
28 #Some of these options are overridable by the component's component.mk Makefile
29
30 #Name of the component
31 COMPONENT_NAME ?= $(lastword $(subst /, ,$(realpath $(COMPONENT_PATH))))
32
33 #Absolute path of the .a file
34 COMPONENT_LIBRARY := lib$(COMPONENT_NAME).a
35
36 #Source dirs a component has. Default to root directory of component.
37 COMPONENT_SRCDIRS ?= .
38
39 #Object files which need to be linked into the library
40 #By default we take all .c/.S files in the component directory.
41 ifeq ("$(COMPONENT_OBJS)", "")
42 #Find all source files in all COMPONENT_SRCDIRS
43 COMPONENT_OBJS := $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.c,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.c)))
44 COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cpp,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cpp)))
45 COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.S,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.S)))
46 #Make relative by removing COMPONENT_PATH from all found object paths
47 COMPONENT_OBJS := $(patsubst $(COMPONENT_PATH)/%,%,$(COMPONENT_OBJS))
48 endif
49
50 #By default, include only the include/ dir.
51 COMPONENT_ADD_INCLUDEDIRS ?= include
52 COMPONENT_ADD_LDFLAGS ?= -l$(COMPONENT_NAME)
53
54 #If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
55 #variable with all the include dirs from all the components in random order. This
56 #means we can accidentally grab a header from another component before grabbing our own.
57 #To make sure that does not happen, re-order the includes so ours come first.
58 OWN_INCLUDES:=$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS) $(COMPONENT_PRIV_INCLUDEDIRS)))
59 COMPONENT_INCLUDES := $(OWN_INCLUDES) $(filter-out $(OWN_INCLUDES),$(COMPONENT_INCLUDES))
60
61 # This target is used to take component.mk variables COMPONENT_ADD_INCLUDEDIRS,
62 # COMPONENT_ADD_LDFLAGS and COMPONENT_DEPENDS and inject them into the project
63 # makefile level.
64 #
65 # The target here has no dependencies, as the parent target in
66 # project.mk evaluates dependencies before calling down to here. See
67 # GenerateProjectVarsTarget in project.mk.
68 component_project_vars.mk::
69         $(details) "Rebuilding component project variables list $(abspath $@)"
70         @echo "# Automatically generated build file. Do not edit." > $@
71         @echo "COMPONENT_INCLUDES += $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS))" >> $@
72         @echo "COMPONENT_LDFLAGS += $(COMPONENT_ADD_LDFLAGS)" >> $@
73         @echo "$(COMPONENT_NAME)-build: $(addsuffix -build,$(COMPONENT_DEPENDS))" >> $@
74
75 #Targets for build/clean. Use builtin recipe if component Makefile
76 #hasn't defined its own.
77 ifeq ("$(COMPONENT_OWNBUILDTARGET)", "")
78 build: $(COMPONENT_LIBRARY)
79         @mkdir -p $(COMPONENT_SRCDIRS)
80
81 #Build the archive. We remove the archive first, otherwise ar will get confused if we update
82 #an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
83 $(COMPONENT_LIBRARY): $(COMPONENT_OBJS)
84         $(summary) AR $@
85         $(Q) rm -f $@
86         $(Q) $(AR) cru $@ $(COMPONENT_OBJS)
87 endif
88
89 CLEAN_FILES = $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN) component_project_vars.mk
90
91 ifeq ("$(COMPONENT_OWNCLEANTARGET)", "")
92 clean:
93         $(summary) RM $(CLEAN_FILES)
94         $(Q) rm -f $(CLEAN_FILES)
95 endif
96
97 #Include all dependency files already generated
98 -include $(COMPONENT_OBJS:.o=.d)
99
100 #This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
101 define GenerateCompileTargets
102 # $(1) - directory containing source files, relative to $(COMPONENT_PATH)
103 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c | $(1)
104         $$(summary) CC $$@
105         $$(Q) $$(CC) $$(CFLAGS) $(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
106
107 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp | $(1)
108         $$(summary) CXX $$@
109         $$(Q) $$(CXX) $$(CXXFLAGS) $(CPPFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
110
111 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S | $(1)
112         $$(summary) AS $$@
113         $$(Q) $$(CC) $$(CFLAGS) $(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
114
115 # CWD is build dir, create the build subdirectory if it doesn't exist
116 $(1):
117         @mkdir -p $(1)
118 endef
119
120 #Generate all the compile target recipes
121 $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir))))