refactor(build): use a standard rule to run the preprocessor

There are a few, functionally identical, ways to call the preprocessor
on a non-C file, depending on the file. They differ in subtle, not
entirely correct, ways - one is missing a dependency to the makefiles,
another generates its .d inline, and the prints are different. That has
resulted in platforms reimplementing this functionality, making the
build brittle - a change to the overall build system doesn't propagate.
So add a MAKE_PRE macro that will make a rule with all the bells and
whistles to run the preprocessor on an arbitrary file.

This patch converts the arm platforms' cot_descriptors DTS rules. The
files are renamed to fit with the build rule and all extra flags are
dropped. Those flags are only necessary for building BL2 c files, which
will be passed to the output C file. Only the DTS flags are needed for
the preprocessing step, which will be passed automatically.

Change-Id: I3c1cc0ecf93b87d828f868214928c1bc9bcb5758
Signed-off-by: Boyan Karatotev <boyan.karatotev@arm.com>
diff --git a/docs/security_advisories/security-advisory-tfv-10.rst b/docs/security_advisories/security-advisory-tfv-10.rst
index f53bae1..6067b52 100644
--- a/docs/security_advisories/security-advisory-tfv-10.rst
+++ b/docs/security_advisories/security-advisory-tfv-10.rst
@@ -98,7 +98,7 @@
 ``drivers/auth/``) require that the certificate's signature has already been
 validated prior to calling ``get_ext()``, or any function that calls ``get_ext()``.
 Platforms taking their chain of trust from a dynamic configuration file (such as
-``fdts/tbbr_cot_descriptors.dtsi``) are also safe, as signature verification will
+``fdts/tbbr_cot_descriptors.dts``) are also safe, as signature verification will
 always be done prior to any calls to ``get_ext()`` or ``auth_nvctr()`` in this
 case, no matter the order of the properties in the file.  Therefore, it is not
 possible to exploit this vulnerability pre-authentication in upstream TF-A.
diff --git a/docs/tools/cot-dt2c.rst b/docs/tools/cot-dt2c.rst
index e8bb1ac..64f92b6 100644
--- a/docs/tools/cot-dt2c.rst
+++ b/docs/tools/cot-dt2c.rst
@@ -58,7 +58,7 @@
 .. code::
 
     cot-dt2c convert-to-c [INPUT DTS PATH] [OUTPUT C PATH]
-    cot-dt2c convert-to-c fdts/tbbr_cot_descriptors.dtsi test.c
+    cot-dt2c convert-to-c fdts/tbbr_cot_descriptors.dts test.c
 
 
 Validate CoT descriptors
@@ -81,7 +81,7 @@
 .. code::
 
     cot-dt2c validate-cot [INPUT DTS PATH]
-    cot-dt2c validate-cot fdts/tbbr_cot_descriptors.dtsi
+    cot-dt2c validate-cot fdts/tbbr_cot_descriptors.dts
 
 
 Visualize CoT descriptors
@@ -93,7 +93,7 @@
 .. code::
 
     cot-dt2c visualize-cot [INPUT DTS PATH]
-    cot-dt2c visualize-cot fdts/tbbr_cot_descriptors.dtsi
+    cot-dt2c visualize-cot fdts/tbbr_cot_descriptors.dts
 
 
 Validate Other DT files
@@ -113,7 +113,7 @@
 
 --------------
 
-*Copyright (c) 2024, Arm Limited. All rights reserved.*
+*Copyright (c) 2024-2025, Arm Limited. All rights reserved.*
 
 .. _tools/cot_dt2c/pyproject.toml: https://review.trustedfirmware.org/plugins/gitiles/TF-A/trusted-firmware-a/+/refs/heads/integration/tools/cot_dt2c/pyproject.toml
 .. _Poetry: https://python-poetry.org/docs/
