1#-------------------------------------------------------------------------------
2# Copyright (c) 2022, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8set(TARGET_PATH "${CMAKE_SOURCE_DIR}/platform/ext/target")
9
10if (NOT IS_ABSOLUTE "${TFM_PLATFORM}" AND NOT IS_DIRECTORY "${TARGET_PATH}/${TFM_PLATFORM}")
11    # If TFM_PLATFORM is not a relative patch to ${TARGET_PATH}, then it could
12    # be a platform name, for example an521. Search directories which contain
13    # the "preload.cmake" and find the the match one.
14
15    # Get the list of directories which have preload.cmake
16    file(GLOB_RECURSE PLATFORM_PATHS ${TARGET_PATH} "preload.cmake")
17
18    # Search the list with platform name and store the result in PLATFORM_PATHS
19    list(FILTER PLATFORM_PATHS INCLUDE REGEX "/${TFM_PLATFORM}/")
20
21    # Get the length of list PLATFORM_PATHS
22    list(LENGTH PLATFORM_PATHS _PLATFORM_NUM)
23
24    if (${_PLATFORM_NUM} STREQUAL 1)
25        # Get the absolute path of the platform
26        get_filename_component(PLATFORM_ABS_PATH ${PLATFORM_PATHS} DIRECTORY)
27        set(TFM_PLATFORM ${PLATFORM_ABS_PATH} CACHE STRING "Target platform set as an absolute path." FORCE)
28    elseif (${_PLATFORM_NUM} STREQUAL 0)
29        Message(FATAL_ERROR "Platform ${TFM_PLATFORM} is not found in TF-M")
30    elseif (${_PLATFORM_NUM} GREATER 1)
31        Message(FATAL_ERROR "Two or more platforms ${TFM_PLATFORM} are found in TF-M")
32    endif()
33endif()
34
35# If TFM_PLATFORM is an absolute path which maybe inputed by developer or
36# transformed from platform name by the process above, it will be converted to
37# relative path here.
38if (IS_ABSOLUTE "${TFM_PLATFORM}")
39    file(RELATIVE_PATH TFM_PLATFORM_RELATIVE_PATH ${TARGET_PATH} ${TFM_PLATFORM})
40    set(TFM_PLATFORM "${TFM_PLATFORM_RELATIVE_PATH}" CACHE STRING "Target platform set as an relative path." FORCE)
41endif()
42
43# Some compiler flags depend on the CPU / platform config. This include should
44# be run before the toolchain file so the compiler can be configured properly.
45if (NOT EXISTS "${TARGET_PATH}/${TFM_PLATFORM}/preload.cmake" OR NOT EXISTS "${TARGET_PATH}/${TFM_PLATFORM}/CMakeLists.txt")
46    Message(FATAL_ERROR "Unsupported TFM_PLATFORM ${TFM_PLATFORM}")
47else()
48    include(platform/ext/target/${TFM_PLATFORM}/preload.cmake)
49endif()
50