-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
299 lines (242 loc) · 10.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
############################################################################
# Copyright (c) 2024, Isabel Paredes #
# Copyright (c) 2024, QuantStack #
# #
# Distributed under the terms of the BSD 3-Clause License. #
# #
# The full license is in the file LICENSE, distributed with this software. #
############################################################################
cmake_minimum_required(VERSION 3.10)
project(xeus-uv)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
set(XEUS_UV_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(XEUS_UV_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(XEUS_UV_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
# Versioning
# ===========
# Project version
file(STRINGS "${XEUS_UV_INCLUDE_DIR}/xeus-uv/xeus-uv.hpp" xeus_uv_version_defines
REGEX "#define XEUS_UV_VERSION_(MAJOR|MINOR|PATCH)")
foreach(ver ${xeus_uv_version_defines})
if(ver MATCHES "#define XEUS_UV_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
set(XEUS_UV_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
endif()
endforeach()
set(XEUS_UV_VERSION
${XEUS_UV_VERSION_MAJOR}.${XEUS_UV_VERSION_MINOR}.${XEUS_UV_VERSION_PATCH})
message(STATUS "xeus-uv version: v${XEUS_UV_VERSION}")
# Binary version
# See the following URL for explanations about the binary versioning
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
file(STRINGS "${XEUS_UV_INCLUDE_DIR}/xeus-uv/xeus-uv.hpp" xeus_uv_version_defines
REGEX "#define XEUS_UV_BINARY_(CURRENT|REVISION|AGE)")
foreach(ver ${xeus_uv_version_defines})
if(ver MATCHES "#define XEUS_UV_BINARY_(CURRENT|REVISION|AGE) +([^ ]+)$")
set(XEUS_UV_BINARY_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
endif()
endforeach()
set(XEUS_UV_BINARY_VERSION
${XEUS_UV_BINARY_CURRENT}.${XEUS_UV_BINARY_REVISION}.${XEUS_UV_BINARY_AGE})
message(STATUS "xeus-uv binary version: v${XEUS_UV_BINARY_VERSION}")
# Build options
# =============
# Compilation options
option(XEUS_UV_BUILD_SHARED_LIBS "Build xeus shared library." ON)
option(XEUS_UV_BUILD_STATIC_LIBS "Build xeus static library (default if BUILD_SHARED_LIBS is OFF)." ON)
option(XEUS_UV_STATIC_DEPENDENCIES "link statically with xeus dependencies" OFF)
# Test options
option(XEUS_UV_BUILD_TESTS "xeus test suite" OFF)
# Static build configuration
# ==========================
if(XEUS_UV_STATIC_DEPENDENCIES)
set(XEUS_TARGET_NAME xeus-static)
set(XEUS_ZMQ_TARGET_NAME xeus-zmq-static)
else()
set(XEUS_TARGET_NAME xeus)
set(XEUS_ZMQ_TARGET_NAME xeus-zmq)
endif()
# Print build configuration
# ==========================
message(STATUS "XEUS_UV_BUILD_SHARED_LIBS: ${XEUS_UV_BUILD_SHARED_LIBS}")
message(STATUS "XEUS_UV_BUILD_STATIC_LIBS: ${XEUS_UV_BUILD_STATIC_LIBS}")
message(STATUS "XEUS_UV_STATIC_DEPENDENCIES: ${XEUS_UV_STATIC_DEPENDENCIES}")
message(STATUS "XEUS_UV_BUILD_TESTS: ${XEUS_UV_BUILD_TESTS}")
# Dependencies
# ============
set(xeus_REQUIRED_VERSION 5.0)
set(xeus_zmq_REQUIRED_VERSION 3.0)
if(NOT TARGET xeus AND NOT TARGET xeus-static)
find_package(xeus ${xeus_REQUIRED_VERSION} REQUIRED)
message(STATUS "Found xeus ${xeus_VERSION}")
endif()
find_package(xeus-zmq ${xeus_zmq_REQUIRED_VERSION} REQUIRED)
find_library(UVW_LIBRARY uvw)
find_path(UVW_INCLUDE_DIR uvw.hpp)
message(STATUS "Found UVW_LIBRARY: ${UVW_LIBRARY}")
# Source files
# ============
set(XEUS_UV_HEADERS
${XEUS_UV_INCLUDE_DIR}/xeus-uv/xeus-uv.hpp
${XEUS_UV_INCLUDE_DIR}/xeus-uv/xhook_base.hpp
${XEUS_UV_INCLUDE_DIR}/xeus-uv/xserver_uv.hpp
)
set(XEUS_UV_SOURCES
${XEUS_UV_SOURCE_DIR}/xhook_base.cpp
${XEUS_UV_SOURCE_DIR}/xserver_uv.cpp
${XEUS_UV_SOURCE_DIR}/xuv_runner.cpp
${XEUS_UV_SOURCE_DIR}/xuv_runner.hpp
)
# Targets and link
# ================
include(CheckCXXCompilerFlag)
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
macro(xeus_uv_create_target target_name linkage output_name)
string(TOUPPER "${linkage}" linkage_upper)
if(NOT ${linkage_upper} MATCHES "^(SHARED|STATIC)$")
message(FATAL_ERROR "Invalid library linkage: ${linkage}")
endif()
# Output
# ======
add_library(${target_name} ${linkage_upper} ${XEUS_UV_SOURCES} ${XEUS_UV_HEADERS})
target_include_directories(
${target_name}
PUBLIC $<BUILD_INTERFACE:${XEUS_UV_INCLUDE_DIR}>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(
${target_name}
PUBLIC ${UVW_LIBRARY}
PUBLIC ${XEUS_TARGET_NAME}
PUBLIC ${XEUS_ZMQ_TARGET_NAME}
)
if(APPLE)
target_link_libraries(${target_name} PUBLIC "-framework CoreFoundation")
endif()
if(UNIX)
# CMake does not compute the version number of so files as libtool
# does on Linux. Strictly speaking, we should exclude FreeBSD and
# Apple from this, but that would require having different version
# numbers depending on the platform. We prefer to follow the
# libtool pattern everywhere.
math(EXPR XEUS_UV_BINARY_COMPATIBLE "${XEUS_UV_BINARY_CURRENT} - ${XEUS_UV_BINARY_AGE}")
set_target_properties(
${target_name}
PROPERTIES
PUBLIC_HEADER "${XEUS_UV_HEADERS}"
COMPILE_OPTIONS "-fvisibility=hidden"
COMPILE_DEFINITIONS "XEUS_UV_EXPORTS"
PREFIX ""
VERSION
"${XEUS_UV_BINARY_COMPATIBLE}.${XEUS_UV_BINARY_REVISION}.${XEUS_UV_BINARY_AGE}"
SOVERSION ${XEUS_UV_BINARY_COMPATIBLE}
OUTPUT_NAME "lib${output_name}"
)
else()
set_target_properties(
${target_name}
PROPERTIES
PUBLIC_HEADER "${XEUS_UV_HEADERS}"
COMPILE_DEFINITIONS "XEUS_UV_EXPORTS"
PREFIX ""
VERSION ${XEUS_UV_BINARY_VERSION}
SOVERSION ${XEUS_UV_BINARY_CURRENT}
OUTPUT_NAME "lib${output_name}"
)
endif()
# Compilation flags
# =================
target_compile_features(${target_name} PRIVATE cxx_std_17)
target_compile_definitions(${target_name} PUBLIC UVW_AS_LIB)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR
CMAKE_CXX_COMPILER_ID MATCHES "Intel")
target_compile_options(${target_name} PUBLIC -Wunused-parameter -Wextra -Wreorder)
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
endif()
if(${linkage_upper} STREQUAL "STATIC")
target_compile_definitions(${target_name} PUBLIC XEUS_UV_STATIC_LIB)
endif()
if(MSVC)
target_compile_definitions(${target_name} PUBLIC -DNOMINMAX)
target_compile_options(${target_name} PUBLIC /DGUID_WINDOWS /MP /bigobj)
target_compile_options(${target_name} PUBLIC /wd4251 /wd4996)
elseif(APPLE)
target_compile_definitions(${target_name} PUBLIC -DGUID_CFUUID)
else ()
target_compile_definitions(${target_name} PUBLIC -DGUID_LIBUUID)
endif()
if(XEUS_UV_STATIC_DEPENDENCIES)
if(CMAKE_DL_LIBS)
target_link_libraries(${target_name} PRIVATE ${CMAKE_DL_LIBS})
endif()
if(UNIX AND NOT APPLE)
target_link_libraries(${target_name} PRIVATE util rt)
endif()
endif()
endmacro()
set(xeus_uv_targets "")
if(XEUS_UV_BUILD_SHARED_LIBS)
xeus_uv_create_target(xeus-uv SHARED xeus-uv)
list(APPEND xeus_uv_targets xeus-uv)
endif()
if(XEUS_UV_BUILD_STATIC_LIBS)
# On Windows, a static library should use a different output name
# to avoid the conflict with the import library of a shared one.
if(CMAKE_HOST_WIN32)
xeus_uv_create_target(xeus-uv-static STATIC xeus-uv-static)
else ()
xeus_uv_create_target(xeus-uv-static STATIC xeus-uv)
endif()
list(APPEND xeus_uv_targets xeus-uv-static)
endif()
# Tests
# =====
# We need to control from outside whether we enable testing or not. We cannot
# rely on BUILD_TESTING since it does not exist until CTest is included.
include(CTest)
if(XEUS_UV_BUILD_TESTS)
set(BUILD_TESTING ON)
message(STATUS "tests enabled")
else ()
set(BUILD_TESTING OFF)
message(STATUS "tests disabled")
endif()
if(BUILD_TESTING)
add_subdirectory(test)
endif()
# Installation
# ============
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(XEUS_UV_CMAKECONFIG_INSTALL_DIR
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for xeus-uvConfig.cmake")
install(TARGETS ${xeus_uv_targets}
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xeus-uv)
# Makes the project importable from the build directory
export(EXPORT ${PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
# Configure 'xeusConfig.cmake' for a build tree
set(XEUS_UV_CONFIG_CODE "####### Expanded from \@XEUS_CONFIG_CODE\@ #######\n")
set(XEUS_UV_CONFIG_CODE "${XEUS_UV_CONFIG_CODE}set(CMAKE_MODULE_PATH \"${CMAKE_CURRENT_SOURCE_DIR}/cmake;\${CMAKE_MODULE_PATH}\")\n")
set(XEUS_UV_CONFIG_CODE "${XEUS_UV_CONFIG_CODE}##################################################")
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${PROJECT_BINARY_DIR})
# Configure 'xeusConfig.cmake' for an install tree
set(XEUS_UV_CONFIG_CODE "")
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${XEUS_UV_CMAKECONFIG_INSTALL_DIR})
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${XEUS_UV_VERSION}
COMPATIBILITY AnyNewerVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${XEUS_UV_CMAKECONFIG_INSTALL_DIR})
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${XEUS_UV_CMAKECONFIG_INSTALL_DIR})