project.mk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 +=-rR
  36. # Figure out PROJECT_PATH if not set
  37. ifeq ("$(PROJECT_PATH)","")
  38. #The path to the project: we assume the Makefile including this file resides
  39. #in the root of that directory.
  40. PROJECT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
  41. export PROJECT_PATH
  42. endif
  43. #The directory where we put all objects/libraries/binaries. The project Makefile can
  44. #configure this if needed.
  45. BUILD_DIR_BASE ?= $(PROJECT_PATH)/build
  46. export BUILD_DIR_BASE
  47. #Component directories. These directories are searched for components.
  48. #The project Makefile can override these component dirs, or define extra component directories.
  49. COMPONENT_DIRS ?= $(PROJECT_PATH)/components $(EXTRA_COMPONENT_DIRS) $(IDF_PATH)/components
  50. export COMPONENT_DIRS
  51. #The project Makefile can define a list of components, but if it does not do this we just take
  52. #all available components in the component dirs.
  53. ifeq ("$(COMPONENTS)","")
  54. #Find all component names. The component names are the same as the
  55. #directories they're in, so /bla/components/mycomponent/ -> mycomponent. We later use
  56. #the COMPONENT_DIRS bit to find back the component path.
  57. COMPONENTS := $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/*))
  58. COMPONENTS := $(sort $(foreach comp,$(COMPONENTS),$(lastword $(subst /, ,$(comp)))))
  59. endif
  60. export COMPONENTS
  61. #Sources default to only "main"
  62. SRCDIRS ?= main
  63. #Here, we resolve and add all the components and source paths into absolute paths.
  64. #If a component exists in multiple COMPONENT_DIRS, we take the first match.
  65. #WARNING: These directories paths must be generated WITHOUT a trailing / so we
  66. #can use $(notdir x) to get the component name.
  67. COMPONENT_PATHS := $(foreach comp,$(COMPONENTS),$(firstword $(foreach dir,$(COMPONENT_DIRS),$(wildcard $(dir)/$(comp)))))
  68. COMPONENT_PATHS += $(abspath $(SRCDIRS))
  69. #A component is buildable if it has a component.mk makefile; we assume that a
  70. # 'make -C $(component dir) -f component.mk build' results in a lib$(componentname).a
  71. COMPONENT_PATHS_BUILDABLE := $(foreach cp,$(COMPONENT_PATHS),$(if $(wildcard $(cp)/component.mk),$(cp)))
  72. # Assemble global list of include dirs (COMPONENT_INCLUDES), and
  73. # LDFLAGS args (COMPONENT_LDFLAGS) supplied by each component.
  74. COMPONENT_INCLUDES :=
  75. COMPONENT_LDFLAGS :=
  76. #
  77. # Also add any inter-component dependencies for each component.
  78. # Extract a variable from a child make process
  79. #
  80. # $(1) - path to directory to invoke make in
  81. # $(2) - name of variable to print via the get_variable target (passed in GET_VARIABLE)
  82. #
  83. # needs 'sed' processing of stdout because make sometimes echoes other stuff on stdout,
  84. # even if asked not to.
  85. #
  86. # Debugging this? Replace $(shell with $(error and you'll see the full command as-run.
  87. define GetVariable
  88. $(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" )
  89. endef
  90. COMPONENT_INCLUDES := $(abspath $(foreach comp,$(COMPONENT_PATHS_BUILDABLE),$(addprefix $(comp)/, \
  91. $(call GetVariable,$(comp),COMPONENT_ADD_INCLUDEDIRS))))
  92. #Also add project include path, for sdk includes
  93. COMPONENT_INCLUDES += $(BUILD_DIR_BASE)/include/
  94. export COMPONENT_INCLUDES
  95. #COMPONENT_LDFLAGS has a list of all flags that are needed to link the components together. It's collected
  96. #in the same way as COMPONENT_INCLUDES is.
  97. COMPONENT_LDFLAGS := $(foreach comp,$(COMPONENT_PATHS_BUILDABLE), \
  98. $(call GetVariable,$(comp),COMPONENT_ADD_LDFLAGS))
  99. export COMPONENT_LDFLAGS
  100. # Generate component dependency targets from dependencies lists
  101. # each component gains a target of its own <name>-build with dependencies
  102. # of the names of any other components (-build) that need building first
  103. #
  104. # the actual targets (that invoke submakes) are generated below by
  105. # GenerateComponentTarget macro.
  106. define GenerateComponentDependencies
  107. # $(1) = component path
  108. .PHONY: $$(notdir $(1))
  109. $$(notdir $(1))-build: $(addsuffix -build,$(call GetVariable,$(1),COMPONENT_DEPENDS))
  110. endef
  111. $(foreach comp,$(COMPONENT_PATHS_BUILDABLE), $(eval $(call GenerateComponentDependencies,$(comp))))
  112. #Make sure submakes can also use this.
  113. export PROJECT_PATH
  114. #Include functionality common to both project & component
  115. -include $(IDF_PATH)/make/common.mk
  116. # Set default LDFLAGS
  117. LDFLAGS ?= -nostdlib \
  118. -L$(IDF_PATH)/lib \
  119. -L$(IDF_PATH)/ld \
  120. $(addprefix -L$(BUILD_DIR_BASE)/,$(COMPONENTS) $(SRCDIRS)) \
  121. -u call_user_start_cpu0 \
  122. -Wl,--gc-sections \
  123. -Wl,-static \
  124. -Wl,--start-group \
  125. $(COMPONENT_LDFLAGS) \
  126. -lgcc \
  127. -Wl,--end-group \
  128. -Wl,-EL
  129. # Set default CPPFLAGS, CFLAGS, CXXFLAGS
  130. #
  131. # These are exported so that components can use them when compiling.
  132. #
  133. # If you need your component to add CFLAGS/etc for it's own source compilation only, set CFLAGS += in your component's Makefile.
  134. #
  135. # If you need your component to add CFLAGS/etc globally for all source
  136. # files, set CFLAGS += in your component's Makefile.projbuild
  137. # CPPFLAGS used by an compile pass that uses the C preprocessor
  138. CPPFLAGS = -DESP_PLATFORM -Og -g3 -Wpointer-arith -Werror -Wno-error=unused-function -Wno-error=unused-but-set-variable \
  139. -Wno-error=unused-variable -Wall -ffunction-sections -fdata-sections -mlongcalls -nostdlib -MMD -MP
  140. # C flags use by C only
  141. CFLAGS = $(CPPFLAGS) -std=gnu99 -g3 -fstrict-volatile-bitfields
  142. # CXXFLAGS uses by C++ only
  143. CXXFLAGS = $(CPPFLAGS) -Og -std=gnu++11 -g3 -fno-exceptions -fstrict-volatile-bitfields
  144. export CFLAGS CPPFLAGS CXXFLAGS
  145. #Set host compiler and binutils
  146. HOSTCC := $(CC)
  147. HOSTLD := $(LD)
  148. HOSTAR := $(AR)
  149. HOSTOBJCOPY := $(OBJCOPY)
  150. #Set target compiler. Defaults to whatever the user has
  151. #configured as prefix + yer olde gcc commands
  152. CC := $(call dequote,$(CONFIG_TOOLPREFIX))gcc
  153. CXX := $(call dequote,$(CONFIG_TOOLPREFIX))c++
  154. LD := $(call dequote,$(CONFIG_TOOLPREFIX))ld
  155. AR := $(call dequote,$(CONFIG_TOOLPREFIX))ar
  156. OBJCOPY := $(call dequote,$(CONFIG_TOOLPREFIX))objcopy
  157. export CC CXX LD AR OBJCOPY
  158. PYTHON=$(call dequote,$(CONFIG_PYTHON))
  159. # the app is the main executable built by the project
  160. APP_ELF:=$(BUILD_DIR_BASE)/$(PROJECT_NAME).elf
  161. APP_MAP:=$(APP_ELF:.elf=.map)
  162. APP_BIN:=$(APP_ELF:.elf=.bin)
  163. # Include any Makefile.projbuild file letting components add
  164. # configuration at the project level
  165. define includeProjBuildMakefile
  166. $(if $(V),$(if $(wildcard $(1)/Makefile.projbuild),$(info including $(1)/Makefile.projbuild...)))
  167. COMPONENT_PATH := $(1)
  168. -include $(1)/Makefile.projbuild
  169. endef
  170. $(foreach componentpath,$(COMPONENT_PATHS),$(eval $(call includeProjBuildMakefile,$(componentpath))))
  171. # once we know component paths, we can include the config
  172. include $(IDF_PATH)/make/project_config.mk
  173. # A "component" library is any library in the LDFLAGS where
  174. # the name of the library is also a name of the component
  175. APP_LIBRARIES = $(patsubst -l%,%,$(filter -l%,$(LDFLAGS)))
  176. COMPONENT_LIBRARIES = $(filter $(notdir $(COMPONENT_PATHS_BUILDABLE)),$(APP_LIBRARIES))
  177. # ELF depends on the library archive files for COMPONENT_LIBRARIES
  178. # the rules to build these are emitted as part of GenerateComponentTarget below
  179. $(APP_ELF): $(foreach libcomp,$(COMPONENT_LIBRARIES),$(BUILD_DIR_BASE)/$(libcomp)/lib$(libcomp).a)
  180. $(summary) LD $(notdir $@)
  181. $(Q) $(CC) $(LDFLAGS) -o $@ -Wl,-Map=$(APP_MAP)
  182. # Generation of $(APP_BIN) from $(APP_ELF) is added by the esptool
  183. # component's Makefile.projbuild
  184. app: $(APP_BIN)
  185. @echo "App built. Default flash app command is:"
  186. @echo $(ESPTOOLPY_WRITE_FLASH) $(CONFIG_APP_OFFSET) $(APP_BIN)
  187. all_binaries: $(APP_BIN)
  188. $(BUILD_DIR_BASE):
  189. mkdir -p $(BUILD_DIR_BASE)
  190. define GenerateComponentPhonyTarget
  191. # $(1) - path to component dir
  192. # $(2) - target to generate (build, clean)
  193. .PHONY: $(notdir $(1))-$(2)
  194. $(notdir $(1))-$(2): | $(BUILD_DIR_BASE)/$(notdir $(1))
  195. @+$(MAKE) -C $(BUILD_DIR_BASE)/$(notdir $(1)) -f $(1)/component.mk COMPONENT_BUILD_DIR=$(BUILD_DIR_BASE)/$(notdir $(1)) $(2)
  196. endef
  197. define GenerateComponentTargets
  198. # $(1) - path to component dir
  199. $(BUILD_DIR_BASE)/$(notdir $(1)):
  200. @mkdir -p $(BUILD_DIR_BASE)/$(notdir $(1))
  201. # tell make it can build any component's library by invoking the recursive -build target
  202. # (this target exists for all components even ones which don't build libraries, but it's
  203. # only invoked for the targets whose libraries appear in COMPONENT_LIBRARIES and hence the
  204. # APP_ELF dependencies.)
  205. $(BUILD_DIR_BASE)/$(notdir $(1))/lib$(notdir $(1)).a: $(notdir $(1))-build
  206. $(details) "Target '$$^' responsible for '$$@'" # echo which build target built this file
  207. endef
  208. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentTargets,$(component))))
  209. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentPhonyTarget,$(component),build)))
  210. $(foreach component,$(COMPONENT_PATHS_BUILDABLE),$(eval $(call GenerateComponentPhonyTarget,$(component),clean)))
  211. app-clean: $(addsuffix -clean,$(notdir $(COMPONENT_PATHS_BUILDABLE)))
  212. $(summary) RM $(APP_ELF)
  213. $(Q) rm -f $(APP_ELF) $(APP_BIN) $(APP_MAP)
  214. clean: app-clean