]> granicus.if.org Git - esp-idf/blob - make/component_common.mk
build system: Add support for embedded arbitrary binary or text files in .rodata
[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 #Names of binary files to embed as symbols in the component library
40 COMPONENT_EMBED_FILES ?= 
41
42 #Object files which need to be added to the library
43 #By default we take all .c/.S files in the component directory.
44 ifeq ("$(COMPONENT_OBJS)", "")
45 #Find all source files in all COMPONENT_SRCDIRS
46 COMPONENT_OBJS := $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.c,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.c)))
47 COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cpp,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cpp)))
48 COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.S,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.S)))
49 #Make relative by removing COMPONENT_PATH from all found object paths
50 COMPONENT_OBJS := $(patsubst $(COMPONENT_PATH)/%,%,$(COMPONENT_OBJS))
51 endif
52
53 #Object files with embedded binaries to add to the component library
54 #Correspond to the files named in COMPONENT_EMBED_FILES & COMPONENT_EMBED_TXTFILES
55 COMPONENT_EMBED_OBJS ?= $(addsuffix .bin.o,$(COMPONENT_EMBED_FILES)) $(addsuffix .txt.o,$(COMPONENT_EMBED_TXTFILES))
56
57 #By default, include only the include/ dir.
58 COMPONENT_ADD_INCLUDEDIRS ?= include
59 COMPONENT_ADD_LDFLAGS ?= -l$(COMPONENT_NAME)
60
61 #If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
62 #variable with all the include dirs from all the components in random order. This
63 #means we can accidentally grab a header from another component before grabbing our own.
64 #To make sure that does not happen, re-order the includes so ours come first.
65 OWN_INCLUDES:=$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS) $(COMPONENT_PRIV_INCLUDEDIRS)))
66 COMPONENT_INCLUDES := $(OWN_INCLUDES) $(filter-out $(OWN_INCLUDES),$(COMPONENT_INCLUDES))
67
68 #This target is used to collect variable values from inside project.mk
69 # see project.mk GetVariable macro for details.
70 get_variable:
71         @echo "$(GET_VARIABLE)=$(call $(GET_VARIABLE)) "
72
73 #Targets for build/clean. Use builtin recipe if component Makefile
74 #hasn't defined its own.
75 ifeq ("$(COMPONENT_OWNBUILDTARGET)", "")
76 build: $(COMPONENT_LIBRARY)
77         @mkdir -p $(COMPONENT_SRCDIRS)
78
79 #Build the archive. We remove the archive first, otherwise ar will get confused if we update
80 #an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
81 $(COMPONENT_LIBRARY): $(COMPONENT_OBJS) $(COMPONENT_EMBED_OBJS)
82         $(summary) AR $@
83         $(Q) rm -f $@
84         $(Q) $(AR) cru $@ $^
85 endif
86
87 ifeq ("$(COMPONENT_OWNCLEANTARGET)", "")
88 clean:
89         $(summary) RM $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EMBED_OBJS) $(COMPONENT_EXTRA_CLEAN)
90         $(Q) rm -f $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EMBED_OBJS) $(COMPONENT_EXTRA_CLEAN)
91 endif
92
93 #Include all dependency files already generated
94 -include $(COMPONENT_OBJS:.o=.d)
95
96 #This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
97 define GenerateCompileTargets
98 # $(1) - directory containing source files, relative to $(COMPONENT_PATH)
99 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c | $(1)
100         $$(summary) CC $$@
101         $$(Q) $$(CC) $$(CFLAGS) $(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
102
103 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp | $(1)
104         $$(summary) CXX $$@
105         $$(Q) $$(CXX) $$(CXXFLAGS) $(CPPFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
106
107 $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S | $(1)
108         $$(summary) AS $$@
109         $$(Q) $$(CC) $$(CFLAGS) $(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
110
111 endef
112
113 define GenerateBuildDirTarget
114 # CWD is build dir, create the build subdirectory $(1) if it doesn't exist
115 $(1):
116         @mkdir -p $(1)
117 endef
118
119 #Generate all the compile target recipes & the build directory recipes
120 $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir)))  $(eval $(call GenerateBuildDirTarget,$(srcdir))))
121
122 ## Support for embedding binary files into the ELF as symbols
123
124 OBJCOPY_EMBED_ARGS := --input binary --output elf32-xtensa-le --binary-architecture xtensa --rename-section .data=.rodata.embedded
125
126 # Generate pattern for embedding text or binary files into the app
127 # $(1) is name of file (as relative path inside component)
128 # $(2) is txt or bin depending on file contents
129 #
130 # txt files are null-terminated before being embedded (otherwise
131 # identical behaviour.)
132 #
133 # Files are temporarily copied to the build directory before objcopy,
134 # because objcopy generates the symbol name from the full command line
135 # path to the input file.
136 define GenerateEmbedTarget
137 $(1).$(2).o: $$(COMPONENT_PATH)/$(1) | $$(dir $(1))
138         $$(summary) EMBED $$@
139         $$(Q) cp $$< $$(notdir $$<)
140         $$(Q) $(if $(subst bin,,$(2)),echo -ne '\0' >> $$(notdir $$<) )
141         $$(Q) $$(OBJCOPY) $(OBJCOPY_EMBED_ARGS) $$(notdir $$<) $$@
142         $$(Q) rm $$(notdir $$<)
143 endef
144
145 # generate targets to embed binary & text files
146 $(foreach binfile,$(COMPONENT_EMBED_FILES), $(eval $(call GenerateEmbedTarget,$(binfile),bin)))
147
148 $(foreach txtfile,$(COMPONENT_EMBED_TXTFILES), $(eval $(call GenerateEmbedTarget,$(txtfile),txt)))
149
150 # generate targets to create binary embed directories
151 $(foreach bindir,$(sort $(dir $(COMPONENT_EMBED_FILES))), $(eval $(call GenerateBuildDirTarget,$(bindir))))