build_macros.mk 22 KB

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