Build: Perform NS interface build

Move NS interface build from Secure side build. Thus NS specific
flags can be configured in NS interface during NS build in further
improvement.
Split Secure test framework and NS test framework to support the
NS interface build.

Signed-off-by: David Hu <david.hu@arm.com>
Change-Id: I20a15e1d09b205970d259b080b40676cbb698e22
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 1a5b836..67d1617 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -17,6 +17,100 @@
     tfm_toolchain_reload_compiler()
 endif()
 
+# In actual NS integration, NS side build should include the source files
+# exported by TF-M build.
+# Directly include interface folder to simplify the NS build in this demo, since
+# install always occurs at the end of build.
+set(INTERFACE_SRC_DIR    ${CMAKE_SOURCE_DIR}/interface/src)
+set(INTERFACE_INC_DIR    ${CMAKE_SOURCE_DIR}/interface/include)
+
+#################### TF-M NS interface (header only) ###########################
+
+add_library(tfm_ns_interface INTERFACE)
+
+target_include_directories(tfm_ns_interface
+    INTERFACE
+        ${INTERFACE_INC_DIR}
+        ${CMAKE_BINARY_DIR}/generated/interface/include
+        ${INTERFACE_INC_DIR}/os_wrapper
+)
+
+# PSA interface files are generated from a template
+add_dependencies(tfm_ns_interface
+    tfm_generated_files
+)
+
+# Include selection of Secure Partitions from TF-M build.
+# It can be replaced by NS side configurations later.
+target_link_libraries(tfm_ns_interface
+    INTERFACE
+        tfm_partition_defs
+)
+
+target_compile_definitions(tfm_ns_interface
+    INTERFACE
+        $<$<BOOL:${TFM_PSA_API}>:TFM_PSA_API>
+        $<$<STREQUAL:${TEST_PSA_API},IPC>:PSA_API_TEST_IPC>
+        $<$<BOOL:${TFM_NS_CLIENT_IDENTIFICATION}>:TFM_NS_CLIENT_IDENTIFICATION>
+        $<$<BOOL:${TFM_MULTI_CORE_TOPOLOGY}>:TFM_MULTI_CORE_TOPOLOGY>
+        $<$<BOOL:${TFM_MULTI_CORE_MULTI_CLIENT_CALL}>:TFM_MULTI_CORE_MULTI_CLIENT_CALL>
+        $<$<BOOL:${FORWARD_PROT_MSG}>:FORWARD_PROT_MSG>
+)
+
+###################### TF-M NS interface api (NS lib) ##########################
+
+add_library(tfm_api_ns STATIC)
+
+target_sources(tfm_api_ns
+    PRIVATE
+        $<$<BOOL:${TFM_NS_CLIENT_IDENTIFICATION}>:${INTERFACE_SRC_DIR}/tfm_nspm_svc_handler.c>
+        $<$<BOOL:${TFM_NS_CLIENT_IDENTIFICATION}>:${INTERFACE_SRC_DIR}/tfm_nspm_api.c>
+)
+
+if (${TFM_PSA_API})
+    target_sources(tfm_api_ns PRIVATE
+        $<$<OR:$<BOOL:{$FORWARD_PROT_MSG}>,$<BOOL:${TFM_PARTITION_PLATFORM}>>:${INTERFACE_SRC_DIR}/tfm_platform_ipc_api.c>
+        $<$<OR:$<BOOL:{$FORWARD_PROT_MSG}>,$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>>:${INTERFACE_SRC_DIR}/tfm_ps_ipc_api.c>
+        $<$<OR:$<BOOL:{$FORWARD_PROT_MSG}>,$<BOOL:${TFM_PARTITION_INTERNAL_TRUSTED_STORAGE}>>:${INTERFACE_SRC_DIR}/tfm_its_ipc_api.c>
+        $<$<OR:$<BOOL:{$FORWARD_PROT_MSG}>,$<BOOL:${TFM_PARTITION_CRYPTO}>>:${INTERFACE_SRC_DIR}/tfm_crypto_ipc_api.c>
+        $<$<OR:$<BOOL:{$FORWARD_PROT_MSG}>,$<BOOL:${TFM_PARTITION_INITIAL_ATTESTATION}>>:${INTERFACE_SRC_DIR}/tfm_initial_attestation_ipc_api.c>
+    )
+
+    if (TFM_MULTI_CORE_TOPOLOGY)
+        target_sources(tfm_api_ns PRIVATE
+            ${INTERFACE_SRC_DIR}/tfm_ns_mailbox.c
+            ${INTERFACE_SRC_DIR}/tfm_multi_core_api.c
+            ${INTERFACE_SRC_DIR}/tfm_multi_core_psa_ns_api.c
+        )
+    else()
+        target_sources(tfm_api_ns PRIVATE
+           ${INTERFACE_SRC_DIR}/tfm_ns_interface.c
+           ${INTERFACE_SRC_DIR}/tfm_psa_ns_api.c
+        )
+    endif()
+else()
+    target_sources(tfm_api_ns PRIVATE
+        $<$<BOOL:${TFM_PARTITION_PLATFORM}>:${INTERFACE_SRC_DIR}/tfm_platform_func_api.c>
+        $<$<BOOL:${TFM_PARTITION_AUDIT_LOG}>:${INTERFACE_SRC_DIR}/tfm_audit_func_api.c>
+        $<$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>:${INTERFACE_SRC_DIR}/tfm_ps_func_api.c>
+        $<$<BOOL:${TFM_PARTITION_INTERNAL_TRUSTED_STORAGE}>:${INTERFACE_SRC_DIR}/tfm_its_func_api.c>
+        $<$<BOOL:${TFM_PARTITION_CRYPTO}>:${INTERFACE_SRC_DIR}/tfm_crypto_func_api.c>
+        $<$<BOOL:${TFM_PARTITION_INITIAL_ATTESTATION}>:${INTERFACE_SRC_DIR}/tfm_initial_attestation_func_api.c>
+        ${INTERFACE_SRC_DIR}/tfm_psa_ns_api.c
+        ${INTERFACE_SRC_DIR}/tfm_ns_interface.c
+    )
+endif()
+
+target_link_libraries(tfm_api_ns
+    PUBLIC
+        tfm_ns_interface
+    PRIVATE
+        platform_ns
+        # CMSIS is currently only required to provide the NS client IDs. In
+        # future, this should probably be made more OS agnostic
+        $<$<BOOL:${TFM_NS_CLIENT_IDENTIFICATION}>:CMSIS_5_tfm_ns>
+)
+
 ############################# PSA test integration #############################
 
 if(TEST_PSA_API AND NOT PSA_ARCH_TESTS_BINARY_PATH)
