Tools: Allow file generation without cmake

Alter file generation to pick up variables from the environment, as well
as cmake, to allow integration into environments that don't run the
cmake.

Change-Id: Iac921ba69a01459a5959828e3825cd8e901e3a96
Signed-off-by: Raef Coles <raef.coles@arm.com>
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 381a9f1..7accf48 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -10,15 +10,15 @@
 
 ############################### Manifest declaration ###########################
 
-configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tfm_manifest_list.yaml.in
-               ${CMAKE_CURRENT_BINARY_DIR}/tfm_manifest_list.yaml @ONLY)
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tfm_manifest_list.yaml
+               ${CMAKE_CURRENT_BINARY_DIR}/tfm_manifest_list.yaml)
 
 set(MANIFEST_LISTS ${CMAKE_CURRENT_BINARY_DIR}/tfm_manifest_list.yaml)
 set(MANIFEST_LISTS ${MANIFEST_LISTS} ${TFM_EXTRA_MANIFEST_LIST_PATH})
 
 if ("${TEST_PSA_API}" STREQUAL "IPC")
-    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tfm_psa_ff_test_manifest_list.yaml.in
-                   ${CMAKE_CURRENT_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml @ONLY)
+    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tfm_psa_ff_test_manifest_list.yaml
+                   ${CMAKE_CURRENT_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
 
     set(MANIFEST_LISTS ${MANIFEST_LISTS} ${CMAKE_CURRENT_BINARY_DIR}/tfm_psa_ff_test_manifest_list.yaml)
 endif()
diff --git a/tools/tfm_manifest_list.yaml.in b/tools/tfm_manifest_list.yaml
similarity index 84%
rename from tools/tfm_manifest_list.yaml.in
rename to tools/tfm_manifest_list.yaml
index 7751e58..82dcdd7 100644
--- a/tools/tfm_manifest_list.yaml.in
+++ b/tools/tfm_manifest_list.yaml
@@ -107,8 +107,8 @@
     {
       "name": "TFM Core Test Service",
       "short_name": "TFM_SP_CORE_TEST",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_core_test/tfm_ss_core_test.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_core_test/tfm_ss_core_test.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_CORE",
       "version_major": 0,
@@ -123,8 +123,8 @@
     {
       "name": "TFM Core Test Service 2",
       "short_name": "TFM_SP_CORE_TEST_2",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_core_test_2/tfm_ss_core_test_2.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_core_test_2/tfm_ss_core_test_2.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_CORE",
       "version_major": 0,
@@ -139,8 +139,8 @@
     {
       "name": "TFM Secure Client Service",
       "short_name": "TFM_SP_SECURE_TEST_PARTITION",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_secure_client_service/tfm_secure_client_service.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_secure_client_service/tfm_secure_client_service.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_SECURE_SERVICES",
       "version_major": 0,
@@ -163,8 +163,8 @@
     {
       "name": "TFM IPC Service Test",
       "short_name": "TFM_SP_IPC_SERVICE_TEST",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_ipc_service/tfm_ipc_service_test.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_ipc_service/tfm_ipc_service_test.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_CORE_IPC",
       "version_major": 0,
@@ -179,8 +179,8 @@
     {
       "name": "TFM IPC Client Service",
       "short_name": "TFM_SP_IPC_CLIENT_TEST",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_ipc_client/tfm_ipc_client_test.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_ipc_client/tfm_ipc_client_test.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_CORE_IPC",
       "version_major": 0,
@@ -195,8 +195,8 @@
     {
       "name": "TFM IRQ Test Service 1",
       "short_name": "TFM_IRQ_TEST_1",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_irq_test_service_1/tfm_irq_test_service_1.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_irq_test_service_1/tfm_irq_test_service_1.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_ENABLE_IRQ_TEST",
       "version_major": 0,
@@ -212,8 +212,8 @@
     {
       "name": "TF-M PS Test Service",
       "short_name": "TFM_SP_PS_TEST",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_ps_test_service/tfm_ps_test_service.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_ps_test_service/tfm_ps_test_service.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_PS",
       "version_major": 0,
@@ -228,8 +228,8 @@
     {
       "name": "TF-M Secure Client 2 Service",
       "short_name": "TFM_SP_SECURE_CLIENT_2",
-      "manifest": "@TFM_TEST_PATH@/test_services/tfm_secure_client_2/tfm_secure_client_2.yaml",
-      "source_path": "@TFM_TEST_PATH@",
+      "manifest": "${TFM_TEST_PATH}/test_services/tfm_secure_client_2/tfm_secure_client_2.yaml",
+      "source_path": "${TFM_TEST_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "TFM_PARTITION_TEST_SECURE_SERVICES",
       "version_major": 0,
diff --git a/tools/tfm_parse_manifest_list.py b/tools/tfm_parse_manifest_list.py
index a2628d6..1d6b64a 100644
--- a/tools/tfm_parse_manifest_list.py
+++ b/tools/tfm_parse_manifest_list.py
@@ -79,6 +79,7 @@
 
     print("Start to generate PSA manifests:")
     for manifest_item in manifest_list:
+        # Replace environment variables in the manifest path
         manifest_path = os.path.expandvars(manifest_item['manifest'])
         file = open(manifest_path)
         manifest = yaml.safe_load(file)
@@ -103,7 +104,9 @@
         interpreted as a relative path from the OUT_DIR.
         """
         if 'source_path' in manifest_item:
-            outfile_name = os.path.relpath(outfile_name, start = manifest_item['source_path'])
+            # Replace environment variables in the source path
+            source_path = os.path.expandvars(manifest_item['source_path'])
+            outfile_name = os.path.relpath(outfile_name, start = source_path)
 
         manifest_header_list.append(outfile_name)
 
@@ -140,7 +143,9 @@
 
     print("Start to generate file from the generated list:")
     for file in file_list:
+        # Replace environment variables in the output filepath
         outfile_name = os.path.expandvars(file["output"])
+        # Replace environment variables in the template filepath
         templatefile_name = os.path.expandvars(file["template"])
 
         if OUT_DIR is not None:
@@ -161,7 +166,9 @@
     print ("Generation of files done")
 
 def parse_args():
-    parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list')
+    parser = argparse.ArgumentParser(description='Parse secure partition manifest list and generate files listed by the file list',
+                                     epilog='Note that environment variables in template files will be replaced with their values')
+
     parser.add_argument('-o', '--outdir'
                         , dest='outdir'
                         , required=False
@@ -212,20 +219,8 @@
     gen_file_args = args.gen_file_args
     OUT_DIR = args.outdir
 
-    if len(manifest_args) == 0:
-        manifest_list = DEFAULT_MANIFEST_LIST
-    else:
-        """
-        Only convert to abs path when value is not default
-        Because the default value is a fixed relative path to TF-M root folder,
-        it will be various to different execution path if converted to absolute path.
-        The same for gen_file_list
-        """
-        manifest_list = [os.path.abspath(x) for x in args.manifest_args]
-    if len(gen_file_args) == 0:
-        gen_file_list = DEFAULT_GEN_FILE_LIST
-    else:
-        gen_file_list = [os.path.abspath(x) for x in args.gen_file_args]
+    manifest_list = [os.path.abspath(x) for x in args.manifest_args]
+    gen_file_list = [os.path.abspath(x) for x in args.gen_file_args]
 
     # Arguments could be relative path.
     # Convert to absolute path as we are going to change diretory later
diff --git a/tools/tfm_psa_ff_test_manifest_list.yaml.in b/tools/tfm_psa_ff_test_manifest_list.yaml
similarity index 76%
rename from tools/tfm_psa_ff_test_manifest_list.yaml.in
rename to tools/tfm_psa_ff_test_manifest_list.yaml
index 7fc4965..cba0c5c 100644
--- a/tools/tfm_psa_ff_test_manifest_list.yaml.in
+++ b/tools/tfm_psa_ff_test_manifest_list.yaml
@@ -14,8 +14,8 @@
    {
       "name": "PSA FF Test Client Partition",
       "short_name": "PSA_FF_TEST_CLIENT",
-      "manifest": "@PSA_ARCH_TESTS_PATH@/api-tests/platform/manifests/client_partition_psa.json",
-      "source_path":"@PSA_ARCH_TESTS_PATH@",
+      "manifest": "${PSA_ARCH_TESTS_PATH}/api-tests/platform/manifests/client_partition_psa.json",
+      "source_path":"${PSA_ARCH_TESTS_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "PSA_API_TEST_IPC",
       "version_major": 0,
@@ -30,8 +30,8 @@
     {
       "name": "PSA FF Test Server Partition",
       "short_name": "PSA_FF_TEST_SERVER",
-      "manifest": "@PSA_ARCH_TESTS_PATH@/api-tests/platform/manifests/server_partition_psa.json",
-      "source_path":"@PSA_ARCH_TESTS_PATH@",
+      "manifest": "${PSA_ARCH_TESTS_PATH}/api-tests/platform/manifests/server_partition_psa.json",
+      "source_path":"${PSA_ARCH_TESTS_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "PSA_API_TEST_IPC",
       "version_major": 0,
@@ -46,8 +46,8 @@
     {
       "name": "PSA FF Test Driver Partition",
       "short_name": "PSA_FF_TEST_DRIVER",
-      "manifest": "@PSA_ARCH_TESTS_PATH@/api-tests/platform/manifests/driver_partition_psa.json",
-      "source_path":"@PSA_ARCH_TESTS_PATH@",
+      "manifest": "${PSA_ARCH_TESTS_PATH}/api-tests/platform/manifests/driver_partition_psa.json",
+      "source_path":"${PSA_ARCH_TESTS_PATH}",
       "tfm_partition_ipc": true,
       "conditional": "PSA_API_TEST_IPC",
       "version_major": 0,