xref: /rk3399_ARM-atf/make_helpers/build_macros.mk (revision 88154678c5e6d0e6e28ca17beb01049a9ea4c0a0)
173c99d4eSJuan Castillo#
273c99d4eSJuan Castillo# Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
373c99d4eSJuan Castillo#
473c99d4eSJuan Castillo# Redistribution and use in source and binary forms, with or without
573c99d4eSJuan Castillo# modification, are permitted provided that the following conditions are met:
673c99d4eSJuan Castillo#
773c99d4eSJuan Castillo# Redistributions of source code must retain the above copyright notice, this
873c99d4eSJuan Castillo# list of conditions and the following disclaimer.
973c99d4eSJuan Castillo#
1073c99d4eSJuan Castillo# Redistributions in binary form must reproduce the above copyright notice,
1173c99d4eSJuan Castillo# this list of conditions and the following disclaimer in the documentation
1273c99d4eSJuan Castillo# and/or other materials provided with the distribution.
1373c99d4eSJuan Castillo#
1473c99d4eSJuan Castillo# Neither the name of ARM nor the names of its contributors may be used
1573c99d4eSJuan Castillo# to endorse or promote products derived from this software without specific
1673c99d4eSJuan Castillo# prior written permission.
1773c99d4eSJuan Castillo#
1873c99d4eSJuan Castillo# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1973c99d4eSJuan Castillo# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2073c99d4eSJuan Castillo# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2173c99d4eSJuan Castillo# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2273c99d4eSJuan Castillo# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2373c99d4eSJuan Castillo# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2473c99d4eSJuan Castillo# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2573c99d4eSJuan Castillo# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2673c99d4eSJuan Castillo# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2773c99d4eSJuan Castillo# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2873c99d4eSJuan Castillo# POSSIBILITY OF SUCH DAMAGE.
2973c99d4eSJuan Castillo#
3073c99d4eSJuan Castillo
3173c99d4eSJuan Castillo# Convenience function for adding build definitions
3273c99d4eSJuan Castillo# $(eval $(call add_define,FOO)) will have:
3373c99d4eSJuan Castillo# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise
3473c99d4eSJuan Castillodefine add_define
3573c99d4eSJuan Castillo    DEFINES			+=	-D$(1)$(if $(value $(1)),=$(value $(1)),)
3673c99d4eSJuan Castilloendef
3773c99d4eSJuan Castillo
3873c99d4eSJuan Castillo# Convenience function for verifying option has a boolean value
3973c99d4eSJuan Castillo# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1
4073c99d4eSJuan Castillodefine assert_boolean
4173c99d4eSJuan Castillo    $(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean))
4273c99d4eSJuan Castilloendef
4373c99d4eSJuan Castillo
4473c99d4eSJuan Castillo# IMG_LINKERFILE defines the linker script corresponding to a BL stage
4573c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
4673c99d4eSJuan Castillodefine IMG_LINKERFILE
4773c99d4eSJuan Castillo    ${BUILD_DIR}/bl$(1).ld
4873c99d4eSJuan Castilloendef
4973c99d4eSJuan Castillo
5073c99d4eSJuan Castillo# IMG_MAPFILE defines the output file describing the memory map corresponding
5173c99d4eSJuan Castillo# to a BL stage
5273c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
5373c99d4eSJuan Castillodefine IMG_MAPFILE
5473c99d4eSJuan Castillo    ${BUILD_DIR}/bl$(1).map
5573c99d4eSJuan Castilloendef
5673c99d4eSJuan Castillo
5773c99d4eSJuan Castillo# IMG_ELF defines the elf file corresponding to a BL stage
5873c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
5973c99d4eSJuan Castillodefine IMG_ELF
6073c99d4eSJuan Castillo    ${BUILD_DIR}/bl$(1).elf
6173c99d4eSJuan Castilloendef
6273c99d4eSJuan Castillo
6373c99d4eSJuan Castillo# IMG_DUMP defines the symbols dump file corresponding to a BL stage
6473c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
6573c99d4eSJuan Castillodefine IMG_DUMP
6673c99d4eSJuan Castillo    ${BUILD_DIR}/bl$(1).dump
6773c99d4eSJuan Castilloendef
6873c99d4eSJuan Castillo
6973c99d4eSJuan Castillo# IMG_BIN defines the default image file corresponding to a BL stage
7073c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
7173c99d4eSJuan Castillodefine IMG_BIN
7273c99d4eSJuan Castillo    ${BUILD_PLAT}/bl$(1).bin
7373c99d4eSJuan Castilloendef
7473c99d4eSJuan Castillo
7573c99d4eSJuan Castillo# FIP_ADD_PAYLOAD appends the command line arguments required by the FIP tool
7673c99d4eSJuan Castillo# to package a new payload. Optionally, it adds the dependency on this payload
7773c99d4eSJuan Castillo#   $(1) = payload filename (i.e. bl31.bin)
7873c99d4eSJuan Castillo#   $(2) = command line option for the specified payload (i.e. --bl31)
7973c99d4eSJuan Castillo#   $(3) = fip target dependency (optional) (i.e. bl31)
8073c99d4eSJuan Castillodefine FIP_ADD_PAYLOAD
8173c99d4eSJuan Castillo    $(eval FIP_ARGS += $(2) $(1))
8273c99d4eSJuan Castillo    $(eval $(if $(3),FIP_DEPS += $(3)))
8373c99d4eSJuan Castilloendef
8473c99d4eSJuan Castillo
8573c99d4eSJuan Castillo# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invokation
8673c99d4eSJuan Castillo#   $(1) = parameter filename
8773c99d4eSJuan Castillo#   $(2) = cert_create command line option for the specified parameter
8873c99d4eSJuan Castillo#   $(3) = input parameter (false if empty)
8973c99d4eSJuan Castillodefine CERT_ADD_CMD_OPT
9073c99d4eSJuan Castillo    $(eval $(if $(3),CRT_DEPS += $(1)))
9173c99d4eSJuan Castillo    $(eval CRT_ARGS += $(2) $(1))
9273c99d4eSJuan Castilloendef
9373c99d4eSJuan Castillo
9473c99d4eSJuan Castillo# FIP_ADD_IMG allows the platform to specify an image to be packed in the FIP
9573c99d4eSJuan Castillo# using a build option. It also adds a dependency on the image file, aborting
9673c99d4eSJuan Castillo# the build if the file does not exist.
9773c99d4eSJuan Castillo#   $(1) = build option to specify the image filename (BL30, BL33, etc)
9873c99d4eSJuan Castillo#   $(2) = command line option for the fip_create tool (bl30, bl33, etc)
9973c99d4eSJuan Castillo# Example:
10073c99d4eSJuan Castillo#   $(eval $(call FIP_ADD_IMG,BL33,--bl33))
10173c99d4eSJuan Castillodefine FIP_ADD_IMG
10273c99d4eSJuan Castillo    CRT_DEPS += check_$(1)
10373c99d4eSJuan Castillo    FIP_DEPS += check_$(1)
10473c99d4eSJuan Castillo    $(call FIP_ADD_PAYLOAD,$(value $(1)),$(2))
10573c99d4eSJuan Castillo
10673c99d4eSJuan Castillocheck_$(1):
10773c99d4eSJuan Castillo	$$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file"))
10873c99d4eSJuan Castilloendef
10973c99d4eSJuan Castillo
11073c99d4eSJuan Castillo
11173c99d4eSJuan Castillo################################################################################
11273c99d4eSJuan Castillo# Auxiliary macros to build TF images from sources
11373c99d4eSJuan Castillo################################################################################
11473c99d4eSJuan Castillo
115*88154678SJuan Castillo# If no goal is specified in the command line, .DEFAULT_GOAL is used.
116*88154678SJuan Castillo# .DEFAULT_GOAL is defined in the main Makefile before including this file.
117*88154678SJuan Castilloifeq ($(MAKECMDGOALS),)
118*88154678SJuan CastilloMAKECMDGOALS := $(.DEFAULT_GOAL)
119*88154678SJuan Castilloendif
120*88154678SJuan Castillo
12173c99d4eSJuan Castillodefine match_goals
12273c99d4eSJuan Castillo$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS))))
12373c99d4eSJuan Castilloendef
12473c99d4eSJuan Castillo
12573c99d4eSJuan Castillo# List of rules that involve building things
12673c99d4eSJuan CastilloBUILD_TARGETS := all bl1 bl2 bl31 bl32 certificates fip
12773c99d4eSJuan Castillo
12873c99d4eSJuan Castillo# Does the list of goals specified on the command line include a build target?
12973c99d4eSJuan Castilloifneq ($(call match_goals,${BUILD_TARGETS}),)
13073c99d4eSJuan CastilloIS_ANYTHING_TO_BUILD := 1
13173c99d4eSJuan Castilloendif
13273c99d4eSJuan Castillo
13373c99d4eSJuan Castillo
13473c99d4eSJuan Castillo# MAKE_C builds a C source file and generates the dependency file
13573c99d4eSJuan Castillo#   $(1) = output directory
13673c99d4eSJuan Castillo#   $(2) = source file (%.c)
13773c99d4eSJuan Castillo#   $(3) = BL stage (2, 30, 31, 32, 33)
13873c99d4eSJuan Castillodefine MAKE_C
13973c99d4eSJuan Castillo
14073c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2))))
14173c99d4eSJuan Castillo$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
14273c99d4eSJuan Castillo
14373c99d4eSJuan Castillo$(OBJ): $(2)
14473c99d4eSJuan Castillo	@echo "  CC      $$<"
14573c99d4eSJuan Castillo	$$(Q)$$(CC) $$(CFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@
14673c99d4eSJuan Castillo
14773c99d4eSJuan Castillo
14873c99d4eSJuan Castillo$(PREREQUISITES): $(2)
14973c99d4eSJuan Castillo	@echo "  DEPS    $$@"
15073c99d4eSJuan Castillo	@mkdir -p $(1)
15173c99d4eSJuan Castillo	$$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$<
15273c99d4eSJuan Castillo
15373c99d4eSJuan Castilloifdef IS_ANYTHING_TO_BUILD
15473c99d4eSJuan Castillo-include $(PREREQUISITES)
15573c99d4eSJuan Castilloendif
15673c99d4eSJuan Castillo
15773c99d4eSJuan Castilloendef
15873c99d4eSJuan Castillo
15973c99d4eSJuan Castillo
16073c99d4eSJuan Castillo# MAKE_S builds an assembly source file and generates the dependency file
16173c99d4eSJuan Castillo#   $(1) = output directory
16273c99d4eSJuan Castillo#   $(2) = assembly file (%.S)
16373c99d4eSJuan Castillo#   $(3) = BL stage (2, 30, 31, 32, 33)
16473c99d4eSJuan Castillodefine MAKE_S
16573c99d4eSJuan Castillo
16673c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2))))
16773c99d4eSJuan Castillo$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ)))
16873c99d4eSJuan Castillo
16973c99d4eSJuan Castillo$(OBJ): $(2)
17073c99d4eSJuan Castillo	@echo "  AS      $$<"
17173c99d4eSJuan Castillo	$$(Q)$$(AS) $$(ASFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@
17273c99d4eSJuan Castillo
17373c99d4eSJuan Castillo$(PREREQUISITES): $(2)
17473c99d4eSJuan Castillo	@echo "  DEPS    $$@"
17573c99d4eSJuan Castillo	@mkdir -p $(1)
17673c99d4eSJuan Castillo	$$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$<
17773c99d4eSJuan Castillo
17873c99d4eSJuan Castilloifdef IS_ANYTHING_TO_BUILD
17973c99d4eSJuan Castillo-include $(PREREQUISITES)
18073c99d4eSJuan Castilloendif
18173c99d4eSJuan Castillo
18273c99d4eSJuan Castilloendef
18373c99d4eSJuan Castillo
18473c99d4eSJuan Castillo
18573c99d4eSJuan Castillo# MAKE_LD generate the linker script using the C preprocessor
18673c99d4eSJuan Castillo#   $(1) = output linker script
18773c99d4eSJuan Castillo#   $(2) = input template
18873c99d4eSJuan Castillodefine MAKE_LD
18973c99d4eSJuan Castillo
19073c99d4eSJuan Castillo$(eval PREREQUISITES := $(1).d)
19173c99d4eSJuan Castillo
19273c99d4eSJuan Castillo$(1): $(2)
19373c99d4eSJuan Castillo	@echo "  PP      $$<"
19473c99d4eSJuan Castillo	$$(Q)$$(AS) $$(ASFLAGS) -P -E -D__LINKER__ -o $$@ $$<
19573c99d4eSJuan Castillo
19673c99d4eSJuan Castillo$(PREREQUISITES): $(2)
19773c99d4eSJuan Castillo	@echo "  DEPS    $$@"
19873c99d4eSJuan Castillo	@mkdir -p $$(dir $$@)
19973c99d4eSJuan Castillo	$$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$<
20073c99d4eSJuan Castillo
20173c99d4eSJuan Castilloifdef IS_ANYTHING_TO_BUILD
20273c99d4eSJuan Castillo-include $(PREREQUISITES)
20373c99d4eSJuan Castilloendif
20473c99d4eSJuan Castillo
20573c99d4eSJuan Castilloendef
20673c99d4eSJuan Castillo
20773c99d4eSJuan Castillo
20873c99d4eSJuan Castillo# MAKE_OBJS builds both C and assembly source files
20973c99d4eSJuan Castillo#   $(1) = output directory
21073c99d4eSJuan Castillo#   $(2) = list of source files (both C and assembly)
21173c99d4eSJuan Castillo#   $(3) = BL stage (2, 30, 31, 32, 33)
21273c99d4eSJuan Castillodefine MAKE_OBJS
21373c99d4eSJuan Castillo        $(eval C_OBJS := $(filter %.c,$(2)))
21473c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.c,$(2)))
21573c99d4eSJuan Castillo        $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3))))
21673c99d4eSJuan Castillo
21773c99d4eSJuan Castillo        $(eval S_OBJS := $(filter %.S,$(REMAIN)))
21873c99d4eSJuan Castillo        $(eval REMAIN := $(filter-out %.S,$(REMAIN)))
21973c99d4eSJuan Castillo        $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3))))
22073c99d4eSJuan Castillo
22173c99d4eSJuan Castillo        $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN)))
22273c99d4eSJuan Castilloendef
22373c99d4eSJuan Castillo
22473c99d4eSJuan Castillo
22573c99d4eSJuan Castillo# NOTE: The line continuation '\' is required in the next define otherwise we
22673c99d4eSJuan Castillo# end up with a line-feed characer at the end of the last c filename.
22773c99d4eSJuan Castillo# Also bare this issue in mind if extending the list of supported filetypes.
22873c99d4eSJuan Castillodefine SOURCES_TO_OBJS
22973c99d4eSJuan Castillo        $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \
23073c99d4eSJuan Castillo        $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1))))
23173c99d4eSJuan Castilloendef
23273c99d4eSJuan Castillo
23373c99d4eSJuan Castillo
23473c99d4eSJuan Castillo# MAKE_TOOL_ARGS macro defines the command line arguments for the FIP tool for
23573c99d4eSJuan Castillo# each BL image. Arguments:
23673c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
23773c99d4eSJuan Castillo#   $(2) = Binary file
23873c99d4eSJuan Castillo#   $(3) = In FIP (false if empty)
23973c99d4eSJuan Castillodefine MAKE_TOOL_ARGS
24073c99d4eSJuan Castillo        $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--bl$(1),bl$(1))))
24173c99d4eSJuan Castilloendef
24273c99d4eSJuan Castillo
24373c99d4eSJuan Castillo
24473c99d4eSJuan Castillo# MAKE_BL macro defines the targets and options to build each BL image.
24573c99d4eSJuan Castillo# Arguments:
24673c99d4eSJuan Castillo#   $(1) = BL stage (2, 30, 31, 32, 33)
24773c99d4eSJuan Castillo#   $(2) = In FIP (false if empty)
24873c99d4eSJuan Castillodefine MAKE_BL
24973c99d4eSJuan Castillo        $(eval BUILD_DIR  := ${BUILD_PLAT}/bl$(1))
25073c99d4eSJuan Castillo        $(eval SOURCES    := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES))
25173c99d4eSJuan Castillo        $(eval OBJS       := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES))))
25273c99d4eSJuan Castillo        $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1)))
25373c99d4eSJuan Castillo        $(eval MAPFILE    := $(call IMG_MAPFILE,$(1)))
25473c99d4eSJuan Castillo        $(eval ELF        := $(call IMG_ELF,$(1)))
25573c99d4eSJuan Castillo        $(eval DUMP       := $(call IMG_DUMP,$(1)))
25673c99d4eSJuan Castillo        $(eval BIN        := $(call IMG_BIN,$(1)))
25773c99d4eSJuan Castillo
25873c99d4eSJuan Castillo        $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1)))
25973c99d4eSJuan Castillo        $(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE)))
26073c99d4eSJuan Castillo
26173c99d4eSJuan Castillo$(BUILD_DIR):
26273c99d4eSJuan Castillo	$$(Q)mkdir -p "$$@"
26373c99d4eSJuan Castillo
26473c99d4eSJuan Castillo$(ELF): $(OBJS) $(LINKERFILE)
26573c99d4eSJuan Castillo	@echo "  LD      $$@"
26673c99d4eSJuan Castillo	@echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \
26773c99d4eSJuan Castillo	       const char version_string[] = "${VERSION_STRING}";' | \
26873c99d4eSJuan Castillo		$$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o
26973c99d4eSJuan Castillo	$$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \
27073c99d4eSJuan Castillo					$(BUILD_DIR)/build_message.o $(OBJS)
27173c99d4eSJuan Castillo
27273c99d4eSJuan Castillo$(DUMP): $(ELF)
27373c99d4eSJuan Castillo	@echo "  OD      $$@"
27473c99d4eSJuan Castillo	$${Q}$${OD} -dx $$< > $$@
27573c99d4eSJuan Castillo
27673c99d4eSJuan Castillo$(BIN): $(ELF)
27773c99d4eSJuan Castillo	@echo "  BIN     $$@"
27873c99d4eSJuan Castillo	$$(Q)$$(OC) -O binary $$< $$@
27973c99d4eSJuan Castillo	@echo
28073c99d4eSJuan Castillo	@echo "Built $$@ successfully"
28173c99d4eSJuan Castillo	@echo
28273c99d4eSJuan Castillo
28373c99d4eSJuan Castillo.PHONY: bl$(1)
28473c99d4eSJuan Castillobl$(1): $(BUILD_DIR) $(BIN) $(DUMP)
28573c99d4eSJuan Castillo
28673c99d4eSJuan Castilloall: bl$(1)
28773c99d4eSJuan Castillo
28873c99d4eSJuan Castillo$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2)))
28973c99d4eSJuan Castillo
29073c99d4eSJuan Castilloendef
29173c99d4eSJuan Castillo
292