]> granicus.if.org Git - esp-idf/blob - make/component_common.mk
Merge branch 'master' into feature/menuconfig_cpu_frequency_option
[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 collect variable values from inside project.mk
62 # see project.mk GetVariable macro for details.
63 get_variable:
64         @echo "$(GET_VARIABLE)=$(call $(GET_VARIABLE)) "
65
66 #Targets for build/clean. Use builtin recipe if component Makefile
67 #hasn't defined its own.
68 ifeq ("$(COMPONENT_OWNBUILDTARGET)", "")
69 build: $(COMPONENT_LIBRARY)
70         @mkdir -p $(COMPONENT_SRCDIRS)
71
72 #Build the archive. We remove the archive first, otherwise ar will get confused if we update
73 #an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
74 $(COMPONENT_LIBRARY): $(COMPONENT_OBJS)
75         $(summary) AR $@
76         $(Q) rm -f $@
77         $(Q) $(AR) cru $@ $(COMPONENT_OBJS)
78 endif
79
80 ifeq ("$(COMPONENT_OWNCLEANTARGET)", "")
81 clean:
82         $(summary) RM $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN)
83         $(Q) rm -f $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN)
84 endif
85
86 #Include all dependency files already generated
87 -include $(COMPONENT_OBJS:.o=.d)
88
89 #This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
90 define GenerateCompileTargets
91 # $(1) - directory containing source files, relative to $(COMPONENT_PATH)
92 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c | $(1)
93         $$(summary) CC $$@
94         $$(Q) $$(CC) $$(CFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
95
96 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp | $(1)
97         $$(summary) CC $$@
98         $$(Q) $$(CXX) $$(CXXFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
99
100 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S | $(1)
101         $$(summary) CC $$@
102         $$(Q) $$(CC) $$(CFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
103
104 # CWD is build dir, create the build subdirectory if it doesn't exist
105 $(1):
106         @mkdir -p $(1)
107 endef
108
109 #Generate all the compile target recipes
110 $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir))))