-
Notifications
You must be signed in to change notification settings - Fork 3
/
CMakeLists.txt
226 lines (186 loc) · 8.76 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright 2022 The Foundry Visionmongers Ltd
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.21)
# Set a default build type if none was specified.
# The CMake default is toolchain-specific so ensure consistency by
# having an explicit default.
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui, ccmake
set_property(
CACHE CMAKE_BUILD_TYPE
PROPERTY STRINGS
"Debug"
"Release"
"MinSizeRel"
"RelWithDebInfo")
endif ()
project(OpenAssetIO-Test-CMake)
enable_testing()
#-----------------------------------------------------------------------
# Options
option(OPENASSETIOTEST_ENABLE_SUBPROJECT "Use OpenAssetIO as a subproject" OFF)
option(OPENASSETIO_MEDIACREATIONTEST_ENABLE_SUBPROJECT
"Use OpenAssetIO-MediaCreation as a subproject" OFF)
option(OPENASSETIOTEST_ENABLE_PYTHON "Test Python bindings" ON)
option(OPENASSETIOTEST_ENABLE_C "Test C bindings" ON)
if (CMAKE_TOOLCHAIN_FILE)
message(STATUS "Using toolchain file = ${CMAKE_TOOLCHAIN_FILE}")
endif ()
message(STATUS "Testing OpenAssetIO as a subproject = ${OPENASSETIOTEST_ENABLE_OPENASSETIO_SUBPROJECT}")
message(STATUS "Testing OpenAssetIO-MediaCreation as a subproject = "
"${OPENASSETIOTEST_ENABLE_MEDIACREATION_SUBPROJECT}")
message(STATUS "Testing OpenAssetIO Python bindings = ${OPENASSETIOTEST_ENABLE_PYTHON}")
message(STATUS "Testing OpenAssetIO C bindings = ${OPENASSETIOTEST_ENABLE_C}")
#-----------------------------------------------------------------------
# OpenAssetIO dependency.
if (OPENASSETIOTEST_ENABLE_OPENASSETIO_SUBPROJECT)
# Use OpenAssetIO as a subproject.
#
# This will add to the project using `add_subdirectory`.
#
# OpenAssetIO must therefore be built, meaning its `find_package`
# calls for dependencies must work.
add_subdirectory(OpenAssetIO)
else ()
# Use OpenAssetIO as an external CMake package
#
# The `OpenAssetIOConfig.cmake` file must be discoverable. E.g.
# add the install directory (`dist`) to CMAKE_PREFIX_PATH.
find_package(OpenAssetIO REQUIRED)
endif ()
if (OPENASSETIOTEST_ENABLE_MEDIACREATION_SUBPROJECT)
# Use OpenAssetIO-MediaCreation as a subproject.
#
# This will add to the project using `add_subdirectory`.
add_subdirectory(OpenAssetIO-MediaCreation)
else ()
# Use OpenAssetIO-MediaCreation as an external CMake package
#
# The `OpenAssetIO-MediaCreationConfig.cmake` file must be
# discoverable. E.g. add the install directory (`dist`) to
# CMAKE_PREFIX_PATH.
find_package(OpenAssetIO-MediaCreation REQUIRED)
endif ()
# For Windows tests to work we must add the dlls to the PATH
if (WIN32 AND DEFINED OpenAssetIO_BINARY_DIR)
file(TO_NATIVE_PATH "${OpenAssetIO_BINARY_DIR}" OpenAssetIO_BINARY_DIR_NATIVE)
endif ()
#-----------------------------------------------------------------------
# C++ OpenAssetIO tests.
add_executable(test.cpp.core src/test.core.cpp)
add_test(cpp.core test.cpp.core)
target_link_libraries(test.cpp.core PRIVATE OpenAssetIO::openassetio-core)
target_compile_features(test.cpp.core PRIVATE cxx_std_17)
if (WIN32 AND DEFINED OpenAssetIO_BINARY_DIR)
set_tests_properties(cpp.core PROPERTIES ENVIRONMENT "PATH=${OpenAssetIO_BINARY_DIR_NATIVE}")
endif ()
#-----------------------------------------------------------------------
# C++ MediaCreation tests.
add_executable(test.cpp.mediacreation src/test.mediacreation.cpp)
add_test(cpp.mediacreation test.cpp.mediacreation)
target_link_libraries(test.cpp.mediacreation PRIVATE OpenAssetIO::openassetio-core OpenAssetIO-MediaCreation::openassetio-mediacreation)
target_compile_features(test.cpp.mediacreation PRIVATE cxx_std_17)
if (WIN32 AND DEFINED OpenAssetIO_BINARY_DIR)
set_tests_properties(cpp.mediacreation PROPERTIES ENVIRONMENT "PATH=${OpenAssetIO_BINARY_DIR_NATIVE}")
endif ()
#-----------------------------------------------------------------------
# C tests.
if (OPENASSETIOTEST_ENABLE_C)
add_executable(test.c.core src/test.core.c)
add_test(c.core test.c.core)
target_link_libraries(test.c.core PRIVATE OpenAssetIO::openassetio-core-c)
# Must use C++ linker settings or the C++ standard library might not
# be linked. This is required since openassetio-core-c depends on
# openassetio-core, which is a C++ library.
set_target_properties(test.c.core PROPERTIES LINKER_LANGUAGE CXX)
target_compile_features(test.c.core PRIVATE c_std_99)
if (WIN32 AND DEFINED OpenAssetIO_BINARY_DIR)
set_tests_properties(c.core PROPERTIES ENVIRONMENT "PATH=${OpenAssetIO_BINARY_DIR_NATIVE}")
endif ()
endif ()
#-----------------------------------------------------------------------
# Python tests.
if (OPENASSETIOTEST_ENABLE_PYTHON)
if (OPENASSETIOTEST_ENABLE_SUBPROJECT)
message(
FATAL_ERROR
"Testing OpenAssetIO as a subproject when Python is enabled is currently unsupported."
" The binary Python module must be built and made available alongside the Python"
" sources for tests to work."
)
endif ()
find_package(Python COMPONENTS Interpreter Development.Embed REQUIRED)
if (NOT ${Python_VERSION_MAJOR} EQUAL ${OpenAssetIO_Python_VERSION_MAJOR} OR
NOT ${Python_VERSION_MINOR} EQUAL ${OpenAssetIO_Python_VERSION_MINOR})
message(
FATAL_ERROR
"Found Python '${Python_VERSION}' but OpenAssetIO was built against"
" '${OpenAssetIO_Python_VERSION}'"
)
endif ()
#-----------------------------------------------------------------------
# Python sources tests.
# Python sources test.
# -v - verbose output.
# -B - don't write pycache files (ensure script is reinterpreted on
# re-runs).
# -s - ignore the user site-directory (avoid unexpected things
# leaking in).
add_test(NAME python COMMAND ${Python_EXECUTABLE} -vBs ${PROJECT_SOURCE_DIR}/src/test.py)
set_tests_properties(python PROPERTIES ENVIRONMENT PYTHONPATH=${OpenAssetIO_Python_SITELIB})
#-----------------------------------------------------------------------
# Python bridge test (loading Python plugin system from C++).
add_executable(test.python.bridge src/test.python.bridge.cpp)
add_test(python.bridge.cpp test.python.bridge)
target_link_libraries(
test.python.bridge
PRIVATE
OpenAssetIO::openassetio-python-bridge
Python::Python
)
target_compile_features(test.python.bridge PRIVATE cxx_std_17)
# If libpython is linked in as a static library, then we must export
# symbols for dynamically loaded Python extension modules to use.
set_target_properties(test.python.bridge PROPERTIES ENABLE_EXPORTS ON)
# Storage for list of environment variables to set before running
# the test.
set(_envvars)
# Add location of Python sources in the OpenAssetIO package to the
# PYTHONPATH.
list(APPEND _envvars "PYTHONPATH=${OpenAssetIO_Python_SITELIB}")
# The test has an embedded Python interpreter, but is missing all
# the required basic Python libraries, so we need to set PYTHONHOME
# to the source Python package to avoid nasty errors.
execute_process(
COMMAND ${Python_EXECUTABLE} -c "import sys; sys.stdout.write(sys.prefix)"
OUTPUT_VARIABLE Python_PREFIX
)
list(APPEND _envvars "PYTHONHOME=${Python_PREFIX}")
# Fix library search path on MacOS.
if (APPLE)
# The `Python::Python` target's link options are set to e.g
# `@rpath/Python.framework/Versions/3.10/Python` but all our
# target's (CMake generated) @rpaths already contain
# `Python.framework/Versions/3.10`. So add another RPATH with
# the framework subdirectories stripped. This then allows our
# test target to find the Python library.
set_target_properties(test.python.bridge PROPERTIES BUILD_RPATH ${Python_PREFIX}/../../..)
endif ()
# Augment the PATH on Windows to find Python library dlls.
if (WIN32)
if (DEFINED OpenAssetIO_BINARY_DIR)
list(APPEND _envvars.path ${OpenAssetIO_BINARY_DIR_NATIVE})
endif ()
foreach (path ${Python_RUNTIME_LIBRARY_DIRS})
file(TO_NATIVE_PATH "${path}" path_native)
list(APPEND _envvars.path ${path_native})
endforeach ()
# Frustratingly, $<SEMICOLON> doesn't work here, we have to find
# the magic number of backslashes instead.
list(JOIN _envvars.path \\\; _envvars.path)
list(APPEND _envvars "PATH=${_envvars.path}")
endif ()
set_tests_properties(python.bridge.cpp PROPERTIES ENVIRONMENT "${_envvars}")
endif ()