-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
441 lines (381 loc) · 14.9 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright 2023 TikTok Pte. Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.14)
####################################################
# Project VERSE includes the following components: #
# 1. VERSE C++ library #
# 2. VERSE C++ test #
# 3. VERSE C++ example #
####################################################
# [OPTION] CMAKE_BUILD_TYPE (DEFAULT: "Release")
# Select from Release, Debug, MiniSizeRel, or RelWithDebInfo.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
STRINGS "Release" "Debug" "MinSizeRel" "RelWithDebInfo")
endif()
message(STATUS "Build type (CMAKE_BUILD_TYPE): ${CMAKE_BUILD_TYPE}")
project(VERSE VERSION 0.4.0 LANGUAGES CXX C)
########################
# Global configuration #
########################
# CMake modules
include(CMakeDependentOption)
include(CMakePushCheckState)
include(CheckIncludeFiles)
include(CheckCXXSourceCompiles)
include(CheckCXXSourceRuns)
# Custom modules
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
include(VerseCustomMacros)
# In Debug mode, define VERSE_DEBUG.
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(VERSE_DEBUG ON)
else()
set(VERSE_DEBUG OFF)
endif()
message(STATUS "VERSE debug mode: ${VERSE_DEBUG}")
# In Debug mode, enable extra compiler flags.
include(EnableDebugFlags)
# Always build position-independent-code
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# [OPTION] VERSE_USE_CXX17 (default: ON)
# Use C++17, use C++14 otherwise.
set(VERSE_USE_CXX17_OPTION_STR "Use C++17")
option(VERSE_USE_CXX17 ${VERSE_USE_CXX17_OPTION_STR} OFF)
message(STATUS "VERSE_USE_CXX17: ${VERSE_USE_CXX17}")
# Enable features from C++17 if available, disable features if set to OFF.
include(EnableCXX17)
# Add default files and directories.
include(GNUInstallDirs)
# Runtime path
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Source Tree
set(VERSE_INCLUDES_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
set(VERSE_CONFIG_IN_FILENAME ${CMAKE_CURRENT_LIST_DIR}/cmake/PETAce-VerseConfig.cmake.in)
set(VERSE_CONFIG_H_IN_FILENAME ${VERSE_INCLUDES_DIR}/verse/util/config.h.in)
# Build tree
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(VERSE_CONFIG_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/cmake/PETAce-VerseConfig.cmake)
set(VERSE_TARGETS_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/cmake/PETAce-VerseTargets.cmake)
set(VERSE_CONFIG_VERSION_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/cmake/PETAce-VerseConfigVersion.cmake)
set(VERSE_CONFIG_H_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/src/verse/util/config.h)
set(VERSE_THIRDPARTY_DIR ${CMAKE_CURRENT_BINARY_DIR}/thirdparty)
# Installation tree
set(VERSE_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/PETAce-Verse-${VERSE_VERSION_MAJOR}.${VERSE_VERSION_MINOR})
set(VERSE_INCLUDES_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}/PETAce-${VERSE_VERSION_MAJOR}.${VERSE_VERSION_MINOR})
set(VERSE_THIRDPARTY_INCLUDES_INSTALL_DIR ${VERSE_INCLUDES_INSTALL_DIR}/thirdparty)
# Make the install target depend on the all target.
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY OFF)
# Supported target operating systems are Linux and macOS.
if (NOT DEFINED LINUX)
if (UNIX AND NOT APPLE AND NOT CYGWIN AND NOT MINGW)
set(LINUX ON)
endif()
endif()
if (UNIX AND APPLE)
set(MACOS ON)
endif()
if (NOT LINUX AND NOT MACOS)
message(FATAL_ERROR "Supported target operating systems are Linux and macOS")
endif()
# Only support x86_64 and arm64
set(CMAKE_REQUIRED_QUIET_OLD ${CMAKE_REQUIRED_QUIET})
set(CMAKE_REQUIRED_QUIET ON)
check_cxx_source_runs("
#if defined(__aarch64__)
int main() {
return 0;
}
#else
#error
#endif
"
VERSE_ARM64
)
check_cxx_source_runs("
#if defined(__amd64)
int main() {
return 0;
}
#else
#error
#endif
"
VERSE_AMD64
)
set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_OLD})
if (NOT VERSE_AMD64 AND NOT VERSE_ARM64)
message(FATAL_ERROR "Supported target architectures are x86_64 and arm64")
endif()
add_compile_options(-msse4.2 -Wno-ignored-attributes)
set(VERSE_ENABLE_GCOV_STR "Enable gcov")
option(VERSE_ENABLE_GCOV ${VERSE_ENABLE_GCOV_STR} OFF)
message(STATUS "VERSE_ENABLE_GCOV: ${VERSE_ENABLE_GCOV_STR}")
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND VERSE_ENABLE_GCOV)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -fprofile-arcs -ftest-coverage -lgcov")
endif()
# CMAKE_CXX_LINK_EXECUTABLE
if (LINUX)
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -ldl -lrt")
else()
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
endif()
#########################
# External dependencies #
#########################
# [OPTION] VERSE_BUILD_DEPS (DEFAULT: ON)
# Download and build dependencies if set to ON.
# Look for dependencies using find_package, otherwise.
set(VERSE_BUILD_DEPS_OPTION_STR "Automatically download and build unmet dependencies")
option(VERSE_BUILD_DEPS ${VERSE_BUILD_DEPS_OPTION_STR} ON)
message(STATUS "VERSE_BUILD_DEPS: ${VERSE_BUILD_DEPS}")
if(VERSE_BUILD_DEPS)
include(FetchContent)
mark_as_advanced(FETCHCONTENT_BASE_DIR)
mark_as_advanced(FETCHCONTENT_FULLY_DISCONNECTED)
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED)
mark_as_advanced(FETCHCONTENT_QUIET)
endif()
# [OPTION] VERSE_BUILD_SHARED_LIBS (DEFAULT: OFF)
# Build a shared library if set to ON.
set(VERSE_BUILD_SHARED_LIBS_STR "Build shared library")
option(VERSE_BUILD_SHARED_LIBS ${VERSE_BUILD_SHARED_LIBS_STR} OFF)
message(STATUS "VERSE_BUILD_SHARED_LIBS: ${VERSE_BUILD_SHARED_LIBS}")
# Require Threads::Threads
if(NOT TARGET Threads::Threads)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
if(NOT Threads_FOUND)
message(FATAL_ERROR "Threads: not found, please download and install manually")
else()
message(STATUS "Threads: found")
endif()
endif()
# PETAce-Network::network
find_package(PETAce-Network 0.3 QUIET CONFIG)
if(PETAce-Network_FOUND)
message(STATUS "PETAce-Network: found")
if(PETAce-Network_STATIC_FOUND)
set(network "PETAce-Network::network")
else()
set(network "PETAce-Network::network_shared")
endif()
else()
if(VERSE_BUILD_DEPS)
message(STATUS "PETAce-Network: download ...")
verse_fetch_thirdparty_content(PETAce-Network)
if(TARGET network)
set(network "network")
else()
set(network "network_shared")
endif()
set(VERSE_BUILD_NETWORK TRUE CACHE BOOL "" FORCE)
else()
message(FATAL_ERROR "PETAce-Network:: not found, please download and install manually")
endif()
endif()
# PETAce-Solo::solo
find_package(PETAce-Solo 0.3 QUIET CONFIG)
if(PETAce-Solo_FOUND)
message(STATUS "PETAce-Solo: found")
if(PETAce-Solo_STATIC_FOUND)
set(solo "PETAce-Solo::solo")
else()
set(solo "PETAce-Solo::solo_shared")
endif()
else()
if(VERSE_BUILD_DEPS)
message(STATUS "PETAce-Solo: download ...")
verse_fetch_thirdparty_content(PETAce-Solo)
if(TARGET solo)
set(solo "solo")
else()
set(solo "solo_shared")
endif()
set(VERSE_BUILD_SOLO TRUE CACHE BOOL "" FORCE)
else()
message(FATAL_ERROR "PETAce-Solo: not found, please download and install manually")
endif()
endif()
#####################
# VERSE C++ library #
#####################
# Add source files to library and header files to install
set(VERSE_SOURCE_FILES "")
add_subdirectory(src/verse)
# Create the config file
configure_file(${VERSE_CONFIG_H_IN_FILENAME} ${VERSE_CONFIG_H_FILENAME})
install(
FILES ${VERSE_CONFIG_H_FILENAME}
DESTINATION ${VERSE_INCLUDES_INSTALL_DIR}/verse/util)
# Build only a static library
if(NOT VERSE_BUILD_SHARED_LIBS)
add_library(verse STATIC ${VERSE_SOURCE_FILES})
if(VERSE_USE_CXX17)
target_compile_features(verse PUBLIC cxx_std_17)
else()
target_compile_features(verse PUBLIC cxx_std_14)
endif()
target_include_directories(verse PUBLIC
$<BUILD_INTERFACE:${VERSE_INCLUDES_DIR}>
$<INSTALL_INTERFACE:${VERSE_INCLUDES_INSTALL_DIR}>)
target_include_directories(verse PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src/>)
set_target_properties(verse PROPERTIES VERSION ${VERSE_VERSION})
set_target_properties(verse PROPERTIES OUTPUT_NAME petace_verse-${VERSE_VERSION_MAJOR}.${VERSE_VERSION_MINOR})
if(VERSE_BUILD_NETWORK)
add_dependencies(verse ${network})
target_include_directories(verse PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:${network},INTERFACE_INCLUDE_DIRECTORIES>>
$<INSTALL_INTERFACE:${VERSE_INCLUDES_INSTALL_DIR}>)
endif()
target_link_libraries(verse PUBLIC ${network})
set(VERSE_CARRY_NETWORK FALSE)
if(VERSE_BUILD_SOLO)
add_dependencies(verse ${solo})
target_include_directories(verse PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:${solo},INTERFACE_INCLUDE_DIRECTORIES>>
$<INSTALL_INTERFACE:${VERSE_INCLUDES_INSTALL_DIR}>)
endif()
target_link_libraries(verse PUBLIC ${solo})
set(VERSE_CARRY_SOLO FALSE)
target_link_libraries(verse PUBLIC Threads::Threads)
install(TARGETS verse EXPORT PETAce-VerseTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
# Build only a shared library
else()
add_library(verse_shared SHARED ${VERSE_SOURCE_FILES})
if(VERSE_USE_CXX17)
target_compile_features(verse_shared PUBLIC cxx_std_17)
else()
target_compile_features(verse_shared PUBLIC cxx_std_14)
endif()
target_include_directories(verse_shared PUBLIC
$<BUILD_INTERFACE:${VERSE_INCLUDES_DIR}>
$<INSTALL_INTERFACE:${VERSE_INCLUDES_INSTALL_DIR}>)
target_include_directories(verse_shared PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src/>)
set_target_properties(verse_shared PROPERTIES VERSION ${VERSE_VERSION})
set_target_properties(verse_shared PROPERTIES SOVERSION ${VERSE_VERSION_MAJOR}.${VERSE_VERSION_MINOR})
set_target_properties(verse_shared PROPERTIES OUTPUT_NAME petace_verse)
if(VERSE_BUILD_NETWORK)
target_include_directories(verse_shared PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:${network},INTERFACE_INCLUDE_DIRECTORIES>>
$<INSTALL_INTERFACE:${VERSE_INCLUDES_INSTALL_DIR}>)
endif()
target_link_libraries(verse_shared PUBLIC ${network})
set(VERSE_CARRY_NETWORK FALSE)
if(VERSE_BUILD_SOLO)
target_include_directories(verse_shared PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:${solo},INTERFACE_INCLUDE_DIRECTORIES>>
$<INSTALL_INTERFACE:${VERSE_INCLUDES_INSTALL_DIR}>)
endif()
target_link_libraries(verse_shared PUBLIC ${solo})
set(VERSE_CARRY_SOLO FALSE)
target_link_libraries(verse_shared PUBLIC Threads::Threads)
install(TARGETS verse_shared EXPORT PETAce-VerseTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
# Add standard alias targets for PETAce-Verse::verse and PETAce-Verse::verse_shared
if(TARGET verse)
add_library(PETAce-Verse::verse ALIAS verse)
endif()
if(TARGET verse_shared)
add_library(PETAce-Verse::verse_shared ALIAS verse_shared)
endif()
#################################
# Installation and CMake config #
#################################
# Create the CMake config file
include(CMakePackageConfigHelpers)
configure_package_config_file(
${VERSE_CONFIG_IN_FILENAME} ${VERSE_CONFIG_FILENAME}
INSTALL_DESTINATION ${VERSE_CONFIG_INSTALL_DIR})
# Install the export
install(
EXPORT PETAce-VerseTargets
NAMESPACE PETAce-Verse::
DESTINATION ${VERSE_CONFIG_INSTALL_DIR})
# Version file; we require exact version match for downstream
write_basic_package_version_file(
${VERSE_CONFIG_VERSION_FILENAME}
VERSION ${VERSE_VERSION}
COMPATIBILITY SameMinorVersion)
# Install config and module files
install(
FILES
${VERSE_CONFIG_FILENAME}
${VERSE_CONFIG_VERSION_FILENAME}
DESTINATION ${VERSE_CONFIG_INSTALL_DIR})
# We export PETAce-VerseTargets from the build tree so it can be used by other projects
# without requiring an install.
export(
EXPORT PETAce-VerseTargets
NAMESPACE PETAce-Verse::
FILE ${VERSE_TARGETS_FILENAME})
# Install header files of dependencies if VERSE_BUILD_DEPS is ON
if(VERSE_BUILD_DEPS)
# Insert dependencies here
if(VERSE_BUILD_SOLO)
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} --build ${solo_BINARY_DIR} -t install)")
endif()
if(VERSE_BUILD_NETWORK)
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} --build ${network_BINARY_DIR} -t install)")
endif()
endif()
#####################
# VERSE C++ example #
#####################
# [option] VERSE_BUILD_EXAMPLE
set(VERSE_BUILD_EXAMPLE_OPTION_STR "Build C++ example for VERSE")
option(VERSE_BUILD_EXAMPLE ${VERSE_BUILD_EXAMPLE_OPTION_STR} ON)
message(STATUS "VERSE_BUILD_EXAMPLE: ${VERSE_BUILD_EXAMPLE}")
if(VERSE_BUILD_EXAMPLE)
add_subdirectory(example)
endif()
##################
# VERSE C++ bench #
##################
# [option] VERSE_BUILD_BENCH
set(VERSE_BUILD_BENCH_OPTION_STR "Build C++ benchmark for VERSE")
option(VERSE_BUILD_BENCH ${VERSE_BUILD_BENCH_OPTION_STR} ON)
message(STATUS "VERSE_BUILD_BENCH: ${VERSE_BUILD_BENCH}")
if(VERSE_BUILD_BENCH)
add_subdirectory(bench)
endif()
##################
# VERSE C++ test #
##################
# [option] VERSE_BUILD_TEST
set(VERSE_BUILD_TEST_OPTION_STR "Build C++ test for VERSE")
option(VERSE_BUILD_TEST ${VERSE_BUILD_TEST_OPTION_STR} ON)
message(STATUS "VERSE_BUILD_TEST: ${VERSE_BUILD_TEST}")
if(VERSE_BUILD_TEST)
add_subdirectory(test)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND VERSE_ENABLE_GCOV)
add_custom_target(test_coverage
COMMAND gcovr -r ${CMAKE_CURRENT_LIST_DIR} -f \"src\" -e \".+\(test\\.cpp\)\" --xml-pretty -o "${CMAKE_CURRENT_BINARY_DIR}/report/coverage.xml"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
endif()
endif()