project.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 check-submodules
  12. all: all_binaries
  13. # see below for recipe of 'all' target
  14. #
  15. # # other components will add dependencies to 'all_binaries'. The
  16. # reason all_binaries is used instead of 'all' is so that the flash
  17. # target can build everything without triggering the per-component "to
  18. # flash..." output targets.)
  19. help:
  20. @echo "Welcome to Espressif IDF build system. Some useful make targets:"
  21. @echo ""
  22. @echo "make menuconfig - Configure IDF project"
  23. @echo "make defconfig - Set defaults for all new configuration options"
  24. @echo ""
  25. @echo "make all - Build app, bootloader, partition table"
  26. @echo "make flash - Flash all components to a fresh chip"
  27. @echo "make clean - Remove all build output"
  28. @echo ""
  29. @echo "make app - Build just the app"
  30. @echo "make app-flash - Flash just the app"
  31. @echo "make app-clean - Clean just the app"
  32. @echo ""
  33. @echo "See also 'make bootloader', 'make bootloader-flash', 'make bootloader-clean', "
  34. @echo "'make partition_table', etc, etc."
  35. # dependency checks
  36. ifndef MAKE_RESTARTS
  37. ifeq ("$(filter 4.% 3.81 3.82,$(MAKE_VERSION))","")
  38. $(warning "esp-idf build system only supports GNU Make versions 3.81 or newer. You may see unexpected results with other Makes.")
  39. endif
  40. endif
  41. # disable built-in make rules, makes debugging saner
  42. MAKEFLAGS_OLD := $(MAKEFLAGS)
  43. MAKEFLAGS +=-rR
  44. # Default path to the project: we assume the Makefile including this file
  45. # is in the project directory
  46. ifndef PROJECT_PATH
  47. PROJECT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
  48. export PROJECT_PATH
  49. endif
  50. # A list of the "common" makefiles, to use as a target dependency
  51. COMMON_MAKEFILES := $(abspath $(IDF_PATH)/make/project.mk $(IDF_PATH)/make/common.mk $(IDF_PATH)/make/component_wrapper.mk)
  52. export COMMON_MAKEFILES
  53. # The directory where we put all objects/libraries/binaries. The project Makefile can
  54. # configure this if needed.
  55. BUILD_DIR_BASE ?= $(PROJECT_PATH)/build
  56. export BUILD_DIR_BASE
  57. # Component directories. These directories are searched for components.
  58. # The project Makefile can override these component dirs, or define extra component directories.
  59. COMPONENT_DIRS ?= $(PROJECT_PATH)/components $(EXTRA_COMPONENT_DIRS) $(IDF_PATH)/components
  60. export COMPONENT_DIRS
  61. # Source directories of the project itself (a special, project-specific component.) Defaults to only "main".
  62. SRCDIRS ?= main
  63. # The project Makefile can define a list of components, but if it does not do this we just take
  64. # all available components in the component dirs.
  65. ifndef COMPONENTS
  66. # Find all component names. The component names are the same as the
  67. # directories they're in, so /bla/components/mycomponent/ -> mycomponent. We then use
  68. # COMPONENT_DIRS to build COMPONENT_PATHS with the full path to each component.
  69. COMPONENTS := $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/*))
  70. COMPONENTS := $(sort $(foreach comp,$(COMPONENTS),$(lastword $(subst /, ,$(comp)))))
  71. endif
  72. export COMPONENTS
  73. # Resolve all of COMPONENTS into absolute paths in COMPONENT_PATHS.
  74. #
  75. # If a component name exists in multiple COMPONENT_DIRS, we take the first match.
  76. #
  77. # NOTE: These paths must be generated WITHOUT a trailing / so we
  78. # can use $(notdir x) to get the component name.
  79. COMPONENT_PATHS := $(foreach comp,$(COMPONENTS),$(firstword $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/$(comp)))))
  80. COMPONENT_PATHS += $(abspath $(SRCDIRS))
  81. # A component is buildable if it has a component.mk makefile in it
  82. COMPONENT_PATHS_BUILDABLE := $(foreach cp,$(COMPONENT_PATHS),$(if $(wildcard $(cp)/component.mk),$(cp)))
  83. # If TESTS_ALL set to 1, set TEST_COMPONENTS to all components
  84. ifeq ($(TESTS_ALL),1)
  85. TEST_COMPONENTS := $(COMPONENTS)
  86. endif
  87. # If TEST_COMPONENTS is set, create variables for building unit tests
  88. ifdef TEST_COMPONENTS
  89. override TEST_COMPONENTS := $(foreach comp,$(TEST_COMPONENTS),$(wildcard $(IDF_PATH)/components/$(comp)/test))
  90. TEST_COMPONENT_PATHS := $(TEST_COMPONENTS)
  91. TEST_COMPONENT_NAMES := $(foreach comp,$(TEST_COMPONENTS),$(lastword $(subst /, ,$(dir $(comp))))_test)
  92. endif
  93. # Initialise project-wide variables which can be added to by
  94. # each component.
  95. #
  96. # These variables are built up via the component_project_vars.mk
  97. # generated makefiles (one per component).
  98. #
  99. # See docs/build-system.rst for more details.
  100. COMPONENT_INCLUDES :=
  101. COMPONENT_LDFLAGS :=
  102. COMPONENT_SUBMODULES :=
  103. # COMPONENT_PROJECT_VARS is the list of component_project_vars.mk generated makefiles
  104. # for each component.
  105. #
  106. # Including $(COMPONENT_PROJECT_VARS) builds the COMPONENT_INCLUDES,
  107. # COMPONENT_LDFLAGS variables and also targets for any inter-component
  108. # dependencies.
  109. #
  110. # See the component_project_vars.mk target in component_wrapper.mk
  111. COMPONENT_PROJECT_VARS := $(addsuffix /component_project_vars.mk,$(notdir $(COMPONENT_PATHS_BUILDABLE) ) $(TEST_COMPONENT_NAMES))
  112. COMPONENT_PROJECT_VARS := $(addprefix $(BUILD_DIR_BASE)/,$(COMPONENT_PROJECT_VARS))
  113. # this line is -include instead of include to prevent a spurious error message on make 3.81
  114. -include $(COMPONENT_PROJECT_VARS)
  115. # Also add top-level project include path, for top-level includes
  116. COMPONENT_INCLUDES += $(abspath $(BUILD_DIR_BASE)/include/)
  117. export COMPONENT_INCLUDES
  118. # Set variables common to both project & component
  119. include $(IDF_PATH)/make/common.mk
  120. all:
  121. ifdef CONFIG_SECURE_BOOTLOADER_ENABLED
  122. @echo "(Secure boot enabled, so bootloader not flashed automatically. See 'make bootloader' output)"
  123. @echo "To flash app & partition table, run 'make flash' or:"
  124. else
  125. @echo "To flash all build output, run 'make flash' or:"
  126. endif
  127. @echo $(ESPTOOLPY_WRITE_FLASH) $(ESPTOOL_ALL_FLASH_ARGS)
  128. # Set default LDFLAGS
  129. LDFLAGS ?= -nostdlib \
  130. -L$(IDF_PATH)/lib \
  131. -L$(IDF_PATH)/ld \
  132. $(addprefix -L$(BUILD_DIR_BASE)/,$(COMPONENTS) $(TEST_COMPONENT_NAMES) $(SRCDIRS) ) \
  133. -u call_user_start_cpu0 \
  134. $(EXTRA_LDFLAGS) \
  135. -Wl,--gc-sections \
  136. -Wl,-static \
  137. -Wl,--start-group \
  138. $(COMPONENT_LDFLAGS) \
  139. -lgcc \
  140. -Wl,--end-group \
  141. -Wl,-EL
  142. # Set default CPPFLAGS, CFLAGS, CXXFLAGS
  143. # These are exported so that components can use them when compiling.
  144. # If you need your component to add CFLAGS/etc for it's own source compilation only, set CFLAGS += in your component's Makefile.
  145. # If you need your component to add CFLAGS/etc globally for all source
  146. # files, set CFLAGS += in your component's Makefile.projbuild
  147. # If you need to set CFLAGS/CPPFLAGS/CXXFLAGS at project level, set them in application Makefile
  148. # before including project.mk. Default flags will be added before the ones provided in application Makefile.
  149. # CPPFLAGS used by C preprocessor
  150. # If any flags are defined in application Makefile, add them at the end.
  151. CPPFLAGS := -DESP_PLATFORM $(CPPFLAGS) $(EXTRA_CPPFLAGS)
  152. # Warnings-related flags relevant both for C and C++
  153. COMMON_WARNING_FLAGS = -Wall -Werror=all \
  154. -Wno-error=unused-function \
  155. -Wno-error=unused-but-set-variable \
  156. -Wno-error=unused-variable \
  157. -Wno-error=deprecated-declarations \
  158. -Wextra \
  159. -Wno-unused-parameter -Wno-sign-compare
  160. # Flags which control code generation and dependency generation, both for C and C++
  161. COMMON_FLAGS = \
  162. -ffunction-sections -fdata-sections \
  163. -fstrict-volatile-bitfields \
  164. -mlongcalls \
  165. -nostdlib \
  166. -MMD -MP
  167. # Optimization flags are set based on menuconfig choice
  168. ifneq ("$(CONFIG_OPTIMIZATION_LEVEL_RELEASE)","")
  169. OPTIMIZATION_FLAGS = -Os
  170. CPPFLAGS += -DNDEBUG
  171. else
  172. OPTIMIZATION_FLAGS = -Og
  173. endif
  174. # Enable generation of debugging symbols
  175. OPTIMIZATION_FLAGS += -ggdb
  176. # List of flags to pass to C compiler
  177. # If any flags are defined in application Makefile, add them at the end.
  178. CFLAGS := $(strip \
  179. -std=gnu99 \
  180. $(OPTIMIZATION_FLAGS) \
  181. $(COMMON_FLAGS) \
  182. $(COMMON_WARNING_FLAGS) -Wno-old-style-declaration \
  183. $(CFLAGS) \
  184. $(EXTRA_CFLAGS))
  185. # List of flags to pass to C++ compiler
  186. # If any flags are defined in application Makefile, add them at the end.
  187. CXXFLAGS := $(strip \
  188. -std=gnu++11 \
  189. -fno-exceptions \
  190. -fno-rtti \
  191. $(OPTIMIZATION_FLAGS) \
  192. $(COMMON_FLAGS) \
  193. $(COMMON_WARNING_FLAGS) \
  194. $(CXXFLAGS) \
  195. $(EXTRA_CXXFLAGS))
  196. export CFLAGS CPPFLAGS CXXFLAGS
  197. # Set host compiler and binutils
  198. HOSTCC := $(CC)
  199. HOSTLD := $(LD)
  200. HOSTAR := $(AR)
  201. HOSTOBJCOPY := $(OBJCOPY)
  202. export HOSTCC HOSTLD HOSTAR HOSTOBJCOPY
  203. # Set target compiler. Defaults to whatever the user has
  204. # configured as prefix + ye olde gcc commands
  205. CC := $(call dequote,$(CONFIG_TOOLPREFIX))gcc
  206. CXX := $(call dequote,$(CONFIG_TOOLPREFIX))c++
  207. LD := $(call dequote,$(CONFIG_TOOLPREFIX))ld
  208. AR := $(call dequote,$(CONFIG_TOOLPREFIX))ar
  209. OBJCOPY := $(call dequote,$(CONFIG_TOOLPREFIX))objcopy
  210. export CC CXX LD AR OBJCOPY
  211. PYTHON=$(call dequote,$(CONFIG_PYTHON))
  212. # the app is the main executable built by the project
  213. APP_ELF:=$(BUILD_DIR_BASE)/$(PROJECT_NAME).elf
  214. APP_MAP:=$(APP_ELF:.elf=.map)
  215. APP_BIN:=$(APP_ELF:.elf=.bin)
  216. # Include any Makefile.projbuild file letting components add
  217. # configuration at the project level
  218. define includeProjBuildMakefile
  219. $(if $(V),$(if $(wildcard $(1)/Makefile.projbuild),$(info including $(1)/Makefile.projbuild...)))
  220. COMPONENT_PATH := $(1)
  221. -include $(1)/Makefile.projbuild
  222. endef
  223. $(foreach componentpath,$(COMPONENT_PATHS),$(eval $(call includeProjBuildMakefile,$(componentpath))))
  224. # once we know component paths, we can include the config generation targets
  225. #
  226. # (bootloader build doesn't need this, config is exported from top-level)
  227. ifndef IS_BOOTLOADER_BUILD
  228. include $(IDF_PATH)/make/project_config.mk
  229. endif
  230. # A "component" library is any library in the LDFLAGS where
  231. # the name of the library is also a name of the component
  232. APP_LIBRARIES = $(patsubst -l%,%,$(filter -l%,$(LDFLAGS)))
  233. COMPONENT_LIBRARIES = $(filter $(notdir $(COMPONENT_PATHS_BUILDABLE)) $(TEST_COMPONENT_NAMES),$(APP_LIBRARIES))
  234. # ELF depends on the library archive files for COMPONENT_LIBRARIES
  235. # the rules to build these are emitted as part of GenerateComponentTarget below
  236. $(APP_ELF): $(foreach libcomp,$(COMPONENT_LIBRARIES),$(BUILD_DIR_BASE)/$(libcomp)/lib$(libcomp).a)
  237. $(summary) LD $(notdir $@)
  238. $(CC) $(LDFLAGS) -o $@ -Wl,-Map=$(APP_MAP)
  239. # Generation of $(APP_BIN) from $(APP_ELF) is added by the esptool
  240. # component's Makefile.projbuild
  241. app: $(APP_BIN)
  242. @echo "App built. Default flash app command is:"
  243. @echo $(ESPTOOLPY_WRITE_FLASH) $(CONFIG_APP_OFFSET) $(APP_BIN)
  244. all_binaries: $(APP_BIN)
  245. $(BUILD_DIR_BASE):
  246. mkdir -p $(BUILD_DIR_BASE)
  247. # Macro for the recursive sub-make for each component
  248. # $(1) - component directory
  249. # $(2) - component name only
  250. #
  251. # Is recursively expanded by the GenerateComponentTargets macro
  252. define ComponentMake
  253. +$(MAKE) -C $(BUILD_DIR_BASE)/$(2) -f $(IDF_PATH)/make/component_wrapper.mk COMPONENT_MAKEFILE=$(1)/component.mk COMPONENT_NAME=$(2)
  254. endef
  255. # Generate top-level component-specific targets for each component
  256. # $(1) - path to component dir
  257. # $(2) - name of component
  258. #
  259. define GenerateComponentTargets
  260. .PHONY: $(2)-build $(2)-clean
  261. $(2)-build: check-submodules
  262. $(call ComponentMake,$(1),$(2)) build
  263. $(2)-clean:
  264. $(call ComponentMake,$(1),$(2)) clean
  265. $(BUILD_DIR_BASE)/$(2):
  266. @mkdir -p $(BUILD_DIR_BASE)/$(2)
  267. # tell make it can build any component's library by invoking the -build target
  268. # (this target exists for all components even ones which don't build libraries, but it's
  269. # only invoked for the targets whose libraries appear in COMPONENT_LIBRARIES and hence the
  270. # APP_ELF dependencies.)
  271. $(BUILD_DIR_BASE)/$(2)/lib$(2).a: $(2)-build
  272. $(details) "Target '$$^' responsible for '$$@'" # echo which build target built this file
  273. # add a target to generate the component_project_vars.mk files that
  274. # are used to inject variables into project make pass (see matching
  275. # component_project_vars.mk target in component_wrapper.mk).
  276. #
  277. # If any component_project_vars.mk file is out of date, the make
  278. # process will call this target to rebuild it and then restart.
  279. #
  280. # Note: $(SDKCONFIG) is a normal prereq as we need to rebuild these
  281. # files whenever the config changes. $(SDKCONFIG_MAKEFILE) is an
  282. # order-only prereq because if it hasn't been rebuilt, we need to
  283. # build it first - but including it as a normal prereq can lead to
  284. # infinite restarts as the conf process will keep updating it.
  285. $(BUILD_DIR_BASE)/$(2)/component_project_vars.mk: $(1)/component.mk $(COMMON_MAKEFILES) $(SDKCONFIG) | $(BUILD_DIR_BASE)/$(2) $(SDKCONFIG_MAKEFILE)
  286. $(call ComponentMake,$(1),$(2)) component_project_vars.mk
  287. endef
  288. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentTargets,$(component),$(notdir $(component)))))
  289. $(foreach component,$(TEST_COMPONENT_PATHS),$(eval $(call GenerateComponentTargets,$(component),$(lastword $(subst /, ,$(dir $(component))))_test)))
  290. app-clean: $(addsuffix -clean,$(notdir $(COMPONENT_PATHS_BUILDABLE)))
  291. $(summary) RM $(APP_ELF)
  292. rm -f $(APP_ELF) $(APP_BIN) $(APP_MAP)
  293. # NB: this ordering is deliberate (app-clean before config-clean),
  294. # so config remains valid during all component clean targets
  295. config-clean: app-clean
  296. clean: config-clean
  297. # phony target to check if any git submodule listed in COMPONENT_SUBMODULES are missing
  298. # or out of date, and exit if so. Components can add paths to this variable.
  299. #
  300. # This only works for components inside IDF_PATH
  301. check-submodules:
  302. # Generate a target to check this submodule
  303. # $(1) - submodule directory, relative to IDF_PATH
  304. define GenerateSubmoduleCheckTarget
  305. check-submodules: $(IDF_PATH)/$(1)/.git
  306. $(IDF_PATH)/$(1)/.git:
  307. @echo "WARNING: Missing submodule $(1)..."
  308. [ -d ${IDF_PATH}/.git ] || ( echo "ERROR: esp-idf must be cloned from git to work."; exit 1)
  309. [ -x $(which git) ] || ( echo "ERROR: Need to run 'git submodule init $(1)' in esp-idf root directory."; exit 1)
  310. @echo "Attempting 'git submodule update --init $(1)' in esp-idf root directory..."
  311. cd ${IDF_PATH} && git submodule update --init $(1)
  312. # Parse 'git submodule status' output for out-of-date submodule.
  313. # Status output prefixes status line with '+' if the submodule commit doesn't match
  314. ifneq ("$(shell cd ${IDF_PATH} && git submodule status $(1) | grep '^+')","")
  315. $$(info WARNING: git submodule $(1) may be out of date. Run 'git submodule update' to update.)
  316. endif
  317. endef
  318. # filter/subst in expression ensures all submodule paths begin with $(IDF_PATH), and then strips that prefix
  319. # so the argument is suitable for use with 'git submodule' commands
  320. $(foreach submodule,$(subst $(IDF_PATH)/,,$(filter $(IDF_PATH)/%,$(COMPONENT_SUBMODULES))),$(eval $(call GenerateSubmoduleCheckTarget,$(submodule))))