project.mk 10 KB

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