build_macros.mk 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #
  2. # Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. #
  6. # Report an error if the eval make function is not available.
  7. $(eval eval_available := T)
  8. ifneq (${eval_available},T)
  9. $(error This makefile only works with a Make program that supports $$(eval))
  10. endif
  11. # A user defined function to recursively search for a filename below a directory
  12. # $1 is the directory root of the recursive search (blank for current directory).
  13. # $2 is the file name to search for.
  14. define rwildcard
  15. $(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
  16. endef
  17. # Convenience function for setting a variable to 0 if not previously set
  18. # $(eval $(call default_zero,FOO))
  19. define default_zero
  20. $(eval $(1) ?= 0)
  21. endef
  22. # Convenience function for setting a list of variables to 0 if not previously set
  23. # $(eval $(call default_zeros,FOO BAR))
  24. define default_zeros
  25. $(foreach var,$1,$(eval $(call default_zero,$(var))))
  26. endef
  27. # Convenience function for setting a variable to 1 if not previously set
  28. # $(eval $(call default_one,FOO))
  29. define default_one
  30. $(eval $(1) ?= 1)
  31. endef
  32. # Convenience function for setting a list of variables to 1 if not previously set
  33. # $(eval $(call default_ones,FOO BAR))
  34. define default_ones
  35. $(foreach var,$1,$(eval $(call default_one,$(var))))
  36. endef
  37. # Convenience function for adding build definitions
  38. # $(eval $(call add_define,FOO)) will have:
  39. # -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
  40. define add_define
  41. DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),)
  42. endef
  43. # Convenience function for addding multiple build definitions
  44. # $(eval $(call add_defines,FOO BOO))
  45. define add_defines
  46. $(foreach def,$1,$(eval $(call add_define,$(def))))
  47. endef
  48. # Convenience function for adding build definitions
  49. # $(eval $(call add_define_val,FOO,BAR)) will have:
  50. # -DFOO=BAR
  51. define add_define_val
  52. DEFINES += -D$(1)=$(2)
  53. endef
  54. # Convenience function for verifying option has a boolean value
  55. # $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
  56. define assert_boolean
  57. $(if $($(1)),,$(error $(1) must not be empty))
  58. $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean))
  59. endef
  60. # Convenience function for verifying options have boolean values
  61. # $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values
  62. define assert_booleans
  63. $(foreach bool,$1,$(eval $(call assert_boolean,$(bool))))
  64. endef
  65. 0-9 := 0 1 2 3 4 5 6 7 8 9
  66. # Function to verify that a given option $(1) contains a numeric value
  67. define assert_numeric
  68. $(if $($(1)),,$(error $(1) must not be empty))
  69. $(eval __numeric := $($(1)))
  70. $(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric))))
  71. $(if $(__numeric),$(error $(1) must be numeric))
  72. endef
  73. # Convenience function for verifying options have numeric values
  74. # $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values
  75. define assert_numerics
  76. $(foreach num,$1,$(eval $(call assert_numeric,$(num))))
  77. endef
  78. # Convenience function to check for a given linker option. An call to
  79. # $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker
  80. ld_option = $(shell $($(ARCH)-ld) $(1) -Wl,--version >/dev/null 2>&1 || $($(ARCH)-ld) $(1) -v >/dev/null 2>&1 && echo $(1))
  81. # Convenience function to check for a given compiler option. A call to
  82. # $(call cc_option, --no-XYZ) will return --no-XYZ if supported by the compiler
  83. define cc_option
  84. $(shell if $($(ARCH)-cc) $(1) -c -x c /dev/null -o /dev/null >/dev/null 2>&1; then echo $(1); fi )
  85. endef
  86. # CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
  87. # $(2) and assign the sequence to $(1)
  88. define CREATE_SEQ
  89. $(if $(word $(2), $($(1))),\
  90. $(eval $(1) += $(words $($(1))))\
  91. $(eval $(1) := $(filter-out 0,$($(1)))),\
  92. $(eval $(1) += $(words $($(1))))\
  93. $(call CREATE_SEQ,$(1),$(2))\
  94. )
  95. endef
  96. # IMG_MAPFILE defines the output file describing the memory map corresponding
  97. # to a BL stage
  98. # $(1) = BL stage
  99. define IMG_MAPFILE
  100. ${BUILD_DIR}/$(1).map
  101. endef
  102. # IMG_ELF defines the elf file corresponding to a BL stage
  103. # $(1) = BL stage
  104. define IMG_ELF
  105. ${BUILD_DIR}/$(1).elf
  106. endef
  107. # IMG_DUMP defines the symbols dump file corresponding to a BL stage
  108. # $(1) = BL stage
  109. define IMG_DUMP
  110. ${BUILD_DIR}/$(1).dump
  111. endef
  112. # IMG_BIN defines the default image file corresponding to a BL stage
  113. # $(1) = BL stage
  114. define IMG_BIN
  115. ${BUILD_PLAT}/$(1).bin
  116. endef
  117. # IMG_ENC_BIN defines the default encrypted image file corresponding to a
  118. # BL stage
  119. # $(1) = BL stage
  120. define IMG_ENC_BIN
  121. ${BUILD_PLAT}/$(1)_enc.bin
  122. endef
  123. # ENCRYPT_FW invokes enctool to encrypt firmware binary
  124. # $(1) = input firmware binary
  125. # $(2) = output encrypted firmware binary
  126. define ENCRYPT_FW
  127. $(2): $(1) enctool
  128. $$(s)echo " ENC $$<"
  129. $$(q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
  130. endef
  131. # TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
  132. # package a new payload and/or by cert_create to generate certificate.
  133. # Optionally, it adds the dependency on this payload
  134. # $(1) = payload filename (i.e. bl31.bin)
  135. # $(2) = command line option for the specified payload (i.e. --soc-fw)
  136. # $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
  137. # $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  138. # $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
  139. define TOOL_ADD_PAYLOAD
  140. ifneq ($(5),)
  141. $(4)FIP_ARGS += $(2) $(5)
  142. $(if $(3),$(4)CRT_DEPS += $(1))
  143. else
  144. $(4)FIP_ARGS += $(2) $(1)
  145. $(if $(3),$(4)CRT_DEPS += $(3))
  146. endif
  147. $(if $(3),$(4)FIP_DEPS += $(3))
  148. $(4)CRT_ARGS += $(2) $(1)
  149. endef
  150. # TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
  151. # before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
  152. # $(1) = image_type (scp_bl2, bl33, etc.)
  153. # $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
  154. # $(3) = command line option for the specified payload (ex. --soc-fw)
  155. # $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
  156. # $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  157. # $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
  158. define TOOL_ADD_IMG_PAYLOAD
  159. $(eval PRE_TOOL_FILTER := $($(call uppercase,$(1))_PRE_TOOL_FILTER))
  160. ifneq ($(PRE_TOOL_FILTER),)
  161. $(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
  162. $(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
  163. $(PROCESSED_PATH): $(4)
  164. $(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
  165. else
  166. $(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
  167. endif
  168. endef
  169. # CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
  170. # $(1) = parameter filename
  171. # $(2) = cert_create command line option for the specified parameter
  172. # $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  173. define CERT_ADD_CMD_OPT
  174. $(3)CRT_ARGS += $(2) $(1)
  175. endef
  176. # TOOL_ADD_IMG allows the platform to specify an external image to be packed
  177. # in the FIP and/or for which certificate is generated. It also adds a
  178. # dependency on the image file, aborting the build if the file does not exist.
  179. # $(1) = image_type (scp_bl2, bl33, etc.)
  180. # $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
  181. # $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  182. # $(4) = Image encryption flag (optional) (0, 1)
  183. # Example:
  184. # $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
  185. define TOOL_ADD_IMG
  186. # Build option to specify the image filename (SCP_BL2, BL33, etc)
  187. # This is the uppercase form of the first parameter
  188. $(eval _V := $(call uppercase,$(1)))
  189. # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
  190. # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
  191. # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
  192. $(eval check_$(1)_cmd := \
  193. $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
  194. $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
  195. )
  196. $(3)CRT_DEPS += check_$(1)
  197. CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
  198. ifeq ($(4),1)
  199. $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
  200. $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
  201. $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
  202. $(ENC_BIN))
  203. else
  204. $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
  205. endif
  206. .PHONY: check_$(1)
  207. check_$(1):
  208. $(check_$(1)_cmd)
  209. endef
  210. # SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
  211. # build the host tools by checking the version of OpenSSL located under
  212. # the path defined by the OPENSSL_DIR variable. It receives no parameters.
  213. define SELECT_OPENSSL_API_VERSION
  214. # Set default value for USING_OPENSSL3 macro to 0
  215. $(eval USING_OPENSSL3 = 0)
  216. # Obtain the OpenSSL version for the build located under OPENSSL_DIR
  217. $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
  218. $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
  219. $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
  220. # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
  221. $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
  222. endef
  223. ################################################################################
  224. # Generic image processing filters
  225. ################################################################################
  226. # GZIP
  227. define GZIP_RULE
  228. $(1): $(2)
  229. $(s)echo " GZIP $$@"
  230. $(q)gzip -n -f -9 $$< --stdout > $$@
  231. endef
  232. GZIP_SUFFIX := .gz
  233. ################################################################################
  234. # Auxiliary macros to build TF images from sources
  235. ################################################################################
  236. MAKE_DEP = -Wp,-MD,$(DEP) -MT $$@ -MP
  237. # MAKE_C_LIB builds a C source file and generates the dependency file
  238. # $(1) = output directory
  239. # $(2) = source file (%.c)
  240. # $(3) = library name
  241. define MAKE_C_LIB
  242. $(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
  243. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  244. $(eval LIB := $(call uppercase, $(notdir $(1))))
  245. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
  246. $$(s)echo " CC $$<"
  247. $$(q)$($(ARCH)-cc) $$($(LIB)_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -c $$< -o $$@
  248. -include $(DEP)
  249. endef
  250. # MAKE_S_LIB builds an assembly source file and generates the dependency file
  251. # $(1) = output directory
  252. # $(2) = source file (%.S)
  253. # $(3) = library name
  254. define MAKE_S_LIB
  255. $(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
  256. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  257. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
  258. $$(s)echo " AS $$<"
  259. $$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS_$(ARCH)) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
  260. -include $(DEP)
  261. endef
  262. # MAKE_C builds a C source file and generates the dependency file
  263. # $(1) = output directory
  264. # $(2) = source file (%.c)
  265. # $(3) = BL stage
  266. define MAKE_C
  267. $(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
  268. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  269. $(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
  270. $(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
  271. $(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
  272. $(eval BL_CFLAGS := $($(call uppercase,$(3))_CFLAGS) $(PLAT_BL_COMMON_CFLAGS))
  273. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
  274. $$(s)echo " CC $$<"
  275. $$(q)$($(ARCH)-cc) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(MAKE_DEP) -c $$< -o $$@
  276. -include $(DEP)
  277. endef
  278. # MAKE_S builds an assembly source file and generates the dependency file
  279. # $(1) = output directory
  280. # $(2) = assembly file (%.S)
  281. # $(3) = BL stage
  282. define MAKE_S
  283. $(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
  284. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  285. $(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
  286. $(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
  287. $(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
  288. $(eval BL_ASFLAGS := $($(call uppercase,$(3))_ASFLAGS) $(PLAT_BL_COMMON_ASFLAGS))
  289. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
  290. $$(s)echo " AS $$<"
  291. $$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS_$(ARCH)) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
  292. -include $(DEP)
  293. endef
  294. # MAKE_LD generate the linker script using the C preprocessor
  295. # $(1) = output linker script
  296. # $(2) = input template
  297. # $(3) = BL stage
  298. define MAKE_LD
  299. $(eval DEP := $(1).d)
  300. $(eval BL_DEFINES := IMAGE_$(call uppercase,$(3)) $($(call uppercase,$(3))_DEFINES) $(PLAT_BL_COMMON_DEFINES))
  301. $(eval BL_INCLUDE_DIRS := $($(call uppercase,$(3))_INCLUDE_DIRS) $(PLAT_BL_COMMON_INCLUDE_DIRS))
  302. $(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)) $(PLAT_BL_COMMON_CPPFLAGS))
  303. $(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
  304. $$(s)echo " PP $$<"
  305. $$(q)$($(ARCH)-cpp) -E $$(CPPFLAGS) $(BL_CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(MAKE_DEP) -o $$@ $$<
  306. -include $(DEP)
  307. endef
  308. # MAKE_LIB_OBJS builds both C and assembly source files
  309. # $(1) = output directory
  310. # $(2) = list of source files
  311. # $(3) = name of the library
  312. define MAKE_LIB_OBJS
  313. $(eval C_OBJS := $(filter %.c,$(2)))
  314. $(eval REMAIN := $(filter-out %.c,$(2)))
  315. $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3))))
  316. $(eval S_OBJS := $(filter %.S,$(REMAIN)))
  317. $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
  318. $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3))))
  319. $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
  320. endef
  321. # MAKE_OBJS builds both C and assembly source files
  322. # $(1) = output directory
  323. # $(2) = list of source files (both C and assembly)
  324. # $(3) = BL stage
  325. define MAKE_OBJS
  326. $(eval C_OBJS := $(filter %.c,$(2)))
  327. $(eval REMAIN := $(filter-out %.c,$(2)))
  328. $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
  329. $(eval S_OBJS := $(filter %.S,$(REMAIN)))
  330. $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
  331. $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
  332. $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
  333. endef
  334. # NOTE: The line continuation '\' is required in the next define otherwise we
  335. # end up with a line-feed characer at the end of the last c filename.
  336. # Also bear this issue in mind if extending the list of supported filetypes.
  337. define SOURCES_TO_OBJS
  338. $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
  339. $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
  340. endef
  341. .PHONY: libraries
  342. # MAKE_LIB macro defines the targets and options to build each BL image.
  343. # Arguments:
  344. # $(1) = Library name
  345. define MAKE_LIB
  346. $(eval BUILD_DIR := ${BUILD_PLAT}/lib$(1))
  347. $(eval LIB_DIR := ${BUILD_PLAT}/lib)
  348. $(eval ROMLIB_DIR := ${BUILD_PLAT}/romlib)
  349. $(eval SOURCES := $(LIB$(call uppercase,$(1))_SRCS))
  350. $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
  351. $(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
  352. libraries: ${LIB_DIR}/lib$(1).a
  353. ifeq ($($(ARCH)-ld-id),arm-link)
  354. LDPATHS = --userlibpath=${LIB_DIR}
  355. LDLIBS += --library=$(1)
  356. else
  357. LDPATHS = -L${LIB_DIR}
  358. LDLIBS += -l$(1)
  359. endif
  360. ifeq ($(USE_ROMLIB),1)
  361. LIBWRAPPER = -lwrappers
  362. endif
  363. all: ${LIB_DIR}/lib$(1).a
  364. ${LIB_DIR}/lib$(1).a: $(OBJS) | $$$$(@D)/
  365. $$(s)echo " AR $$@"
  366. $$(q)$($(ARCH)-ar) cr $$@ $$?
  367. endef
  368. # Generate the path to one or more preprocessed linker scripts given the paths
  369. # of their sources.
  370. #
  371. # Arguments:
  372. # $(1) = path to one or more linker script sources
  373. define linker_script_path
  374. $(patsubst %.S,$(BUILD_DIR)/%,$(1))
  375. endef
  376. ifeq ($(USE_ROMLIB),1)
  377. WRAPPER_FLAGS := @${BUILD_PLAT}/romlib/romlib.ldflags
  378. endif
  379. # MAKE_BL macro defines the targets and options to build each BL image.
  380. # Arguments:
  381. # $(1) = BL stage
  382. # $(2) = FIP command line option (if empty, image will not be included in the FIP)
  383. # $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  384. # $(4) = BL encryption flag (optional) (0, 1)
  385. define MAKE_BL
  386. $(eval BUILD_DIR := ${BUILD_PLAT}/$(1))
  387. $(eval BL_SOURCES := $($(call uppercase,$(1))_SOURCES))
  388. $(eval SOURCES := $(sort $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)))
  389. $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
  390. $(eval MAPFILE := $(call IMG_MAPFILE,$(1)))
  391. $(eval ELF := $(call IMG_ELF,$(1)))
  392. $(eval DUMP := $(call IMG_DUMP,$(1)))
  393. $(eval BIN := $(call IMG_BIN,$(1)))
  394. $(eval ENC_BIN := $(call IMG_ENC_BIN,$(1)))
  395. $(eval BL_LIBS := $($(call uppercase,$(1))_LIBS))
  396. $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(call uppercase,$(1))_DEFAULT_LINKER_SCRIPT_SOURCE))
  397. $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
  398. $(eval LINKER_SCRIPT_SOURCES := $($(call uppercase,$(1))_LINKER_SCRIPT_SOURCES))
  399. $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
  400. $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
  401. # Generate targets to preprocess each required linker script
  402. $(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
  403. $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1))))
  404. $(eval BL_LDFLAGS := $($(call uppercase,$(1))_LDFLAGS))
  405. ifeq ($(USE_ROMLIB),1)
  406. $(ELF): romlib.bin | $$$$(@D)/
  407. endif
  408. # MODULE_OBJS can be assigned by vendors with different compiled
  409. # object file path, and prebuilt object file path.
  410. $(eval OBJS += $(MODULE_OBJS))
  411. $(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $$$$(@D)/ libraries $(BL_LIBS)
  412. $$(s)echo " LD $$@"
  413. ifeq ($($(ARCH)-ld-id),arm-link)
  414. $$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
  415. --predefine=$(call escape-shell,-D__LINKER__=$(__LINKER__)) \
  416. --predefine=$(call escape-shell,-DTF_CFLAGS=$(TF_CFLAGS)) \
  417. --map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
  418. $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) $(OBJS)
  419. else ifeq ($($(ARCH)-ld-id),gnu-gcc)
  420. $$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $$(WRAPPER_FLAGS) $(BL_LDFLAGS) -Wl,-Map=$(MAPFILE) \
  421. $(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \
  422. $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
  423. else
  424. $$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $$(WRAPPER_FLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
  425. $(addprefix -T ,$(LINKER_SCRIPTS)) --script $(DEFAULT_LINKER_SCRIPT) \
  426. $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
  427. endif
  428. ifeq ($(DISABLE_BIN_GENERATION),1)
  429. $(s)echo
  430. $(s)echo "Built $$@ successfully"
  431. $(s)echo
  432. endif
  433. $(DUMP): $(ELF) | $$$$(@D)/
  434. $$(s)echo " OD $$@"
  435. $$(q)$($(ARCH)-od) -dx $$< > $$@
  436. $(BIN): $(ELF) | $$$$(@D)/
  437. $$(s)echo " BIN $$@"
  438. $$(q)$($(ARCH)-oc) -O binary $$< $$@
  439. $(s)echo
  440. $(s)echo "Built $$@ successfully"
  441. $(s)echo
  442. .PHONY: $(1)
  443. ifeq ($(DISABLE_BIN_GENERATION),1)
  444. $(1): $(ELF) $(DUMP)
  445. else
  446. $(1): $(BIN) $(DUMP)
  447. endif
  448. all: $(1)
  449. ifeq ($(4),1)
  450. $(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
  451. $(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \
  452. $(ENC_BIN)))
  453. else
  454. $(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(BIN),$(3)))
  455. endif
  456. endef
  457. # Convert device tree source file names to matching blobs
  458. # $(1) = input dts
  459. define SOURCES_TO_DTBS
  460. $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
  461. endef
  462. # MAKE_DTB generate the Flattened device tree binary
  463. # $(1) = output directory
  464. # $(2) = input dts
  465. define MAKE_DTB
  466. # List of DTB file(s) to generate, based on DTS file basename list
  467. $(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
  468. # List of the pre-compiled DTS file(s)
  469. $(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
  470. # Dependencies of the pre-compiled DTS file(s) on its source and included files
  471. $(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
  472. # Dependencies of the DT compilation on its pre-compiled DTS
  473. $(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
  474. $(DPRE): $(2) | $$$$(@D)/
  475. $$(s)echo " CPP $$<"
  476. $(eval DTBS := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
  477. $$(q)$($(ARCH)-cpp) -E $$(TF_CFLAGS_$(ARCH)) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
  478. $(DOBJ): $(DPRE) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
  479. $$(s)echo " DTC $$<"
  480. $$(q)$($(ARCH)-dtc) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $$<
  481. -include $(DTBDEP)
  482. -include $(DTSDEP)
  483. endef
  484. # MAKE_DTBS builds flattened device tree sources
  485. # $(1) = output directory
  486. # $(2) = list of flattened device tree source files
  487. define MAKE_DTBS
  488. $(eval DOBJS := $(filter %.dts,$(2)))
  489. $(eval REMAIN := $(filter-out %.dts,$(2)))
  490. $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
  491. $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
  492. dtbs: $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))
  493. all: dtbs
  494. endef