build_macros.mk 24 KB

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