Build: Refine psa_arch_tests configuration

- Now command line options can be passed to SPE build.
- The TARGET and CPU_ARCH can be deduced from TF-M configs.
- Align with the new test config setup approach.
- Other improvements and fixes.

Change-Id: Ic29849526f75dc4395a5599374356342463b61db
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/tests_psa_arch/CMakeLists.txt b/tests_psa_arch/CMakeLists.txt
index 5720128..dc4f2e2 100644
--- a/tests_psa_arch/CMakeLists.txt
+++ b/tests_psa_arch/CMakeLists.txt
@@ -29,16 +29,36 @@
 include(spe_config)
 include(${CONFIG_SPE_PATH}/platform/config.cmake)
 
-set(CPU_ARCH ${PSA_API_TEST_CPU_ARCH})
+#--- NSPE side project ---------------------------------------------------------
+
+# Setup required configs: SUITE, CPU_ARCH and TARGET
 set(SUITE    ${TEST_PSA_API})
 
-if (NOT "${TEST_PSA_API}" STREQUAL "IPC")
-    set(TARGET tgt_dev_apis_tfm_${PSA_API_TEST_TARGET})
-else()
-    set(TARGET tgt_ff_tfm_${PSA_API_TEST_TARGET})
+if (NOT DEFINED CPU_ARCH)
+    if (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv8-m.main)
+        set(CPU_ARCH armv8m_ml)
+    elseif (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv8-m.base)
+        set(CPU_ARCH armv8m_bl)
+    elseif (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv8.1-m.main)
+        set(CPU_ARCH armv81m_ml)
+    elseif (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv7-m)
+        set(CPU_ARCH armv7m)
+    endif()
 endif()
 
-#--- NSPE side project ---------------------------------------------------------
+if (NOT DEFINED PSA_API_TEST_TARGET)
+    string(REGEX REPLACE ".*/" "" PSA_API_TEST_TARGET ${TFM_PLATFORM})
+endif()
+
+if(NOT SP_HEAP_MEM_SUPP)
+    set(SP_HEAP_MEM_SUPP 0)
+endif()
+
+if ("${TEST_PSA_API}" STREQUAL "IPC")
+    set(TARGET tgt_ff_tfm_${PSA_API_TEST_TARGET})
+else()
+    set(TARGET tgt_dev_apis_tfm_${PSA_API_TEST_TARGET})
+endif()
 
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/../cmake)
 include(toolchain_selection)
@@ -49,6 +69,7 @@
 
 add_executable(tfm_ns)
 
+add_subdirectory(../lib/ext ${CMAKE_BINARY_DIR}/lib/ext)
 add_subdirectory(../app_broker ${CMAKE_BINARY_DIR}/app_broker)
 
 list(APPEND PSA_INCLUDE_PATHS ${CONFIG_SPE_PATH}/interface/include)
diff --git a/tests_psa_arch/spe/CMakeLists.txt b/tests_psa_arch/spe/CMakeLists.txt
index c0f47b8..42a3a88 100644
--- a/tests_psa_arch/spe/CMakeLists.txt
+++ b/tests_psa_arch/spe/CMakeLists.txt
@@ -6,6 +6,9 @@
 #-------------------------------------------------------------------------------
 cmake_minimum_required(VERSION 3.15)
 
+list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../cmake)
+include(utils)
+
 # Force cross compilation and avoid compiler search and test
 set(CMAKE_SYSTEM_NAME Generic)
 set(CMAKE_C_COMPILER_FORCED TRUE)
@@ -30,14 +33,15 @@
     add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../fetch_repo ${CMAKE_BINARY_DIR}/fetch_repo)
 endif()
 
-if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)
-    set(CROSS_COMPILE         arm-none-eabi CACHE STRING "Cross-compiler prefix")
-    set(CMAKE_TOOLCHAIN_FILE  ${CONFIG_TFM_SOURCE_PATH}/toolchain_GNUARM.cmake)
-endif()
+collect_build_cmd_args(TFM_CMDLINE_CONFIGS)
 
