forked from rollbear/trompeloeil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
462 lines (373 loc) · 10.1 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
cmake_minimum_required(VERSION 3.2)
project(trompeloeil)
option(TROMPELOEIL_INSTALL_DOCS "Install documentation" ON)
include(GNUInstallDirs)
include(ExternalProject)
include(CMakePackageConfigHelpers)
include(CheckCXXCompilerFlag)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/trompeloeil/trompeloeil-config-version.cmake"
VERSION 40
COMPATIBILITY AnyNewerVersion)
add_library(trompeloeil INTERFACE)
add_library(trompeloeil::trompeloeil ALIAS trompeloeil)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(
trompeloeil
INTERFACE
$<BUILD_INTERFACE:${INCLUDE_DIR}>
)
target_include_directories(
trompeloeil
INTERFACE
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>
)
set(MASTER_PROJECT OFF)
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
set(MASTER_PROJECT ON)
endif()
if (MASTER_PROJECT AND CMAKE_BUILD_TYPE MATCHES Debug)
if (${CXX_STANDARD})
set(CMAKE_CXX_STANDARD ${CXX_STANDARD})
else()
set(CMAKE_CXX_STANDARD 14)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CATCH_DIR ${CMAKE_CURRENT_BINARY_DIR}/catch)
if(NOT EXISTS ${CATCH_DIR}/catch.hpp)
if (NOT EXISTS ${CATCH_DIR})
make_directory(${CATCH_DIR})
endif()
file(
DOWNLOAD
https://github.com/catchorg/Catch2/releases/download/v2.9.2/catch.hpp ${CATCH_DIR}/catch.hpp
STATUS
status
LOG
log
)
list(GET status 0 status_code)
list(GET status 1 status_string)
if(NOT status_code EQUAL 0)
message(FATAL_ERROR "error downloading catch: ${status_string}"
"${log}")
endif()
endif()
# Assumptions:
# Clang and GNU compilers run on Linux or Linux-like platforms.
# MSVC compilers run on Windows platforms.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(
LIBCXX_PREFIX_PATH
"/usr/lib/llvm-8"
CACHE
PATH
"Path prefix to libc++"
)
# Interpret CXX_STDLIB, if specified
if (NOT "${CXX_STDLIB}" STREQUAL "")
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
# Emulate -stdlib for g++
if ("${CXX_STDLIB}" STREQUAL "libc++")
# Disable standard library and
# set include path to appropriate libc++ headers
string(CONCAT
LIBCXX_INCLUDE_PATH
"-isystem"
"${LIBCXX_PREFIX_PATH}"
"/include/c++/v1")
string(CONCAT
STDLIB_COMPILE_FLAGS
"-nostdinc++"
" "
"${LIBCXX_INCLUDE_PATH}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STDLIB_COMPILE_FLAGS}")
elseif ("${CXX_STDLIB}" STREQUAL "libstdc++")
# This is the default library for g++, nothing to do.
else()
# Unknown CXX_STDLIB option
message(FATAL_ERROR
"CXX_STDLIB only understands libc++ and libstdc++, not "
"'${CXX_STDLIB}'")
endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if (("${CXX_STDLIB}" STREQUAL "libc++") OR
("${CXX_STDLIB}" STREQUAL "libstdc++"))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=${CXX_STDLIB}")
else()
# Unknown CXX_STDLIB option
message(FATAL_ERROR
"CXX_STDLIB only understands libc++ and libstdc++, not "
"'${CXX_STDLIB}'")
endif()
else()
message(FATAL_ERROR
"Only g++ and Clang++ compilers support CXX_STDLIB")
endif()
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
string(CONCAT
WARN_FLAGS
"-Weverything"
" -Wno-c++98-compat-pedantic"
" -Wno-padded"
" -Wno-weak-vtables"
" -Wno-exit-time-destructors"
" -Wno-global-constructors")
check_cxx_compiler_flag("-Wno-extra-semi-stmt" WARN_SEMI_STMT)
if (WARN_SEMI_STMT)
string(APPEND
WARN_FLAGS
" -Wno-extra-semi-stmt")
endif()
check_cxx_compiler_flag("-Wno-zero-as-null-pointer-constant" WARN_ZERO_NULL)
if (WARN_ZERO_NULL)
string(APPEND
WARN_FLAGS
" -Wno-zero-as-null-pointer-constant")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
string(CONCAT
WARN_FLAGS
"-Wall"
" -Wextra"
" -pedantic"
" -Wshadow"
" -Wconversion")
check_cxx_compiler_flag("-Wnonnull" WARN_NONNULL)
if (WARN_NONNULL)
string(APPEND
WARN_FLAGS
" -Wnonnull")
endif()
endif()
set(WARN_FLAGS "${WARN_FLAGS} -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARN_FLAGS}")
# Default sanitizer target properties.
set(TSAN "-fsanitize=undefined,thread")
set(SSAN "-fsanitize=undefined,address")
# Exceptions to sanitizer target properties based on compiler and compiler version.
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if (NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.0"))
set(SSAN "-fsanitize=undefined,address -fsanitize-address-use-after-scope")
else()
set(SSAN "-fsanitize=undefined,address")
endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if ((NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8")) AND
(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
set(TSAN "-fsanitize=thread")
set(SSAN "-fsanitize=address")
endif()
endif()
endif() # Clang or GNU
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/W4)
add_compile_options(/bigobj)
add_compile_options(/EHsc)
check_cxx_compiler_flag(/permissive HAS_PERMISSIVE_FLAG)
if(HAS_PERMISSIVE_FLAG)
add_compile_options(/permissive-)
endif()
endif() # MSVC
add_executable(
self_test
EXCLUDE_FROM_ALL
test/compiling_tests.cpp
test/compiling_tests_11.cpp
test/compiling_tests_14.cpp
include/catch2/trompeloeil.hpp)
target_include_directories(
self_test
PRIVATE
${CATCH_DIR}
)
if (SANITIZE AND NOT APPLE)
set_target_properties(
self_test
PROPERTIES
LINK_FLAGS
"${SSAN} -fuse-ld=gold"
COMPILE_FLAGS
${SSAN}
)
endif()
target_link_libraries(
self_test
PUBLIC
trompeloeil
)
add_executable(
thread_terror
EXCLUDE_FROM_ALL
test/thread_terror.cpp
)
target_link_libraries(
thread_terror
PUBLIC
trompeloeil
pthread
)
if (SANITIZE)
set_target_properties(
thread_terror
PROPERTIES
LINK_FLAGS
${TSAN}
COMPILE_FLAGS
${TSAN}
)
endif()
add_executable(
custom_recursive_mutex
EXCLUDE_FROM_ALL
test/custom_recursive_mutex.cpp
)
target_link_libraries(
custom_recursive_mutex
PUBLIC
trompeloeil
)
if (NOT MSVC)
target_link_libraries(
custom_recursive_mutex
PUBLIC
pthread
)
endif()
if (SANITIZE AND NOT APPLE)
set_target_properties(
custom_recursive_mutex
PROPERTIES
LINK_FLAGS
"${SSAN} -fuse-ld=gold"
COMPILE_FLAGS
${SSAN}
)
endif()
# Linker support for CXX_STDLIB argument
if (NOT ("${CXX_STDLIB}" STREQUAL ""))
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if ("${CXX_STDLIB}" STREQUAL "libc++")
set(LIBCXX_LIBRARY_PATH "${LIBCXX_PREFIX_PATH}/lib")
set(STDLIB_LINK_FLAGS
-nodefaultlibs
-L${LIBCXX_LIBRARY_PATH}
-Wl,-rpath,${LIBCXX_LIBRARY_PATH}
)
set(STDLIB_LINK_LIBRARIES
"-lc++"
"-lc++abi"
"-lm"
"-lc"
"-lgcc_s"
"-lgcc"
)
set_target_properties(
self_test
PROPERTIES
LINK_FLAGS
${STDLIB_LINK_FLAGS}
)
target_link_libraries(
self_test
PUBLIC
${STDLIB_LINK_LIBRARIES}
)
set_target_properties(
thread_terror
PROPERTIES
LINK_FLAGS
${STDLIB_LINK_FLAGS}
)
target_link_libraries(
thread_terror
PUBLIC
${STDLIB_LINK_LIBRARIES}
)
set_target_properties(
custom_recursive_mutex
PROPERTIES
LINK_FLAGS
${STDLIB_LINK_FLAGS}
)
target_link_libraries(
custom_recursive_mutex
PUBLIC
${STDLIB_LINK_LIBRARIES}
)
endif()
endif()
endif()
# Shameless hack to get target to work on Windows.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_custom_target(
run_self_test
COMMAND
${CMAKE_CURRENT_BINARY_DIR}/self_test
DEPENDS
self_test
)
else()
add_custom_target(
run_self_test
COMMAND
${CMAKE_CURRENT_BINARY_DIR}/Debug/self_test
DEPENDS
self_test
)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_custom_target(
run_coverage
COMMAND
kcov --skip-solibs --include-pattern=trompeloeil.hpp ./coverage ${CMAKE_CURRENT_BINARY_DIR}/self_test
DEPENDS
self_test
)
endif()
endif() # MASTER_PROJECT AND CMAKE_BUILD_TYPE MATCHES Debug
install(
TARGETS
trompeloeil
EXPORT
trompeloeil-targets
INCLUDES DESTINATION
include
)
install(
EXPORT
trompeloeil-targets
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/trompeloeil
)
install(
FILES
trompeloeil-config.cmake
"${CMAKE_CURRENT_BINARY_DIR}/trompeloeil/trompeloeil-config-version.cmake"
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/trompeloeil
COMPONENT
Devel
)
install(
DIRECTORY
include/
DESTINATION
${CMAKE_INSTALL_INCLUDEDIR}
)
if(TROMPELOEIL_INSTALL_DOCS)
install(
FILES
LICENSE_1_0.txt
DESTINATION
${CMAKE_INSTALL_DOCDIR}
)
install(
DIRECTORY
docs
DESTINATION
${CMAKE_INSTALL_DOCDIR}
)
endif()