build(clang): introduce clang toolchain support

Only the compiler is switched to clang. The assembler and the
linker are provided  by the GCC toolchain.

clang is used to build TFTF when the base name of the path assigned
to  "CC" variable contains the string 'clang'.

'CROSS_COMPILE' flag is still required and must point to the
appropriate GCC toolchain.

*Note:
Compiling TF-A tests with clang toolchain has following
issues which are not listed/overlooked in GCC. Henceforth, few
sections of the code have been refactored accordingly:

1. error: instruction requires: wfxt
   fix : build with ARM_ARCH_MINOR=7.

2. error: initializer element is not a compile-time constant
   test_ffa_setup_and_discovery.c:33:11[.uuid = sp_uuids[0]]
   sp_test_ffa.c:38:11:.uuid = sp_uuids[0]

   fix: gcc overlooks this error but clang doesn't. Hence
   initializer elements are explicitly specified.

This is an introductory patch to cover the following targets.
1. tftf
2. ivy
3. realm
4. cactus
5. cactus_mm
6. ns_bl1u

Other tf-a tests targets need to be ported and will be
implemented explicitly  in separate patches.

In summary, with this patch, we will be able to compile
TFTF code covering the targets with the following command:

**********************************************************
* Build TFTF Aarch64 ( Default Build ) with Clang support
* tested with clang version: 14.0.0
**********************************************************
make CC=${CLANG_PATH}/clang \
PLAT=fvp \
CROSS_COMPILE=aarch64-none-elf- \
ARM_ARCH_MINOR=7 \
V=0 tftf ivy realm cactus cactus_mm ns_bl1u -j12
********************************************************

Signed-off-by: Jayanth Dodderi Chidanand <jayanthdodderi.chidanand@arm.com>
Change-Id: I409d11b596dfd50a3d25598679808dd9fcfc51e5
diff --git a/Makefile b/Makefile
index 97afc34..4896ba9 100644
--- a/Makefile
+++ b/Makefile
@@ -55,6 +55,19 @@
 endif
 export Q
 
+################################################################################
+# Toolchain configs
+################################################################################
+CC			:=	${CROSS_COMPILE}gcc
+CPP			:=	${CROSS_COMPILE}cpp
+AS			:=	${CROSS_COMPILE}gcc
+AR			:=	${CROSS_COMPILE}ar
+LD			:=	${CROSS_COMPILE}ld
+OC			:=	${CROSS_COMPILE}objcopy
+OD			:=	${CROSS_COMPILE}objdump
+NM			:=	${CROSS_COMPILE}nm
+PP			:=	${CROSS_COMPILE}gcc
+
 ifneq (${DEBUG}, 0)
 	BUILD_TYPE	:=	debug
 	# Use LOG_LEVEL_INFO by default for debug builds
@@ -182,7 +195,9 @@
 
 ################################################################################
 
+################################################################################
 # Assembler, compiler and linker flags shared across all test images.
+################################################################################
 COMMON_ASFLAGS		:=
 COMMON_CFLAGS		:=
 COMMON_LDFLAGS		:=
@@ -215,44 +230,69 @@
 $(info Arm Architecture Features specified: $(subst +, ,$(arch-features)))
 endif	# arch-features
 
-COMMON_ASFLAGS_aarch64	:=	-mgeneral-regs-only ${march64-directive}
-COMMON_CFLAGS_aarch64	:=	-mgeneral-regs-only -mstrict-align ${march64-directive}
+################################################################################
+# Compiler settings
+################################################################################
+ifneq ($(findstring clang,$(notdir $(CC))),)
+CLANG_CFLAGS_aarch64	:=	-target aarch64-elf
 
-COMMON_ASFLAGS_aarch32	:=	${march32-directive}
-COMMON_CFLAGS_aarch32	:=	${march32-directive} -mno-unaligned-access
+CPP			:=	$(CC) -E $(COMMON_CFLAGS_$(ARCH))
+PP			:=	$(CC) -E $(COMMON_CFLAGS_$(ARCH))
 