diff --git a/fdts/cca_cot_descriptors.dtsi b/fdts/cca_cot_descriptors.dts
similarity index 100%
rename from fdts/cca_cot_descriptors.dtsi
rename to fdts/cca_cot_descriptors.dts
diff --git a/fdts/dualroot_cot_descriptors.dtsi b/fdts/dualroot_cot_descriptors.dts
similarity index 100%
rename from fdts/dualroot_cot_descriptors.dtsi
rename to fdts/dualroot_cot_descriptors.dts
diff --git a/fdts/tbbr_cot_descriptors.dtsi b/fdts/tbbr_cot_descriptors.dts
similarity index 100%
rename from fdts/tbbr_cot_descriptors.dtsi
rename to fdts/tbbr_cot_descriptors.dts
diff --git a/make_helpers/build_macros.mk b/make_helpers/build_macros.mk
index c28a14e..46a1ce0 100644
--- a/make_helpers/build_macros.mk
+++ b/make_helpers/build_macros.mk
@@ -411,6 +411,20 @@
 
 endef
 
+# MAKE_PRE run the C preprocessor on a file
+#   $(1) = output file
+#   $(2) = list of input files
+#   $(3) = dep file
+#   $(4) = list of rule-specific flags to pass
+define MAKE_PRE
+$(eval OUT := $(1))
+$(eval SRC := $(2))
+$(eval DEP := $(3))
+$(eval CUSTOM_FLAGS := $(4))
+$(OUT): $(SRC) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
+	$$(s)echo "  CPP     $$<"
+	$$(q)$($(ARCH)-cpp) -E -P -x assembler-with-cpp $$(TF_CFLAGS) $(CUSTOM_FLAGS) $(call MAKE_DEP,$(DEP),$(OUT)) -o $$@ $$<
+endef
 
 # MAKE_LD generate the linker script using the C preprocessor
 #   $(1) = output linker script
@@ -424,11 +438,9 @@
 $(eval BL_DEFINES := IMAGE_$(4) $($(4)_DEFINES))
 $(eval BL_INCLUDE_DIRS := $($(4)_INCLUDE_DIRS))
 $(eval BL_CPPFLAGS := $($(4)_CPPFLAGS) $(addprefix -D,$(BL_DEFINES)) $(addprefix -I,$(BL_INCLUDE_DIRS)))
+$(eval FLAGS := -D__LINKER__ $(BL_CPPFLAGS))
 
-$(1): $(2) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
-	$$(s)echo "  PP      $$<"
-	$$(q)$($(ARCH)-cpp) -E $$(CPPFLAGS) $(BL_CPPFLAGS) $(TF_CFLAGS_$(ARCH)) -P -x assembler-with-cpp -D__LINKER__ $(call MAKE_DEP,$(DEP),$1) -o $$@ $$<
-
+$(eval $(call MAKE_PRE,$(1),$(2),$(DEP),$(FLAGS)))
 -include $(DEP)
 
 endef
@@ -639,10 +651,7 @@
 # Dependencies of the DT compilation on its pre-compiled DTS
 $(eval DTBDEP := $(patsubst %.dtb,%.d,$(DOBJ)))
 
-$(DPRE): $(2) | $$$$(@D)/
-	$$(s)echo "  CPP     $$<"
-	$(eval DTBS       := $(addprefix $(1)/,$(call SOURCES_TO_DTBS,$(2))))
-	$$(q)$($(ARCH)-cpp) -E $$(TF_CFLAGS_$(ARCH)) $$(DTC_CPPFLAGS) -MT $(DTBS) -MMD -MF $(DTSDEP) -o $(DPRE) $$<
+$(eval $(call MAKE_PRE,$(DPRE),$(2),$(DTSDEP),$(DTC_CPPFLAGS)))
 
 $(DOBJ): $(DPRE) $(filter-out %.d,$(MAKEFILE_LIST)) | $$$$(@D)/
 	$$(s)echo "  DTC     $$<"
diff --git a/make_helpers/cflags.mk b/make_helpers/cflags.mk
index 939650a..01df0db 100644
--- a/make_helpers/cflags.mk
+++ b/make_helpers/cflags.mk
@@ -298,6 +298,5 @@
 
 
 DTC_FLAGS		+=	-I dts -O dtb
-DTC_CPPFLAGS		+=	-P -nostdinc $(INCLUDES) -Ifdts -undef \
-				-x assembler-with-cpp $(DEFINES)
+DTC_CPPFLAGS		+=	-Ifdts -undef
 
