Build: Simplify TFM_PLATFORM to support platform name only
Current TF-M build requires TFM_PLATFORM to be set as the relative
path to platform/ext/targets. If vendor platform folder structure
is changed, TFM_PLATFORM shall be adjusted accordingly, which may
break the backward combability of build command.
This patch will simplify the TFM_PLATFORM to support platform name
only. For example, -DTFM_PLATFORM=arm/msp2/an521 and
-DTFM_PLATFORM=an521 shall be both accepted.
Signed-off-by: Jianliang Shen <jianliang.shen@arm.com>
Change-Id: I3b03f6dfa88cc1ee8059f7802f717e86676c377d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 03fe0ba..fb232c0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,20 +11,8 @@
############################ CONFIGURATION #####################################
-if (IS_ABSOLUTE "${TFM_PLATFORM}")
- file(RELATIVE_PATH TFM_PLATFORM_RELATIVE_PATH
- "${CMAKE_CURRENT_SOURCE_DIR}/platform/ext/target"
- ${TFM_PLATFORM})
- set(TFM_PLATFORM "${TFM_PLATFORM_RELATIVE_PATH}" CACHE STRING "Target platform set as an absolute path." FORCE)
-endif()
-
-# Some compiler flags depend on the CPU / platform config. This include should
-# be run before the toolchain file so the compiler can be configured properly.
-if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/platform/ext/target/${TFM_PLATFORM}/preload.cmake)
- Message(FATAL_ERROR "Unsupported TFM_PLATFORM ${TFM_PLATFORM}")
-else()
- include(platform/ext/target/${TFM_PLATFORM}/preload.cmake)
-endif()
+# Configure TFM_PLATFORM
+include(${CMAKE_SOURCE_DIR}/config/tfm_platform.cmake)
if(TFM_SYSTEM_MVE)
message(FATAL_ERROR "Hardware MVE is currently not supported in TF-M")
diff --git a/config/tfm_platform.cmake b/config/tfm_platform.cmake
new file mode 100644
index 0000000..b410b29
--- /dev/null
+++ b/config/tfm_platform.cmake
@@ -0,0 +1,49 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(TARGET_PATH "${CMAKE_SOURCE_DIR}/platform/ext/target")
+
+if (NOT IS_ABSOLUTE "${TFM_PLATFORM}" AND NOT IS_DIRECTORY "${TARGET_PATH}/${TFM_PLATFORM}")
+ # If TFM_PLATFORM is not a relative patch to ${TARGET_PATH}, then it could
+ # be a platform name, for example an521. Search directories which contain
+ # the "preload.cmake" and find the the match one.
+
+ # Get the list of directories which have preload.cmake
+ file(GLOB_RECURSE PLATFORM_PATHS ${TARGET_PATH} "preload.cmake")
+
+ # Search the list with platform name and store the result in PLATFORM_PATHS
+ list(FILTER PLATFORM_PATHS INCLUDE REGEX "${TFM_PLATFORM}")
+
+ # Get the length of list PLATFORM_PATHS
+ list(LENGTH PLATFORM_PATHS _PLATFORM_NUM)
+
+ if (${_PLATFORM_NUM} STREQUAL 1)
+ # Get the absolute path of the platform
+ get_filename_component(PLATFORM_ABS_PATH ${PLATFORM_PATHS} DIRECTORY)
+ set(TFM_PLATFORM ${PLATFORM_ABS_PATH} CACHE STRING "Target platform set as an absolute path." FORCE)
+ elseif (${_PLATFORM_NUM} STREQUAL 0)
+ Message(FATAL_ERROR "Platform ${TFM_PLATFORM} is not found in TF-M")
+ elseif (${_PLATFORM_NUM} GREATER 1)
+ Message(FATAL_ERROR "Two or more platforms ${TFM_PLATFORM} are found in TF-M")
+ endif()
+endif()
+
+# If TFM_PLATFORM is an absolute path which maybe inputed by developer or
+# transformed from platform name by the process above, it will be converted to
+# relative path here.
+if (IS_ABSOLUTE "${TFM_PLATFORM}")
+ file(RELATIVE_PATH TFM_PLATFORM_RELATIVE_PATH ${TARGET_PATH} ${TFM_PLATFORM})
+ set(TFM_PLATFORM "${TFM_PLATFORM_RELATIVE_PATH}" CACHE STRING "Target platform set as an relative path." FORCE)
+endif()
+
+# Some compiler flags depend on the CPU / platform config. This include should
+# be run before the toolchain file so the compiler can be configured properly.
+if (NOT EXISTS "${TARGET_PATH}/${TFM_PLATFORM}/preload.cmake" OR NOT EXISTS "${TARGET_PATH}/${TFM_PLATFORM}/CMakeLists.txt")
+ Message(FATAL_ERROR "Unsupported TFM_PLATFORM ${TFM_PLATFORM}")
+else()
+ include(platform/ext/target/${TFM_PLATFORM}/preload.cmake)
+endif()