1*73c99d4eSJuan Castillo# 2*73c99d4eSJuan Castillo# Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3*73c99d4eSJuan Castillo# 4*73c99d4eSJuan Castillo# Redistribution and use in source and binary forms, with or without 5*73c99d4eSJuan Castillo# modification, are permitted provided that the following conditions are met: 6*73c99d4eSJuan Castillo# 7*73c99d4eSJuan Castillo# Redistributions of source code must retain the above copyright notice, this 8*73c99d4eSJuan Castillo# list of conditions and the following disclaimer. 9*73c99d4eSJuan Castillo# 10*73c99d4eSJuan Castillo# Redistributions in binary form must reproduce the above copyright notice, 11*73c99d4eSJuan Castillo# this list of conditions and the following disclaimer in the documentation 12*73c99d4eSJuan Castillo# and/or other materials provided with the distribution. 13*73c99d4eSJuan Castillo# 14*73c99d4eSJuan Castillo# Neither the name of ARM nor the names of its contributors may be used 15*73c99d4eSJuan Castillo# to endorse or promote products derived from this software without specific 16*73c99d4eSJuan Castillo# prior written permission. 17*73c99d4eSJuan Castillo# 18*73c99d4eSJuan Castillo# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*73c99d4eSJuan Castillo# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*73c99d4eSJuan Castillo# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*73c99d4eSJuan Castillo# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*73c99d4eSJuan Castillo# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*73c99d4eSJuan Castillo# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*73c99d4eSJuan Castillo# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*73c99d4eSJuan Castillo# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*73c99d4eSJuan Castillo# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*73c99d4eSJuan Castillo# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*73c99d4eSJuan Castillo# POSSIBILITY OF SUCH DAMAGE. 29*73c99d4eSJuan Castillo# 30*73c99d4eSJuan Castillo 31*73c99d4eSJuan Castillo# Convenience function for adding build definitions 32*73c99d4eSJuan Castillo# $(eval $(call add_define,FOO)) will have: 33*73c99d4eSJuan Castillo# -DFOO if $(FOO) is empty; -DFOO=$(FOO) otherwise 34*73c99d4eSJuan Castillodefine add_define 35*73c99d4eSJuan Castillo DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),) 36*73c99d4eSJuan Castilloendef 37*73c99d4eSJuan Castillo 38*73c99d4eSJuan Castillo# Convenience function for verifying option has a boolean value 39*73c99d4eSJuan Castillo# $(eval $(call assert_boolean,FOO)) will assert FOO is 0 or 1 40*73c99d4eSJuan Castillodefine assert_boolean 41*73c99d4eSJuan Castillo $(and $(patsubst 0,,$(value $(1))),$(patsubst 1,,$(value $(1))),$(error $(1) must be boolean)) 42*73c99d4eSJuan Castilloendef 43*73c99d4eSJuan Castillo 44*73c99d4eSJuan Castillo# IMG_LINKERFILE defines the linker script corresponding to a BL stage 45*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 46*73c99d4eSJuan Castillodefine IMG_LINKERFILE 47*73c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).ld 48*73c99d4eSJuan Castilloendef 49*73c99d4eSJuan Castillo 50*73c99d4eSJuan Castillo# IMG_MAPFILE defines the output file describing the memory map corresponding 51*73c99d4eSJuan Castillo# to a BL stage 52*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 53*73c99d4eSJuan Castillodefine IMG_MAPFILE 54*73c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).map 55*73c99d4eSJuan Castilloendef 56*73c99d4eSJuan Castillo 57*73c99d4eSJuan Castillo# IMG_ELF defines the elf file corresponding to a BL stage 58*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 59*73c99d4eSJuan Castillodefine IMG_ELF 60*73c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).elf 61*73c99d4eSJuan Castilloendef 62*73c99d4eSJuan Castillo 63*73c99d4eSJuan Castillo# IMG_DUMP defines the symbols dump file corresponding to a BL stage 64*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 65*73c99d4eSJuan Castillodefine IMG_DUMP 66*73c99d4eSJuan Castillo ${BUILD_DIR}/bl$(1).dump 67*73c99d4eSJuan Castilloendef 68*73c99d4eSJuan Castillo 69*73c99d4eSJuan Castillo# IMG_BIN defines the default image file corresponding to a BL stage 70*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 71*73c99d4eSJuan Castillodefine IMG_BIN 72*73c99d4eSJuan Castillo ${BUILD_PLAT}/bl$(1).bin 73*73c99d4eSJuan Castilloendef 74*73c99d4eSJuan Castillo 75*73c99d4eSJuan Castillo# FIP_ADD_PAYLOAD appends the command line arguments required by the FIP tool 76*73c99d4eSJuan Castillo# to package a new payload. Optionally, it adds the dependency on this payload 77*73c99d4eSJuan Castillo# $(1) = payload filename (i.e. bl31.bin) 78*73c99d4eSJuan Castillo# $(2) = command line option for the specified payload (i.e. --bl31) 79*73c99d4eSJuan Castillo# $(3) = fip target dependency (optional) (i.e. bl31) 80*73c99d4eSJuan Castillodefine FIP_ADD_PAYLOAD 81*73c99d4eSJuan Castillo $(eval FIP_ARGS += $(2) $(1)) 82*73c99d4eSJuan Castillo $(eval $(if $(3),FIP_DEPS += $(3))) 83*73c99d4eSJuan Castilloendef 84*73c99d4eSJuan Castillo 85*73c99d4eSJuan Castillo# CERT_ADD_CMD_OPT adds a new command line option to the cert_create invokation 86*73c99d4eSJuan Castillo# $(1) = parameter filename 87*73c99d4eSJuan Castillo# $(2) = cert_create command line option for the specified parameter 88*73c99d4eSJuan Castillo# $(3) = input parameter (false if empty) 89*73c99d4eSJuan Castillodefine CERT_ADD_CMD_OPT 90*73c99d4eSJuan Castillo $(eval $(if $(3),CRT_DEPS += $(1))) 91*73c99d4eSJuan Castillo $(eval CRT_ARGS += $(2) $(1)) 92*73c99d4eSJuan Castilloendef 93*73c99d4eSJuan Castillo 94*73c99d4eSJuan Castillo# FIP_ADD_IMG allows the platform to specify an image to be packed in the FIP 95*73c99d4eSJuan Castillo# using a build option. It also adds a dependency on the image file, aborting 96*73c99d4eSJuan Castillo# the build if the file does not exist. 97*73c99d4eSJuan Castillo# $(1) = build option to specify the image filename (BL30, BL33, etc) 98*73c99d4eSJuan Castillo# $(2) = command line option for the fip_create tool (bl30, bl33, etc) 99*73c99d4eSJuan Castillo# Example: 100*73c99d4eSJuan Castillo# $(eval $(call FIP_ADD_IMG,BL33,--bl33)) 101*73c99d4eSJuan Castillodefine FIP_ADD_IMG 102*73c99d4eSJuan Castillo CRT_DEPS += check_$(1) 103*73c99d4eSJuan Castillo FIP_DEPS += check_$(1) 104*73c99d4eSJuan Castillo $(call FIP_ADD_PAYLOAD,$(value $(1)),$(2)) 105*73c99d4eSJuan Castillo 106*73c99d4eSJuan Castillocheck_$(1): 107*73c99d4eSJuan Castillo $$(if $(value $(1)),,$$(error "Platform '${PLAT}' requires $(1). Please set $(1) to point to the right file")) 108*73c99d4eSJuan Castilloendef 109*73c99d4eSJuan Castillo 110*73c99d4eSJuan Castillo 111*73c99d4eSJuan Castillo################################################################################ 112*73c99d4eSJuan Castillo# Auxiliary macros to build TF images from sources 113*73c99d4eSJuan Castillo################################################################################ 114*73c99d4eSJuan Castillo 115*73c99d4eSJuan Castillodefine match_goals 116*73c99d4eSJuan Castillo$(strip $(foreach goal,$(1),$(filter $(goal),$(MAKECMDGOALS)))) 117*73c99d4eSJuan Castilloendef 118*73c99d4eSJuan Castillo 119*73c99d4eSJuan Castillo# List of rules that involve building things 120*73c99d4eSJuan CastilloBUILD_TARGETS := all bl1 bl2 bl31 bl32 certificates fip 121*73c99d4eSJuan Castillo 122*73c99d4eSJuan Castillo# Does the list of goals specified on the command line include a build target? 123*73c99d4eSJuan Castilloifneq ($(call match_goals,${BUILD_TARGETS}),) 124*73c99d4eSJuan CastilloIS_ANYTHING_TO_BUILD := 1 125*73c99d4eSJuan Castilloendif 126*73c99d4eSJuan Castillo 127*73c99d4eSJuan Castillo 128*73c99d4eSJuan Castillo# MAKE_C builds a C source file and generates the dependency file 129*73c99d4eSJuan Castillo# $(1) = output directory 130*73c99d4eSJuan Castillo# $(2) = source file (%.c) 131*73c99d4eSJuan Castillo# $(3) = BL stage (2, 30, 31, 32, 33) 132*73c99d4eSJuan Castillodefine MAKE_C 133*73c99d4eSJuan Castillo 134*73c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.c,%.o,$(notdir $(2)))) 135*73c99d4eSJuan Castillo$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 136*73c99d4eSJuan Castillo 137*73c99d4eSJuan Castillo$(OBJ): $(2) 138*73c99d4eSJuan Castillo @echo " CC $$<" 139*73c99d4eSJuan Castillo $$(Q)$$(CC) $$(CFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@ 140*73c99d4eSJuan Castillo 141*73c99d4eSJuan Castillo 142*73c99d4eSJuan Castillo$(PREREQUISITES): $(2) 143*73c99d4eSJuan Castillo @echo " DEPS $$@" 144*73c99d4eSJuan Castillo @mkdir -p $(1) 145*73c99d4eSJuan Castillo $$(Q)$$(CC) $$(CFLAGS) -M -MT $(OBJ) -MF $$@ $$< 146*73c99d4eSJuan Castillo 147*73c99d4eSJuan Castilloifdef IS_ANYTHING_TO_BUILD 148*73c99d4eSJuan Castillo-include $(PREREQUISITES) 149*73c99d4eSJuan Castilloendif 150*73c99d4eSJuan Castillo 151*73c99d4eSJuan Castilloendef 152*73c99d4eSJuan Castillo 153*73c99d4eSJuan Castillo 154*73c99d4eSJuan Castillo# MAKE_S builds an assembly source file and generates the dependency file 155*73c99d4eSJuan Castillo# $(1) = output directory 156*73c99d4eSJuan Castillo# $(2) = assembly file (%.S) 157*73c99d4eSJuan Castillo# $(3) = BL stage (2, 30, 31, 32, 33) 158*73c99d4eSJuan Castillodefine MAKE_S 159*73c99d4eSJuan Castillo 160*73c99d4eSJuan Castillo$(eval OBJ := $(1)/$(patsubst %.S,%.o,$(notdir $(2)))) 161*73c99d4eSJuan Castillo$(eval PREREQUISITES := $(patsubst %.o,%.d,$(OBJ))) 162*73c99d4eSJuan Castillo 163*73c99d4eSJuan Castillo$(OBJ): $(2) 164*73c99d4eSJuan Castillo @echo " AS $$<" 165*73c99d4eSJuan Castillo $$(Q)$$(AS) $$(ASFLAGS) -DIMAGE_BL$(3) -c $$< -o $$@ 166*73c99d4eSJuan Castillo 167*73c99d4eSJuan Castillo$(PREREQUISITES): $(2) 168*73c99d4eSJuan Castillo @echo " DEPS $$@" 169*73c99d4eSJuan Castillo @mkdir -p $(1) 170*73c99d4eSJuan Castillo $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(OBJ) -MF $$@ $$< 171*73c99d4eSJuan Castillo 172*73c99d4eSJuan Castilloifdef IS_ANYTHING_TO_BUILD 173*73c99d4eSJuan Castillo-include $(PREREQUISITES) 174*73c99d4eSJuan Castilloendif 175*73c99d4eSJuan Castillo 176*73c99d4eSJuan Castilloendef 177*73c99d4eSJuan Castillo 178*73c99d4eSJuan Castillo 179*73c99d4eSJuan Castillo# MAKE_LD generate the linker script using the C preprocessor 180*73c99d4eSJuan Castillo# $(1) = output linker script 181*73c99d4eSJuan Castillo# $(2) = input template 182*73c99d4eSJuan Castillodefine MAKE_LD 183*73c99d4eSJuan Castillo 184*73c99d4eSJuan Castillo$(eval PREREQUISITES := $(1).d) 185*73c99d4eSJuan Castillo 186*73c99d4eSJuan Castillo$(1): $(2) 187*73c99d4eSJuan Castillo @echo " PP $$<" 188*73c99d4eSJuan Castillo $$(Q)$$(AS) $$(ASFLAGS) -P -E -D__LINKER__ -o $$@ $$< 189*73c99d4eSJuan Castillo 190*73c99d4eSJuan Castillo$(PREREQUISITES): $(2) 191*73c99d4eSJuan Castillo @echo " DEPS $$@" 192*73c99d4eSJuan Castillo @mkdir -p $$(dir $$@) 193*73c99d4eSJuan Castillo $$(Q)$$(AS) $$(ASFLAGS) -M -MT $(1) -MF $$@ $$< 194*73c99d4eSJuan Castillo 195*73c99d4eSJuan Castilloifdef IS_ANYTHING_TO_BUILD 196*73c99d4eSJuan Castillo-include $(PREREQUISITES) 197*73c99d4eSJuan Castilloendif 198*73c99d4eSJuan Castillo 199*73c99d4eSJuan Castilloendef 200*73c99d4eSJuan Castillo 201*73c99d4eSJuan Castillo 202*73c99d4eSJuan Castillo# MAKE_OBJS builds both C and assembly source files 203*73c99d4eSJuan Castillo# $(1) = output directory 204*73c99d4eSJuan Castillo# $(2) = list of source files (both C and assembly) 205*73c99d4eSJuan Castillo# $(3) = BL stage (2, 30, 31, 32, 33) 206*73c99d4eSJuan Castillodefine MAKE_OBJS 207*73c99d4eSJuan Castillo $(eval C_OBJS := $(filter %.c,$(2))) 208*73c99d4eSJuan Castillo $(eval REMAIN := $(filter-out %.c,$(2))) 209*73c99d4eSJuan Castillo $(eval $(foreach obj,$(C_OBJS),$(call MAKE_C,$(1),$(obj),$(3)))) 210*73c99d4eSJuan Castillo 211*73c99d4eSJuan Castillo $(eval S_OBJS := $(filter %.S,$(REMAIN))) 212*73c99d4eSJuan Castillo $(eval REMAIN := $(filter-out %.S,$(REMAIN))) 213*73c99d4eSJuan Castillo $(eval $(foreach obj,$(S_OBJS),$(call MAKE_S,$(1),$(obj),$(3)))) 214*73c99d4eSJuan Castillo 215*73c99d4eSJuan Castillo $(and $(REMAIN),$(error Unexpected source files present: $(REMAIN))) 216*73c99d4eSJuan Castilloendef 217*73c99d4eSJuan Castillo 218*73c99d4eSJuan Castillo 219*73c99d4eSJuan Castillo# NOTE: The line continuation '\' is required in the next define otherwise we 220*73c99d4eSJuan Castillo# end up with a line-feed characer at the end of the last c filename. 221*73c99d4eSJuan Castillo# Also bare this issue in mind if extending the list of supported filetypes. 222*73c99d4eSJuan Castillodefine SOURCES_TO_OBJS 223*73c99d4eSJuan Castillo $(notdir $(patsubst %.c,%.o,$(filter %.c,$(1)))) \ 224*73c99d4eSJuan Castillo $(notdir $(patsubst %.S,%.o,$(filter %.S,$(1)))) 225*73c99d4eSJuan Castilloendef 226*73c99d4eSJuan Castillo 227*73c99d4eSJuan Castillo 228*73c99d4eSJuan Castillo# MAKE_TOOL_ARGS macro defines the command line arguments for the FIP tool for 229*73c99d4eSJuan Castillo# each BL image. Arguments: 230*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 231*73c99d4eSJuan Castillo# $(2) = Binary file 232*73c99d4eSJuan Castillo# $(3) = In FIP (false if empty) 233*73c99d4eSJuan Castillodefine MAKE_TOOL_ARGS 234*73c99d4eSJuan Castillo $(if $(3),$(eval $(call FIP_ADD_PAYLOAD,$(2),--bl$(1),bl$(1)))) 235*73c99d4eSJuan Castilloendef 236*73c99d4eSJuan Castillo 237*73c99d4eSJuan Castillo 238*73c99d4eSJuan Castillo# MAKE_BL macro defines the targets and options to build each BL image. 239*73c99d4eSJuan Castillo# Arguments: 240*73c99d4eSJuan Castillo# $(1) = BL stage (2, 30, 31, 32, 33) 241*73c99d4eSJuan Castillo# $(2) = In FIP (false if empty) 242*73c99d4eSJuan Castillodefine MAKE_BL 243*73c99d4eSJuan Castillo $(eval BUILD_DIR := ${BUILD_PLAT}/bl$(1)) 244*73c99d4eSJuan Castillo $(eval SOURCES := $(BL$(1)_SOURCES) $(BL_COMMON_SOURCES) $(PLAT_BL_COMMON_SOURCES)) 245*73c99d4eSJuan Castillo $(eval OBJS := $(addprefix $(BUILD_DIR)/,$(call SOURCES_TO_OBJS,$(SOURCES)))) 246*73c99d4eSJuan Castillo $(eval LINKERFILE := $(call IMG_LINKERFILE,$(1))) 247*73c99d4eSJuan Castillo $(eval MAPFILE := $(call IMG_MAPFILE,$(1))) 248*73c99d4eSJuan Castillo $(eval ELF := $(call IMG_ELF,$(1))) 249*73c99d4eSJuan Castillo $(eval DUMP := $(call IMG_DUMP,$(1))) 250*73c99d4eSJuan Castillo $(eval BIN := $(call IMG_BIN,$(1))) 251*73c99d4eSJuan Castillo 252*73c99d4eSJuan Castillo $(eval $(call MAKE_OBJS,$(BUILD_DIR),$(SOURCES),$(1))) 253*73c99d4eSJuan Castillo $(eval $(call MAKE_LD,$(LINKERFILE),$(BL$(1)_LINKERFILE))) 254*73c99d4eSJuan Castillo 255*73c99d4eSJuan Castillo$(BUILD_DIR): 256*73c99d4eSJuan Castillo $$(Q)mkdir -p "$$@" 257*73c99d4eSJuan Castillo 258*73c99d4eSJuan Castillo$(ELF): $(OBJS) $(LINKERFILE) 259*73c99d4eSJuan Castillo @echo " LD $$@" 260*73c99d4eSJuan Castillo @echo 'const char build_message[] = "Built : "__TIME__", "__DATE__; \ 261*73c99d4eSJuan Castillo const char version_string[] = "${VERSION_STRING}";' | \ 262*73c99d4eSJuan Castillo $$(CC) $$(CFLAGS) -xc - -o $(BUILD_DIR)/build_message.o 263*73c99d4eSJuan Castillo $$(Q)$$(LD) -o $$@ $$(LDFLAGS) -Map=$(MAPFILE) --script $(LINKERFILE) \ 264*73c99d4eSJuan Castillo $(BUILD_DIR)/build_message.o $(OBJS) 265*73c99d4eSJuan Castillo 266*73c99d4eSJuan Castillo$(DUMP): $(ELF) 267*73c99d4eSJuan Castillo @echo " OD $$@" 268*73c99d4eSJuan Castillo $${Q}$${OD} -dx $$< > $$@ 269*73c99d4eSJuan Castillo 270*73c99d4eSJuan Castillo$(BIN): $(ELF) 271*73c99d4eSJuan Castillo @echo " BIN $$@" 272*73c99d4eSJuan Castillo $$(Q)$$(OC) -O binary $$< $$@ 273*73c99d4eSJuan Castillo @echo 274*73c99d4eSJuan Castillo @echo "Built $$@ successfully" 275*73c99d4eSJuan Castillo @echo 276*73c99d4eSJuan Castillo 277*73c99d4eSJuan Castillo.PHONY: bl$(1) 278*73c99d4eSJuan Castillobl$(1): $(BUILD_DIR) $(BIN) $(DUMP) 279*73c99d4eSJuan Castillo 280*73c99d4eSJuan Castilloall: bl$(1) 281*73c99d4eSJuan Castillo 282*73c99d4eSJuan Castillo$(eval $(call MAKE_TOOL_ARGS,$(1),$(BIN),$(2))) 283*73c99d4eSJuan Castillo 284*73c99d4eSJuan Castilloendef 285*73c99d4eSJuan Castillo 286