-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
153 lines (128 loc) · 4.63 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
cmake_minimum_required(VERSION 3.12)
project(cxxdes)
set(CMAKE_CXX_STANDARD 20)
function (print_var VARNAME)
message(STATUS "${VARNAME}=${${VARNAME}}")
endfunction ()
# default values to be stored in the cache
set(CXXDES_CLANG_TIDY OFF CACHE BOOL "Use clang tidy.")
set(CXXDES_EXAMPLES ON CACHE BOOL "Build examples.")
set(CXXDES_TESTS ON CACHE BOOL "Build tests.")
set(CXXDES_SANITIZE_ADDRESS ON CACHE BOOL "Enable -sanitize=address for examples and tests.")
set(CXXDES_SANITIZE_LEAK ON CACHE BOOL "Enable -sanitize=leak for examples and tests.")
set(CXXDES_SANITIZE_UNDEFINED ON CACHE BOOL "Enable -sanitize=undefined behavior for examples and tests.")
set(CXXDES_NO_FETCH OFF CACHE BOOL "Do not automatically fetch content.")
message(STATUS "libcxxdes build variables:")
print_var(CXXDES_CLANG_TIDY)
print_var(CXXDES_EXAMPLES)
print_var(CXXDES_TESTS)
print_var(CXXDES_SANITIZE_ADDRESS)
print_var(CXXDES_SANITIZE_LEAK)
print_var(CXXDES_SANITIZE_UNDEFINED)
print_var(CXXDES_NO_FETCH)
if (NOT CXXDES_NO_FETCH)
# library dependencies
include(FetchContent)
# fmt
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG master)
FetchContent_MakeAvailable(fmt)
# googletest
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
endif ()
# sanitization interface target
add_library(sanitize_config INTERFACE)
if (MSVC)
else ()
if (CXXDES_SANITIZE_ADDRESS)
target_compile_options(sanitize_config INTERFACE "-fsanitize=address")
target_link_options(sanitize_config INTERFACE "-fsanitize=address")
target_compile_definitions(sanitize_config INTERFACE "CXXDES_SANITIZE_ADDRESS")
endif ()
if (CXXDES_SANITIZE_LEAK)
target_compile_options(sanitize_config INTERFACE "-fsanitize=leak")
target_link_options(sanitize_config INTERFACE "-fsanitize=leak")
target_compile_definitions(sanitize_config INTERFACE "CXXDES_SANITIZE_LEAK")
endif ()
if (CXXDES_SANITIZE_UNDEFINED)
target_compile_options(sanitize_config INTERFACE "-fsanitize=undefined")
target_link_options(sanitize_config INTERFACE "-fsanitize=undefined")
target_compile_definitions(sanitize_config INTERFACE "CXXDES_SANITIZE_UNDEFINED")
endif ()
endif ()
# default error levels
if (MSVC)
# warning level 4
add_compile_options(/W4) # /WX for warnings as errors
else ()
# lots of warnings
add_compile_options(-Wall -Wextra -pedantic) # -Werror for warnings as errors
endif ()
# clang tidy
if (CXXDES_CLANG_TIDY)
find_program(CLANG_TIDY_EXE NAMES "clang-tidy")
set(
CMAKE_CXX_CLANG_TIDY
"${CLANG_TIDY_EXE}"
"-header-filter=."
"-checks=-*,modernize-*")
endif ()
# TODO: please see
# https://dominikberner.ch/cmake-interface-lib/
# Library for cxxdes
add_library(cxxdes INTERFACE)
target_include_directories(
cxxdes
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
if (MSVC)
target_compile_options(cxxdes INTERFACE "/std:c++20")
else ()
target_compile_options(cxxdes INTERFACE "--std=c++20")
endif ()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(cxxdes INTERFACE "-fcoroutines")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(cxxdes INTERFACE "-fcoroutines-ts")
endif ()
add_library(cxxdes::cxxdes ALIAS cxxdes)
add_library(cxxdes::sanitize_config ALIAS sanitize_config)
if (CXXDES_EXAMPLES)
file(GLOB example_srcs ${PROJECT_SOURCE_DIR}/examples/*.cpp)
foreach (src ${example_srcs})
get_filename_component(basename ${src} NAME_WE)
add_executable("example_${basename}" "${src}")
target_link_libraries(
"example_${basename}"
PRIVATE
cxxdes::cxxdes
fmt::fmt-header-only
sanitize_config)
endforeach ()
endif ()
if (CXXDES_TESTS)
enable_testing()
file(GLOB test_srcs ${PROJECT_SOURCE_DIR}/tests/*.test.cpp)
foreach (src ${test_srcs})
get_filename_component(basename ${src} NAME_WE)
add_executable("test_${basename}" "${src}")
target_link_libraries(
"test_${basename}"
PRIVATE
cxxdes::cxxdes
GTest::gtest_main
fmt::fmt-header-only
sanitize_config)
gtest_discover_tests("test_${basename}")
endforeach ()
endif ()