feat(build): put fiptool in the build directory
For some reason, tools are not built in the build directory, but rather
in the source directory, regardless of what BUILD_BASE might say. This
is especially annoying when trying to have a few builds going at the
same time (like in a CI). For example, run A may check if a file X
exists. Seeing that it does not, it will start building the file. At the
same time, run B may also check if file X exists but before A has
written it, starting a build again. Then it is possible for A to use the
file it believes to have built right at the moment B begins writing to
it (and truncating it beforehand). This results in gcc failing to read a
fail and a failed build. The more parallel runs there are, the more
likely this is to happen, and this is virtually guaranteed to happen
with just a handful onwards.
So move fiptool and all of its build artefacts to the build directory.
Since there are platform differences between the various fiptool
incarnations, this goes in the plat build rather than a more top-level
location.
This patch adds the build macro MAKE_TOOL to do this generically for any
kind of tool since the other tools suffer from the same shortfall. This
macro takes care to use unique names for every type of argument that
building a tool might need. The fiptool makefile and platform add-ons
are updated accordingly.
This should have never been allowed in the first place, but downstream
scripts almost certainly depend on this behaviour now. So add a symlink
in the place of the old fiptool so these continue working. With any
luck, this will be removed in the future.
Change-Id: I3d4d87dab0f53deab5b43fbc6652146f0e4e7e81
Signed-off-by: Boyan Karatotev <boyan.karatotev@arm.com>
diff --git a/Makefile b/Makefile
index 4bc7053..055921c 100644
--- a/Makefile
+++ b/Makefile
@@ -107,7 +107,7 @@
# Variables for use with Firmware Image Package
FIPTOOLPATH ?= tools/fiptool
-FIPTOOL ?= ${FIPTOOLPATH}/fiptool$(.exe)
+FIPTOOL ?= ${BUILD_PLAT}/${FIPTOOLPATH}/fiptool$(.exe)
# Variables for use with sptool
SPTOOLPATH ?= tools/sptool
@@ -1364,7 +1364,8 @@
clean:
$(s)echo " CLEAN"
$(q)rm -rf $(BUILD_PLAT)
- $(q)${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean
+ $(q)${MAKE} PLAT=${PLAT} BUILD_PLAT=${BUILD_PLAT} --no-print-directory -C ${FIPTOOLPATH} clean
+ $(q)rm -rf ${FIPTOOLPATH}/fiptool
$(q)${MAKE} PLAT=${PLAT} --no-print-directory -C ${CRTTOOLPATH} clean
$(q)${MAKE} PLAT=${PLAT} --no-print-directory -C ${ENCTOOLPATH} clean
$(q)${MAKE} --no-print-directory -C ${ROMLIBPATH} clean
@@ -1373,7 +1374,8 @@
$(s)echo " REALCLEAN"
$(q)rm -rf $(BUILD_BASE)
$(q)rm -rf $(CURDIR)/cscope.*
- $(q)${MAKE} --no-print-directory -C ${FIPTOOLPATH} clean
+ $(q)${MAKE} PLAT=${PLAT} BUILD_PLAT=${BUILD_PLAT} --no-print-directory -C ${FIPTOOLPATH} clean
+ $(q)rm -rf ${FIPTOOLPATH}/fiptool
$(q)${MAKE} PLAT=${PLAT} --no-print-directory -C ${CRTTOOLPATH} realclean
$(q)${MAKE} PLAT=${PLAT} --no-print-directory -C ${ENCTOOLPATH} realclean
$(q)${MAKE} --no-print-directory -C ${ROMLIBPATH} clean
@@ -1457,8 +1459,10 @@
fip: ${BUILD_PLAT}/${FIP_NAME}
fwu_fip: ${BUILD_PLAT}/${FWU_FIP_NAME}
+# symlink for compatibility before tools were in the build directory
${FIPTOOL}: FORCE
- $(q)${MAKE} PLAT=${PLAT} CPPFLAGS="-DVERSION='\"${VERSION_STRING}\"'" FIPTOOL=${FIPTOOL} OPENSSL_DIR=${OPENSSL_DIR} DEBUG=${DEBUG} --no-print-directory -C ${FIPTOOLPATH} all
+ $(q)${MAKE} PLAT=${PLAT} BUILD_PLAT=$(abspath ${BUILD_PLAT}) CPPFLAGS="-DVERSION='\"${VERSION_STRING}\"'" OPENSSL_DIR=${OPENSSL_DIR} DEBUG=${DEBUG} --no-print-directory -C ${FIPTOOLPATH} all
+ $(q)ln -sf ${FIPTOOL} ${FIPTOOLPATH}/fiptool
$(BUILD_PLAT)/romlib/romlib.bin $(BUILD_PLAT)/lib/libwrappers.a $&: $(BUILD_PLAT)/lib/libfdt.a $(BUILD_PLAT)/lib/libc.a $(CRYPTO_LIB)
$(q)${MAKE} PLAT_DIR=${PLAT_DIR} BUILD_PLAT=${BUILD_PLAT} ENABLE_BTI=${ENABLE_BTI} CRYPTO_SUPPORT=${CRYPTO_SUPPORT} ARM_ARCH_MINOR=${ARM_ARCH_MINOR} INCLUDES=$(call escape-shell,$(INCLUDES)) DEFINES=$(call escape-shell,$(DEFINES)) --no-print-directory -C ${ROMLIBPATH} all
diff --git a/make_helpers/build_macros.mk b/make_helpers/build_macros.mk
index e913abd..c28a14e 100644
--- a/make_helpers/build_macros.mk
+++ b/make_helpers/build_macros.mk
@@ -1,5 +1,5 @@
#
-# Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2015-2025, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -284,6 +284,49 @@
MAKE_DEP = -Wp,-MD,$1 -MT $2 -MP
+# MAKE_TOOL_C builds a C source file and generates the dependency file
+# $(1) = output directory
+# $(2) = source file (%.c)
+# $(3) = lowercase name of the tool
+# $(4) = uppercase name of the tool
+define MAKE_TOOL_C
+
+$(eval SRC := $(2))
+$(eval OBJ := $(patsubst %.c,$(1)/$(3)/%.o,$(SRC)))
+$(eval DEP := $(patsubst %.o,%.d,$(OBJ)))
+
+$(eval TOOL_DEFINES := $($(4)_DEFINES))
+$(eval TOOL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
+$(eval TOOL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(TOOL_DEFINES)) $(addprefix -I,$(TOOL_INCLUDE_DIRS)))
+$(eval TOOL_CFLAGS := $($(4)_CFLAGS))
+
+$(OBJ): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
+ $$(s)echo " HOSTCC $$<"
+ $$(q)$(host-cc) $$(HOSTCCFLAGS) $(TOOL_CPPFLAGS) $(TOOL_CFLAGS) $(call MAKE_DEP,$(DEP),$(OBJ)) -c $$< -o $$@
+
+-include $(DEP)
+
+endef
+
+# MAKE_TOOL
+# $(1) = output directory
+# $(2) = lowercase name of the tool
+# $(3) = uppercase name of the tool
+define MAKE_TOOL
+$(eval SRCS := $($(3)_SOURCES))
+$(eval OBJS := $(patsubst %.c,$(1)/$(2)/%.o,$(SRCS)))
+$(eval DST := $(1)/$(2)/$(2)$(.exe))
+$(eval $(foreach src,$(SRCS),$(call MAKE_TOOL_C,$(1),$(src),$(2),$(3))))
+
+$(DST): $(OBJS) $(filter-out %.d,$(MAKEFILE_LIST))
+ $$(s)echo " HOSTLD $$@"
+ $$(q)$(host-cc) $${OBJS} -o $$@ $($(3)_LDFLAGS)
+ $$(s)echo
+ $$(s)echo "Built $$@ successfully"
+ $$(s)echo
+
+all: $(DST)
+endef
# MAKE_C_LIB builds a C source file and generates the dependency file
# $(1) = output directory
diff --git a/tools/fiptool/Makefile b/tools/fiptool/Makefile
index a660a50..6deac9d 100644
--- a/tools/fiptool/Makefile
+++ b/tools/fiptool/Makefile
@@ -6,29 +6,28 @@
MAKE_HELPERS_DIRECTORY := ../../make_helpers/
include ${MAKE_HELPERS_DIRECTORY}build_macros.mk
+include ${MAKE_HELPERS_DIRECTORY}build-rules.mk
include ${MAKE_HELPERS_DIRECTORY}common.mk
include ${MAKE_HELPERS_DIRECTORY}defaults.mk
include ${MAKE_HELPERS_DIRECTORY}toolchain.mk
-FIPTOOL ?= fiptool$(.exe)
-PROJECT := $(notdir ${FIPTOOL})
-OBJECTS := fiptool.o tbbr_config.o
+FIPTOOL_SOURCES := fiptool.c tbbr_config.c
STATIC ?= 0
-override CPPFLAGS += -D_GNU_SOURCE -D_XOPEN_SOURCE=700
-HOSTCCFLAGS := -Wall -Werror -pedantic -std=c99
+FIPTOOL_DEFINES += _GNU_SOURCE _XOPEN_SOURCE=700
+FIPTOOL_CFLAGS := -Wall -Werror -pedantic -std=c99
ifeq (${DEBUG},1)
- HOSTCCFLAGS += -g -O0 -DDEBUG
+ FIPTOOL_CFLAGS += -g -O0 -DDEBUG
else
- HOSTCCFLAGS += -O2
+ FIPTOOL_CFLAGS += -O2
endif
-INCLUDE_PATHS := -I../../include/tools_share
+FIPTOOL_INCLUDE_DIRS := ../../include/tools_share
-DEFINES += -DSTATIC=$(STATIC)
+FIPTOOL_DEFINES += STATIC=$(STATIC)
ifeq (${STATIC},1)
-LDOPTS := -static
+FIPTOOL_LDFLAGS := -static
else
OPENSSL_DIR := /usr
@@ -38,7 +37,7 @@
# USING_OPENSSL3 flag will be added to the HOSTCCFLAGS variable with the proper
# computed value.
-DEFINES += -DUSING_OPENSSL3=$(USING_OPENSSL3)
+FIPTOOL_DEFINES += USING_OPENSSL3=$(USING_OPENSSL3)
# Include library directories where OpenSSL library files are located.
# For a normal installation (i.e.: when ${OPENSSL_DIR} = /usr or
@@ -46,12 +45,10 @@
# directory. However, for a local build of OpenSSL, the built binaries are
# located under the main project directory (i.e.: ${OPENSSL_DIR}, not
# ${OPENSSL_DIR}/lib/).
-LDOPTS := -L${OPENSSL_DIR}/lib -L${OPENSSL_DIR} -lcrypto
-INCLUDE_PATHS += -I${OPENSSL_DIR}/include
+FIPTOOL_LDFLAGS := -L${OPENSSL_DIR}/lib -L${OPENSSL_DIR} -lcrypto
+FIPTOOL_INCLUDE_DIRS += ${OPENSSL_DIR}/include
endif # STATIC
-HOSTCCFLAGS += ${DEFINES}
-
ifneq (${PLAT},)
TF_PLATFORM_ROOT := ../../plat/
include ${MAKE_HELPERS_DIRECTORY}plat_helpers.mk
@@ -65,24 +62,11 @@
include ${PLAT_FIPTOOL_HELPER_MK}
endif
-DEPS := $(patsubst %.o,%.d,$(OBJECTS))
+$(eval $(call MAKE_TOOL,$(BUILD_PLAT)/tools,fiptool,FIPTOOL))
.PHONY: all clean distclean --openssl
-all: --openssl ${PROJECT}
-
-${PROJECT}: ${OBJECTS} Makefile
- $(s)echo " HOSTLD $@"
- $(q)$(host-cc) ${OBJECTS} -o $@ $(LDOPTS)
- $(s)echo
- $(s)echo "Built $@ successfully"
- $(s)echo
-
-%.o: %.c Makefile
- $(s)echo " HOSTCC $<"
- $(q)$(host-cc) -c ${CPPFLAGS} ${HOSTCCFLAGS} ${INCLUDE_PATHS} -MD -MP $< -o $@
-
--include $(DEPS)
+all: --openssl
--openssl:
ifeq ($(STATIC),0)
@@ -92,4 +76,4 @@
endif # STATIC
clean:
- $(q)rm -rf $(PROJECT) $(OBJECTS) $(DEPS)
+ $(q)rm -rf $(BUILD_PLAT)/tools/fiptool
diff --git a/tools/fiptool/plat_fiptool/arm/board/juno/plat_fiptool.mk b/tools/fiptool/plat_fiptool/arm/board/juno/plat_fiptool.mk
index 5549b0d..f36071f 100644
--- a/tools/fiptool/plat_fiptool/arm/board/juno/plat_fiptool.mk
+++ b/tools/fiptool/plat_fiptool/arm/board/juno/plat_fiptool.mk
@@ -1,5 +1,5 @@
#
-# Copyright (c) 2023, Arm Limited. All rights reserved.
+# Copyright (c) 2023-2025, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -7,10 +7,10 @@
PLAT_DEF_UUID := yes
ifeq (${PLAT_DEF_UUID}, yes)
-HOSTCCFLAGS += -DPLAT_DEF_FIP_UUID
+FIPTOOL_DEFINES += PLAT_DEF_FIP_UUID
ifeq (${ETHOSN_NPU_TZMP1},1)
-HOSTCCFLAGS += -DETHOSN_NPU_TZMP1
+FIPTOOL_DEFINES += ETHOSN_NPU_TZMP1
endif
-INCLUDE_PATHS += -I./ -I../../plat/arm/board/juno/fip -I../../include
-OBJECTS += plat_fiptool/arm/board/juno/plat_def_uuid_config.o
+FIPTOOL_INCLUDE_DIRS += ./ ../../plat/arm/board/juno/fip ../../include
+FIPTOOL_SOURCES += plat_fiptool/arm/board/juno/plat_def_uuid_config.c
endif
diff --git a/tools/fiptool/plat_fiptool/arm/board/tc/plat_fiptool.mk b/tools/fiptool/plat_fiptool/arm/board/tc/plat_fiptool.mk
index 70ccfc5..d808451 100644
--- a/tools/fiptool/plat_fiptool/arm/board/tc/plat_fiptool.mk
+++ b/tools/fiptool/plat_fiptool/arm/board/tc/plat_fiptool.mk
@@ -1,12 +1,12 @@
#
# Copyright (c) 2021, NXP. All rights reserved.
-# Copyright (c) 2022-2023, Arm Limited. All rights reserved.
+# Copyright (c) 2022-2025, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
-INCLUDE_PATHS += -I./ \
- -I../../plat/arm/board/tc
+FIPTOOL_INCLUDE_DIRS += ./ \
+ ../../plat/arm/board/tc
-HOSTCCFLAGS += -DPLAT_DEF_FIP_UUID
-OBJECTS += plat_fiptool/arm/board/tc/plat_def_uuid_config.o
+FIPTOOL_DEFINES += PLAT_DEF_FIP_UUID
+FIPTOOL_SOURCES += plat_fiptool/arm/board/tc/plat_def_uuid_config.c
diff --git a/tools/fiptool/plat_fiptool/nxp/plat_fiptool.mk b/tools/fiptool/plat_fiptool/nxp/plat_fiptool.mk
index 6d7b07b..3d69500 100644
--- a/tools/fiptool/plat_fiptool/nxp/plat_fiptool.mk
+++ b/tools/fiptool/plat_fiptool/nxp/plat_fiptool.mk
@@ -1,6 +1,6 @@
#
# Copyright (c) 2021, NXP. All rights reserved.
-# Copyright (c) 2023, Arm Limited. All rights reserved.
+# Copyright (c) 2023-2025, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -17,16 +17,14 @@
PLAT_DEF_UUID_OID_CONFIG_PATH := ../../plat/nxp/common/fip_handler/common
-INCLUDE_PATHS += -I${PLAT_DEF_UUID_OID_CONFIG_PATH} \
- -I./
+FIPTOOL_INCLUDE_DIRS += ${PLAT_DEF_UUID_OID_CONFIG_PATH} \
+ ./
ifeq (${PLAT_DEF_OID},yes)
-HOSTCCFLAGS += -DPLAT_DEF_OID
+FIPTOOL_DEFINES += PLAT_DEF_OID
endif
ifeq (${PLAT_DEF_UUID},yes)
-HOSTCCFLAGS += -DPLAT_DEF_FIP_UUID
-PLAT_OBJECTS += ${PLAT_DEF_UUID_CONFIG_FILE_PATH}/${PLAT_DEF_UUID_CONFIG_FILE_NAME}.o
+FIPTOOL_DEFINES += PLAT_DEF_FIP_UUID
+FIPTOOL_SOURCES += ${PLAT_DEF_UUID_CONFIG_FILE_PATH}/${PLAT_DEF_UUID_CONFIG_FILE_NAME}.c
endif
-
-OBJECTS += ${PLAT_OBJECTS}
diff --git a/tools/fiptool/plat_fiptool/st/plat_fiptool.mk b/tools/fiptool/plat_fiptool/st/plat_fiptool.mk
index 494715c..02950e9 100644
--- a/tools/fiptool/plat_fiptool/st/plat_fiptool.mk
+++ b/tools/fiptool/plat_fiptool/st/plat_fiptool.mk
@@ -9,17 +9,12 @@
# in the plat_def_toc_entries[].
PLAT_DEF_UUID_FILE_NAME := plat_def_uuid_config
-INCLUDE_PATHS += -I../../plat/st/common/include -I./
+FIPTOOL_INCLUDE_DIRS += ../../plat/st/common/include ./
PLAT_DEF_UUID := yes
ifeq (${PLAT_DEF_UUID},yes)
-HOSTCCFLAGS += -DPLAT_DEF_FIP_UUID
+FIPTOOL_DEFINES += PLAT_DEF_FIP_UUID
-${PLAT_DEF_UUID_FILE_NAME}.o: plat_fiptool/st/${PLAT_DEF_UUID_FILE_NAME}.c
- $(host-cc) -c ${CPPFLAGS} ${HOSTCCFLAGS} ${INCLUDE_PATHS} $< -o $@
-
-PLAT_OBJECTS += ${PLAT_DEF_UUID_FILE_NAME}.o
+FIPTOOL_SOURCES += plat_fiptool/st/${PLAT_DEF_UUID_FILE_NAME}.c
endif
-
-OBJECTS += ${PLAT_OBJECTS}