@@ -38,7 +132,7 @@
 
 
     if(NOT PSA_INCLUDE_PATHS)
-        set(PSA_INCLUDE_PATHS ${CMAKE_SOURCE_DIR}/interface/include/
+        set(PSA_INCLUDE_PATHS ${EXPORT_INC_DIR}/
                               ${CMAKE_BINARY_DIR}/generated/api-tests/platform/manifests/
                               ${CMAKE_BINARY_DIR}/generated/interface/include
         )
@@ -89,10 +183,10 @@
 target_link_libraries(tfm_ns_integration_test
     PUBLIC
         $<$<BOOL:${TEST_NS}>:tfm_ns_tests>
-        tfm_test_framework
+        tfm_test_framework_ns
     PRIVATE
-        psa_interface
-        psa_api_ns
+        tfm_ns_interface
+        tfm_api_ns
         CMSIS_5_tfm_ns
 )
 
@@ -121,7 +215,7 @@
         $<$<BOOL:${TEST_PSA_API}>:pal_nspe>
         $<$<BOOL:${TEST_PSA_API}>:test_combine>
         $<$<NOT:$<BOOL:${TFM_MULTI_CORE_TOPOLOGY}>>:tfm_s_veneers>
-        psa_api_ns
+        tfm_api_ns
         tfm_ns_log
 )
 
diff --git a/log/CMakeLists.txt b/log/CMakeLists.txt
index 1993428..ab6e179 100644
--- a/log/CMakeLists.txt
+++ b/log/CMakeLists.txt
@@ -32,3 +32,22 @@
     PRIVATE
         platform_ns
 )
+
+################################ S Log #########################################
+add_library(tfm_s_log STATIC EXCLUDE_FROM_ALL)
+
+target_sources(tfm_s_log
+    PRIVATE
+        ${CMAKE_SOURCE_DIR}/interface/src/log/tfm_log_raw.c
+)
+
+target_include_directories(tfm_s_log
+    PUBLIC
+        ${CMAKE_SOURCE_DIR}/interface/include/
+        ${CMAKE_SOURCE_DIR}/interface/include/log
+)
+
+target_link_libraries(tfm_s_log
+    PRIVATE
+        platform_s
+)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 9499398..5f8c614 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -8,7 +8,8 @@
 cmake_minimum_required(VERSION 3.13)
 
 add_library(tfm_ns_tests INTERFACE)
