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