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/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