diff --git a/plat/arm/board/fvp/fdts/fvp_cot_desc.dtsi b/plat/arm/board/fvp/fdts/fvp_cot_desc.dtsi
index 9c8328b..27e32b1 100644
--- a/plat/arm/board/fvp/fdts/fvp_cot_desc.dtsi
+++ b/plat/arm/board/fvp/fdts/fvp_cot_desc.dtsi
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2024, Arm Limited. All rights reserved.
+ * Copyright (c) 2024-2025, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -7,10 +7,10 @@
 
 #if COT_DESC_IN_DTB
 	#if defined(ARM_COT_cca)
-		#include "cca_cot_descriptors.dtsi"
+		#include "cca_cot_descriptors.dts"
 	#elif defined(ARM_COT_dualroot)
-		#include "dualroot_cot_descriptors.dtsi"
+		#include "dualroot_cot_descriptors.dts"
 	#elif defined(ARM_COT_tbbr)
-		#include "tbbr_cot_descriptors.dtsi"
+		#include "tbbr_cot_descriptors.dts"
 	#endif
 #endif
diff --git a/plat/arm/common/arm_common.mk b/plat/arm/common/arm_common.mk
index e76f03b..c668380 100644
--- a/plat/arm/common/arm_common.mk
+++ b/plat/arm/common/arm_common.mk
@@ -437,12 +437,12 @@
 
     ifeq (${COT_DESC_IN_DTB},0)
       ifeq (${COT},dualroot)
-        COTDTPATH := fdts/dualroot_cot_descriptors.dtsi
+        COTDTPATH := fdts/dualroot_cot_descriptors.dts
       else ifeq (${COT},cca)
-        COTDTPATH := fdts/cca_cot_descriptors.dtsi
+        COTDTPATH := fdts/cca_cot_descriptors.dts
       else ifeq (${COT},tbbr)
         ifneq (${PLAT},juno)
-          COTDTPATH := fdts/tbbr_cot_descriptors.dtsi
+          COTDTPATH := fdts/tbbr_cot_descriptors.dts
         endif
       endif
     endif
@@ -515,22 +515,12 @@
 endif
 
 ifneq ($(COTDTPATH),)
-        cot-dt-defines = IMAGE_BL2 $(BL2_DEFINES)
-        cot-dt-include-dirs = $(BL2_INCLUDE_DIRS)
+        # no custom flags
+        $(eval $(call MAKE_PRE,$(BUILD_PLAT)/$(COTDTPATH),$(COTDTPATH),$(BUILD_PLAT)/$(COTDTPATH:.dts=.o.d)))
 
-        cot-dt-cpp-flags  = $(cot-dt-defines:%=-D%)
-        cot-dt-cpp-flags += $(cot-dt-include-dirs:%=-I%)
-
-        cot-dt-cpp-flags += $(BL2_CPPFLAGS)
-        cot-dt-cpp-flags += $(CPPFLAGS) $(TF_CFLAGS_$(ARCH))
-        cot-dt-cpp-flags += -c -x assembler-with-cpp -E -P -o $@ $<
-
-        $(BUILD_PLAT)/$(COTDTPATH:.dtsi=.dts): $(COTDTPATH) | $$(@D)/
-		$(q)$($(ARCH)-cpp) $(cot-dt-cpp-flags)
-
-        $(BUILD_PLAT)/$(COTDTPATH:.dtsi=.c): $(BUILD_PLAT)/$(COTDTPATH:.dtsi=.dts) | $$(@D)/
+        $(BUILD_PLAT)/$(COTDTPATH:.dts=.c): $(BUILD_PLAT)/$(COTDTPATH) | $$(@D)/
 		$(if $(host-poetry),$(q)poetry -q install --no-root)
 		$(q)$(if $(host-poetry),poetry run )cot-dt2c convert-to-c $< $@
 
-        BL2_SOURCES += $(BUILD_PLAT)/$(COTDTPATH:.dtsi=.c)
+        BL2_SOURCES += $(BUILD_PLAT)/$(COTDTPATH:.dts=.c)
 endif