xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision 1369fb82c8e809c1a59a0d99184dbfd2d0b81afa)
1#
2# Copyright (c) 2015-2023, 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# Some utility macros for manipulating awkward (whitespace) characters.
14blank			:=
15space			:=${blank} ${blank}
16comma			:= ,
17
18# A user defined function to recursively search for a filename below a directory
19#    $1 is the directory root of the recursive search (blank for current directory).
20#    $2 is the file name to search for.
21define rwildcard
22$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
23endef
24
25# This table is used in converting lower case to upper case.
26uppercase_table:=a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
27
28# Internal macro used for converting lower case to upper case.
29#   $(1) = upper case table
30#   $(2) = String to convert
31define uppercase_internal
32$(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2))
33endef
34
35# A macro for converting a string to upper case
36#   $(1) = String to convert
37define uppercase
38$(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result)
39endef
40
41# Convenience function for setting a variable to 0 if not previously set
42# $(eval $(call default_zero,FOO))
43define default_zero
44	$(eval $(1) ?= 0)
45endef
46
47# Convenience function for setting a list of variables to 0 if not previously set
48# $(eval $(call default_zeros,FOO BAR))
49define default_zeros
50	$(foreach var,$1,$(eval $(call default_zero,$(var))))
51endef
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. An call to
103# $(call ld_option, --no-XYZ) will return --no-XYZ if supported by the linker
104define ld_option
105	$(shell if $(LD) $(1) -v >/dev/null 2>&1; then echo $(1); fi )
106endef
107
108# CREATE_SEQ is a recursive function to create sequence of numbers from 1 to
109# $(2) and assign the sequence to $(1)
110define CREATE_SEQ
111$(if $(word $(2), $($(1))),\
112  $(eval $(1) += $(words $($(1))))\
113  $(eval $(1) := $(filter-out 0,$($(1)))),\
114  $(eval $(1) += $(words $($(1))))\
115  $(call CREATE_SEQ,$(1),$(2))\
116)
117endef
118
119# IMG_MAPFILE defines the output file describing the memory map corresponding
120# to a BL stage
121#   $(1) = BL stage
122define IMG_MAPFILE
123    ${BUILD_DIR}/$(1).map
124endef
125
126# IMG_ELF defines the elf file corresponding to a BL stage
127#   $(1) = BL stage
128define IMG_ELF
129    ${BUILD_DIR}/$(1).elf
130endef
131
132# IMG_DUMP defines the symbols dump file corresponding to a BL stage
133#   $(1) = BL stage
134define IMG_DUMP
135    ${BUILD_DIR}/$(1).dump
136endef
137
138# IMG_BIN defines the default image file corresponding to a BL stage
139#   $(1) = BL stage
140define IMG_BIN
141    ${BUILD_PLAT}/$(1).bin
142endef
143
144# IMG_ENC_BIN defines the default encrypted image file corresponding to a
145# BL stage
146#   $(1) = BL stage
147define IMG_ENC_BIN
148    ${BUILD_PLAT}/$(1)_enc.bin
149endef
150
151# ENCRYPT_FW invokes enctool to encrypt firmware binary
152#   $(1) = input firmware binary
153#   $(2) = output encrypted firmware binary
154define ENCRYPT_FW
155$(2): $(1) enctool
156	$$(ECHO) "  ENC     $$<"
157	$$(Q)$$(ENCTOOL) $$(ENC_ARGS) -i $$< -o $$@
158endef
159
160# TOOL_ADD_PAYLOAD appends the command line arguments required by fiptool to
161# package a new payload and/or by cert_create to generate certificate.
162# Optionally, it adds the dependency on this payload
163#   $(1) = payload filename (i.e. bl31.bin)
164#   $(2) = command line option for the specified payload (i.e. --soc-fw)
165#   $(3) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
166#   $(4) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
167#   $(5) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
168define TOOL_ADD_PAYLOAD
169ifneq ($(5),)
170    $(4)FIP_ARGS += $(2) $(5)
171    $(if $(3),$(4)CRT_DEPS += $(1))
172else
173    $(4)FIP_ARGS += $(2) $(1)
174    $(if $(3),$(4)CRT_DEPS += $(3))
175endif
176    $(if $(3),$(4)FIP_DEPS += $(3))
177    $(4)CRT_ARGS += $(2) $(1)
178endef
179
180# TOOL_ADD_IMG_PAYLOAD works like TOOL_ADD_PAYLOAD, but applies image filters
181# before passing them to host tools if BL*_PRE_TOOL_FILTER is defined.
182#   $(1) = image_type (scp_bl2, bl33, etc.)
183#   $(2) = payload filepath (ex. build/fvp/release/bl31.bin)
184#   $(3) = command line option for the specified payload (ex. --soc-fw)
185#   $(4) = tool target dependency (optional) (ex. build/fvp/release/bl31.bin)
186#   $(5) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
187#   $(6) = encrypted payload (optional) (ex. build/fvp/release/bl31_enc.bin)
188
189define TOOL_ADD_IMG_PAYLOAD
190
191$(eval PRE_TOOL_FILTER := $($(call uppercase,$(1))_PRE_TOOL_FILTER))
192
193ifneq ($(PRE_TOOL_FILTER),)
194
195$(eval PROCESSED_PATH := $(BUILD_PLAT)/$(1).bin$($(PRE_TOOL_FILTER)_SUFFIX))
196
197$(call $(PRE_TOOL_FILTER)_RULE,$(PROCESSED_PATH),$(2))
198
199$(PROCESSED_PATH): $(4)
200
201$(call TOOL_ADD_PAYLOAD,$(PROCESSED_PATH),$(3),$(PROCESSED_PATH),$(5),$(6))
202
203else
204$(call TOOL_ADD_PAYLOAD,$(2),$(3),$(4),$(5),$(6))
205endif
206endef
207
208# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invocation
209#   $(1) = parameter filename
210#   $(2) = cert_create command line option for the specified parameter
211#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
212define CERT_ADD_CMD_OPT
213    $(3)CRT_ARGS += $(2) $(1)
214endef
215
216# TOOL_ADD_IMG allows the platform to specify an external image to be packed
217# in the FIP and/or for which certificate is generated. It also adds a
218# dependency on the image file, aborting the build if the file does not exist.
219#   $(1) = image_type (scp_bl2, bl33, etc.)
220#   $(2) = command line option for fiptool (--scp-fw, --nt-fw, etc)
221#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
222#   $(4) = Image encryption flag (optional) (0, 1)
223# Example:
224#   $(eval $(call TOOL_ADD_IMG,bl33,--nt-fw))
225define TOOL_ADD_IMG
226    # Build option to specify the image filename (SCP_BL2, BL33, etc)
227    # This is the uppercase form of the first parameter
228    $(eval _V := $(call uppercase,$(1)))
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,$(1),$(value $(_V)),$(2),$(ENC_BIN),$(3), \
244		$(ENC_BIN))
245else
246    $(call TOOL_ADD_IMG_PAYLOAD,$(1),$(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	$(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,$(DEP) -MT $$@ -MP
286
287
288# MAKE_C_LIB builds a C source file and generates the dependency file
289#   $(1) = output directory
290#   $(2) = source file (%.c)
291#   $(3) = library name
292define MAKE_C_LIB
293$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
294$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
295$(eval LIB := $(call uppercase, $(notdir $(1))))
296
297$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
298	$$(ECHO) "  CC      $$<"
299	$$(Q)$$(CC) $$($(LIB)_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(MAKE_DEP) -c $$< -o $$@
300
301-include $(DEP)
302
303endef
304
305# MAKE_S_LIB builds an assembly source file and generates the dependency file
306#   $(1) = output directory
307#   $(2) = source file (%.S)
308#   $(3) = library name
309define MAKE_S_LIB
310$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
311$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
312
313$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | lib$(3)_dirs
314	$$(ECHO) "  AS      $$<"
315	$$(Q)$$(AS) $$(ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
316
317-include $(DEP)
318
319endef
320
321
322# MAKE_C builds a C source file and generates the dependency file
323#   $(1) = output directory
324#   $(2) = source file (%.c)
325#   $(3) = BL stage
326define MAKE_C
327
328$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
329$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
330$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
331$(eval BL_CFLAGS := $($(call uppercase,$(3))_CFLAGS))
332
333$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
334	$$(ECHO) "  CC      $$<"
335	$$(Q)$$(CC) $$(LTO_CFLAGS) $$(TF_CFLAGS) $$(CFLAGS) $(BL_CPPFLAGS) $(BL_CFLAGS) $(MAKE_DEP) -c $$< -o $$@
336
337-include $(DEP)
338
339endef
340
341
342# MAKE_S builds an assembly source file and generates the dependency file
343#   $(1) = output directory
344#   $(2) = assembly file (%.S)
345#   $(3) = BL stage
346define MAKE_S
347
348$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
349$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
350$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
351$(eval BL_ASFLAGS := $($(call uppercase,$(3))_ASFLAGS))
352
353$(OBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
354	$$(ECHO) "  AS      $$<"
355	$$(Q)$$(AS) $$(ASFLAGS) $(BL_CPPFLAGS) $(BL_ASFLAGS) $(MAKE_DEP) -c $$< -o $$@
356
357-include $(DEP)
358
359endef
360
361
362# MAKE_LD generate the linker script using the C preprocessor
363#   $(1) = output linker script
364#   $(2) = input template
365#   $(3) = BL stage
366define MAKE_LD
367
368$(eval DEP := $(1).d)
369$(eval BL_CPPFLAGS := $($(call uppercase,$(3))_CPPFLAGS) -DIMAGE_$(call uppercase,$(3)))
370
371$(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $(3)_dirs
372	$$(ECHO) "  PP      $$<"
373	$$(Q)$$(CPP) $$(CPPFLAGS) $(BL_CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(MAKE_DEP) -o $$@ $$<
374
375-include $(DEP)
376
377endef
378
379# MAKE_LIB_OBJS builds both C and assembly source files
380#   $(1) = output directory
381#   $(2) = list of source files
382#   $(3) = name of the library
383define MAKE_LIB_OBJS
384        $(eval C_OBJS := $(filter %.c,$(2)))
385        $(eval REMAIN := $(filter-out %.c,$(2)))
386        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C_LIB,$(1),$(obj),$(3))))
387
388        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
389        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
390        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S_LIB,$(1),$(obj),$(3))))
391
392        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
393endef
394
395
396# MAKE_OBJS builds both C and assembly source files
397#   $(1) = output directory
398#   $(2) = list of source files (both C and assembly)
399#   $(3) = BL stage
400define MAKE_OBJS
401        $(eval C_OBJS := $(filter %.c,$(2)))
402        $(eval REMAIN := $(filter-out %.c,$(2)))
403        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
404
405        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
406        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
407        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
408
409        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
410endef
411
412
413# NOTE: The line continuation '\' is required in the next define otherwise we
414# end up with a line-feed characer at the end of the last c filename.
415# Also bear this issue in mind if extending the list of supported filetypes.
416define SOURCES_TO_OBJS
417        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
418        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
419endef
420
421# Allow overriding the timestamp, for example for reproducible builds, or to
422# synchronize timestamps across multiple projects.
423# This must be set to a C string (including quotes where applicable).
424BUILD_MESSAGE_TIMESTAMP ?= __TIME__", "__DATE__
425
426.PHONY: libraries
427
428# MAKE_LIB_DIRS macro defines the target for the directory where
429# libraries are created
430define MAKE_LIB_DIRS
431        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
432        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
433        $(eval LIBWRAPPER_DIR := ${BUILD_PLAT}/libwrapper)
434        $(eval $(call MAKE_PREREQ_DIR,${LIB_DIR},${BUILD_PLAT}))
435        $(eval $(call MAKE_PREREQ_DIR,${ROMLIB_DIR},${BUILD_PLAT}))
436        $(eval $(call MAKE_PREREQ_DIR,${LIBWRAPPER_DIR},${BUILD_PLAT}))
437endef
438
439# MAKE_LIB macro defines the targets and options to build each BL image.
440# Arguments:
441#   $(1) = Library name
442define MAKE_LIB
443        $(eval BUILD_DIR  := ${BUILD_PLAT}/lib$(1))
444        $(eval LIB_DIR    := ${BUILD_PLAT}/lib)
445        $(eval ROMLIB_DIR    := ${BUILD_PLAT}/romlib)
446        $(eval SOURCES    := $(LIB$(call uppercase,$(1))_SRCS))
447        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
448
449$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
450$(eval $(call MAKE_LIB_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
451
452.PHONY : lib${1}_dirs
453lib${1}_dirs: | ${BUILD_DIR} ${LIB_DIR}  ${ROMLIB_DIR} ${LIBWRAPPER_DIR}
454libraries: ${LIB_DIR}/lib$(1).a
455ifneq ($(findstring armlink,$(notdir $(LD))),)
456LDPATHS = --userlibpath=${LIB_DIR}
457LDLIBS += --library=$(1)
458else
459LDPATHS = -L${LIB_DIR}
460LDLIBS += -l$(1)
461endif
462
463ifeq ($(USE_ROMLIB),1)
464LIBWRAPPER = -lwrappers
465endif
466
467all: ${LIB_DIR}/lib$(1).a
468
469${LIB_DIR}/lib$(1).a: $(OBJS)
470	$$(ECHO) "  AR      $$@"
471	$$(Q)$$(AR) cr $$@ $$?
472endef
473
474# Generate the path to one or more preprocessed linker scripts given the paths
475# of their sources.
476#
477# Arguments:
478#   $(1) = path to one or more linker script sources
479define linker_script_path
480        $(patsubst %.S,$(BUILD_DIR)/%,$(1))
481endef
482
483# MAKE_BL macro defines the targets and options to build each BL image.
484# Arguments:
485#   $(1) = BL stage
486#   $(2) = FIP command line option (if empty, image will not be included in the FIP)
487#   $(3) = FIP prefix (optional) (if FWU_, target is fwu_fip instead of fip)
488#   $(4) = BL encryption flag (optional) (0, 1)
489define MAKE_BL
490        $(eval BUILD_DIR  := ${BUILD_PLAT}/$(1))
491        $(eval BL_SOURCES := $($(call uppercase,$(1))_SOURCES))
492        $(eval SOURCES    := $(BL_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
493        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
494        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
495        $(eval ELF        := $(call IMG_ELF,$(1)))
496        $(eval DUMP       := $(call IMG_DUMP,$(1)))
497        $(eval BIN        := $(call IMG_BIN,$(1)))
498        $(eval ENC_BIN    := $(call IMG_ENC_BIN,$(1)))
499        $(eval BL_LIBS    := $($(call uppercase,$(1))_LIBS))
500
501        $(eval DEFAULT_LINKER_SCRIPT_SOURCE := $($(call uppercase,$(1))_DEFAULT_LINKER_SCRIPT_SOURCE))
502        $(eval DEFAULT_LINKER_SCRIPT := $(call linker_script_path,$(DEFAULT_LINKER_SCRIPT_SOURCE)))
503
504        $(eval LINKER_SCRIPT_SOURCES := $($(call uppercase,$(1))_LINKER_SCRIPT_SOURCES))
505        $(eval LINKER_SCRIPTS := $(call linker_script_path,$(LINKER_SCRIPT_SOURCES)))
506
507        # We use sort only to get a list of unique object directory names.
508        # ordering is not relevant but sort removes duplicates.
509        $(eval TEMP_OBJ_DIRS := $(sort $(dir ${OBJS} ${DEFAULT_LINKER_SCRIPT} ${LINKER_SCRIPTS})))
510        # The $(dir ) function leaves a trailing / on the directory names
511        # Rip off the / to match directory names with make rule targets.
512        $(eval OBJ_DIRS   := $(patsubst %/,%,$(TEMP_OBJ_DIRS)))
513
514# Create generators for object directory structure
515
516$(eval $(call MAKE_PREREQ_DIR,${BUILD_DIR},${BUILD_PLAT}))
517
518$(eval $(foreach objd,${OBJ_DIRS},
519        $(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
520
521.PHONY : ${1}_dirs
522
523# We use order-only prerequisites to ensure that directories are created,
524# but do not cause re-builds every time a file is written.
525${1}_dirs: | ${OBJ_DIRS}
526
527$(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
528
529# Generate targets to preprocess each required linker script
530$(eval $(foreach source,$(DEFAULT_LINKER_SCRIPT_SOURCE) $(LINKER_SCRIPT_SOURCES), \
531        $(call MAKE_LD,$(call linker_script_path,$(source)),$(source),$(1))))
532
533$(eval BL_LDFLAGS := $($(call uppercase,$(1))_LDFLAGS))
534
535ifeq ($(USE_ROMLIB),1)
536$(ELF): romlib.bin
537endif
538
539# MODULE_OBJS can be assigned by vendors with different compiled
540# object file path, and prebuilt object file path.
541$(eval OBJS += $(MODULE_OBJS))
542
543$(ELF): $(OBJS) $(DEFAULT_LINKER_SCRIPT) $(LINKER_SCRIPTS) | $(1)_dirs libraries $(BL_LIBS)
544	$$(ECHO) "  LD      $$@"
545ifdef MAKE_BUILD_STRINGS
546	$(call MAKE_BUILD_STRINGS, $(BUILD_DIR)/build_message.o)
547else
548	@echo 'const char build_message[] = "Built : "$(BUILD_MESSAGE_TIMESTAMP); \
549	       const char version_string[] = "${VERSION_STRING}"; \
550	       const char version[] = "${VERSION}";' | \
551		$$(CC) $$(TF_CFLAGS) $$(CFLAGS) -xc -c - -o $(BUILD_DIR)/build_message.o
552endif
553ifneq ($(findstring armlink,$(notdir $(LD))),)
554	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) --entry=${1}_entrypoint \
555		--predefine="-D__LINKER__=$(__LINKER__)" \
556		--predefine="-DTF_CFLAGS=$(TF_CFLAGS)" \
557		--map --list="$(MAPFILE)" --scatter=${PLAT_DIR}/scat/${1}.scat \
558		$(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS) \
559		$(BUILD_DIR)/build_message.o $(OBJS)
560else ifneq ($(findstring gcc,$(notdir $(LD))),)
561	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) -Wl,-Map=$(MAPFILE) \
562		$(addprefix -Wl$(comma)--script$(comma),$(LINKER_SCRIPTS)) -Wl,--script,$(DEFAULT_LINKER_SCRIPT) \
563		$(BUILD_DIR)/build_message.o \
564		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
565else
566	$$(Q)$$(LD) -o $$@ $$(TF_LDFLAGS) $$(LDFLAGS) $(BL_LDFLAGS) -Map=$(MAPFILE) \
567		$(addprefix -T ,$(LINKER_SCRIPTS)) --script $(DEFAULT_LINKER_SCRIPT) \
568		$(BUILD_DIR)/build_message.o \
569		$(OBJS) $(LDPATHS) $(LIBWRAPPER) $(LDLIBS) $(BL_LIBS)
570endif
571ifeq ($(DISABLE_BIN_GENERATION),1)
572	@${ECHO_BLANK_LINE}
573	@echo "Built $$@ successfully"
574	@${ECHO_BLANK_LINE}
575endif
576
577$(DUMP): $(ELF)
578	$${ECHO} "  OD      $$@"
579	$${Q}$${OD} -dx $$< > $$@
580
581$(BIN): $(ELF)
582	$${ECHO} "  BIN     $$@"
583	$$(Q)$$(OC) -O binary $$< $$@
584	@${ECHO_BLANK_LINE}
585	@echo "Built $$@ successfully"
586	@${ECHO_BLANK_LINE}
587
588.PHONY: $(1)
589ifeq ($(DISABLE_BIN_GENERATION),1)
590$(1): $(ELF) $(DUMP)
591else
592$(1): $(BIN) $(DUMP)
593endif
594
595all: $(1)
596
597ifeq ($(4),1)
598$(call ENCRYPT_FW,$(BIN),$(ENC_BIN))
599$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(ENC_BIN),$(3), \
600		$(ENC_BIN)))
601else
602$(if $(2),$(call TOOL_ADD_IMG_PAYLOAD,$(1),$(BIN),--$(2),$(BIN),$(3)))
603endif
604
605endef
606
607# Convert device tree source file names to matching blobs
608#   $(1) = input dts
609define SOURCES_TO_DTBS
610        $(notdir $(patsubst %.dts,%.dtb,$(filter %.dts,$(1))))
611endef
612
613# MAKE_FDT_DIRS macro creates the prerequisite directories that host the
614# FDT binaries
615#   $(1) = output directory
616#   $(2) = input dts
617define MAKE_FDT_DIRS
618        $(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
619        $(eval TEMP_DTB_DIRS := $(sort $(dir ${DTBS})))
620        # The $(dir ) function leaves a trailing / on the directory names
621        # Rip off the / to match directory names with make rule targets.
622        $(eval DTB_DIRS   := $(patsubst %/,%,$(TEMP_DTB_DIRS)))
623
624$(eval $(foreach objd,${DTB_DIRS},$(call MAKE_PREREQ_DIR,${objd},${BUILD_DIR})))
625
626fdt_dirs: ${DTB_DIRS}
627endef
628
629# MAKE_DTB generate the Flattened device tree binary
630#   $(1) = output directory
631#   $(2) = input dts
632define MAKE_DTB
633
634# List of DTB file(s) to generate, based on DTS file basename list
635$(eval DOBJ := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
636# List of the pre-compiled DTS file(s)
637$(eval DPRE := $(addprefix $(1)/,$(patsubst %.dts,%.pre.dts,$(notdir $(2)))))
638# Dependencies of the pre-compiled DTS file(s) on its source and included files
639$(eval DTSDEP := $(patsubst %.dtb,%.o.d,$(DOBJ)))
640# Dependencies of the DT compilation on its pre-compiled DTS
641$(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
642
643$(DOBJ): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | fdt_dirs
644	$${ECHO} "  CPP     $$<"
645	$(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
646	$$(Q)$$(PP) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
647	$${ECHO} "  DTC     $$<"
648	$$(Q)$$(DTC) $$(DTC_FLAGS) -d $(DTBDEP) -o $$@ $(DPRE)
649
650-include $(DTBDEP)
651-include $(DTSDEP)
652
653endef
654
655# MAKE_DTBS builds flattened device tree sources
656#   $(1) = output directory
657#   $(2) = list of flattened device tree source files
658define MAKE_DTBS
659        $(eval DOBJS := $(filter %.dts,$(2)))
660        $(eval REMAIN := $(filter-out %.dts,$(2)))
661        $(and $(REMAIN),$(error FDT_SOURCES contain non-DTS files: $(REMAIN)))
662        $(eval $(foreach obj,$(DOBJS),$(call MAKE_DTB,$(1),$(obj))))
663
664        $(eval $(call MAKE_FDT_DIRS,$(1),$(2)))
665
666dtbs: $(DTBS)
667all: dtbs
668endef
669