-COMMON_ASFLAGS		+=	-nostdinc -ffreestanding -Wa,--fatal-warnings	\
-				-Werror -Wmissing-include-dirs			\
-				-D__ASSEMBLY__ $(COMMON_ASFLAGS_$(ARCH))	\
-				${INCLUDES}
-COMMON_CFLAGS		+=	-nostdinc -ffreestanding -Wall	-Werror 	\
-				-Wmissing-include-dirs $(COMMON_CFLAGS_$(ARCH))	\
+CLANG_WARNINGS		+=	-nostdinc -ffreestanding -Wall	\
+				-Wmissing-include-dirs $(CLANG_CFLAGS_$(ARCH))	\
+				-Wlogical-op-parentheses \
+				-Wno-initializer-overrides \
+				-Wno-sometimes-uninitialized \
+				-Wno-unused-function \
+				-Wno-unused-variable \
+				-Wno-unused-parameter \
+				-Wno-tautological-compare \
+				-Wno-memset-transposed-args \
+				-Wno-parentheses
+
+CLANG_CFLAGS		+= 	-Wno-error=deprecated-declarations \
+				-Wno-error=cpp \
+				$(CLANG_WARNINGS)
+endif #(clang)
+
+ifneq ($(findstring gcc,$(notdir $(CC))),)
+GCC_CFLAGS_aarch32	:=	${march32-directive} -mno-unaligned-access
+GCC_CFLAGS_aarch64	:=	-mgeneral-regs-only
+
+GCC_ASFLAGS_aarch32	:=	${march32-directive}
+GCC_ASFLAGS_aarch64	:=	-mgeneral-regs-only ${march64-directive}
+
+GCC_WARNINGS		+=	-nostdinc -ffreestanding -Wall -Werror 	\
+				-Wmissing-include-dirs  $(GCC_CFLAGS_$(ARCH)) \
 				-std=gnu99 -Os
+
+# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
+GCC_CFLAGS		+=	$(call cc_option, --param=min-pagesize=0)
+GCC_CFLAGS		+= 	$(GCC_WARNINGS)
+endif #(gcc)
+
+COMMON_CFLAGS_aarch64	+=	${march64-directive} -mstrict-align \
+				$(CLANG_CFLAGS_$(ARCH)) $(GCC_CFLAGS_$(ARCH))
+
+COMMON_CFLAGS		+=	$(COMMON_CFLAGS_$(ARCH))
 COMMON_CFLAGS		+=	-ffunction-sections -fdata-sections
 
 # Get the content of CFLAGS user defined value last so they are appended after
 # the options defined in the Makefile
-COMMON_CFLAGS 		+=	${CFLAGS} ${INCLUDES}
+COMMON_CFLAGS 		+=	${CLANG_CFLAGS} ${GCC_CFLAGS} ${INCLUDES}
+
+COMMON_ASFLAGS		+=	-nostdinc -ffreestanding -Wa,--fatal-warnings	\
+				-Werror -Wmissing-include-dirs			\
+				-D__ASSEMBLY__ $(GCC_ASFLAGS_$(ARCH))	\
+				${INCLUDES}
 
 COMMON_LDFLAGS		+=	${LDFLAGS} --fatal-warnings -O1 --gc-sections --build-id=none
 
-CC			:=	${CROSS_COMPILE}gcc
-CPP			:=	${CROSS_COMPILE}cpp
-AS			:=	${CROSS_COMPILE}gcc
-AR			:=	${CROSS_COMPILE}ar
-LD			:=	${CROSS_COMPILE}ld
-OC			:=	${CROSS_COMPILE}objcopy
-OD			:=	${CROSS_COMPILE}objdump
-NM			:=	${CROSS_COMPILE}nm
-PP			:=	${CROSS_COMPILE}gcc
-
 # With ld.bfd version 2.39 and newer new warnings are added. Skip those since we
 # are not loaded by a elf loader.
 COMMON_LDFLAGS		+=	$(call ld_option, --no-warn-rwx-segments)
 
-# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
-COMMON_CFLAGS		+=	$(call cc_option, --param=min-pagesize=0)
-
 ################################################################################
 
 TFTF_SOURCES		:= ${FRAMEWORK_SOURCES}	${TESTS_SOURCES} ${PLAT_SOURCES} ${LIBC_SRCS} ${LIBFDT_SRCS}
@@ -590,6 +630,9 @@
 # The EL3 test payload is only supported in AArch64. It has an independent build
 # system.
 .PHONY: el3_payload
