blob: 661b40dfc20f5190961281e6fc4e841a041b3474 [file] [log] [blame]
Gabor Toth172659d2023-04-27 08:51:21 +02001#-------------------------------------------------------------------------------
2# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#-------------------------------------------------------------------------------
9# Import libpsa into a dependent in-tree deployment build. Where another
10# deployment uses libpsa, including this file in the dependent deployment
11# CMake build file allows libpsa to be built and installed into the binary
12# directory of the dependent.
13#-------------------------------------------------------------------------------
14option(CFG_FORCE_PREBUILT_LIBPSA Off)
15# Try to find a pre-build package.
16version_semver_read(FILE "${CMAKE_CURRENT_LIST_DIR}/version.txt" MAJOR _major MINOR _minor PATCH _patch)
17set(_verstring "${_major}.${_minor}.${_patch}")
18
19if (COVERAGE)
20 set(LIBPSA_BUILD_TYPE "DebugCoverage" CACHE STRING "Build type." FORCE)
21endif()
22
23find_package(libpsa "${_verstring}" QUIET PATHS ${CMAKE_CURRENT_BINARY_DIR}/libpsa_install/${TS_ENV}/lib/cmake/libpsa)
24if(NOT libpsa_FOUND)
25 if (CFG_FORCE_PREBUILT_LIBPSA)
26 string(CONCAT _msg "find_package() failed to find the \"libpsa\" package. Please pass -Dlibpsa_ROOT=<full path>"
27 " to cmake, where <full path> is the directory of the libpsaConfig.cmake file, or "
28 " pass -DCMAKE_FIND_ROOT_PATH=<path>, where <path> is the INSTALL_PREFIX used"
29 " when building libpsa. libpsa_ROOT can be set in the environment too."
30 "If you wish to debug the search process pass -DCMAKE_FIND_DEBUG_MODE=ON to cmake.")
31 message(FATAL_ERROR ${_msg})
32 endif()
33 # Set build type, if a specific value is required. This leaves the default value in the hands of the
34 # libpsa deployment being built.
35 if (DEFINED LIBPSA_BUILD_TYPE)
36 set(_libpsa_build_type_arg "-DCMAKE_BUILD_TYPE=${LIBPSA_BUILD_TYPE}")
37 endif()
38
39 # If not successful, build libpsa as a sub-project.
40 execute_process(COMMAND
41 ${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
42 ${CMAKE_COMMAND}
43 ${_libpsa_build_type_arg}
44 -S ${TS_ROOT}/deployments/libpsa/${TS_ENV}
45 -B ${CMAKE_CURRENT_BINARY_DIR}/libpsa
46 RESULT_VARIABLE
47 _exec_error
48 )
49 unset(_libpsa_build_type_arg)
50 if (NOT _exec_error EQUAL 0)
51 message(FATAL_ERROR "Configuring libpsa failed. ${_exec_error}")
52 endif()
53 execute_process(COMMAND
54 ${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
55 ${CMAKE_COMMAND}
56 --build ${CMAKE_CURRENT_BINARY_DIR}/libpsa
57 --parallel ${PROCESSOR_COUNT}
58 RESULT_VARIABLE
59 _exec_error
60 )
61 if (NOT _exec_error EQUAL 0)
62 message(FATAL_ERROR "Installing libpsa failed. ${_exec_error}")
63 endif()
64 execute_process(COMMAND
65 ${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
66 ${CMAKE_COMMAND}
67 --install ${CMAKE_CURRENT_BINARY_DIR}/libpsa
68 --prefix ${CMAKE_CURRENT_BINARY_DIR}/libpsa_install
69 RESULT_VARIABLE
70 _exec_error
71 )
72 if (NOT _exec_error EQUAL 0)
73 message(FATAL_ERROR "Installing libpsa failed. ${_exec_error}")
74 endif()
75
76 install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/libpsa/cmake_install.cmake)
77
78 find_package(libpsa "${_verstring}" QUIET REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/libpsa_install/${TS_ENV}/lib/cmake/libpsa)
79else()
80 message(STATUS "Using prebuilt libpsa from ${libpsa_DIR}")
81endif()
82
83# Cmake will use the same build type of the imported target as used by the main project. If no mapping is configured and
84# the matching build type is not found, cmake will fall back to any build type. Details of the fall back mechanism are not
85# documented.
86# If a mapping is defined, and the imported target does not define the mapped build type, cmake will treat the library
87# as not found.
88#
89# If LIBPSA_BUILD_TYPE is set and the main project wants to use a specific build type, configure build type mapping to
90# only allow using the requested build type.
91if (DEFINED LIBPSA_BUILD_TYPE)
92 set_target_properties(libpsa::psa PROPERTIES
93 MAP_IMPORTED_CONFIG_DEBUG ${LIBPSA_BUILD_TYPE}
94 MAP_IMPORTED_CONFIG_MINSIZEREL ${LIBPSA_BUILD_TYPE}
95 MAP_IMPORTED_CONFIG_MINSIZWITHDEBINFO ${LIBPSA_BUILD_TYPE}
96 MAP_IMPORTED_CONFIG_RELEASE ${LIBPSA_BUILD_TYPE}
97 MAP_IMPORTED_CONFIG_RELWITHDEBINFO ${LIBPSA_BUILD_TYPE}
98 MAP_IMPORTED_CONFIG_DEBUGCOVERAGE ${LIBPSA_BUILD_TYPE}
99 )
100
101 # Do a manual check and issue a better message than the default one.
102 get_property(_libpsa_build_type TARGET libpsa::psa PROPERTY IMPORTED_CONFIGURATIONS)
103 string(TOUPPER ${LIBPSA_BUILD_TYPE} _uc_libpsa_build_type)
104 if(${_uc_libpsa_build_type} IN_LIST _libpsa_build_type)
105 else()
106 message(FATAL_ERROR "Installed libpsa package does not supports required build type ${LIBPSA_BUILD_TYPE}.")
107 endif()
108 unset(_libpsa_build_type)
109 unset(_uc_libpsa_build_type)
110endif()
111
112# libpsa can not be used without libts, so add the needed dependency.
113include(${TS_ROOT}/deployments/libts/libts-import.cmake)
114target_link_libraries(libpsa::psa INTERFACE libts::ts)