-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCMakeLists.txt
171 lines (148 loc) · 5.27 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
cmake_minimum_required(VERSION 3.0)
project(warpcore VERSION 0.2.0 LANGUAGES C CXX)
# Check for dependencies before mucking with the compiler flags
# See if we can enable testing
include(CheckCXXSymbolExists)
set(CMAKE_REQUIRED_LIBRARIES benchmark)
check_cxx_symbol_exists(benchmark::Initialize benchmark/benchmark.h
HAVE_BENCHMARK_H)
include(CMakePushCheckState)
cmake_reset_check_state()
# Look for netmap
include(CheckIncludeFile)
set(CMAKE_REQUIRED_INCLUDES "/usr/include /usr/local/include")
check_include_file(net/netmap_user.h HAVE_NETMAP_H)
cmake_reset_check_state()
# Check if _GNU_SOURCE is available.
include(CheckSymbolExists)
check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
if(_GNU_SOURCE)
add_definitions(-D_GNU_SOURCE)
endif()
# Build "Debug" type by default
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug
CACHE STRING "Set build type (default Debug)" FORCE)
endif()
# Use modern C and C++
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_STANDARD 11)
# Set general compile flags, if they are supported
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
foreach(FLAG -march=native -Wextra -Wpedantic -Weverything -Werror
-fcolor-diagnostics)
string(REGEX REPLACE "[-=+]" "_" F ${FLAG})
check_cxx_compiler_flag(${FLAG} ${F})
if(${F})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAG}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}")
endif()
endforeach()
# Set CXX flags
foreach(FLAG -Wno-c++11-extensions -Wno-c++98-compat -Wno-global-constructors)
string(REGEX REPLACE "[-=+]" "_" F ${FLAG})
check_cxx_compiler_flag(${FLAG} ${F})
if(${F})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}")
endif()
endforeach()
# Set C flags
foreach(FLAG -Wno-missing-field-initializers -Wno-missing-braces)
string(REGEX REPLACE "[-=+]" "_" F ${FLAG})
check_c_compiler_flag(${FLAG} ${F})
if(${F})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAG}")
endif()
endforeach()
# flags for "Debug" and "None" builds
set(CMAKE_C_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
if(CMAKE_BUILD_TYPE MATCHES Debug)
# Default debug logging level
if(NOT DEFINED DLEVEL)
set(DLEVEL "debug")
endif()
# Set some additional flags for Debug builds
set(FLAGS -fsanitize=address -fsanitize=memory -fsanitize-memory-track-origins
-fsanitize=thread -fno-omit-frame-pointer)
if(NOT CMAKE_SYSTEM_NAME MATCHES FreeBSD)
# -fsanitize=leak does not work on FreeBSD
set(FLAGS ${FLAGS} -fsanitize=leak)
elseif(NOT CMAKE_SYSTEM_NAME MATCHES Darwin)
# -fsanitize=undefined -fno-sanitize=alignment does not work on Darwin
set(FLAGS ${FLAGS} -fsanitize=undefined -fno-sanitize=alignment)
endif()
foreach(FLAG ${FLAGS})
string(REGEX REPLACE "[-=+]" "_" F ${FLAG})
check_c_compiler_flag(${FLAG} ${F})
if(${F})
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${FLAG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${FLAG}")
endif()
endforeach()
# Use include-what-you-use to check #includes, if it's installed
find_program(IWYU NAMES include-what-you-use iwyu)
if(IWYU)
message(STATUS "Found include-what-you-use: ${IWYU}")
string(TOLOWER ${CMAKE_SYSTEM_NAME} CMAKE_SYSTEM_NAME_LC)
set(CMAKE_C_INCLUDE_WHAT_YOU_USE
${IWYU} -Xiwyu
--mapping_file=${PROJECT_SOURCE_DIR}/misc/${CMAKE_SYSTEM_NAME_LC}.imp)
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${CMAKE_C_INCLUDE_WHAT_YOU_USE})
endif()
# Use clang-tidy for extra warnings, if it's installed
find_program(CLANGTIDY NAMES clang-tidy)
if(CLANGTIDY)
message(STATUS "Found clang-tidy: ${CLANGTIDY}")
set(CLANG_TIDY_CHECKS *
-google-readability-braces-around-statements
-readability-braces-around-statements
-google-runtime-references
-clang-analyzer-alpha.deadcode.UnreachableCode
-cert-err58-cpp
)
string(REPLACE ";" "," CLANG_TIDY_CHECKS "${CLANG_TIDY_CHECKS}")
set(CMAKE_C_CLANG_TIDY ${CLANGTIDY} -checks=${CLANG_TIDY_CHECKS})
set(CMAKE_CXX_CLANG_TIDY ${CMAKE_C_CLANG_TIDY})
endif()
else()
# Default logging level
if(NOT DEFINED DLEVEL)
set(DLEVEL "notice")
endif()
# See if we can use LTO
check_c_compiler_flag(-flto LTO)
if(${LTO})
# Turn LTO on
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
# Use the correct toolchain for LTO, if enabled
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(CMAKE_AR llvm-ar)
set(CMAKE_RANLIB llvm-ranlib)
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_AR gcc-ar)
set(CMAKE_RANLIB gcc-ranlib)
endif()
endif()
endif()
# flags for "Release" builds
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -Ofast")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -Ofast")
# flags for "RelWithDebInfo" builds
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-Ofast -g")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-Ofast -g")
# Extract compile options into variable, for doxygen's CLANG_ASSISTED_PARSING
get_property(COMPILE_OPTIONS DIRECTORY PROPERTY COMPILE_OPTIONS)
string(REPLACE ";" " " COMPILE_OPTIONS "${COMPILE_OPTIONS}")
add_definitions(-DDLEVEL=${DLEVEL})
add_subdirectory(doc)
add_subdirectory(bin)
add_subdirectory(lib)
if(HAVE_BENCHMARK_H)
add_subdirectory(test)
enable_testing()
else()
message(STATUS "Google Benchmark not found; not enabling tests")
endif()