-configure_file(${CMAKE_SOURCE_DIR}/tfm_psa_ff_test_manifest_list.yaml
-               ${CMAKE_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
-list(APPEND TFM_EXTRA_MANIFEST_LIST_FILES ${CMAKE_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
+if ("${TEST_PSA_API}" STREQUAL "IPC")
+    # Workaround: amend IPC backend config to command-line options so that users (including CI)
+    # do not need to set backend for FF tests.
+    # This should be done in the config_test_psa_api.cmake but it is included by TF-M too late.
+    # So setting backend in it does not take effect.
+    set(TFM_CMDLINE_CONFIGS "${TFM_CMDLINE_CONFIGS};-DCONFIG_TFM_SPM_BACKEND:UNINITIALIZED=IPC")
+endif()
 
 #------------------------------------------------------------------------------
 
@@ -49,14 +53,11 @@
     SOURCE_DIR        ${CONFIG_TFM_SOURCE_PATH}
     BINARY_DIR        build-spe
     INSTALL_DIR       api_ns
-    CMAKE_ARGS        -DTFM_PLATFORM=${TFM_PLATFORM}
-    CMAKE_ARGS        -DTEST_PSA_API=${TEST_PSA_API}
-    CMAKE_ARGS        -DTFM_EXTRA_CONFIG_PATH=${CMAKE_SOURCE_DIR}/config_spe.cmake
-    CMAKE_ARGS        -DPROJECT_CONFIG_HEADER_FILE=${CMAKE_SOURCE_DIR}/config_test_psa_api.h
     CMAKE_ARGS        -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-    CMAKE_ARGS        -DTFM_EXTRA_MANIFEST_LIST_FILES=${TFM_EXTRA_MANIFEST_LIST_FILES}
     CMAKE_ARGS        -DPSA_ARCH_TESTS_PATH=${PSA_ARCH_TESTS_PATH}
     CMAKE_ARGS        -DCONFIG_TFM_TEST_DIR=${CMAKE_SOURCE_DIR}/partitions
+    CMAKE_ARGS        -DCONFIG_TFM_TEST_CONFIG_FILE=${CMAKE_CURRENT_LIST_DIR}/config/config_test_psa_api.cmake
+    CMAKE_CACHE_DEFAULT_ARGS ${TFM_CMDLINE_CONFIGS}
     PREFIX             "temp"
 )
 
diff --git a/tests_psa_arch/spe/config_spe.cmake b/tests_psa_arch/spe/config/config_test_psa_api.cmake
similarity index 69%
rename from tests_psa_arch/spe/config_spe.cmake
rename to tests_psa_arch/spe/config/config_test_psa_api.cmake
index bc147c6..d46a912 100644
--- a/tests_psa_arch/spe/config_spe.cmake
+++ b/tests_psa_arch/spe/config/config_test_psa_api.cmake
@@ -4,24 +4,13 @@
 # SPDX-License-Identifier: BSD-3-Clause
 #
 #-------------------------------------------------------------------------------
-#
-# This is a configuration files for building SPE, necessary for PSA API Arch tests.
-# It shall be passed to SPE build via TFM_EXTRA_CONFIG_PATH option.
-#-------------------------------------------------------------------------------
-
-# Do not forget to specify configuration file While building SPE via:
-# -DPROJECT_CONFIG_HEADER_FILE=<>/config_test_psa_api.h
-
-set(TEST_S                  OFF      CACHE BOOL      "Whether to build S regression tests")
-set(TFM_S_REG_TEST          OFF      CACHE BOOL      "Enable S regression test")
-set(NS                      OFF      CACHE BOOL      "Enalbe NS side build")
 
 if ("${TEST_PSA_API}" STREQUAL "IPC")
-    # PSA Arch test partitions only support IPC model so far
     set(CONFIG_TFM_SPM_BACKEND      "IPC"       CACHE STRING    "The SPM backend [IPC, SFN]")
-    set(TFM_PARTITION_FF_TEST   ON)
-else()
-    set(TFM_PARTITION_FF_TEST   OFF)
+
+    configure_file(${CMAKE_CURRENT_LIST_DIR}/../tfm_psa_ff_test_manifest_list.yaml
+                   ${CMAKE_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
+    list(APPEND TFM_EXTRA_MANIFEST_LIST_FILES ${CMAKE_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
 endif()
 
 if ("${TEST_PSA_API}" STREQUAL "INITIAL_ATTESTATION")
@@ -43,3 +32,10 @@
     set(TFM_PARTITION_CRYPTO                   ON       CACHE BOOL      "Enable Crypto partition")
     set(TFM_PARTITION_PLATFORM                 ON       CACHE BOOL      "Enable Platform partition")
 endif()
+
+set(PROJECT_CONFIG_HEADER_FILE
+    ${CMAKE_CURRENT_LIST_DIR}/../config/config_test_psa_api.h
+    CACHE FILEPATH "The psa-arch-test config header file")
+
+# Set default value for INCLUDE_PANIC_TESTS explicitly
+set(INCLUDE_PANIC_TESTS     0   CACHE BOOL      "Include panic tests")
diff --git a/tests_psa_arch/spe/config_test_psa_api.h b/tests_psa_arch/spe/config/config_test_psa_api.h
similarity index 100%
rename from tests_psa_arch/spe/config_test_psa_api.h
rename to tests_psa_arch/spe/config/config_test_psa_api.h
diff --git a/tests_psa_arch/spe/partitions/CMakeLists.txt b/tests_psa_arch/spe/partitions/CMakeLists.txt
index 657fd33..c51f450 100644
--- a/tests_psa_arch/spe/partitions/CMakeLists.txt
+++ b/tests_psa_arch/spe/partitions/CMakeLists.txt
@@ -11,10 +11,23 @@
     return()
 endif()
 
-set(SP_HEAP_MEM_SUPP 0)
+if (NOT DEFINED CPU_ARCH)
+    if (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv8-m.main)
+        set(CPU_ARCH armv8m_ml)
+    elseif (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv8-m.base)
+        set(CPU_ARCH armv8m_bl)
+    elseif (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv8.1-m.main)
+        set(CPU_ARCH armv81m_ml)
+    elseif (${TFM_SYSTEM_ARCHITECTURE} STREQUAL armv7-m)
+        set(CPU_ARCH armv7m)
+    endif()
+endif()
+
+if (NOT DEFINED PSA_API_TEST_TARGET)
+    string(REGEX REPLACE ".*/" "" PSA_API_TEST_TARGET ${TFM_PLATFORM})
+endif()
 
 set(TARGET tgt_ff_tfm_${PSA_API_TEST_TARGET})
-set(CPU_ARCH ${PSA_API_TEST_CPU_ARCH})
 set(SUITE    ${TEST_PSA_API})
 set(PLATFORM_PSA_ISOLATION_LEVEL ${TFM_ISOLATION_LEVEL})
 
diff --git a/tests_psa_arch/spe/tfm_psa_ff_test_manifest_list.yaml b/tests_psa_arch/spe/tfm_psa_ff_test_manifest_list.yaml
index 1151b9e..b9965d0 100644
--- a/tests_psa_arch/spe/tfm_psa_ff_test_manifest_list.yaml
+++ b/tests_psa_arch/spe/tfm_psa_ff_test_manifest_list.yaml
@@ -17,7 +17,6 @@
       "description": "PSA FF Test Client Partition",
       "manifest": "${PSA_ARCH_TESTS_PATH}/api-tests/platform/manifests/client_partition_psa.json",
       "output_path": "api-tests/platform/manifests",
-      "conditional": "TFM_PARTITION_FF_TEST",
       "version_major": 0,
       "version_minor": 1,
       "pid": 5000,
@@ -34,7 +33,6 @@
       "description": "PSA FF Test Server Partition",
       "manifest": "${PSA_ARCH_TESTS_PATH}/api-tests/platform/manifests/server_partition_psa.json",
       "output_path": "api-tests/platform/manifests",
-      "conditional": "TFM_PARTITION_FF_TEST",
       "version_major": 0,
       "version_minor": 1,
       "pid": 5001,
@@ -51,7 +49,6 @@
       "description": "PSA FF Test Driver Partition",
       "manifest": "${PSA_ARCH_TESTS_PATH}/api-tests/platform/manifests/driver_partition_psa.json",
       "output_path": "api-tests/platform/manifests",
-      "conditional": "TFM_PARTITION_FF_TEST",
       "version_major": 0,
       "version_minor": 1,
       "pid": 5002,