-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
301 lines (255 loc) · 9.86 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
#===============================================================================
# #
# MACVETH CMake Configuration #
# #
#===============================================================================
# Minimum CMake version required
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
#===============================================================================
# PROJECT VARIABLES
#===============================================================================
# Minimum C++ std
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Project name
set(PROJECT_NAME MACVETH)
# Project version
set(MACVETH_VERSION_MAJOR 0)
set(MACVETH_VERSION_MINOR 2)
set(MACVETH_VERSION_PATCH 0)
set(MACVETH_VERSION
${MACVETH_VERSION_MAJOR}.${MACVETH_VERSION_MINOR}.${MACVETH_VERSION_PATCH})
# Setup project
project(${PROJECT_NAME} VERSION ${MACVETH_VERSION})
message(STATUS
"Generating building files for ${PROJECT_NAME} v${MACVETH_VERSION}")
#===============================================================================
# BUILD TYPE, Compiler used and project flags
# -DCMAKE_BUILD_TYPE="Debug|Release|RelWithDebInfo|MinSizeRel"
#===============================================================================
# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(WARNING "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE "Release" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
else()
message("Setting build type to " ${CMAKE_BUILD_TYPE})
endif()
if (CMAKE_BUILD_TYPE MATCHES Debug)
# Compiler flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall\
-fdiagnostics-color=always")
endif()
#===============================================================================
# DOCUMENTATION GENERATION
# -DGENERATE_DOC:BOOL=TRUE to activate
#===============================================================================
# Check if Doxygen is installed
option(GENERATE_DOC "Generate documentation with Doxygen" OFF)
if ((CMAKE_BUILD_TYPE MATCHES Debug) AND GENERATE_DOC)
# dot requirements:
# https://github.com/labapart/gattlib/issues/129#issuecomment-609394695
find_package(Doxygen REQUIRED dot)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.cfg)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
set(abs_srcdir ${CMAKE_CURRENT_SOURCE_DIR})
set(abs_builddir ${CMAKE_CURRENT_SOURCE_DIR}/docs)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
message(STATUS "Documentation: Doxygen build started")
# note the option ALL which allows to build the docs together with
# the application
add_custom_target(macveth_doxygen ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
elseif(NOT DOXYGEN_FOUND)
message(WARNING
"Documentation: Doxygen need to be installed to generate the doxygen documentation")
endif()
#===============================================================================
# CODE COVERAGE CONFIGURATION
# -DCODE_COVERAGE:BOOL=TRUE to activate
#===============================================================================
option(CODE_COVERAGE "Enable coverage reporting" OFF)
add_library(coverage_config INTERFACE)
if((CMAKE_BUILD_TYPE MATCHES Debug) OR
(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang"))
message(STATUS "Code coverage: enabled")
# Add required flags (GCC & LLVM/Clang)
target_compile_options(coverage_config INTERFACE
-O0 # no optimization
-g # generate debug info
--coverage # sets all required flags
)
if(CMAKE_VERSION VERSION_GREATER "3.16")
target_link_options(coverage_config INTERFACE --coverage)
else()
target_link_libraries(coverage_config INTERFACE --coverage)
endif()
endif()
#===============================================================================
# LOAD CLANG/LLVM CONFIGURATION
# For more info: http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project
#===============================================================================
# IMPORTANT NOTE:
# If you have installed LLVM locally using CMake, then you need to pass
# variable -DLLVM_DIR=<path> to the cmake building command, where <path> is the
# path returned by your llvm-config --cmakedir
# This list must be in descendant order. This is just a hack...
set(TESTED_LLVM_MAJOR 14 13 12 11)
set(TESTED_LLVM_MINOR 1 0)
set(TESTED_LLVM_PATCH 1 0)
foreach(LLVM_COMP_MAJOR ${TESTED_LLVM_MAJOR})
# CONFIG file (LLVMConfig.cmake), do not fail [QUIET] if not found: maybe
# we do not have 11.x but we have 10.x
foreach(LLVM_COMP_MINOR ${TESTED_LLVM_MINOR})
foreach(LLVM_COMP_PATCH ${TESTED_LLVM_PATCH})
message(STATUS "Checking if LLVM: ${LLVM_COMP_MAJOR}.${LLVM_COMP_MINOR}.${LLVM_COMP_PATCH}")
find_package(LLVM ${LLVM_COMP_MAJOR}.${LLVM_COMP_MINOR}.${LLVM_COMP_PATCH} CONFIG QUIET)
if (DEFINED LLVM_FOUND AND LLVM_FOUND EQUAL 1)
break()
endif()
endforeach()
if (DEFINED LLVM_FOUND AND LLVM_FOUND EQUAL 1)
break()
endif()
endforeach()
if (DEFINED LLVM_FOUND AND LLVM_FOUND EQUAL 1)
break()
endif()
endforeach()
# LLVM not found in any version.
if (DEFINED LLVM_FOUND AND LLVM_FOUND EQUAL 0)
message(FATAL_ERROR "Please, install LLVM/Clang >= 11.x\n"
"Consider installing llvm-VERSION-dev if you are in Debian/Ubuntu")
endif()
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
# This is a hack, actually...
set(Clang_DIR ${LLVM_LIBRARY_DIR}/cmake/clang)
find_package(Clang REQUIRED HINTS ${Clang_DIR} NO_DEFAULT_PATH)
if (NOT DEFINED Clang_FOUND)
message(FATAL_ERROR "Clang library not found. Check your paths.")
endif()
message(STATUS "Using ClangConfig.cmake in: ${CLANG_CMAKE_DIR}")
# Setting LLVM/Clang paths to CMake's
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
"${LLVM_CMAKE_DIR}"
"${CLANG_CMAKE_DIR}"
)
# Import LLVM CMake functions
include(AddLLVM)
include(AddClang)
list(APPEND CMAKE_PREFIX_PATH "${LLVM_CMAKE_DIR}")
list(APPEND CMAKE_PREFIX_PATH "${CLANG_CMAKE_DIR}")
# Set the LLVM and Clang header and library paths
include_directories(SYSTEM "${LLVM_INCLUDE_DIRS};${CLANG_INCLUDE_DIRS}")
link_directories(SYSTEM "${LLVM_LIBRARY_DIRS}")
message(STATUS "${LLVM_LIBRARY_DIRS} ${LLVM_INCLUDE_DIRS};${CLANG_INCLUDE_DIRS}")
# LLVM/Clang is normally built without RTTI. Be consistent with that.
if(NOT LLVM_ENABLE_RTTI)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
endif()
# For generating compile_options.json for IDEs
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Configuration file, for macros for instance
configure_file(macveth.h.in lib/macveth.h)
# Add a subdirectory to the build
add_subdirectory(src)
set(LLVM_LINK_COMPONENTS support)
# Define the binary to compile
add_clang_executable(macveth
lib/macveth.cpp
)
#===============================================================================
# TESTING CONFIGURATION
# -DBUILD_TESTING:BOOL=TRUE to activate
#===============================================================================
option(BUILD_TESTING "Build the testing tree" OFF)
# Only build tests if we are the top-level project
# Allows this to be used by super projects with `add_subdirectory`
if ((CMAKE_BUILD_TYPE MATCHES Debug) OR
(BUILD_TESTING AND (PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)))
message(STATUS "Testing enabled")
enable_testing()
add_subdirectory(tests)
endif()
#===============================================================================
# BUILDING TARGET
#===============================================================================
# Threads library
find_package(Threads REQUIRED)
# Configuration file
configure_file(macveth.h.in lib/macveth.h)
# Include directories
target_include_directories(macveth PUBLIC
include
${CLANG_INCLUDE_DIRS}
${PROJECT_BINARY_DIR}
)
# Custom libraries needed
target_link_libraries(macveth
PRIVATE
macveth_lib
coverage_config
)
# MACVETH Clang libraries dependencies
# Clang libraries needed
if(CLANG_LINK_CLANG_DYLIB)
message(STATUS "Linking with clang-cpp library")
target_link_libraries(macveth PRIVATE clang-cpp)
else()
target_link_libraries(macveth PRIVATE
clangTooling
clangBasic
clangASTMatchers
clangAnalysis
clangAST
clangASTMatchers
clangBasic
clangDriver
clangEdit
clangFrontend
clangFrontendTool
clangLex
clangParse
clangSema
clangRewrite
clangRewriteFrontend
clangStaticAnalyzerFrontend
clangStaticAnalyzerCheckers
clangStaticAnalyzerCore
clangCrossTU
clangIndex
clangSerialization
clangToolingCore
clangToolingInclusions
clangTooling
clangFormat
)
endif()
# LLVM flags needed
target_link_libraries(macveth
PRIVATE
LLVM
)
# Flags needed
target_link_libraries(macveth
PUBLIC
pthread
tinfo
dl
stdc++fs # needed in some gcc versions
)
#===============================================================================
# INSTALLING TARGET
#===============================================================================
install(TARGETS macveth DESTINATION build)