xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision 116d2c09446c8630d152bd3bbd70032d9d79a0fb)
1#
2# Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7# Report an error if the eval make function is not available.
8$(eval eval_available := T)
9ifneq (${eval_available},T)
10    $(error This makefile only works with a Make program that supports $$(eval))
11endif
12
13# A user defined function to recursively search for a filename below a directory
14#    $1 is the directory root of the recursive search (blank for current directory).
15#    $2 is the file name to search for.
16define rwildcard
17$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
18endef
19
20# Convenience function for setting a variable to 0 if not previously set
21# $(eval $(call default_zero,FOO))
22define default_zero
23	$(eval $(1) ?= 0)
24endef
25
26# Convenience function for setting a list of variables to 0 if not previously set
27# $(eval $(call default_zeros,FOO BAR))
28define default_zeros
29	$(foreach var,$1,$(eval $(call default_zero,$(var))))
30endef
31
32# Convenience function for setting a variable to 1 if not previously set
33# $(eval $(call default_one,FOO))
34define default_one
35	$(eval $(1) ?= 1)
36endef
37
38# Convenience function for setting a list of variables to 1 if not previously set
39# $(eval $(call default_ones,FOO BAR))
40define default_ones
41	$(foreach var,$1,$(eval $(call default_one,$(var))))
42endef
43
44# Convenience function for creating a build definition
45# $(call make_define,FOO) will have:
46# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
47make_define = -D$(1)$(if $($(1)),=$($(1)))
48
49# Convenience function for creating multiple build definitions
50# For BL1, BL1_CPPFLAGS += $(call make_defines,FOO BOO)
51make_defines = $(foreach def,$(1),$(call make_define,$(def)))
52
53# Convenience function for adding build definitions
54# $(eval $(call add_define,FOO)) will have:
55# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
56define add_define
57    DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
58endef
59
60# Convenience function for addding multiple build definitions
61# $(eval $(call add_defines,FOO BOO))
62define add_defines
63    $(foreach def,$1,$(eval $(call add_define,$(def))))
64endef
65
66# Convenience function for adding build definitions
67# $(eval $(call add_define_val,FOO,BAR)) will have:
68# -DFOO=BAR
69define add_define_val
70    DEFINES			+=	-D$(1)=$(2)
71endef
72
73# Convenience function for verifying option has a boolean value
74# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
75define assert_boolean
76    $(if $($(1)),,$(error $(1) must not be empty))
77    $(if $(filter-out 0 1,$($1)),$(error $1 must be boolean))
78endef
79
80# Convenience function for verifying options have boolean values
81# $(eval $(call assert_booleans,FOO BOO)) will assert FOO and BOO for 0 or 1 values
82define assert_booleans
83    $(foreach bool,$1,$(eval $(call assert_boolean,$(bool))))
84endef
85
860-9 := 0 1 2 3 4 5 6 7 8 9
87
88# Function to verify that a given option $(1) contains a numeric value
89define assert_numeric
90$(if $($(1)),,$(error $(1) must not be empty))
91$(eval __numeric := $($(1)))
92$(foreach d,$(0-9),$(eval __numeric := $(subst $(d),,$(__numeric))))
93$(if $(__numeric),$(error $(1) must be numeric))
94endef
95
96# Convenience function for verifying options have numeric values
97# $(eval $(call assert_numerics,FOO BOO)) will assert FOO and BOO contain numeric values
98define assert_numerics
99    $(foreach num,$1,$(eval $(call assert_numeric,$(num))))
100endef
101
102# Convenience function to check for a given linker option. A call to
103# $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker,
104# prefixed appropriately for the linker in use
105ld_option = $(call toolchain-ld-option,$(ARCH),$(1))
106
107# Convenience function to add a prefix to a linker flag if necessary. Useful
108# when the flag is known to be supported and it just needs to be prefixed
109# correctly.
110ld_prefix = $(toolchain-ld-prefix-$($(ARCH)-ld-id))
111
112# Convenience function to check for a given compiler option. A call to
113# $(call cc_option, --no-XYZ) will return --no-XYZ if supported by the compiler
114# NOTE: consider assigning to an immediately expanded temporary variable before
115# assigning. This is because variables like TF_CFLAGS are recursively expanded
116# and assigning this directly will cause it to be expanded every time the
117# variable is used, potentially thrashing multicore performance.
118define cc_option
119	$(shell if $($(ARCH)-cc) $(1) -c -x c /dev/null -o /dev/null >/dev/null 2>&1; then echo $(1); fi )
120endef
121
122# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
123# $(2) and assign the sequence to $(1)
124define CREATE_SEQ
125$(if $(word $(2), $($(1))),\
126  $(eval $(1) += $(words $($(1))))\
127  $(eval $(1) := $(filter-out 0,$($(1)))),\
128  $(eval $(1) += $(words $($(1))))\
129  $(call CREATE_SEQ,$(1),$(2))\
130)
131endef
132
133# IMG_MAPFILE defines the output file describing the memory map corresponding
134# to a BL stage
135#   $(1) = BL stage
136define IMG_MAPFILE
137    ${BUILD_DIR}/$(1).map
138endef
139
140# IMG_ELF defines the elf file corresponding to a BL stage
141#   $(1) = BL stage
142define IMG_ELF
143    ${BUILD_DIR}/$(1).elf
144endef
145
146# IMG_DUMP defines the symbols dump file corresponding to a BL stage
147#   $(1) = BL stage
148define IMG_DUMP
149    ${BUILD_DIR}/$(1).dump
150endef
151
152# IMG_BIN defines the default image file corresponding to a BL stage
153#   $(1) = BL stage
154define IMG_BIN
155    ${BUILD_PLAT}/$(1).bin
156endef
157
158# IMG_ENC_BIN defines the default encrypted image file corresponding to a
159# BL stage
160#   $(1) = BL stage
161define IMG_ENC_BIN
162    ${BUILD_PLAT}/$(1)_enc.bin
163endef
164
165# ENCRYPT_FW invokes enctool to encrypt firmware binary
166#   $(1) = input firmware binary
167#   $(2) = output encrypted firmware binary
168define ENCRYPT_FW
169$(2): $(1) enctool
170	$$(s)echo "  ENC     $$<"
171	$$(q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
172endef
173
174# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
175# package a new payload and/or by cert_create to generate certificate.
176# Optionally, it adds the dependency on this payload
177#   $(1) = payload filename (i.e. bl31.bin)
178#   $(2) = command line option for the specified payload (i.e. --soc-fw)
179#   $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
180#   $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
181#   $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
182define TOOL_ADD_PAYLOAD
183ifneq ($(5),)
184    $(4)FIP_ARGS += $(2) $(5)
185    $(if $(3),$(4)CRT_DEPS += $(1))
186else
187    $(4)FIP_ARGS += $(2) $(1)
188    $(if $(3),$(4)CRT_DEPS += $(3))
189endif
190    $(if $(3),$(4)FIP_DEPS += $(3))
191    $(4)CRT_ARGS += $(2) $(1)
192endef
193
194# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
195# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
196#   $(1) = image_type (scp_bl2, bl33, etc.)
197#   $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
198#   $(3) = command line option for the specified payload (ex. --soc-fw)
199#   $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
200#   $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
201#   $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
202
203define TOOL_ADD_IMG_PAYLOAD
204
205$(eval PRE_TOOL_FILTER := $($(1)_PRE_TOOL_FILTER))
206
207ifneq ($(PRE_TOOL_FILTER),)
208
209$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
210
211$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
212
213$(PROCESSED_PATH): $(4)
214
215$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
216
217else
218$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
219endif
220endef
221
222# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
223#   $(1) = parameter filename
224#   $(2) = cert_create command line option for the specified parameter
225#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
226define CERT_ADD_CMD_OPT
227    $(3)CRT_ARGS += $(2) $(1)
228endef
229
230# TOOL_ADD_IMG allows the platform to specify an external image to be packed
231# in the FIP and/or for which certificate is generated. It also adds a
232# dependency on the image file, aborting the build if the file does not exist.
233#   $(1) = image_type (scp_bl2, bl33, etc.)
234#   $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
235#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
236#   $(4) = Image encryption flag (optional) (0, 1)
237# Example:
238#   $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
239define TOOL_ADD_IMG
240    # Build option to specify the image filename (SCP_BL2, BL33, etc)
241    # This is the uppercase form of the first parameter
242    $(eval BL := $(call uppercase,$(1)))
243    $(eval _V := $(BL))
244
245    # $(check_$(1)_cmd) variable is executed in the check_$(1) target and also
246    # is put into the ${CHECK_$(3)FIP_CMD} variable which is executed by the
247    # target ${BUILD_PLAT}/${$(3)FIP_NAME}.
248    $(eval check_$(1)_cmd := \
249        $(if $(value $(_V)),,$$$$(error "Platform '${PLAT}' requires $(_V). Please set $(_V) to point to the right file")) \
250        $(if $(wildcard $(value $(_V))),,$$$$(error '$(_V)=$(value $(_V))' was specified, but '$(value $(_V))' does not exist)) \
251    )
252
253    $(3)CRT_DEPS += check_$(1)
254    CHECK_$(3)FIP_CMD += $$(check_$(1)_cmd)
255ifeq ($(4),1)
256    $(eval ENC_BIN := ${BUILD_PLAT}/$(1)_enc.bin)
257    $(call ENCRYPT_FW,$(value $(_V)),$(ENC_BIN))
258    $(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
259		$(ENC_BIN))
260else
261    $(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(value $(_V)),$(2),$(if $(wildcard $(value $(_V))),$(value $(_V)),FORCE),$(3))
262endif
263
264.PHONY: check_$(1)
265check_$(1):
266	$(check_$(1)_cmd)
267endef
268
269# SELECT_OPENSSL_API_VERSION selects the OpenSSL API version to be used to
270# build the host tools by checking the version of OpenSSL located under
271# the path defined by the OPENSSL_DIR variable. It receives no parameters.
272define SELECT_OPENSSL_API_VERSION
273    # Set default value for USING_OPENSSL3 macro to 0
274    $(eval USING_OPENSSL3 = 0)
275    # Obtain the OpenSSL version for the build located under OPENSSL_DIR
276    $(eval OPENSSL_INFO := $(shell LD_LIBRARY_PATH=${OPENSSL_DIR}:${OPENSSL_DIR}/lib ${OPENSSL_BIN_PATH}/openssl version))
277    $(eval OPENSSL_CURRENT_VER = $(word 2, ${OPENSSL_INFO}))
278    $(eval OPENSSL_CURRENT_VER_MAJOR = $(firstword $(subst ., ,$(OPENSSL_CURRENT_VER))))
279    # If OpenSSL version is 3.x, then set USING_OPENSSL3 flag to 1
280    $(if $(filter 3,$(OPENSSL_CURRENT_VER_MAJOR)), $(eval USING_OPENSSL3 = 1))
281endef
282
283################################################################################
284# Generic image processing filters
285################################################################################
286
287# GZIP
288define GZIP_RULE
289$(1): $(2)
290	$(s)echo "  GZIP    $$@"
291	$(q)gzip -n -f -9 $$< --stdout > $$@
292endef
293
294GZIP_SUFFIX := .gz
295
296################################################################################
297# Auxiliary macros to build TF images from sources
298################################################################################
299
300MAKE_DEP = -Wp,-MD,$1 -MT $2 -MP
301
302# MAKE_TOOL_C builds a C source file and generates the dependency file
303#   $(1) = output directory
304#   $(2) = source file (%.c)
305#   $(3) = lowercase name of the tool
306#   $(4) = uppercase name of the tool
307define MAKE_TOOL_C
308
309$(eval SRC := $(2))
310$(eval OBJ := $(patsubst %.c,$(1)/$(3)/%.o,$(SRC)))
311$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
312
313$(eval TOOL_DEFINES := $($(4)_DEFINES))
314$(eval TOOL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
315$(eval TOOL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(TOOL_DEFINES)) $(addprefix -I,$(TOOL_INCLUDE_DIRS)))
316$(eval TOOL_CFLAGS := $($(4)_CFLAGS))
317
318$(OBJ): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
319	$$(s)echo "  HOSTCC      $$<"
320	$$(q)$(host-cc) $$(HOSTCCFLAGS) $(TOOL_CPPFLAGS) $(TOOL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
321
322-include $(DEP)
323
324endef
325
326# MAKE_TOOL
327#   $(1) = output directory
328#   $(2) = lowercase name of the tool
329#   $(3) = uppercase name of the tool
330define MAKE_TOOL
331$(eval SRCS := $($(3)_SOURCES))
332$(eval OBJS := $(patsubst %.c,$(1)/$(2)/%.o,$(SRCS)))
333$(eval DST := $(1)/$(2)/$(2)$(.exe))
334$(eval $(foreach src,$(SRCS),$(call MAKE_TOOL_C,$(1),$(src),$(2),$(3))))
335
336$(DST): $(OBJS) $(filter-out %.d,$(MAKEFILE_LIST)) | $(1)
337	$$(s)echo "  HOSTLD  $$@"
338	$$(q)$(host-cc) $${OBJS} -o $$@ $($(3)_LDFLAGS)
339	$$(s)echo
340	$$(s)echo "Built $$@ successfully"
341	$$(s)echo
342
343all: $(DST)
344endef
345
346# MAKE_C_LIB builds a C source file and generates the dependency file
347#   $(1) = output directory
348#   $(2) = source file (%.c)
349#   $(3) = library name
350#   $(4) = uppercase name of the library
351define MAKE_C_LIB
352$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
353$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
354$(eval LIB := $(notdir $(1)))
355
356$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
357	$$(s)echo "  CC      $$<"
358	$$(q)$($(ARCH)-cc) $$(LIB$(4)_CFLAGS) $$(TF_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
359
360-include $(DEP)
361
362endef
363
364# MAKE_S_LIB builds an assembly source file and generates the dependency file
365#   $(1) = output directory
366#   $(2) = source file (%.S)
367#   $(3) = library name
368#   $(4) = uppercase name of the library
369define MAKE_S_LIB
370$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
371$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
372
373$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
374	$$(s)echo "  AS      $$<"
375	$$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS) $$(ASFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
376
377-include $(DEP)
378
379endef
380
381
382# MAKE_C builds a C source file and generates the dependency file
383#   $(1) = output directory
384#   $(2) = source file (%.c)
385#   $(3) = BL stage
386#   $(4) = uppercase BL stage
387define MAKE_C
388
389$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
390$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
391
392$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
393$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
394$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
395$(eval BL_CFLAGS := $($(4)_CFLAGS))
396
397$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
398	$$(s)echo "  CC      $$<"
399	$$(q)$($(ARCH)-cc) $$(LTO_CFLAGS) $$(TF_CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
400
401-include $(DEP)
402
403endef
404
405
406# MAKE_S builds an assembly source file and generates the dependency file
407#   $(1) = output directory
408#   $(2) = assembly file (%.S)
409#   $(3) = BL stage
410#   $(4) = uppercase BL stage
411define MAKE_S
412
413$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
414$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
415
416$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
417$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
418$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
419$(eval BL_ASFLAGS := $($(4)_ASFLAGS))
420
421$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
422	$$(s)echo "  AS      $$<"
423	$$(q)$($(ARCH)-as) -x assembler-with-cpp $$(TF_CFLAGS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
424
425-include $(DEP)
426
427endef
428
429# MAKE_PRE run the C preprocessor on a file
430#   $(1) = output file
431#   $(2) = list of input files
432#   $(3) = dep file
433#   $(4) = list of rule-specific flags to pass
434define MAKE_PRE
435$(eval OUT := $(1))
436$(eval SRC := $(2))
437$(eval DEP := $(3))
438$(eval CUSTOM_FLAGS := $(4))
439$(OUT): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
440	$$(s)echo "  CPP     $$<"
441	$$(q)$($(ARCH)-cpp) -E -P -x assembler-with-cpp $$(TF_CFLAGS) $(CUSTOM_FLAGS) $(call MAKE_DEP,$(DEP),$(OUT)) -o $$@ $$<
442endef
443
444# MAKE_LD generate the linker script using the C preprocessor
445#   $(1) = output linker script
446#   $(2) = input template
447#   $(3) = BL stage
448#   $(4) = uppercase BL stage
449define MAKE_LD
450
451$(eval DEP := $(1).d)
452
453$(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
454$(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
455$(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
456$(eval FLAGS := -D__LINKER__ $(BL_CPPFLAGS))
457
458$(eval $(call MAKE_PRE,$(1),$(2),$(DEP),$(FLAGS)))
459-include $(DEP)
460
461endef
462
463# MAKE_LIB_OBJS builds both C and assembly source files
464#   $(1) = output directory
465#   $(2) = list of source files
466#   $(3) = name of the library
467#   $(4) = uppercase name of the library
468define MAKE_LIB_OBJS
469        $(eval C_OBJS := $(filter %.c,$(2)))
470        $(eval REMAIN := $(filter-out %.c,$(2)))
471        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3),$(4))))
472
473        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
474        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
475        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3),$(4))))
476
477        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
478endef
479
480
481# MAKE_OBJS builds both C and assembly source files
482#   $(1) = output directory
483#   $(2) = list of source files (both C and assembly)
484#   $(3) = BL stage
485#   $(4) = uppercase BL stage
486define MAKE_OBJS
487        $(eval C_OBJS := $(filter %.c,$(2)))
488        $(eval REMAIN := $(filter-out %.c,$(2)))
489        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3),$(4))))
490
491        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
492        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
493        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3),$(4))))
494
495        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
496endef
497
498
499# NOTE: The line continuation '\' is required in the next define otherwise we
500# end up with a line-feed characer at the end of the last c filename.
501# Also bear this issue in mind if extending the list of supported filetypes.
502define SOURCES_TO_OBJS
503        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
504        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
505endef
506
507.PHONY: libraries
508
509# MAKE_LIB macro defines the targets and options to build each BL image.
510# Arguments:
511#   $(1) = Library name
512define MAKE_LIB
513        $(eval BL         := $(call uppercase,$(1)))
514        $(eval BUILD_DIR  := ${BUILD_PLAT}/lib$(1))
515        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
516        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
517        $(eval SOURCES    := $(LIB$(BL)_SRCS))
518        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
519
520$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1),$(BL)))
521
522libraries: ${LIB_DIR}/lib$(1).a
523ifeq ($($(ARCH)-ld-id),arm-link)
524LDPATHS = --userlibpath=${LIB_DIR}
525LDLIBS += --library=$(1)
526else
527LDPATHS = -L${LIB_DIR}
528LDLIBS += -l$(1)
529endif
530
531ifeq ($(USE_ROMLIB),1)
532LIBWRAPPER = -lwrappers
533endif
534
535all: ${LIB_DIR}/lib$(1).a
536
537${LIB_DIR}/lib$(1).a: $(OBJS) | $$$$(@D)/
538	$$(s)echo "  AR      $$@"
539	$$(q)$($(ARCH)-ar) cr $$@ $$?
540endef
541
542# Generate the path to one or more preprocessed linker scripts given the paths
543# of their sources.
544#
545# Arguments:
546#   $(1) = path to one or more linker script sources
547define linker_script_path
548        $(patsubst %.S,$(BUILD_DIR)/%,$(1))
549endef
550
551# MAKE_BL macro defines the targets and options to build each BL image.
552# Arguments:
553#   $(1) = BL stage
554#   $(2) = FIP command line option (if empty, image will not be included in the FIP)
555#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
556#   $(4) = BL encryption flag (optional) (0, 1)
557define MAKE_BL
558        $(eval BL         := $(call uppercase,$(1)))
559        $(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
560        $(eval BL_SOURCES := $($(BL)_SOURCES))
561        $(eval SOURCES    := $(sort $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)))
562        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
563        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
564        $(eval ELF        := $(call IMG_ELF,$(1)))
565        $(eval DUMP       := $(call IMG_DUMP,$(1)))
566        $(eval BIN        := $(call IMG_BIN,$(1)))
567        $(eval ENC_BIN    := $(call IMG_ENC_BIN,$(1)))
568        $(eval BL_LIBS    := $($(BL)_LIBS))
569
570        $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(BL)_DEFAULT_LINKER_SCRIPT_SOURCE))
571        $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
572
573        $(eval LINKER_SCRIPT_SOURCES := $($(BL)_LINKER_SCRIPT_SOURCES))
574        $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
575        $(eval GNU_LINKER_ARGS := $(call ld_prefix,-Map=$(MAPFILE)) $(foreach script,$(LINKER_SCRIPTS) $(DEFAULT_LINKER_SCRIPT), $(call ld_prefix,--script $(script))))
576
577$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1),$(BL)))
578
579# Generate targets to preprocess each required linker script
580$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
581        $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1),$(BL))))
582
583$(eval BL_LDFLAGS := $($(BL)_LDFLAGS))
584
585ifeq ($(USE_ROMLIB),1)
586$(ELF): $(BUILD_PLAT)/romlib/romlib.bin | $$$$(@D)/
587endif
588
589# MODULE_OBJS can be assigned by vendors with different compiled
590# object file path, and prebuilt object file path.
591$(eval OBJS += $(MODULE_OBJS))
592
593$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $$$$(@D)/ libraries $(BL_LIBS)
594	$$(s)echo "  LD      $$@"
595ifeq ($($(ARCH)-ld-id),arm-link)
596	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
597		--predefine=$(call escape-shell,-D__LINKER__=$(__LINKER__)) \
598		--predefine=$(call escape-shell,-DTF_CFLAGS=$(TF_CFLAGS)) \
599		--map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
600		$(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) $(OBJS)
601else
602	$$(q)$($(ARCH)-ld) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) $(GNU_LINKER_ARGS) \
603		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
604endif
605ifeq ($(DISABLE_BIN_GENERATION),1)
606	$(s)echo
607	$(s)echo "Built $$@ successfully"
608	$(s)echo
609endif
610
611$(DUMP): $(ELF) | $$$$(@D)/
612	$$(s)echo "  OD      $$@"
613	$$(q)$($(ARCH)-od) -dx $$< > $$@
614
615$(BIN): $(ELF) | $$$$(@D)/
616	$$(s)echo "  BIN     $$@"
617	$$(q)$($(ARCH)-oc) -O binary $$< $$@
618	$(s)echo
619	$(s)echo "Built $$@ successfully"
620	$(s)echo
621
622.PHONY: $(1)
623ifeq ($(DISABLE_BIN_GENERATION),1)
624$(1): $(ELF) $(DUMP)
625else
626$(1): $(BIN) $(DUMP)
627endif
628
629all: $(1)
630
631ifeq ($(4),1)
632$(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
633$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(BIN),--$(2),$(ENC_BIN),$(3), \
634		$(ENC_BIN)))
635else
636$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(BL),$(BIN),--$(2),$(BIN),$(3)))
637endif
638
639endef
640
641# Convert device tree source file names to matching blobs
642#   $(1) = input dts
643define SOURCES_TO_DTBS
644        $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
645endef
646
647# MAKE_DTB generate the Flattened device tree binary
648#   $(1) = output directory
649#   $(2) = input dts
650define MAKE_DTB
651
652# List of DTB file(s) to generate, based on DTS file basename list
653$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
654# List of the pre-compiled DTS file(s)
655$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
656# Dependencies of the pre-compiled DTS file(s) on its source and included files
657$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
658# Dependencies of the DT compilation on its pre-compiled DTS
659$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
660
661$(eval $(call MAKE_PRE,$(DPRE),$(2),$(DTSDEP),$(DTC_CPPFLAGS)))
662
663$(DOBJ): $(DPRE) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
664	$$(s)echo "  DTC     $$<"
665	$$(q)$($(ARCH)-dtc) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $$<
666	$$($$@-after)
667
668-include $(DTBDEP)
669-include $(DTSDEP)
670
671endef
672
673# MAKE_DTBS builds flattened device tree sources
674#   $(1) = output directory
675#   $(2) = list of flattened device tree source files
676define MAKE_DTBS
677        $(eval DOBJS := $(filter %.dts,$(2)))
678        $(eval REMAIN := $(filter-out %.dts,$(2)))
679        $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
680        $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
681
682dtbs: $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2)))
683all: dtbs
684endef
685