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