1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8#-------------------------------------------------------------------------------
9# Import libts into a dependent in-tree deployment build.  Where another
10# deployment uses libts, including this file in the dependent deployment
11# CMake build file allows libts to be built and installed into the binary
12# directory of the dependent.
13#-------------------------------------------------------------------------------
14option(CFG_FORCE_PREBUILT_LIBTS 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(LIBTS_BUILD_TYPE "DebugCoverage" CACHE STRING "Build type." FORCE)
21endif()
22
23find_package(libts "${_verstring}" QUIET PATHS ${CMAKE_CURRENT_BINARY_DIR}/libts_install/${TS_ENV}/lib/cmake/libts)
24if(NOT libts_FOUND)
25	if (CFG_FORCE_PREBUILT_LIBTS)
26		string(CONCAT _msg "find_package() failed to find the \"libts\" package. Please pass -Dlibts_ROOT=<path> or"
27						   " -DCMAKE_FIND_ROOT_PATH=<path> cmake variable, where <path> is the INSTALL_PREFIX used"
28						   " when building libts. libts_ROOT can be set in the environment too."
29						   "If you wish to debug the search process pass -DCMAKE_FIND_DEBUG_MODE=ON to cmake.")
30		message(FATAL_ERROR ${_msg})
31	endif()
32	# Set build type, if a specific value is required. This leaves the default value in the hands of the
33	# libts deployment being built.
34	if (DEFINED LIBTS_BUILD_TYPE)
35		set(_libts_build_type_arg "-DCMAKE_BUILD_TYPE=${LIBTS_BUILD_TYPE}")
36	endif()
37
38	# If not successful, build libts as a sub-project.
39	execute_process(COMMAND
40		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
41		${CMAKE_COMMAND}
42			${_libts_build_type_arg}
43			-S ${TS_ROOT}/deployments/libts/${TS_ENV}
44			-B ${CMAKE_CURRENT_BINARY_DIR}/libts
45		RESULT_VARIABLE
46			_exec_error
47		)
48	unset(_libts_build_type_arg)
49	if (NOT _exec_error EQUAL 0)
50		message(FATAL_ERROR "Configuring libts failed. ${_exec_error}")
51	endif()
52	execute_process(COMMAND
53		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
54		${CMAKE_COMMAND}
55			--build ${CMAKE_CURRENT_BINARY_DIR}/libts
56			--parallel ${PROCESSOR_COUNT}
57		RESULT_VARIABLE
58			_exec_error
59		)
60	if (NOT _exec_error EQUAL 0)
61		message(FATAL_ERROR "Installing libts failed. ${_exec_error}")
62	endif()
63	execute_process(COMMAND
64		${CMAKE_COMMAND} -E env "CROSS_COMPILE=${CROSS_COMPILE}"
65		${CMAKE_COMMAND}
66			--install ${CMAKE_CURRENT_BINARY_DIR}/libts
67			--prefix ${CMAKE_CURRENT_BINARY_DIR}/libts_install
68		RESULT_VARIABLE
69			_exec_error
70		)
71	if (NOT _exec_error EQUAL 0)
72		message(FATAL_ERROR "Installing libts failed. ${_exec_error}")
73	endif()
74
75	install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/libts/cmake_install.cmake)
76
77	find_package(libts "${_verstring}" QUIET REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/libts_install/${TS_ENV}/lib/cmake/libts)
78else()
79	message(STATUS "Using prebuilt libts from ${libts_DIR}")
80endif()
81
82# Cmake will use the same build type of the imported target as used by the main project. If no mapping is configured and
83# the matching build type is not found, cmake will fall back to any build type. Details of the fall back mechanism are not
84# documented.
85# If a mapping is defined, and the imported target does not define the mapped build type, cmake will treat the library
86# as not found.
87#
88# If LIBTS_BUILD_TYPE is set and the main project wants to use a specific build type, configure build type mapping to
89# only allow using the requested build type.
90if (DEFINED LIBTS_BUILD_TYPE)
91	set_target_properties(libts::ts PROPERTIES
92		MAP_IMPORTED_CONFIG_DEBUG				${LIBTS_BUILD_TYPE}
93		MAP_IMPORTED_CONFIG_MINSIZEREL			${LIBTS_BUILD_TYPE}
94		MAP_IMPORTED_CONFIG_MINSIZWITHDEBINFO	${LIBTS_BUILD_TYPE}
95		MAP_IMPORTED_CONFIG_RELEASE				${LIBTS_BUILD_TYPE}
96		MAP_IMPORTED_CONFIG_RELWITHDEBINFO		${LIBTS_BUILD_TYPE}
97		MAP_IMPORTED_CONFIG_DEBUGCOVERAGE		${LIBTS_BUILD_TYPE}
98	)
99
100	# Do a manual check and issue a better message than the default one.
101	get_property(_libts_build_type TARGET libts::ts PROPERTY IMPORTED_CONFIGURATIONS)
102	string(TOUPPER ${LIBTS_BUILD_TYPE} _uc_libts_build_type)
103	if(${_uc_libts_build_type} IN_LIST _libts_build_type)
104	else()
105		message(FATAL_ERROR "Installed libts package does not supports required build type ${LIBTS_BUILD_TYPE}.")
106	endif()
107	unset(_libts_build_type)
108	unset(_uc_libts_build_type)
109endif()
110