project.mk 17 KB

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