-add_library(tfm_test_framework INTERFACE)
+add_library(tfm_test_framework_ns INTERFACE)
+add_library(tfm_test_framework_s INTERFACE)
 
 # For multi-core projects, the NS app can be run on a different CPU to the
 # Secure code. To facilitate this, we once again reload the compiler to load the
@@ -37,21 +38,24 @@
     add_subdirectory(suites/multi_core)
 endif()
 
-target_sources(tfm_test_framework
+add_library(tfm_test_framework_common INTERFACE)
+
+target_sources(tfm_test_framework_common
     INTERFACE
     ${CMAKE_CURRENT_SOURCE_DIR}/framework/test_framework.c
     ${CMAKE_CURRENT_SOURCE_DIR}/framework/test_framework_helpers.c
     ${CMAKE_CURRENT_SOURCE_DIR}/framework/test_framework_integ_test_helper.c
 )
 
-target_include_directories(tfm_test_framework
+target_include_directories(tfm_test_framework_common
     INTERFACE
         framework
 )
 
-target_link_libraries(tfm_test_framework
+target_link_libraries(tfm_test_framework_ns
     INTERFACE
-        psa_interface
+        tfm_test_framework_common
+        tfm_ns_interface
         tfm_ns_log
 )
 
@@ -62,7 +66,7 @@
 
 target_link_libraries(tfm_ns_tests
     INTERFACE
-        tfm_test_framework
+        tfm_test_framework_ns
         tfm_partition_defs
 )
 
@@ -75,6 +79,13 @@
 
 ####################### Secure #################################################
 
+target_link_libraries(tfm_test_framework_s
+    INTERFACE
+        psa_interface
+        tfm_test_framework_common
+        tfm_s_log
+)
+
 if (TEST_S)
     target_sources(tfm_s_tests
         INTERFACE
@@ -83,7 +94,7 @@
 
     target_link_libraries(tfm_s_tests
         INTERFACE
-            tfm_test_framework
+            tfm_test_framework_s
             tfm_partition_defs
             tfm_spm
     )
diff --git a/test/suites/attestation/CMakeLists.txt b/test/suites/attestation/CMakeLists.txt
index f5ab79f..60912d7 100644
--- a/test/suites/attestation/CMakeLists.txt
+++ b/test/suites/attestation/CMakeLists.txt
@@ -44,7 +44,7 @@
     PRIVATE
         tfm_qcbor
         tfm_t_cose
-        tfm_test_framework
+        tfm_test_framework_ns
         #TODO I'm not sure this should _really_ be linked here
         tfm_attestation_defs
         tfm_boot_status
@@ -87,7 +87,7 @@
         PRIVATE
             tfm_qcbor
             tfm_t_cose
-            tfm_test_framework
+            tfm_test_framework_s
             tfm_attestation_defs
             tfm_boot_status
             $<$<NOT:$<BOOL:${FORWARD_PROT_MSG}>>:tfm_partition_crypto>
diff --git a/test/suites/audit/CMakeLists.txt b/test/suites/audit/CMakeLists.txt
index ddc1555..82ebc7b 100644
--- a/test/suites/audit/CMakeLists.txt
+++ b/test/suites/audit/CMakeLists.txt
@@ -38,7 +38,7 @@
 
 target_link_libraries(tfm_test_suite_audit_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
         tfm_audit_logging_defs
 )
 
@@ -62,7 +62,7 @@
 
     target_link_libraries(tfm_test_suite_audit_s
         PRIVATE
-            tfm_test_framework
+            tfm_test_framework_s
             tfm_partition_audit
     )
 
diff --git a/test/suites/core/CMakeLists.txt b/test/suites/core/CMakeLists.txt
index 0e0832d..fc76498 100644
--- a/test/suites/core/CMakeLists.txt
+++ b/test/suites/core/CMakeLists.txt
@@ -32,12 +32,13 @@
 
 target_link_libraries(tfm_test_suite_core_ns
     PRIVATE
-        psa_api_ns
-        tfm_test_framework
+        tfm_api_ns
+        tfm_test_framework_ns
         tfm_ns_integration_test
         CMSIS_5_tfm_ns
         tfm_partition_defs
         platform_ns
+        tfm_partition_core_test_defs
 )
 
 
diff --git a/test/suites/crypto/CMakeLists.txt b/test/suites/crypto/CMakeLists.txt
index 3c19fd9..5568c47 100644
--- a/test/suites/crypto/CMakeLists.txt
+++ b/test/suites/crypto/CMakeLists.txt
@@ -28,7 +28,7 @@
 
 target_link_libraries(tfm_test_suite_crypto_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
 )
 
 target_link_libraries(tfm_ns_tests
@@ -65,7 +65,7 @@
     target_link_libraries(tfm_test_suite_crypto_s
         PRIVATE
             tfm_secure_api
-            tfm_test_framework
+            tfm_test_framework_s
             tfm_partition_secure_client_2
             platform_s
     )
diff --git a/test/suites/ipc/CMakeLists.txt b/test/suites/ipc/CMakeLists.txt
index 9551d4d..a61b1d8 100644
--- a/test/suites/ipc/CMakeLists.txt
+++ b/test/suites/ipc/CMakeLists.txt
@@ -32,7 +32,7 @@
 
 target_link_libraries(tfm_test_suite_ipc_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
 )
 
 target_link_libraries(tfm_ns_tests
@@ -55,7 +55,7 @@
 
     target_link_libraries(tfm_test_suite_ipc_s
         PRIVATE
-            tfm_test_framework
+            tfm_test_framework_s
     )
 
     target_link_libraries(tfm_s_tests
diff --git a/test/suites/its/CMakeLists.txt b/test/suites/its/CMakeLists.txt
index e290020..f8d43b7 100644
--- a/test/suites/its/CMakeLists.txt
+++ b/test/suites/its/CMakeLists.txt
@@ -34,7 +34,7 @@
 
 target_link_libraries(tfm_test_suite_its_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
         CMSIS_5_tfm_ns
         platform_region_defs
 )
@@ -67,7 +67,7 @@
     target_link_libraries(tfm_test_suite_its_s
         PRIVATE
             tfm_secure_api
-            tfm_test_framework
+            tfm_test_framework_s
             platform_region_defs
             tfm_partition_secure_client_2
             platform_s
diff --git a/test/suites/multi_core/CMakeLists.txt b/test/suites/multi_core/CMakeLists.txt
index 3751674..7c32112 100644
--- a/test/suites/multi_core/CMakeLists.txt
+++ b/test/suites/multi_core/CMakeLists.txt
@@ -30,8 +30,8 @@
 
 target_link_libraries(tfm_test_suite_multi_core_ns
     PRIVATE
-        tfm_test_framework
-        psa_api_ns
+        tfm_test_framework_ns
+        tfm_api_ns
 )
 
 target_link_libraries(tfm_ns_tests
@@ -39,7 +39,7 @@
         tfm_test_suite_multi_core_ns
 )
 
-target_compile_definitions(psa_interface
+target_compile_definitions(tfm_ns_interface
     INTERFACE
         TFM_MULTI_CORE_TEST
 )
diff --git a/test/suites/platform/CMakeLists.txt b/test/suites/platform/CMakeLists.txt
index faee01d..4f38987 100644
--- a/test/suites/platform/CMakeLists.txt
+++ b/test/suites/platform/CMakeLists.txt
@@ -30,7 +30,7 @@
 
 target_link_libraries(tfm_test_suite_platform_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
 )
 
 target_link_libraries(tfm_ns_tests
@@ -56,7 +56,7 @@
 
     target_link_libraries(tfm_test_suite_platform_s
         PRIVATE
-            tfm_test_framework
+            tfm_test_framework_s
             tfm_secure_api
             platform_s
             tfm_sprt
diff --git a/test/suites/ps/CMakeLists.txt b/test/suites/ps/CMakeLists.txt
index 811badf..af2d9eb 100644
--- a/test/suites/ps/CMakeLists.txt
+++ b/test/suites/ps/CMakeLists.txt
@@ -34,10 +34,10 @@
 
 target_link_libraries(tfm_test_suite_ps_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
         platform_region_defs
         tfm_test_suite_its_ns
-        psa_api_ns
+        tfm_api_ns
 )
 
 target_link_libraries(tfm_ns_tests
@@ -70,7 +70,7 @@
 
     target_link_libraries(tfm_test_suite_ps_s
         PRIVATE
-            tfm_test_framework
+            tfm_test_framework_s
             platform_region_defs
             tfm_partition_ps
             $<$<NOT:$<BOOL:${FORWARD_PROT_MSG}>>:tfm_partition_ps_test>
diff --git a/test/suites/qcbor/CMakeLists.txt b/test/suites/qcbor/CMakeLists.txt
index 0515dab..417c7a4 100644
--- a/test/suites/qcbor/CMakeLists.txt
+++ b/test/suites/qcbor/CMakeLists.txt
@@ -28,7 +28,7 @@
 
 target_link_libraries(tfm_test_suite_qcbor_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
         tfm_qcbor_test
 )
 
diff --git a/test/suites/t_cose/CMakeLists.txt b/test/suites/t_cose/CMakeLists.txt
index 66d7e62..48e0be1 100644
--- a/test/suites/t_cose/CMakeLists.txt
+++ b/test/suites/t_cose/CMakeLists.txt
@@ -32,7 +32,7 @@
 
 target_link_libraries(tfm_test_suite_t_cose_ns
     PRIVATE
-        tfm_test_framework
+        tfm_test_framework_ns
         tfm_t_cose_test
 )
 
diff --git a/test/test_services/tfm_core_test/CMakeLists.txt b/test/test_services/tfm_core_test/CMakeLists.txt
index 8cce0c3..be0700e 100644
--- a/test/test_services/tfm_core_test/CMakeLists.txt
+++ b/test/test_services/tfm_core_test/CMakeLists.txt
@@ -26,7 +26,7 @@
         psa_interface
         platform_s
         tfm_arch
-        tfm_test_framework
+        tfm_test_framework_s
 )
 
 target_link_libraries(tfm_spm
@@ -34,7 +34,15 @@
         tfm_partition_core_test
 )
 
-target_include_directories(tfm_test_framework
+target_include_directories(tfm_test_framework_s
+    INTERFACE
+        .
+)
+
+# header files included by other libraries
+add_library(tfm_partition_core_test_defs INTERFACE)
+
+target_include_directories(tfm_partition_core_test_defs
     INTERFACE
         .
 )
diff --git a/test/test_services/tfm_ipc_service/CMakeLists.txt b/test/test_services/tfm_ipc_service/CMakeLists.txt
index db4a127..2a450f3 100644
--- a/test/test_services/tfm_ipc_service/CMakeLists.txt
+++ b/test/test_services/tfm_ipc_service/CMakeLists.txt
@@ -25,7 +25,7 @@
     PRIVATE
         tfm_secure_api
         platform_s
-        tfm_test_framework
+        tfm_test_framework_s
         tfm_arch
 )
 
diff --git a/test/test_services/tfm_irq_test_service_1/CMakeLists.txt b/test/test_services/tfm_irq_test_service_1/CMakeLists.txt
index f6a7c27..507e90f 100644
--- a/test/test_services/tfm_irq_test_service_1/CMakeLists.txt
+++ b/test/test_services/tfm_irq_test_service_1/CMakeLists.txt
@@ -25,7 +25,7 @@
         tfm_secure_api
         psa_interface
         platform_s
-        tfm_test_framework
+        tfm_test_framework_s
 )
 
 target_link_libraries(tfm_spm
@@ -33,7 +33,7 @@
         tfm_partition_irq_test_1
 )
 
-target_include_directories(tfm_test_framework
+target_include_directories(tfm_test_framework_s
     INTERFACE
         .
 )
diff --git a/test/test_services/tfm_secure_client_service/CMakeLists.txt b/test/test_services/tfm_secure_client_service/CMakeLists.txt
index d9f67f6..6dae0c6 100644
--- a/test/test_services/tfm_secure_client_service/CMakeLists.txt
+++ b/test/test_services/tfm_secure_client_service/CMakeLists.txt
@@ -26,7 +26,7 @@
         tfm_secure_api
         psa_interface
         platform_s
-        tfm_test_framework
+        tfm_test_framework_s
         tfm_s_tests
 )
 
@@ -37,7 +37,7 @@
 
 ############################ NS API ############################################
 
-target_sources(psa_api_ns
+target_sources(tfm_api_ns
     PRIVATE
         ${CMAKE_CURRENT_SOURCE_DIR}/tfm_secure_client_service_api.c
 )
@@ -49,7 +49,7 @@
         COMPILE_FLAGS -Wno-implicit-function-declaration
 )
 
-target_include_directories(psa_api_ns
+target_include_directories(tfm_api_ns
     PUBLIC
         .
 )