build_macros.mk 22 KB

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