build_macros.mk 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #
  2. # Copyright (c) 2015-2022, 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. # Some utility macros for manipulating awkward (whitespace) characters.
  12. blank :=
  13. space :=${blank} ${blank}
  14. # A user defined function to recursively search for a filename below a directory
  15. # $1 is the directory root of the recursive search (blank for current directory).
  16. # $2 is the file name to search for.
  17. define rwildcard
  18. $(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
  19. endef
  20. # This table is used in converting lower case to upper case.
  21. uppercase_table:=a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
  22. # Internal macro used for converting lower case to upper case.
  23. # $(1) = upper case table
  24. # $(2) = String to convert
  25. define uppercase_internal
  26. $(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2))
  27. endef
  28. # A macro for converting a string to upper case
  29. # $(1) = String to convert
  30. define uppercase
  31. $(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result)
  32. endef
  33. # Convenience function for setting a variable to 0 if not previously set
  34. # $(eval $(call default_zero,FOO))
  35. define default_zero
  36. $(eval $(1) ?= 0)
  37. endef
  38. # Convenience function for setting a list of variables to 0 if not previously set
  39. # $(eval $(call default_zeros,FOO BAR))
  40. define default_zeros
  41. $(foreach var,$1,$(eval $(call default_zero,$(var))))
  42. endef
  43. # Convenience function for adding build definitions
  44. # $(eval $(call add_define,FOO)) will have:
  45. # -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
  46. define add_define
  47. DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),)
  48. endef
  49. # Convenience function for addding multiple build definitions
  50. # $(eval $(call add_defines,FOO BOO))
  51. define add_defines
  52. $(foreach def,$1,$(eval $(call add_define,$(def))))
  53. endef
  54. # Convenience function for adding build definitions
  55. # $(eval $(call add_define_val,FOO,BAR)) will have:
  56. # -DFOO=BAR
  57. define add_define_val
  58. DEFINES += -D$(1)=$(2)
  59. endef
  60. # Convenience function for verifying option has a boolean value
  61. # $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
  62. define assert_boolean
  63. $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean))
  64. endef
  65. # Convenience function for verifying options have boolean values
  66. # $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values
  67. define assert_booleans
  68. $(foreach bool,$1,$(eval $(call assert_boolean,$(bool))))
  69. endef
  70. 0-9 := 0 1 2 3 4 5 6 7 8 9
  71. # Function to verify that a given option $(1) contains a numeric value
  72. define assert_numeric
  73. $(if $($(1)),,$(error $(1) must not be empty))
  74. $(eval __numeric := $($(1)))
  75. $(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric))))
  76. $(if $(__numeric),$(error $(1) must be numeric))
  77. endef
  78. # Convenience function for verifying options have numeric values
  79. # $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values
  80. define assert_numerics
  81. $(foreach num,$1,$(eval $(call assert_numeric,$(num))))
  82. endef
  83. # Convenience function to check for a given linker option. An call to
  84. # $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker
  85. define ld_option
  86. $(shell if $(LD) $(1) -v >/dev/null 2>&1; then echo $(1); fi )
  87. endef
  88. # CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
  89. # $(2) and assign the sequence to $(1)
  90. define CREATE_SEQ
  91. $(if $(word $(2), $($(1))),\
  92. $(eval $(1) += $(words $($(1))))\
  93. $(eval $(1) := $(filter-out 0,$($(1)))),\
  94. $(eval $(1) += $(words $($(1))))\
  95. $(call CREATE_SEQ,$(1),$(2))\
  96. )
  97. endef
  98. # IMG_LINKERFILE defines the linker script corresponding to a BL stage
  99. # $(1) = BL stage
  100. define IMG_LINKERFILE
  101. ${BUILD_DIR}/$(1).ld
  102. endef
  103. # IMG_MAPFILE defines the output file describing the memory map corresponding
  104. # to a BL stage
  105. # $(1) = BL stage
  106. define IMG_MAPFILE
  107. ${BUILD_DIR}/$(1).map
  108. endef
  109. # IMG_ELF defines the elf file corresponding to a BL stage
  110. # $(1) = BL stage
  111. define IMG_ELF
  112. ${BUILD_DIR}/$(1).elf
  113. endef
  114. # IMG_DUMP defines the symbols dump file corresponding to a BL stage
  115. # $(1) = BL stage
  116. define IMG_DUMP
  117. ${BUILD_DIR}/$(1).dump
  118. endef
  119. # IMG_BIN defines the default image file corresponding to a BL stage
  120. # $(1) = BL stage
  121. define IMG_BIN
  122. ${BUILD_PLAT}/$(1).bin
  123. endef
  124. # IMG_ENC_BIN defines the default encrypted image file corresponding to a
  125. # BL stage
  126. # $(1) = BL stage
  127. define IMG_ENC_BIN
  128. ${BUILD_PLAT}/$(1)_enc.bin
  129. endef
  130. # ENCRYPT_FW invokes enctool to encrypt firmware binary
  131. # $(1) = input firmware binary
  132. # $(2) = output encrypted firmware binary
  133. define ENCRYPT_FW
  134. $(2): $(1) enctool
  135. $$(ECHO) " ENC $$<"
  136. $$(Q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
  137. endef
  138. # TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
  139. # package a new payload and/or by cert_create to generate certificate.
  140. # Optionally, it adds the dependency on this payload
  141. # $(1) = payload filename (i.e. bl31.bin)
  142. # $(2) = command line option for the specified payload (i.e. --soc-fw)
  143. # $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
  144. # $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  145. # $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
  146. define TOOL_ADD_PAYLOAD
  147. ifneq ($(5),)
  148. $(4)FIP_ARGS += $(2) $(5)
  149. $(if $(3),$(4)CRT_DEPS += $(1))
  150. else
  151. $(4)FIP_ARGS += $(2) $(1)
  152. $(if $(3),$(4)CRT_DEPS += $(3))
  153. endif
  154. $(if $(3),$(4)FIP_DEPS += $(3))
  155. $(4)CRT_ARGS += $(2) $(1)
  156. endef
  157. # TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
  158. # before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
  159. # $(1) = image_type (scp_bl2, bl33, etc.)
  160. # $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
  161. # $(3) = command line option for the specified payload (ex. --soc-fw)
  162. # $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
  163. # $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  164. # $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
  165. define TOOL_ADD_IMG_PAYLOAD
  166. $(eval PRE_TOOL_FILTER := $($(call uppercase,$(1))_PRE_TOOL_FILTER))
  167. ifneq ($(PRE_TOOL_FILTER),)
  168. $(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
  169. $(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
  170. $(PROCESSED_PATH): $(4)
  171. $(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
  172. else
  173. $(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
  174. endif
  175. endef
  176. # CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
  177. # $(1) = parameter filename
  178. # $(2) = cert_create command line option for the specified parameter
  179. # $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  180. define CERT_ADD_CMD_OPT
  181. $(3)CRT_ARGS += $(2) $(1)
  182. endef
  183. # TOOL_ADD_IMG allows the platform to specify an external image to be packed
  184. # in the FIP and/or for which certificate is generated. It also adds a
  185. # dependency on the image file, aborting the build if the file does not exist.
  186. # $(1) = image_type (scp_bl2, bl33, etc.)
  187. # $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
  188. # $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  189. # $(4) = Image encryption flag (optional) (0, 1)
  190. # Example:
  191. # $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
  192. define TOOL_ADD_IMG
  193. # Build option to specify the image filename (SCP_BL2, BL33, etc)
  194. # This is the uppercase form of the first parameter
  195. $(eval _V := $(call uppercase,$(1)))
  196. # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
  197. # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
  198. # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
  199. $(eval check_$(1)_cmd := \
  200. $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
  201. $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
  202. )
  203. $(3)CRT_DEPS += check_$(1)
  204. CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
  205. ifeq ($(4),1)
  206. $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
  207. $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
  208. $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
  209. $(ENC_BIN))
  210. else
  211. $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
  212. endif
  213. .PHONY: check_$(1)
  214. check_$(1):
  215. $(check_$(1)_cmd)
  216. endef
  217. # SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
  218. # build the host tools by checking the version of OpenSSL located under
  219. # the path defined by the OPENSSL_DIR variable. It receives no parameters.
  220. define SELECT_OPENSSL_API_VERSION
  221. # Set default value for USING_OPENSSL3 macro to 0
  222. $(eval USING_OPENSSL3 = 0)
  223. # Obtain the OpenSSL version for the build located under OPENSSL_DIR
  224. $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
  225. $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
  226. $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
  227. # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
  228. $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
  229. endef
  230. ################################################################################
  231. # Generic image processing filters
  232. ################################################################################
  233. # GZIP
  234. define GZIP_RULE
  235. $(1): $(2)
  236. $(ECHO) " GZIP $$@"
  237. $(Q)gzip -n -f -9 $$< --stdout > $$@
  238. endef
  239. GZIP_SUFFIX := .gz
  240. ################################################################################
  241. # Auxiliary macros to build TF images from sources
  242. ################################################################################
  243. MAKE_DEP = -Wp,-MD,$(DEP) -MT $$@ -MP
  244. # MAKE_C_LIB builds a C source file and generates the dependency file
  245. # $(1) = output directory
  246. # $(2) = source file (%.c)
  247. # $(3) = library name
  248. define MAKE_C_LIB
  249. $(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
  250. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  251. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
  252. $$(ECHO) " CC $$<"
  253. $$(Q)$$(CC) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -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. define MAKE_S_LIB
  261. $(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
  262. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  263. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
  264. $$(ECHO) " AS $$<"
  265. $$(Q)$$(AS) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
  266. -include $(DEP)
  267. endef
  268. # MAKE_C builds a C source file and generates the dependency file
  269. # $(1) = output directory
  270. # $(2) = source file (%.c)
  271. # $(3) = BL stage
  272. define MAKE_C
  273. $(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
  274. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  275. $(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
  276. $(eval BL_CFLAGS := $($(call uppercase,$(3))_CFLAGS))
  277. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
  278. $$(ECHO) " CC $$<"
  279. $$(Q)$$(CC) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(MAKE_DEP) -c $$< -o $$@
  280. -include $(DEP)
  281. endef
  282. # MAKE_S builds an assembly source file and generates the dependency file
  283. # $(1) = output directory
  284. # $(2) = assembly file (%.S)
  285. # $(3) = BL stage
  286. define MAKE_S
  287. $(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
  288. $(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
  289. $(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
  290. $(eval BL_ASFLAGS := $($(call uppercase,$(3))_ASFLAGS))
  291. $(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
  292. $$(ECHO) " AS $$<"
  293. $$(Q)$$(AS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
  294. -include $(DEP)
  295. endef
  296. # MAKE_LD generate the linker script using the C preprocessor
  297. # $(1) = output linker script
  298. # $(2) = input template
  299. # $(3) = BL stage
  300. define MAKE_LD
  301. $(eval DEP := $(1).d)
  302. $(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
  303. $(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
  304. $$(ECHO) " PP $$<"
  305. $$(Q)$$(CPP) $$(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. # Allow overriding the timestamp, for example for reproducible builds, or to
  342. # synchronize timestamps across multiple projects.
  343. # This must be set to a C string (including quotes where applicable).
  344. BUILD_MESSAGE_TIMESTAMP ?= __TIME__", "__DATE__
  345. .PHONY: libraries
  346. # MAKE_LIB_DIRS macro defines the target for the directory where
  347. # libraries are created
  348. define MAKE_LIB_DIRS
  349. $(eval LIB_DIR := ${BUILD_PLAT}/lib)
  350. $(eval ROMLIB_DIR := ${BUILD_PLAT}/romlib)
  351. $(eval LIBWRAPPER_DIR := ${BUILD_PLAT}/libwrapper)
  352. $(eval $(call MAKE_PREREQ_DIR,${LIB_DIR},${BUILD_PLAT}))
  353. $(eval $(call MAKE_PREREQ_DIR,${ROMLIB_DIR},${BUILD_PLAT}))
  354. $(eval $(call MAKE_PREREQ_DIR,${LIBWRAPPER_DIR},${BUILD_PLAT}))
  355. endef
  356. # MAKE_LIB macro defines the targets and options to build each BL image.
  357. # Arguments:
  358. # $(1) = Library name
  359. define MAKE_LIB
  360. $(eval BUILD_DIR := ${BUILD_PLAT}/lib$(1))
  361. $(eval LIB_DIR := ${BUILD_PLAT}/lib)
  362. $(eval ROMLIB_DIR := ${BUILD_PLAT}/romlib)
  363. $(eval SOURCES := $(LIB$(call uppercase,$(1))_SRCS))
  364. $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
  365. $(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
  366. $(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
  367. .PHONY : lib${1}_dirs
  368. lib${1}_dirs: | ${BUILD_DIR} ${LIB_DIR} ${ROMLIB_DIR} ${LIBWRAPPER_DIR}
  369. libraries: ${LIB_DIR}/lib$(1).a
  370. ifneq ($(findstring armlink,$(notdir $(LD))),)
  371. LDPATHS = --userlibpath=${LIB_DIR}
  372. LDLIBS += --library=$(1)
  373. else
  374. LDPATHS = -L${LIB_DIR}
  375. LDLIBS += -l$(1)
  376. endif
  377. ifeq ($(USE_ROMLIB),1)
  378. LIBWRAPPER = -lwrappers
  379. endif
  380. all: ${LIB_DIR}/lib$(1).a
  381. ${LIB_DIR}/lib$(1).a: $(OBJS)
  382. $$(ECHO) " AR $$@"
  383. $$(Q)$$(AR) cr $$@ $$?
  384. endef
  385. # MAKE_BL macro defines the targets and options to build each BL image.
  386. # Arguments:
  387. # $(1) = BL stage
  388. # $(2) = FIP command line option (if empty, image will not be included in the FIP)
  389. # $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
  390. # $(4) = BL encryption flag (optional) (0, 1)
  391. define MAKE_BL
  392. $(eval BUILD_DIR := ${BUILD_PLAT}/$(1))
  393. $(eval BL_SOURCES := $($(call uppercase,$(1))_SOURCES))
  394. $(eval SOURCES := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
  395. $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
  396. $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1)))
  397. $(eval MAPFILE := $(call IMG_MAPFILE,$(1)))
  398. $(eval ELF := $(call IMG_ELF,$(1)))
  399. $(eval DUMP := $(call IMG_DUMP,$(1)))
  400. $(eval BIN := $(call IMG_BIN,$(1)))
  401. $(eval ENC_BIN := $(call IMG_ENC_BIN,$(1)))
  402. $(eval BL_LINKERFILE := $($(call uppercase,$(1))_LINKERFILE))
  403. $(eval BL_LIBS := $($(call uppercase,$(1))_LIBS))
  404. # We use sort only to get a list of unique object directory names.
  405. # ordering is not relevant but sort removes duplicates.
  406. $(eval TEMP_OBJ_DIRS := $(sort $(dir ${OBJS} ${LINKERFILE})))
  407. # The $(dir ) function leaves a trailing / on the directory names
  408. # Rip off the / to match directory names with make rule targets.
  409. $(eval OBJ_DIRS := $(patsubst %/,%,$(TEMP_OBJ_DIRS)))
  410. # Create generators for object directory structure
  411. $(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
  412. $(eval $(foreach objd,${OBJ_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
  413. .PHONY : ${1}_dirs
  414. # We use order-only prerequisites to ensure that directories are created,
  415. # but do not cause re-builds every time a file is written.
  416. ${1}_dirs: | ${OBJ_DIRS}
  417. $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
  418. $(eval $(call MAKE_LD,$(LINKERFILE),$(BL_LINKERFILE),$(1)))
  419. $(eval BL_LDFLAGS := $($(call uppercase,$(1))_LDFLAGS))
  420. ifeq ($(USE_ROMLIB),1)
  421. $(ELF): romlib.bin
  422. endif
  423. # MODULE_OBJS can be assigned by vendors with different compiled
  424. # object file path, and prebuilt object file path.
  425. $(eval OBJS += $(MODULE_OBJS))
  426. $(ELF): $(OBJS) $(LINKERFILE) | $(1)_dirs libraries $(BL_LIBS)
  427. $$(ECHO) " LD $$@"
  428. ifdef MAKE_BUILD_STRINGS
  429. $(call MAKE_BUILD_STRINGS, $(BUILD_DIR)/build_message.o)
  430. else
  431. @echo 'const char build_message[] = "Built : "$(BUILD_MESSAGE_TIMESTAMP); \
  432. const char version_string[] = "${VERSION_STRING}"; \
  433. const char version[] = "${VERSION}";' | \
  434. $$(CC) $$(TF_CFLAGS) $$(CFLAGS) -xc -c - -o $(BUILD_DIR)/build_message.o
  435. endif
  436. ifneq ($(findstring armlink,$(notdir $(LD))),)
  437. $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
  438. --predefine="-D__LINKER__=$(__LINKER__)" \
  439. --predefine="-DTF_CFLAGS=$(TF_CFLAGS)" \
  440. --map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
  441. $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \
  442. $(BUILD_DIR)/build_message.o $(OBJS)
  443. else ifneq ($(findstring gcc,$(notdir $(LD))),)
  444. $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) -Wl,-Map=$(MAPFILE) \
  445. -Wl,-dT $(LINKERFILE) $(EXTRA_LINKERFILE) $(BUILD_DIR)/build_message.o \
  446. $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
  447. else
  448. $$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
  449. --script $(LINKERFILE) $(BUILD_DIR)/build_message.o \
  450. $(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
  451. endif
  452. ifeq ($(DISABLE_BIN_GENERATION),1)
  453. @${ECHO_BLANK_LINE}
  454. @echo "Built $$@ successfully"
  455. @${ECHO_BLANK_LINE}
  456. endif
  457. $(DUMP): $(ELF)
  458. $${ECHO} " OD $$@"
  459. $${Q}$${OD} -dx $$< > $$@
  460. $(BIN): $(ELF)
  461. $${ECHO} " BIN $$@"
  462. $$(Q)$$(OC) -O binary $$< $$@
  463. @${ECHO_BLANK_LINE}
  464. @echo "Built $$@ successfully"
  465. @${ECHO_BLANK_LINE}
  466. .PHONY: $(1)
  467. ifeq ($(DISABLE_BIN_GENERATION),1)
  468. $(1): $(ELF) $(DUMP)
  469. else
  470. $(1): $(BIN) $(DUMP)
  471. endif
  472. all: $(1)
  473. ifeq ($(4),1)
  474. $(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
  475. $(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \
  476. $(ENC_BIN)))
  477. else
  478. $(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(BIN),$(3)))
  479. endif
  480. endef
  481. # Convert device tree source file names to matching blobs
  482. # $(1) = input dts
  483. define SOURCES_TO_DTBS
  484. $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
  485. endef
  486. # MAKE_FDT_DIRS macro creates the prerequisite directories that host the
  487. # FDT binaries
  488. # $(1) = output directory
  489. # $(2) = input dts
  490. define MAKE_FDT_DIRS
  491. $(eval DTBS := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
  492. $(eval TEMP_DTB_DIRS := $(sort $(dir ${DTBS})))
  493. # The $(dir ) function leaves a trailing / on the directory names
  494. # Rip off the / to match directory names with make rule targets.
  495. $(eval DTB_DIRS := $(patsubst %/,%,$(TEMP_DTB_DIRS)))
  496. $(eval $(foreach objd,${DTB_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
  497. fdt_dirs: ${DTB_DIRS}
  498. endef
  499. # MAKE_DTB generate the Flattened device tree binary
  500. # $(1) = output directory
  501. # $(2) = input dts
  502. define MAKE_DTB
  503. # List of DTB file(s) to generate, based on DTS file basename list
  504. $(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
  505. # List of the pre-compiled DTS file(s)
  506. $(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
  507. # Dependencies of the pre-compiled DTS file(s) on its source and included files
  508. $(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
  509. # Dependencies of the DT compilation on its pre-compiled DTS
  510. $(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
  511. $(DOBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | fdt_dirs
  512. $${ECHO} " CPP $$<"
  513. $(eval DTBS := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
  514. $$(Q)$$(PP) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
  515. $${ECHO} " DTC $$<"
  516. $$(Q)$$(DTC) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $(DPRE)
  517. -include $(DTBDEP)
  518. -include $(DTSDEP)
  519. endef
  520. # MAKE_DTBS builds flattened device tree sources
  521. # $(1) = output directory
  522. # $(2) = list of flattened device tree source files
  523. define MAKE_DTBS
  524. $(eval DOBJS := $(filter %.dts,$(2)))
  525. $(eval REMAIN := $(filter-out %.dts,$(2)))
  526. $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
  527. $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
  528. $(eval $(call MAKE_FDT_DIRS,$(1),$(2)))
  529. dtbs: $(DTBS)
  530. all: dtbs
  531. endef