project.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #
  2. # Main Project Makefile
  3. # This Makefile is included directly from the user project Makefile in order to call the component.mk
  4. # makefiles of all components (in a separate make process) to build all the libraries, then links them
  5. # together into the final file. If so, PWD is the project dir (we assume).
  6. #
  7. #
  8. # This makefile requires the environment variable IDF_PATH to be set to the top-level esp-idf directory
  9. # where this file is located.
  10. #
  11. .PHONY: build-components menuconfig defconfig all build clean all_binaries
  12. all: all_binaries # other components will add dependencies to 'all_binaries'
  13. @echo "To flash all build output, run 'make flash' or:"
  14. @echo $(ESPTOOLPY_WRITE_FLASH) $(ESPTOOL_ALL_FLASH_ARGS)
  15. # (the reason all_binaries is used instead of 'all' is so that the flash target
  16. # can build everything without triggering the per-component "to flash..."
  17. # output targets.)
  18. help:
  19. @echo "Welcome to Espressif IDF build system. Some useful make targets:"
  20. @echo ""
  21. @echo "make menuconfig - Configure IDF project"
  22. @echo "make defconfig - Set defaults for all new configuration options"
  23. @echo ""
  24. @echo "make all - Build app, bootloader, partition table"
  25. @echo "make flash - Flash all components to a fresh chip"
  26. @echo "make clean - Remove all build output"
  27. @echo ""
  28. @echo "make app - Build just the app"
  29. @echo "make app-flash - Flash just the app"
  30. @echo "make app-clean - Clean just the app"
  31. @echo ""
  32. @echo "See also 'make bootloader', 'make bootloader-flash', 'make bootloader-clean', "
  33. @echo "'make partition_table', etc, etc."
  34. # disable built-in make rules, makes debugging saner
  35. MAKEFLAGS_OLD := $(MAKEFLAGS)
  36. MAKEFLAGS +=-rR
  37. # Figure out PROJECT_PATH if not set
  38. ifeq ("$(PROJECT_PATH)","")
  39. #The path to the project: we assume the Makefile including this file resides
  40. #in the root of that directory.
  41. PROJECT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
  42. export PROJECT_PATH
  43. endif
  44. #The directory where we put all objects/libraries/binaries. The project Makefile can
  45. #configure this if needed.
  46. BUILD_DIR_BASE ?= $(PROJECT_PATH)/build
  47. export BUILD_DIR_BASE
  48. #Component directories. These directories are searched for components.
  49. #The project Makefile can override these component dirs, or define extra component directories.
  50. COMPONENT_DIRS ?= $(PROJECT_PATH)/components $(EXTRA_COMPONENT_DIRS) $(IDF_PATH)/components
  51. export COMPONENT_DIRS
  52. #The project Makefile can define a list of components, but if it does not do this we just take
  53. #all available components in the component dirs.
  54. ifeq ("$(COMPONENTS)","")
  55. #Find all component names. The component names are the same as the
  56. #directories they're in, so /bla/components/mycomponent/ -> mycomponent. We later use
  57. #the COMPONENT_DIRS bit to find back the component path.
  58. COMPONENTS := $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/*))
  59. COMPONENTS := $(sort $(foreach comp,$(COMPONENTS),$(lastword $(subst /, ,$(comp)))))
  60. endif
  61. export COMPONENTS
  62. #Sources default to only "main"
  63. SRCDIRS ?= main
  64. #Here, we resolve and add all the components and source paths into absolute paths.
  65. #If a component exists in multiple COMPONENT_DIRS, we take the first match.
  66. #WARNING: These directories paths must be generated WITHOUT a trailing / so we
  67. #can use $(notdir x) to get the component name.
  68. COMPONENT_PATHS := $(foreach comp,$(COMPONENTS),$(firstword $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/$(comp)))))
  69. COMPONENT_PATHS += $(abspath $(SRCDIRS))
  70. #A component is buildable if it has a component.mk makefile; we assume that a
  71. # 'make -C $(component dir) -f component.mk build' results in a lib$(componentname).a
  72. COMPONENT_PATHS_BUILDABLE := $(foreach cp,$(COMPONENT_PATHS),$(if $(wildcard $(cp)/component.mk),$(cp)))
  73. # Assemble global list of include dirs (COMPONENT_INCLUDES), and
  74. # LDFLAGS args (COMPONENT_LDFLAGS) supplied by each component.
  75. COMPONENT_INCLUDES :=
  76. COMPONENT_LDFLAGS :=
  77. #
  78. # Also add any inter-component dependencies for each component.
  79. # Extract a variable from a child make process
  80. #
  81. # $(1) - path to directory to invoke make in
  82. # $(2) - name of variable to print via the get_variable target (passed in GET_VARIABLE)
  83. #
  84. # needs 'sed' processing of stdout because make sometimes echoes other stuff on stdout,
  85. # even if asked not to.
  86. #
  87. # Debugging this? Replace $(shell with $(error and you'll see the full command as-run.
  88. define GetVariable
  89. $(shell "$(MAKE)" -s --no-print-directory -C $(1) -f component.mk get_variable PROJECT_PATH=$(PROJECT_PATH) GET_VARIABLE=$(2) | sed -En "s/^$(2)=(.+)/\1/p" )
  90. endef
  91. COMPONENT_INCLUDES := $(abspath $(foreach comp,$(COMPONENT_PATHS_BUILDABLE),$(addprefix $(comp)/, \
  92. $(call GetVariable,$(comp),COMPONENT_ADD_INCLUDEDIRS))))
  93. #Also add project include path, for sdk includes
  94. COMPONENT_INCLUDES += $(abspath $(BUILD_DIR_BASE)/include/)
  95. export COMPONENT_INCLUDES
  96. #COMPONENT_LDFLAGS has a list of all flags that are needed to link the components together. It's collected
  97. #in the same way as COMPONENT_INCLUDES is.
  98. COMPONENT_LDFLAGS := $(foreach comp,$(COMPONENT_PATHS_BUILDABLE), \
  99. $(call GetVariable,$(comp),COMPONENT_ADD_LDFLAGS))
  100. export COMPONENT_LDFLAGS
  101. # Generate component dependency targets from dependencies lists
  102. # each component gains a target of its own <name>-build with dependencies
  103. # of the names of any other components (-build) that need building first
  104. #
  105. # the actual targets (that invoke submakes) are generated below by
  106. # GenerateComponentTarget macro.
  107. define GenerateComponentDependencies
  108. # $(1) = component path
  109. .PHONY: $$(notdir $(1))
  110. $$(notdir $(1))-build: $(addsuffix -build,$(call GetVariable,$(1),COMPONENT_DEPENDS))
  111. endef
  112. $(foreach comp,$(COMPONENT_PATHS_BUILDABLE), $(eval $(call GenerateComponentDependencies,$(comp))))
  113. #Make sure submakes can also use this.
  114. export PROJECT_PATH
  115. #Include functionality common to both project & component
  116. -include $(IDF_PATH)/make/common.mk
  117. # Set default LDFLAGS
  118. LDFLAGS ?= -nostdlib \
  119. -L$(IDF_PATH)/lib \
  120. -L$(IDF_PATH)/ld \
  121. $(addprefix -L$(BUILD_DIR_BASE)/,$(COMPONENTS) $(SRCDIRS)) \
  122. -u call_user_start_cpu0 \
  123. -Wl,--gc-sections \
  124. -Wl,-static \
  125. -Wl,--start-group \
  126. $(COMPONENT_LDFLAGS) \
  127. -lgcc \
  128. -Wl,--end-group \
  129. -Wl,-EL
  130. # Set default CPPFLAGS, CFLAGS, CXXFLAGS
  131. # These are exported so that components can use them when compiling.
  132. # If you need your component to add CFLAGS/etc for it's own source compilation only, set CFLAGS += in your component's Makefile.
  133. # If you need your component to add CFLAGS/etc globally for all source
  134. # files, set CFLAGS += in your component's Makefile.projbuild
  135. # If you need to set CFLAGS/CPPFLAGS/CXXFLAGS at project level, set them in application Makefile
  136. # before including project.mk. Default flags will be added before the ones provided in application Makefile.
  137. # CPPFLAGS used by C preprocessor
  138. # If any flags are defined in application Makefile, add them at the end.
  139. CPPFLAGS := -DESP_PLATFORM $(CPPFLAGS)
  140. # Warnings-related flags relevant both for C and C++
  141. COMMON_WARNING_FLAGS = -Wall -Werror \
  142. -Wno-error=unused-function \
  143. -Wno-error=unused-but-set-variable \
  144. -Wno-error=unused-variable
  145. # Flags which control code generation and dependency generation, both for C and C++
  146. COMMON_FLAGS = \
  147. -ffunction-sections -fdata-sections \
  148. -fstrict-volatile-bitfields \
  149. -mlongcalls \
  150. -nostdlib \
  151. -MMD -MP
  152. # Optimization flags are set based on menuconfig choice
  153. ifneq ("$(CONFIG_OPTIMIZATION_LEVEL_RELEASE)","")
  154. OPTIMIZATION_FLAGS = -Os
  155. CPPFLAGS += -DNDEBUG
  156. else
  157. OPTIMIZATION_FLAGS = -Og
  158. endif
  159. # Enable generation of debugging symbols
  160. OPTIMIZATION_FLAGS += -ggdb
  161. # List of flags to pass to C compiler
  162. # If any flags are defined in application Makefile, add them at the end.
  163. CFLAGS := $(strip \
  164. -std=gnu99 \
  165. $(OPTIMIZATION_FLAGS) \
  166. $(COMMON_FLAGS) \
  167. $(COMMON_WARNING_FLAGS) \
  168. $(CFLAGS))
  169. # List of flags to pass to C++ compiler
  170. # If any flags are defined in application Makefile, add them at the end.
  171. CXXFLAGS := $(strip \
  172. -std=gnu++11 \
  173. -fno-exceptions \
  174. -fno-rtti \
  175. $(OPTIMIZATION_FLAGS) \
  176. $(COMMON_FLAGS) \
  177. $(COMMON_WARNING_FLAGS) \
  178. $(CXXFLAGS))
  179. export CFLAGS CPPFLAGS CXXFLAGS
  180. #Set host compiler and binutils
  181. HOSTCC := $(CC)
  182. HOSTLD := $(LD)
  183. HOSTAR := $(AR)
  184. HOSTOBJCOPY := $(OBJCOPY)
  185. export HOSTCC HOSTLD HOSTAR HOSTOBJCOPY
  186. #Set target compiler. Defaults to whatever the user has
  187. #configured as prefix + yer olde gcc commands
  188. CC := $(call dequote,$(CONFIG_TOOLPREFIX))gcc
  189. CXX := $(call dequote,$(CONFIG_TOOLPREFIX))c++
  190. LD := $(call dequote,$(CONFIG_TOOLPREFIX))ld
  191. AR := $(call dequote,$(CONFIG_TOOLPREFIX))ar
  192. OBJCOPY := $(call dequote,$(CONFIG_TOOLPREFIX))objcopy
  193. export CC CXX LD AR OBJCOPY
  194. PYTHON=$(call dequote,$(CONFIG_PYTHON))
  195. # the app is the main executable built by the project
  196. APP_ELF:=$(BUILD_DIR_BASE)/$(PROJECT_NAME).elf
  197. APP_MAP:=$(APP_ELF:.elf=.map)
  198. APP_BIN:=$(APP_ELF:.elf=.bin)
  199. # Include any Makefile.projbuild file letting components add
  200. # configuration at the project level
  201. define includeProjBuildMakefile
  202. $(if $(V),$(if $(wildcard $(1)/Makefile.projbuild),$(info including $(1)/Makefile.projbuild...)))
  203. COMPONENT_PATH := $(1)
  204. -include $(1)/Makefile.projbuild
  205. endef
  206. $(foreach componentpath,$(COMPONENT_PATHS),$(eval $(call includeProjBuildMakefile,$(componentpath))))
  207. # once we know component paths, we can include the config
  208. include $(IDF_PATH)/make/project_config.mk
  209. # A "component" library is any library in the LDFLAGS where
  210. # the name of the library is also a name of the component
  211. APP_LIBRARIES = $(patsubst -l%,%,$(filter -l%,$(LDFLAGS)))
  212. COMPONENT_LIBRARIES = $(filter $(notdir $(COMPONENT_PATHS_BUILDABLE)),$(APP_LIBRARIES))
  213. # ELF depends on the library archive files for COMPONENT_LIBRARIES
  214. # the rules to build these are emitted as part of GenerateComponentTarget below
  215. $(APP_ELF): $(foreach libcomp,$(COMPONENT_LIBRARIES),$(BUILD_DIR_BASE)/$(libcomp)/lib$(libcomp).a)
  216. $(summary) LD $(notdir $@)
  217. $(Q) $(CC) $(LDFLAGS) -o $@ -Wl,-Map=$(APP_MAP)
  218. # Generation of $(APP_BIN) from $(APP_ELF) is added by the esptool
  219. # component's Makefile.projbuild
  220. app: $(APP_BIN)
  221. @echo "App built. Default flash app command is:"
  222. @echo $(ESPTOOLPY_WRITE_FLASH) $(CONFIG_APP_OFFSET) $(APP_BIN)
  223. all_binaries: $(APP_BIN)
  224. $(BUILD_DIR_BASE):
  225. mkdir -p $(BUILD_DIR_BASE)
  226. define GenerateComponentPhonyTarget
  227. # $(1) - path to component dir
  228. # $(2) - target to generate (build, clean)
  229. .PHONY: $(notdir $(1))-$(2)
  230. $(notdir $(1))-$(2): | $(BUILD_DIR_BASE)/$(notdir $(1))
  231. $(Q) +$(MAKE) -C $(BUILD_DIR_BASE)/$(notdir $(1)) -f $(1)/component.mk COMPONENT_BUILD_DIR=$(BUILD_DIR_BASE)/$(notdir $(1)) $(2)
  232. endef
  233. define GenerateComponentTargets
  234. # $(1) - path to component dir
  235. $(BUILD_DIR_BASE)/$(notdir $(1)):
  236. @mkdir -p $(BUILD_DIR_BASE)/$(notdir $(1))
  237. # tell make it can build any component's library by invoking the recursive -build target
  238. # (this target exists for all components even ones which don't build libraries, but it's
  239. # only invoked for the targets whose libraries appear in COMPONENT_LIBRARIES and hence the
  240. # APP_ELF dependencies.)
  241. $(BUILD_DIR_BASE)/$(notdir $(1))/lib$(notdir $(1)).a: $(notdir $(1))-build
  242. $(details) "Target '$$^' responsible for '$$@'" # echo which build target built this file
  243. endef
  244. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentTargets,$(component))))
  245. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentPhonyTarget,$(component),build)))
  246. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentPhonyTarget,$(component),clean)))
  247. app-clean: $(addsuffix -clean,$(notdir $(COMPONENT_PATHS_BUILDABLE)))
  248. $(summary) RM $(APP_ELF)
  249. $(Q) rm -f $(APP_ELF) $(APP_BIN) $(APP_MAP)
  250. clean: app-clean