1#-------------------------------------------------------------------------------
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 libpsats into a dependent in-tree deployment build.  Where another
10# deployment uses libpsats, including this file in the dependent deployment
11# CMake build file allows libpsats to be built and installed into the binary
12# directory of the dependent.
13#-------------------------------------------------------------------------------
14option(CFG_FORCE_PREBUILT_LIBPSATS 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(LIBPSATS_BUILD_TYPE "DebugCoverage" CACHE STRING "Build type." FORCE)
21endif()
22
23find_package(libpsats "${_verstring}" QUIET PATHS ${CMAKE_CURRENT_BINARY_DIR}/libpsats_install/${TS_ENV}/lib/cmake/libpsats)
24if(NOT libpsats_FOUND)
25	if (CFG_FORCE_PREBUILT_LIBPSATS)
26		string(CONCAT _msg "find_package() failed to find the \"libpsats\" package. Please pass -Dlibpsats_ROOT=<full path>"
27							" to cmake, where <full path> is the directory of the libpsatsConfig.cmake file, or "
28							" pass -DCMAKE_FIND_ROOT_PATH=<path>, where <path> is the INSTALL_PREFIX used"
29							" when building libpsats. libpsats_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	# libpsats deployment being built.
35	if (DEFINED LIBPSATS_BUILD_TYPE)
36		set(_libpsats_build_type_arg "-DCMAKE_BUILD_TYPE=${LIBPSATS_BUILD_TYPE}")
37	endif()
38
39	# If not successful, build libpsats as a sub-project.
40	execute_process(COMMAND
41		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
42		${CMAKE_COMMAND}
43			${_libpsats_build_type_arg}
44			-S ${TS_ROOT}/deployments/libpsats/${TS_ENV}
45			-B ${CMAKE_CURRENT_BINARY_DIR}/libpsats
46		RESULT_VARIABLE
47			_exec_error
48		)
49	unset(_libpsats_build_type_arg)
50	if (NOT _exec_error EQUAL 0)
51		message(FATAL_ERROR "Configuring libpsats 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}/libpsats
57			--parallel ${PROCESSOR_COUNT}
58		RESULT_VARIABLE
59			_exec_error
60		)
61	if (NOT _exec_error EQUAL 0)
62		message(FATAL_ERROR "Installing libpsats 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}/libpsats
68			--prefix ${CMAKE_CURRENT_BINARY_DIR}/libpsats_install
69		RESULT_VARIABLE
70			_exec_error
71		)
72	if (NOT _exec_error EQUAL 0)
73		message(FATAL_ERROR "Installing libpsats failed. ${_exec_error}")
74	endif()
75
76	install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/libpsats/cmake_install.cmake)
77
78	find_package(libpsats "${_verstring}" QUIET REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/libpsats_install/${TS_ENV}/lib/cmake/libpsats)
79else()
80	message(STATUS "Using prebuilt libpsats from ${libpsats_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 LIBPSATS_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 LIBPSATS_BUILD_TYPE)
92	set_target_properties(libpsats::psats PROPERTIES
93		MAP_IMPORTED_CONFIG_DEBUG				${LIBPSATS_BUILD_TYPE}
94		MAP_IMPORTED_CONFIG_MINSIZEREL			${LIBPSATS_BUILD_TYPE}
95		MAP_IMPORTED_CONFIG_MINSIZWITHDEBINFO	${LIBPSATS_BUILD_TYPE}
96		MAP_IMPORTED_CONFIG_RELEASE				${LIBPSATS_BUILD_TYPE}
97		MAP_IMPORTED_CONFIG_RELWITHDEBINFO		${LIBPSATS_BUILD_TYPE}
98		MAP_IMPORTED_CONFIG_DEBUGCOVERAGE		${LIBPSATS_BUILD_TYPE}
99	)
100
101	# Do a manual check and issue a better message than the default one.
102	get_property(_libpsats_build_type TARGET libpsats::psats PROPERTY IMPORTED_CONFIGURATIONS)
103	string(TOUPPER ${LIBPSATS_BUILD_TYPE} _uc_libpsats_build_type)
104	if(${_uc_libpsats_build_type} IN_LIST _libpsats_build_type)
105	else()
106		message(FATAL_ERROR "Installed libpsats package does not supports required build type ${LIBPSATS_BUILD_TYPE}.")
107	endif()
108	unset(_libpsats_build_type)
109	unset(_uc_libpsats_build_type)
110endif()
111
112# libpsats can not be used without libts, so add the needed dependency.
113include(${TS_ROOT}/deployments/libts/libts-import.cmake)
114target_link_libraries(libpsats::psats INTERFACE libts::ts)
115