-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
245 lines (205 loc) · 7.88 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
cmake_minimum_required(VERSION 3.17)
project(config-cpp)
set(VERSION "0.1.0")
#---------------------------------------------------------------------------------------
# Build Types including support for sanitizers
#---------------------------------------------------------------------------------------
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel tsan asan lsan msan ubsan"
FORCE)
# ThreadSanitizer
set(CMAKE_C_FLAGS_TSAN
"-fsanitize=thread -g -O1"
CACHE STRING "Flags used by the C compiler during ThreadSanitizer builds."
FORCE)
set(CMAKE_CXX_FLAGS_TSAN
"-fsanitize=thread -g -O1"
CACHE STRING "Flags used by the C++ compiler during ThreadSanitizer builds."
FORCE)
# AddressSanitize
set(CMAKE_C_FLAGS_ASAN
"-fsanitize=address -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer -g -O1"
CACHE STRING "Flags used by the C compiler during AddressSanitizer builds."
FORCE)
set(CMAKE_CXX_FLAGS_ASAN
"-fsanitize=address -fno-optimize-sibling-calls -fsanitize-address-use-after-scope -fno-omit-frame-pointer -g -O1"
CACHE STRING "Flags used by the C++ compiler during AddressSanitizer builds."
FORCE)
# LeakSanitizer
set(CMAKE_C_FLAGS_LSAN
"-fsanitize=leak -fno-omit-frame-pointer -g -O1"
CACHE STRING "Flags used by the C compiler during LeakSanitizer builds."
FORCE)
set(CMAKE_CXX_FLAGS_LSAN
"-fsanitize=leak -fno-omit-frame-pointer -g -O1"
CACHE STRING "Flags used by the C++ compiler during LeakSanitizer builds."
FORCE)
# MemorySanitizer (clang only)
set(CMAKE_C_FLAGS_MSAN
"-fsanitize=memory -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2"
CACHE STRING "Flags used by the C compiler during MemorySanitizer builds."
FORCE)
set(CMAKE_CXX_FLAGS_MSAN
"-fsanitize=memory -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O2"
CACHE STRING "Flags used by the C++ compiler during MemorySanitizer builds."
FORCE)
# UndefinedBehaviour
set(CMAKE_C_FLAGS_UBSAN
"-fsanitize=undefined"
CACHE STRING "Flags used by the C compiler during UndefinedBehaviourSanitizer builds."
FORCE)
set(CMAKE_CXX_FLAGS_UBSAN
"-fsanitize=undefined"
CACHE STRING "Flags used by the C++ compiler during UndefinedBehaviourSanitizer builds."
FORCE)
# MemorySanitizer only supported by clang
if ("${CMAKE_BUILD_TYPE}" STREQUAL "msan" )
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
message( FATAL_ERROR "msan not supported for g++")
endif ()
endif ()
if(NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE Debug)
endif(NOT CMAKE_BUILD_TYPE)
#---------------------------------------------------------------------------------------
# Project options
#---------------------------------------------------------------------------------------
option(YAML_SUPPORT "Support YAML config files" ON)
option(JSON_SUPPORT "Support JSON config files" ON)
option(TOML_SUPPORT "Support TOML config files" ON)
option(BUILD_EXAMPLES "Build examples" ON)
option(BUILD_TESTS "Build unit tests." ON)
option(BUILD_COVERAGE "Build for code coverage." OFF)
option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
option(BUILD_STATIC_LIBS "Build Static Libraries" ON)
option(ENABLE_CLANG_TIDY "Enable testing with clang-tidy" FALSE)
option(ENABLE_CPPCHECK "Enable testing with cppcheck" FALSE)
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/CMake)
#---------------------------------------------------------------------------------------
# compiler config
#---------------------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options("-Wall")
add_compile_options("-Wextra")
add_compile_options("-Wconversion")
add_compile_options("-pedantic")
add_compile_options("-Wfatal-errors")
endif()
#---------------------------------------------------------------------------------------
# Dependencies
#---------------------------------------------------------------------------------------
if (YAML_SUPPORT)
# yaml-cpp library dependency
find_package(yaml-cpp 0.6.2 REQUIRED)
add_definitions(-DYAML_SUPPORT)
endif(YAML_SUPPORT)
if (JSON_SUPPORT)
# nlohman_json library dependency
find_package(nlohmann_json 3.2.0 REQUIRED)
add_definitions(-DJSON_SUPPORT)
endif(JSON_SUPPORT)
if (TOML_SUPPORT)
# toml11 library dependency
find_package(toml11 3.4.0 REQUIRED)
add_definitions(-DTOML_SUPPORT)
endif(TOML_SUPPORT)
find_package(cxxopts 3.1.1 REQUIRED)
# Threads
find_package(Threads)
# Google Test
find_package(GTest)
# clang-tidy
if(ENABLE_CLANG_TIDY)
find_program(CLANGTIDY clang-tidy)
if(CLANGTIDY)
message(STATUS "Using ${CLANGTIDY} for clang-tidy")
set(CMAKE_CXX_CLANG_TIDY "${CLANGTIDY}")
else()
message(SEND_ERROR "clang-tidy requested but executable not found")
endif()
endif()
configure_file(.clang-tidy .clang-tidy COPYONLY)
# cppcheck
if(ENABLE_CPPCHECK)
find_program(CPPCHECK cppcheck)
if(CPPCHECK)
message(STATUS "Enabling cppcheck")
set(CMAKE_CXX_CPPCHECK ${CPPCHECK})
list(
APPEND CMAKE_CXX_CPPCHECK
"--enable=warning,style"
"--suppressions-list=${CMAKE_SOURCE_DIR}/suppressions.txt"
)
else()
message(SEND_ERROR "cppcheck requested but executable not found")
endif()
endif()
#---------------------------------------------------------------------------------------
# Sources, headers, directories and libs
#---------------------------------------------------------------------------------------
# From http://www.cmake.org/pipermail/cmake/2010-March/035992.html:
# function to collect all the sources from sub-directories
# into a single list
function(add_sources)
get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED)
if(NOT is_defined)
define_property(GLOBAL PROPERTY SRCS_LIST
BRIEF_DOCS "List of source files"
FULL_DOCS "List of all source files in the entire project")
endif()
# make absolute paths
set(SRCS)
foreach(s IN LISTS ARGN)
if(NOT IS_ABSOLUTE "${s}")
get_filename_component(s "${s}" ABSOLUTE)
endif()
list(APPEND SRCS "${s}")
endforeach()
# append to global list
set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}")
endfunction(add_sources)
file(GLOB sources "src/[a-zA-Z]*.cpp")
file(GLOB_RECURSE public_headers "include/config-cpp/[a-zA-Z]*.h")
file(GLOB private_headers "src/[a-zA-Z]*.h")
file(GLOB examples "examples/[a-zA-Z]*.[ch]*")
file(GLOB tests "test/[a-zA-Z]*.cpp")
set(library_sources
${sourcse}
${public_headers}
${private_headers}
${examples}
${tests}
)
add_sources(${library_sources})
if(VERBOSE)
message(STATUS "sources: ${sources}")
message(STATUS "public_headers: ${public_headers}")
message(STATUS "private_headers: ${private_headers}")
message(STATUS "examples: ${examples}")
message(STATUS "tests: ${tests}")
endif(VERBOSE)
add_subdirectory(src)
if (BUILD_EXAMPLES)
add_subdirectory(example)
endif(BUILD_EXAMPLES)
if (BUILD_TESTS)
include(CTest)
if (BUILD_COVERAGE)
include (CodeCoverage)
setup_target_for_coverage_gcovr_html( NAME ConfigCppCoverage EXECUTABLE test/ConfigCppTests DEPENDENCIES ConfigCppTests BASE_DIRECTORY "../" )
endif (BUILD_COVERAGE)
add_subdirectory(test)
endif(BUILD_TESTS)
#---------------------------------------------------------------------------------------
# Addons
#---------------------------------------------------------------------------------------
# Formatting
get_property(all_sources GLOBAL PROPERTY SRCS_LIST)
add_custom_target(format
COMMAND clang-format --style=file -i ${all_sources}
COMMENT "Running clang-format"
VERBATIM)