+# TODO: EL3 test payload currently is supported for GCC only. It has an independent
+# build system and support for Clang to be added.
+ifneq ($(findstring gcc,$(notdir $(CC))),)
 ifneq (${ARCH},aarch32)
 ifneq ($(wildcard ${EL3_PAYLOAD_PLAT_MAKEFILE_FULL}),)
 el3_payload: $(BUILD_DIR)
@@ -599,6 +642,7 @@
 all: el3_payload
 endif
 endif
+endif
 
 doc:
 	@echo "  BUILD DOCUMENTATION"
diff --git a/spm/common/sp_tests/sp_test_ffa.c b/spm/common/sp_tests/sp_test_ffa.c
index 614770e..73db187 100644
--- a/spm/common/sp_tests/sp_test_ffa.c
+++ b/spm/common/sp_tests/sp_test_ffa.c
@@ -34,7 +34,7 @@
 			       FFA_PARTITION_DIRECT_REQ_RECV |
 			       FFA_PARTITION_DIRECT_REQ_SEND |
 			       FFA_PARTITION_NOTIFICATION),
-		.uuid = sp_uuids[0]
+		.uuid = {PRIMARY_UUID}
 	},
 	/* Secondary partition info */
 	{
@@ -44,7 +44,7 @@
 			       FFA_PARTITION_DIRECT_REQ_RECV |
 			       FFA_PARTITION_DIRECT_REQ_SEND |
 			       FFA_PARTITION_NOTIFICATION),
-		.uuid = sp_uuids[1]
+		.uuid = {SECONDARY_UUID}
 	},
 	/* Tertiary partition info */
 	{
@@ -54,7 +54,7 @@
 			       FFA_PARTITION_DIRECT_REQ_RECV |
 			       FFA_PARTITION_DIRECT_REQ_SEND |
 			       FFA_PARTITION_NOTIFICATION),
-		.uuid = sp_uuids[2]
+		.uuid = {TERTIARY_UUID}
 	},
 	/* Ivy partition info */
 	{
@@ -63,7 +63,7 @@
 		.properties = (FFA_PARTITION_AARCH64_EXEC |
 			       FFA_PARTITION_DIRECT_REQ_RECV |
 			       FFA_PARTITION_DIRECT_REQ_SEND),
-		.uuid = sp_uuids[3]
+		.uuid = {IVY_UUID}
 	},
 	/* EL3 SPMD logical partition */
 	{
@@ -71,7 +71,7 @@
 		.exec_context = EL3_SPMD_LP_EXEC_CTX_COUNT,
 		.properties = (FFA_PARTITION_AARCH64_EXEC |
 			       FFA_PARTITION_DIRECT_REQ_SEND),
-		.uuid = sp_uuids[4]
+		.uuid = {EL3_SPMD_LP_UUID}
 	},
 };
 
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c b/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c
index 4b0df1c..80a3015 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c
@@ -31,7 +31,7 @@
 		.properties = FFA_PARTITION_AARCH64_EXEC |
 			      FFA_PARTITION_DIRECT_REQ_RECV |
 			      FFA_PARTITION_NOTIFICATION,
-		.uuid = sp_uuids[0]
+		.uuid = {PRIMARY_UUID}
 	},
 	/* Secondary partition info */
 	{
@@ -40,7 +40,7 @@
 		.properties = FFA_PARTITION_AARCH64_EXEC |
 			      FFA_PARTITION_DIRECT_REQ_RECV |
 			      FFA_PARTITION_NOTIFICATION,
-		.uuid = sp_uuids[1]
+		.uuid = {SECONDARY_UUID}
 	},
 	/* Tertiary partition info */
 	{
@@ -49,7 +49,7 @@
 		.properties = FFA_PARTITION_AARCH64_EXEC |
 			      FFA_PARTITION_DIRECT_REQ_RECV |
 			      FFA_PARTITION_NOTIFICATION,
-		.uuid = sp_uuids[2]
+		.uuid = {TERTIARY_UUID}
 	},
 	/* Ivy partition info */
 	{
@@ -57,7 +57,7 @@
 		.exec_context = IVY_EXEC_CTX_COUNT,
 		.properties = FFA_PARTITION_AARCH64_EXEC |
 			      FFA_PARTITION_DIRECT_REQ_RECV,
-		.uuid = sp_uuids[3]
+		.uuid = {IVY_UUID}
 	}
 };