build_macros.mk 22 KB

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