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