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