project.mk 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 size-components size-files size-symbols list-components
  12. MAKECMDGOALS ?= all
  13. all: all_binaries | check_python_dependencies
  14. # see below for recipe of 'all' target
  15. #
  16. # # other components will add dependencies to 'all_binaries'. The
  17. # reason all_binaries is used instead of 'all' is so that the flash
  18. # target can build everything without triggering the per-component "to
  19. # flash..." output targets.)
  20. help:
  21. @echo "Welcome to Espressif IDF build system. Some useful make targets:"
  22. @echo ""
  23. @echo "make menuconfig - Configure IDF project"
  24. @echo "make defconfig - Set defaults for all new configuration options"
  25. @echo ""
  26. @echo "make all - Build app, bootloader, partition table"
  27. @echo "make flash - Flash app, bootloader, partition table to a chip"
  28. @echo "make clean - Remove all build output"
  29. @echo "make size - Display the static memory footprint of the app"
  30. @echo "make size-components, size-files - Finer-grained memory footprints"
  31. @echo "make size-symbols - Per symbol memory footprint. Requires COMPONENT=<component>"
  32. @echo "make erase_flash - Erase entire flash contents"
  33. @echo "make erase_otadata - Erase ota_data partition; First bootable partition (factory or OTAx) will be used on next boot."
  34. @echo " This assumes this project's partition table is the one flashed on the device."
  35. @echo "make monitor - Run idf_monitor tool to monitor serial output from app"
  36. @echo "make simple_monitor - Monitor serial output on terminal console"
  37. @echo "make list-components - List all components in the project"
  38. @echo ""
  39. @echo "make app - Build just the app"
  40. @echo "make app-flash - Flash just the app"
  41. @echo "make app-clean - Clean just the app"
  42. @echo "make print_flash_cmd - Print the arguments for esptool when flash"
  43. @echo "make check_python_dependencies - Check that the required python packages are installed"
  44. @echo ""
  45. @echo "See also 'make bootloader', 'make bootloader-flash', 'make bootloader-clean', "
  46. @echo "'make partition_table', etc, etc."
  47. # Non-interactive targets. Mostly, those for which you do not need to build a binary
  48. NON_INTERACTIVE_TARGET += defconfig clean% %clean help list-components print_flash_cmd check_python_dependencies
  49. # dependency checks
  50. ifndef MAKE_RESTARTS
  51. ifeq ("$(filter 4.% 3.81 3.82,$(MAKE_VERSION))","")
  52. $(warning esp-idf build system only supports GNU Make versions 3.81 or newer. You may see unexpected results with other Makes.)
  53. endif
  54. ifdef MSYSTEM
  55. ifneq ("$(MSYSTEM)","MINGW32")
  56. $(warning esp-idf build system only supports MSYS2 in "MINGW32" mode. Consult the ESP-IDF documentation for details.)
  57. endif
  58. endif # MSYSTEM
  59. endif # MAKE_RESTARTS
  60. # can't run 'clean' along with any non-clean targets
  61. ifneq ("$(filter clean% %clean,$(MAKECMDGOALS))" ,"")
  62. ifneq ("$(filter-out clean% %clean,$(MAKECMDGOALS))", "")
  63. $(error esp-idf build system doesn't support running 'clean' targets along with any others. Run 'make clean' and then run other targets separately.)
  64. endif
  65. endif
  66. OS ?=
  67. # make IDF_PATH a "real" absolute path
  68. # * works around the case where a shell character is embedded in the environment variable value.
  69. # * changes Windows-style C:/blah/ paths to MSYS style /c/blah
  70. ifeq ("$(OS)","Windows_NT")
  71. # On Windows MSYS2, make wildcard function returns empty string for paths of form /xyz
  72. # where /xyz is a directory inside the MSYS root - so we don't use it.
  73. SANITISED_IDF_PATH:=$(realpath $(IDF_PATH))
  74. else
  75. SANITISED_IDF_PATH:=$(realpath $(wildcard $(IDF_PATH)))
  76. endif
  77. export IDF_PATH := $(SANITISED_IDF_PATH)
  78. ifndef IDF_PATH
  79. $(error IDF_PATH variable is not set to a valid directory.)
  80. endif
  81. ifdef IDF_TARGET
  82. ifneq ($(IDF_TARGET),esp32)
  83. $(error GNU Make based build system only supports esp32 target, but IDF_TARGET is set to $(IDF_TARGET))
  84. endif
  85. else
  86. export IDF_TARGET := esp32
  87. endif
  88. ifneq ("$(IDF_PATH)","$(SANITISED_IDF_PATH)")
  89. # implies IDF_PATH was overriden on make command line.
  90. # Due to the way make manages variables, this is hard to account for
  91. #
  92. # if you see this error, do the shell expansion in the shell ie
  93. # make IDF_PATH=~/blah not make IDF_PATH="~/blah"
  94. $(error If IDF_PATH is overriden on command line, it must be an absolute path with no embedded shell special characters)
  95. endif
  96. ifneq ("$(IDF_PATH)","$(subst :,,$(IDF_PATH))")
  97. $(error IDF_PATH cannot contain colons. If overriding IDF_PATH on Windows, use MSYS Unix-style /c/dir instead of C:/dir)
  98. endif
  99. # disable built-in make rules, makes debugging saner
  100. MAKEFLAGS_OLD := $(MAKEFLAGS)
  101. MAKEFLAGS +=-rR
  102. # Default path to the project: we assume the Makefile including this file
  103. # is in the project directory
  104. ifndef PROJECT_PATH
  105. PROJECT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
  106. export PROJECT_PATH
  107. endif
  108. # A list of the "common" makefiles, to use as a target dependency
  109. COMMON_MAKEFILES := $(abspath $(IDF_PATH)/make/project.mk $(IDF_PATH)/make/common.mk $(IDF_PATH)/make/version.mk $(IDF_PATH)/make/component_wrapper.mk $(firstword $(MAKEFILE_LIST)))
  110. export COMMON_MAKEFILES
  111. # The directory where we put all objects/libraries/binaries. The project Makefile can
  112. # configure this if needed.
  113. ifndef BUILD_DIR_BASE
  114. BUILD_DIR_BASE := $(PROJECT_PATH)/build
  115. endif
  116. ifneq ("$(BUILD_DIR_BASE)","$(subst :,,$(BUILD_DIR_BASE))")
  117. $(error BUILD_DIR_BASE ($(BUILD_DIR_BASE)) cannot contain colons. If setting this path on Windows, use MSYS Unix-style /c/dir instead of C:/dir)
  118. endif
  119. BUILD_DIR_BASE := $(abspath $(BUILD_DIR_BASE))
  120. export BUILD_DIR_BASE
  121. # Component directories. These directories are searched for components (either the directory is a component,
  122. # or the directory contains subdirectories which are components.)
  123. # The project Makefile can override these component dirs, or add extras via EXTRA_COMPONENT_DIRS
  124. ifndef COMPONENT_DIRS
  125. EXTRA_COMPONENT_DIRS ?=
  126. COMPONENT_DIRS := $(PROJECT_PATH)/components $(EXTRA_COMPONENT_DIRS) $(IDF_PATH)/components $(PROJECT_PATH)/main
  127. endif
  128. # Make sure that every directory in the list is an absolute path without trailing slash.
  129. # This is necessary to split COMPONENT_DIRS into SINGLE_COMPONENT_DIRS and MULTI_COMPONENT_DIRS below.
  130. COMPONENT_DIRS := $(foreach cd,$(COMPONENT_DIRS),$(abspath $(cd)))
  131. export COMPONENT_DIRS
  132. ifdef SRCDIRS
  133. $(warning SRCDIRS variable is deprecated. These paths can be added to EXTRA_COMPONENT_DIRS or COMPONENT_DIRS instead.)
  134. COMPONENT_DIRS += $(abspath $(SRCDIRS))
  135. endif
  136. # List of component directories, i.e. directories which contain a component.mk file
  137. SINGLE_COMPONENT_DIRS := $(abspath $(dir $(dir $(foreach cd,$(COMPONENT_DIRS),\
  138. $(wildcard $(cd)/component.mk)))))
  139. # List of components directories, i.e. directories which may contain components
  140. MULTI_COMPONENT_DIRS := $(filter-out $(SINGLE_COMPONENT_DIRS),$(COMPONENT_DIRS))
  141. # The project Makefile can define a list of components, but if it does not do this
  142. # we just take all available components in the component dirs.
  143. # A component is COMPONENT_DIRS directory, or immediate subdirectory,
  144. # which contains a component.mk file.
  145. #
  146. # Use the "make list-components" target to debug this step.
  147. ifndef COMPONENTS
  148. # Find all component names. The component names are the same as the
  149. # directories they're in, so /bla/components/mycomponent/component.mk -> mycomponent.
  150. # We need to do this for MULTI_COMPONENT_DIRS only, since SINGLE_COMPONENT_DIRS
  151. # are already known to contain component.mk.
  152. COMPONENTS := $(dir $(foreach cd,$(MULTI_COMPONENT_DIRS),$(wildcard $(cd)/*/component.mk))) \
  153. $(SINGLE_COMPONENT_DIRS)
  154. COMPONENTS := $(sort $(foreach comp,$(COMPONENTS),$(lastword $(subst /, ,$(comp)))))
  155. endif
  156. # After a full manifest of component names is determined, subtract the ones explicitly
  157. # omitted by the project Makefile.
  158. EXCLUDE_COMPONENTS ?=
  159. ifdef EXCLUDE_COMPONENTS
  160. COMPONENTS := $(filter-out $(subst ",,$(EXCLUDE_COMPONENTS)), $(COMPONENTS))
  161. # to keep syntax highlighters happy: "))
  162. endif
  163. export COMPONENTS
  164. # Resolve all of COMPONENTS into absolute paths in COMPONENT_PATHS.
  165. # For each entry in COMPONENT_DIRS:
  166. # - either this is directory with multiple components, in which case check that
  167. # a subdirectory with component name exists, and it contains a component.mk file.
  168. # - or, this is a directory of a single component, in which case the name of this
  169. # directory has to match the component name
  170. #
  171. # If a component name exists in multiple COMPONENT_DIRS, we take the first match.
  172. #
  173. # NOTE: These paths must be generated WITHOUT a trailing / so we
  174. # can use $(notdir x) to get the component name.
  175. COMPONENT_PATHS := $(foreach comp,$(COMPONENTS),\
  176. $(firstword $(foreach cd,$(COMPONENT_DIRS),\
  177. $(if $(findstring $(cd),$(MULTI_COMPONENT_DIRS)),\
  178. $(abspath $(dir $(wildcard $(cd)/$(comp)/component.mk))),)\
  179. $(if $(findstring $(cd),$(SINGLE_COMPONENT_DIRS)),\
  180. $(if $(filter $(comp),$(notdir $(cd))),$(cd),),)\
  181. )))
  182. export COMPONENT_PATHS
  183. TEST_COMPONENTS ?=
  184. TEST_EXCLUDE_COMPONENTS ?=
  185. TESTS_ALL ?=
  186. # If TESTS_ALL set to 1, set TEST_COMPONENTS_LIST to all components.
  187. # Otherwise, use the list supplied in TEST_COMPONENTS.
  188. ifeq ($(TESTS_ALL),1)
  189. TEST_COMPONENTS_LIST := $(filter-out $(TEST_EXCLUDE_COMPONENTS), $(COMPONENTS))
  190. else
  191. TEST_COMPONENTS_LIST := $(TEST_COMPONENTS)
  192. endif
  193. TEST_COMPONENT_PATHS := $(foreach comp,$(TEST_COMPONENTS_LIST),$(firstword $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/$(comp)/test))))
  194. TEST_COMPONENT_NAMES := $(foreach comp,$(TEST_COMPONENT_PATHS),$(lastword $(subst /, ,$(dir $(comp))))_test)
  195. # Set default values that were not previously defined
  196. CC ?= gcc
  197. LD ?= ld
  198. AR ?= ar
  199. OBJCOPY ?= objcopy
  200. OBJDUMP ?= objdump
  201. SIZE ?= size
  202. # Set host compiler and binutils
  203. HOSTCC := $(CC)
  204. HOSTLD := $(LD)
  205. HOSTAR := $(AR)
  206. HOSTOBJCOPY := $(OBJCOPY)
  207. HOSTSIZE := $(SIZE)
  208. export HOSTCC HOSTLD HOSTAR HOSTOBJCOPY SIZE
  209. # Set variables common to both project & component (includes config)
  210. include $(IDF_PATH)/make/common.mk
  211. # Notify users when some of the required python packages are not installed
  212. .PHONY: check_python_dependencies
  213. check_python_dependencies:
  214. ifndef IS_BOOTLOADER_BUILD
  215. $(PYTHON) $(IDF_PATH)/tools/check_python_dependencies.py
  216. endif
  217. # include the config generation targets (dependency: COMPONENT_PATHS)
  218. #
  219. # (bootloader build doesn't need this, config is exported from top-level)
  220. ifndef IS_BOOTLOADER_BUILD
  221. include $(IDF_PATH)/make/project_config.mk
  222. endif
  223. #####################################################################
  224. # If SDKCONFIG_MAKEFILE hasn't been generated yet (detected if no
  225. # CONFIG_IDF_TARGET), stop the Makefile pass now to allow config to
  226. # be created. make will build SDKCONFIG_MAKEFILE and restart,
  227. # reevaluating everything from the top.
  228. #
  229. # This is important so config is present when the
  230. # component_project_vars.mk files are generated.
  231. #
  232. # (After both files exist, if SDKCONFIG_MAKEFILE is updated then the
  233. # normal dependency relationship will trigger a regeneration of
  234. # component_project_vars.mk)
  235. #
  236. #####################################################################
  237. ifndef CONFIG_IDF_TARGET
  238. ifdef IS_BOOTLOADER_BUILD # we expect config to always have been expanded by top level project
  239. $(error "Internal error: config has not been passed correctly to bootloader subproject")
  240. endif
  241. ifdef MAKE_RESTARTS
  242. $(warning "Config was not evaluated after the first pass of 'make'")
  243. endif
  244. else # CONFIG_IDF_TARGET
  245. #####################################################################
  246. # Config is valid, can include rest of the Project Makefile
  247. #####################################################################
  248. # Initialise project-wide variables which can be added to by
  249. # each component.
  250. #
  251. # These variables are built up via the component_project_vars.mk
  252. # generated makefiles (one per component).
  253. #
  254. # See docs/build-system.rst for more details.
  255. COMPONENT_INCLUDES :=
  256. COMPONENT_LDFLAGS :=
  257. COMPONENT_SUBMODULES :=
  258. COMPONENT_LIBRARIES :=
  259. COMPONENT_LDFRAGMENTS :=
  260. # COMPONENT_PROJECT_VARS is the list of component_project_vars.mk generated makefiles
  261. # for each component.
  262. #
  263. # Including $(COMPONENT_PROJECT_VARS) builds the COMPONENT_INCLUDES,
  264. # COMPONENT_LDFLAGS variables and also targets for any inter-component
  265. # dependencies.
  266. #
  267. # See the component_project_vars.mk target in component_wrapper.mk
  268. COMPONENT_PROJECT_VARS := $(addsuffix /component_project_vars.mk,$(notdir $(COMPONENT_PATHS) ) $(TEST_COMPONENT_NAMES))
  269. COMPONENT_PROJECT_VARS := $(addprefix $(BUILD_DIR_BASE)/,$(COMPONENT_PROJECT_VARS))
  270. # this line is -include instead of include to prevent a spurious error message on make 3.81
  271. -include $(COMPONENT_PROJECT_VARS)
  272. # Also add top-level project include path, for top-level includes
  273. COMPONENT_INCLUDES += $(abspath $(BUILD_DIR_BASE)/include/)
  274. export COMPONENT_INCLUDES
  275. all:
  276. ifdef CONFIG_SECURE_BOOT
  277. @echo "(Secure boot enabled, so bootloader not flashed automatically. See 'make bootloader' output)"
  278. ifndef CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES
  279. ifdef SECURE_SIGNED_APPS_ECDSA_SCHEME
  280. @echo "App built but not signed. Sign app & partition data before flashing, via espsecure.py:"
  281. @echo "espsecure.py sign_data --version 1 --keyfile KEYFILE $(APP_BIN)"
  282. @echo "espsecure.py sign_data --version 1 --keyfile KEYFILE $(PARTITION_TABLE_BIN)"
  283. else
  284. @echo "App built but not signed. Sign app & partition data before flashing, via espsecure.py:"
  285. @echo "espsecure.py sign_data --version 2 --keyfile KEYFILE $(APP_BIN)"
  286. endif
  287. endif
  288. @echo "To flash app & partition table, run 'make flash' or:"
  289. else
  290. ifdef CONFIG_APP_BUILD_GENERATE_BINARIES
  291. @echo "To flash all build output, run 'make flash' or:"
  292. endif
  293. endif
  294. ifdef CONFIG_APP_BUILD_GENERATE_BINARIES
  295. @echo $(ESPTOOLPY_WRITE_FLASH) $(ESPTOOL_ALL_FLASH_ARGS)
  296. else
  297. @echo "Binary is not available for flashing"
  298. endif
  299. # If we have `version.txt` then prefer that for extracting IDF version
  300. ifeq ("$(wildcard ${IDF_PATH}/version.txt)","")
  301. IDF_VER_T := $(shell cd ${IDF_PATH} && git describe --always --dirty --match v*.*)
  302. else
  303. IDF_VER_T := $(shell cat ${IDF_PATH}/version.txt)
  304. endif
  305. IDF_VER := $(shell echo "$(IDF_VER_T)" | cut -c 1-31)
  306. # Set default LDFLAGS
  307. EXTRA_LDFLAGS ?=
  308. LDFLAGS ?= -nostdlib \
  309. $(EXTRA_LDFLAGS) \
  310. -Wl,--gc-sections \
  311. -Wl,-static \
  312. -Wl,--start-group \
  313. $(COMPONENT_LDFLAGS) \
  314. -lgcc \
  315. -lstdc++ \
  316. -lgcov \
  317. -Wl,--end-group \
  318. -Wl,-EL
  319. # Set default CPPFLAGS, CFLAGS, CXXFLAGS
  320. # These are exported so that components can use them when compiling.
  321. # If you need your component to add CFLAGS/etc for it's own source compilation only, set CFLAGS += in your component's Makefile.
  322. # If you need your component to add CFLAGS/etc globally for all source
  323. # files, set CFLAGS += in your component's Makefile.projbuild
  324. # If you need to set CFLAGS/CPPFLAGS/CXXFLAGS at project level, set them in application Makefile
  325. # before including project.mk. Default flags will be added before the ones provided in application Makefile.
  326. # CPPFLAGS used by C preprocessor
  327. # If any flags are defined in application Makefile, add them at the end.
  328. CPPFLAGS ?=
  329. EXTRA_CPPFLAGS ?=
  330. CPPFLAGS := -DESP_PLATFORM -D IDF_VER=\"$(IDF_VER)\" -MMD -MP $(CPPFLAGS) $(EXTRA_CPPFLAGS)
  331. PROJECT_VER ?=
  332. export IDF_VER
  333. export PROJECT_NAME
  334. export PROJECT_VER
  335. # Warnings-related flags relevant both for C and C++
  336. COMMON_WARNING_FLAGS = -Wall -Werror=all \
  337. -Wno-error=unused-function \
  338. -Wno-error=unused-but-set-variable \
  339. -Wno-error=unused-variable \
  340. -Wno-error=deprecated-declarations \
  341. -Wextra \
  342. -Wno-unused-parameter -Wno-sign-compare
  343. ifdef CONFIG_COMPILER_DISABLE_GCC8_WARNINGS
  344. COMMON_WARNING_FLAGS += -Wno-parentheses \
  345. -Wno-sizeof-pointer-memaccess \
  346. -Wno-clobbered \
  347. -Wno-format-overflow \
  348. -Wno-stringop-truncation \
  349. -Wno-misleading-indentation \
  350. -Wno-cast-function-type \
  351. -Wno-implicit-fallthrough \
  352. -Wno-unused-const-variable \
  353. -Wno-switch-unreachable \
  354. -Wno-format-truncation \
  355. -Wno-memset-elt-size \
  356. -Wno-int-in-bool-context
  357. endif
  358. ifdef CONFIG_COMPILER_WARN_WRITE_STRINGS
  359. COMMON_WARNING_FLAGS += -Wwrite-strings
  360. endif #CONFIG_COMPILER_WARN_WRITE_STRINGS
  361. # Flags which control code generation and dependency generation, both for C and C++
  362. COMMON_FLAGS = \
  363. -Wno-frame-address \
  364. -ffunction-sections -fdata-sections \
  365. -fstrict-volatile-bitfields \
  366. -mlongcalls \
  367. -nostdlib
  368. ifndef IS_BOOTLOADER_BUILD
  369. # stack protection (only one option can be selected in menuconfig)
  370. ifdef CONFIG_COMPILER_STACK_CHECK_MODE_NORM
  371. COMMON_FLAGS += -fstack-protector
  372. endif
  373. ifdef CONFIG_COMPILER_STACK_CHECK_MODE_STRONG
  374. COMMON_FLAGS += -fstack-protector-strong
  375. endif
  376. ifdef CONFIG_COMPILER_STACK_CHECK_MODE_ALL
  377. COMMON_FLAGS += -fstack-protector-all
  378. endif
  379. # Placing jump tables in flash would cause issues with code that required
  380. # to be placed in IRAM
  381. COMMON_FLAGS += -fno-jump-tables
  382. COMMON_FLAGS += -fno-tree-switch-conversion
  383. # Optimization flags are set based on menuconfig choice
  384. ifdef CONFIG_COMPILER_OPTIMIZATION_SIZE
  385. OPTIMIZATION_FLAGS = -Os -freorder-blocks
  386. endif
  387. ifdef CONFIG_COMPILER_OPTIMIZATION_DEFAULT
  388. OPTIMIZATION_FLAGS = -Og
  389. endif
  390. ifdef CONFIG_COMPILER_OPTIMIZATION_NONE
  391. OPTIMIZATION_FLAGS = -O0
  392. endif
  393. ifdef CONFIG_COMPILER_OPTIMIZATION_PERF
  394. OPTIMIZATION_FLAGS = -O2
  395. endif
  396. ifdef CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE
  397. CPPFLAGS += -DNDEBUG
  398. endif
  399. else # IS_BOOTLOADER_BUILD
  400. ifdef CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE
  401. OPTIMIZATION_FLAGS = -Os -freorder-blocks
  402. endif
  403. ifdef CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG
  404. OPTIMIZATION_FLAGS = -Og
  405. endif
  406. ifdef CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE
  407. OPTIMIZATION_FLAGS = -O0
  408. endif
  409. ifdef CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF
  410. OPTIMIZATION_FLAGS = -O2
  411. endif
  412. endif # IS_BOOTLOADER_BUILD
  413. # IDF uses some GNU extension from libc
  414. CPPFLAGS += -D_GNU_SOURCE
  415. # Enable generation of debugging symbols
  416. # (we generate even in Release mode, as this has no impact on final binary size.)
  417. DEBUG_FLAGS ?= -ggdb
  418. # List of flags to pass to C compiler
  419. # If any flags are defined in application Makefile, add them at the end.
  420. CFLAGS ?=
  421. EXTRA_CFLAGS ?=
  422. CFLAGS := $(strip \
  423. -std=gnu99 \
  424. $(OPTIMIZATION_FLAGS) $(DEBUG_FLAGS) \
  425. $(COMMON_FLAGS) \
  426. $(COMMON_WARNING_FLAGS) -Wno-old-style-declaration \
  427. $(CFLAGS) \
  428. $(EXTRA_CFLAGS))
  429. # List of flags to pass to C++ compiler
  430. # If any flags are defined in application Makefile, add them at the end.
  431. CXXFLAGS ?=
  432. EXTRA_CXXFLAGS ?=
  433. CXXFLAGS := $(strip \
  434. -std=gnu++11 \
  435. $(OPTIMIZATION_FLAGS) $(DEBUG_FLAGS) \
  436. $(COMMON_FLAGS) \
  437. $(COMMON_WARNING_FLAGS) \
  438. $(CXXFLAGS) \
  439. $(EXTRA_CXXFLAGS))
  440. ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
  441. CXXFLAGS += -fexceptions
  442. else
  443. CXXFLAGS += -fno-exceptions
  444. endif
  445. ifdef CONFIG_COMPILER_CXX_RTTI
  446. CXXFLAGS += -frtti
  447. else
  448. CXXFLAGS += -fno-rtti
  449. LDFLAGS += -fno-rtti
  450. endif
  451. ARFLAGS := cr
  452. export CFLAGS CPPFLAGS CXXFLAGS ARFLAGS
  453. # Set target compiler. Defaults to whatever the user has
  454. # configured as prefix + ye olde gcc commands
  455. CC := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))gcc
  456. CXX := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))c++
  457. LD := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))ld
  458. AR := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))ar
  459. OBJCOPY := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))objcopy
  460. OBJDUMP := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))objdump
  461. SIZE := $(call dequote,$(CONFIG_SDK_TOOLPREFIX))size
  462. export CC CXX LD AR OBJCOPY OBJDUMP SIZE
  463. COMPILER_VERSION_STR := $(shell $(CC) -dumpversion)
  464. COMPILER_VERSION_NUM := $(subst .,,$(COMPILER_VERSION_STR))
  465. # the app is the main executable built by the project
  466. APP_ELF:=$(BUILD_DIR_BASE)/$(PROJECT_NAME).elf
  467. APP_MAP:=$(APP_ELF:.elf=.map)
  468. APP_BIN:=$(APP_ELF:.elf=.bin)
  469. # include linker script generation utils makefile
  470. include $(IDF_PATH)/make/ldgen.mk
  471. $(eval $(call ldgen_create_commands))
  472. # Include any Makefile.projbuild file letting components add
  473. # configuration at the project level
  474. define includeProjBuildMakefile
  475. ifeq ("$(V)","1")
  476. $$(info including $(1)/Makefile.projbuild...)
  477. endif
  478. COMPONENT_PATH := $(1)
  479. include $(1)/Makefile.projbuild
  480. endef
  481. $(foreach componentpath,$(COMPONENT_PATHS), \
  482. $(if $(wildcard $(componentpath)/Makefile.projbuild), \
  483. $(eval $(call includeProjBuildMakefile,$(componentpath)))))
  484. # ELF depends on the library archive files for COMPONENT_LIBRARIES
  485. # the rules to build these are emitted as part of GenerateComponentTarget below
  486. #
  487. # also depends on additional dependencies (linker scripts & binary libraries)
  488. # stored in COMPONENT_LINKER_DEPS, built via component.mk files' COMPONENT_ADD_LINKER_DEPS variable
  489. COMPONENT_LINKER_DEPS ?=
  490. $(APP_ELF): $(foreach libcomp,$(COMPONENT_LIBRARIES),$(BUILD_DIR_BASE)/$(libcomp)/lib$(libcomp).a) $(COMPONENT_LINKER_DEPS) $(COMPONENT_PROJECT_VARS)
  491. $(summary) LD $(patsubst $(PWD)/%,%,$@)
  492. $(CC) $(LDFLAGS) -o $@ -Wl,-Map=$(APP_MAP)
  493. ifdef CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME
  494. SECURE_APPS_SIGNING_SCHEME = "1"
  495. else ifdef CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME
  496. SECURE_APPS_SIGNING_SCHEME = "2"
  497. endif
  498. app: $(APP_BIN) partition_table_get_info
  499. ifeq ("$(CONFIG_APP_BUILD_GENERATE_BINARIES)","y")
  500. ifeq ("$(CONFIG_SECURE_BOOT)$(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)","y") # secure boot enabled, but remote sign app image
  501. @echo "App built but not signed. Signing step via espsecure.py:"
  502. @echo "espsecure.py sign_data --version $(SECURE_APPS_SIGNING_SCHEME) --keyfile KEYFILE $(APP_BIN)"
  503. @echo "Then flash app command is:"
  504. @echo $(ESPTOOLPY_WRITE_FLASH) $(APP_OFFSET) $(APP_BIN)
  505. else
  506. @echo "App built. Default flash app command is:"
  507. @echo $(ESPTOOLPY_WRITE_FLASH) $(APP_OFFSET) $(APP_BIN)
  508. endif
  509. else
  510. @echo "Application in not built and cannot be flashed."
  511. endif
  512. all_binaries: $(APP_BIN)
  513. $(BUILD_DIR_BASE):
  514. mkdir -p $(BUILD_DIR_BASE)
  515. # Macro for the recursive sub-make for each component
  516. # $(1) - component directory
  517. # $(2) - component name only
  518. #
  519. # Is recursively expanded by the GenerateComponentTargets macro
  520. define ComponentMake
  521. +$(MAKE) -C $(BUILD_DIR_BASE)/$(2) -f $(IDF_PATH)/make/component_wrapper.mk COMPONENT_MAKEFILE=$(1)/component.mk COMPONENT_NAME=$(2)
  522. endef
  523. # Generate top-level component-specific targets for each component
  524. # $(1) - path to component dir
  525. # $(2) - name of component
  526. #
  527. define GenerateComponentTargets
  528. .PHONY: component-$(2)-build component-$(2)-clean
  529. COMPONENT_$(2)_BUILDTARGET ?= build
  530. COMPONENT_$(2)_CLEANTARGET ?= clean
  531. component-$(2)-build: check-submodules $(call prereq_if_explicit, component-$(2)-clean) | $(BUILD_DIR_BASE)/$(2)
  532. $(call ComponentMake,$(1),$(2)) $$(COMPONENT_$(2)_BUILDTARGET)
  533. component-$(2)-clean: | $(BUILD_DIR_BASE)/$(2) $(BUILD_DIR_BASE)/$(2)/component_project_vars.mk
  534. $(call ComponentMake,$(1),$(2)) $$(COMPONENT_$(2)_CLEANTARGET)
  535. $(BUILD_DIR_BASE)/$(2):
  536. @mkdir -p $(BUILD_DIR_BASE)/$(2)
  537. # tell make it can build any component's library by invoking the -build target
  538. # (this target exists for all components even ones which don't build libraries, but it's
  539. # only invoked for the targets whose libraries appear in COMPONENT_LIBRARIES and hence the
  540. # APP_ELF dependencies.)
  541. $(BUILD_DIR_BASE)/$(2)/lib$(2).a: component-$(2)-build
  542. $(details) "Target '$$^' responsible for '$$@'" # echo which build target built this file
  543. # add a target to generate the component_project_vars.mk files that
  544. # are used to inject variables into project make pass (see matching
  545. # component_project_vars.mk target in component_wrapper.mk).
  546. #
  547. # If any component_project_vars.mk file is out of date, the make
  548. # process will call this target to rebuild it and then restart.
  549. #
  550. $(BUILD_DIR_BASE)/$(2)/component_project_vars.mk: $(1)/component.mk $(COMMON_MAKEFILES) $(SDKCONFIG_MAKEFILE) | $(BUILD_DIR_BASE)/$(2)
  551. $(call ComponentMake,$(1),$(2)) component_project_vars.mk
  552. endef
  553. $(foreach component,$(COMPONENT_PATHS),$(eval $(call GenerateComponentTargets,$(component),$(notdir $(component)))))
  554. $(foreach component,$(TEST_COMPONENT_PATHS),$(eval $(call GenerateComponentTargets,$(component),$(lastword $(subst /, ,$(dir $(component))))_test)))
  555. app-clean: $(addprefix component-,$(addsuffix -clean,$(notdir $(COMPONENT_PATHS))))
  556. $(summary) RM $(APP_ELF)
  557. rm -f $(APP_ELF) $(APP_BIN) $(APP_MAP)
  558. size: $(APP_ELF) | check_python_dependencies
  559. $(PYTHON) $(IDF_PATH)/tools/idf_size.py $(APP_MAP)
  560. size-files: $(APP_ELF) | check_python_dependencies
  561. $(PYTHON) $(IDF_PATH)/tools/idf_size.py --files $(APP_MAP)
  562. size-components: $(APP_ELF) | check_python_dependencies
  563. $(PYTHON) $(IDF_PATH)/tools/idf_size.py --archives $(APP_MAP)
  564. size-symbols: $(APP_ELF) | check_python_dependencies
  565. ifndef COMPONENT
  566. $(error "ERROR: Please enter the component to look symbols for, e.g. COMPONENT=heap")
  567. else
  568. $(PYTHON) $(IDF_PATH)/tools/idf_size.py --archive_details lib$(COMPONENT).a $(APP_MAP)
  569. endif
  570. # NB: this ordering is deliberate (app-clean & bootloader-clean before
  571. # _config-clean), so config remains valid during all component clean
  572. # targets
  573. config-clean: app-clean bootloader-clean
  574. clean: app-clean bootloader-clean config-clean ldgen-clean
  575. # phony target to check if any git submodule listed in COMPONENT_SUBMODULES are missing
  576. # or out of date, and exit if so. Components can add paths to this variable.
  577. #
  578. # This only works for components inside IDF_PATH
  579. #
  580. # For internal use:
  581. # IDF_SKIP_CHECK_SUBMODULES may be set in the environment to skip the submodule check.
  582. # This can be used e.g. in CI when submodules are checked out by different means.
  583. IDF_SKIP_CHECK_SUBMODULES ?= 0
  584. check-submodules:
  585. ifeq ($(IDF_SKIP_CHECK_SUBMODULES),1)
  586. @echo "skip submodule check on internal CI"
  587. else
  588. # Check if .gitmodules exists, otherwise skip submodule check, assuming flattened structure
  589. ifneq ("$(wildcard ${IDF_PATH}/.gitmodules)","")
  590. # Dump the git status for the whole working copy once, then grep it for each submodule. This saves a lot of time on Windows.
  591. GIT_STATUS := $(shell cd ${IDF_PATH} && git status --porcelain --ignore-submodules=dirty)
  592. # Generate a target to check this submodule
  593. # $(1) - submodule directory, relative to IDF_PATH
  594. define GenerateSubmoduleCheckTarget
  595. check-submodules: $(IDF_PATH)/$(1)/.git
  596. $(IDF_PATH)/$(1)/.git:
  597. @echo "WARNING: Missing submodule $(1)..."
  598. [ -e ${IDF_PATH}/.git ] || { echo "ERROR: esp-idf must be cloned from git to work."; exit 1; }
  599. [ -x "$(shell which git)" ] || { echo "ERROR: Need to run 'git submodule init $(1)' in esp-idf root directory."; exit 1; }
  600. @echo "Attempting 'git submodule update --init $(1)' in esp-idf root directory..."
  601. cd ${IDF_PATH} && git submodule update --init $(1)
  602. # Parse 'git status' output to check if the submodule commit is different to expected
  603. ifneq ("$(filter $(1),$(GIT_STATUS))","")
  604. $$(info WARNING: esp-idf git submodule $(1) may be out of date. Run 'git submodule update' in IDF_PATH dir to update.)
  605. endif
  606. endef
  607. # filter/subst in expression ensures all submodule paths begin with $(IDF_PATH), and then strips that prefix
  608. # so the argument is suitable for use with 'git submodule' commands
  609. $(foreach submodule,$(subst $(IDF_PATH)/,,$(filter $(IDF_PATH)/%,$(COMPONENT_SUBMODULES))),$(eval $(call GenerateSubmoduleCheckTarget,$(submodule))))
  610. endif # End check for .gitmodules existence
  611. endif # End check for IDF_SKIP_CHECK_SUBMODULES
  612. # PHONY target to list components in the build and their paths
  613. list-components:
  614. $(info $(call dequote,$(SEPARATOR)))
  615. $(info COMPONENT_DIRS (components searched for here))
  616. $(foreach cd,$(COMPONENT_DIRS),$(info $(cd)))
  617. $(info $(call dequote,$(SEPARATOR)))
  618. $(info TEST_COMPONENTS (list of test component names))
  619. $(info $(TEST_COMPONENTS_LIST))
  620. $(info $(call dequote,$(SEPARATOR)))
  621. $(info TEST_EXCLUDE_COMPONENTS (list of test excluded names))
  622. $(info $(if $(EXCLUDE_COMPONENTS) || $(TEST_EXCLUDE_COMPONENTS),$(EXCLUDE_COMPONENTS) $(TEST_EXCLUDE_COMPONENTS),(none provided)))
  623. $(info $(call dequote,$(SEPARATOR)))
  624. $(info COMPONENT_PATHS (paths to all components):)
  625. $(foreach cp,$(COMPONENT_PATHS),$(info $(cp)))
  626. # print flash command, so users can dump this to config files and download somewhere without idf
  627. print_flash_cmd: partition_table_get_info blank_ota_data
  628. echo $(ESPTOOL_WRITE_FLASH_OPTIONS) $(ESPTOOL_ALL_FLASH_ARGS) | sed -e 's:'$(PWD)/build/'::g'
  629. # Check toolchain version using the output of xtensa-esp32-elf-gcc --version command.
  630. # The output normally looks as follows
  631. # xtensa-esp32-elf-gcc (crosstool-NG crosstool-ng-1.22.0-80-g6c4433a) 5.2.0
  632. # The part in brackets is extracted into TOOLCHAIN_COMMIT_DESC variable
  633. ifdef CONFIG_SDK_TOOLPREFIX
  634. ifndef MAKE_RESTARTS
  635. TOOLCHAIN_HEADER := $(shell $(CC) --version | head -1)
  636. TOOLCHAIN_PATH := $(shell which $(CC))
  637. TOOLCHAIN_COMMIT_DESC := $(shell $(CC) --version | sed -E -n 's|.*\(crosstool-NG (.*)\).*|\1|gp')
  638. TOOLCHAIN_GCC_VER := $(COMPILER_VERSION_STR)
  639. # Officially supported version(s)
  640. include $(IDF_PATH)/tools/toolchain_versions.mk
  641. ifndef IS_BOOTLOADER_BUILD
  642. $(info Toolchain path: $(TOOLCHAIN_PATH))
  643. endif
  644. ifdef TOOLCHAIN_COMMIT_DESC
  645. ifeq (,$(findstring $(SUPPORTED_TOOLCHAIN_COMMIT_DESC),$(TOOLCHAIN_COMMIT_DESC)))
  646. $(info WARNING: Toolchain version is not supported: $(TOOLCHAIN_COMMIT_DESC))
  647. $(info Expected to see version: $(SUPPORTED_TOOLCHAIN_COMMIT_DESC))
  648. $(info Please check ESP-IDF setup instructions and update the toolchain, or proceed at your own risk.)
  649. else
  650. ifndef IS_BOOTLOADER_BUILD
  651. $(info Toolchain version: $(TOOLCHAIN_COMMIT_DESC))
  652. endif
  653. endif
  654. ifeq (,$(findstring $(TOOLCHAIN_GCC_VER), $(SUPPORTED_TOOLCHAIN_GCC_VERSIONS)))
  655. $(info WARNING: Compiler version is not supported: $(TOOLCHAIN_GCC_VER))
  656. $(info Expected to see version(s): $(SUPPORTED_TOOLCHAIN_GCC_VERSIONS))
  657. $(info Please check ESP-IDF setup instructions and update the toolchain, or proceed at your own risk.)
  658. else
  659. ifndef IS_BOOTLOADER_BUILD
  660. $(info Compiler version: $(TOOLCHAIN_GCC_VER))
  661. endif
  662. endif
  663. else
  664. $(info WARNING: Failed to find Xtensa toolchain, may need to alter PATH or set one in the configuration menu)
  665. endif # TOOLCHAIN_COMMIT_DESC
  666. endif #MAKE_RESTARTS
  667. endif #CONFIG_SDK_TOOLPREFIX
  668. #####################################################################
  669. endif #CONFIG_IDF_TARGET