project.mk 10 KB

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