-
Notifications
You must be signed in to change notification settings - Fork 15
/
CMakeLists.txt
200 lines (151 loc) · 6.46 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
################################################################################
# CMakeLists.txt
#
# Copyright (c) 2018 Florian Gauger
# Copyright (c) 2018-2019 Timo Bingmann
#
# All rights reserved. Published under the MIT License in the LICENSE file.
################################################################################
cmake_minimum_required(VERSION 3.9.2)
cmake_policy(VERSION 3.9.2)
project(cobs)
# prohibit in-source builds
if("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
message(SEND_ERROR "In-source builds are not allowed.")
endif()
# Set a default build type if none was specified
set(COBS_DEFAULT_BUILD_TYPE "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${COBS_DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${COBS_DEFAULT_BUILD_TYPE}" 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")
endif()
################################################################################
### Options and Switches
# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to
# make it prominent in the GUI.
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF)
# Override CMAKE_EXECUTABLE_SUFFIX
option(COBS_EXECUTABLE_SUFFIX "Suffix for executables."
"${CMAKE_EXECUTABLE_SUFFIX}")
if(COBS_EXECUTABLE_SUFFIX)
set(CMAKE_EXECUTABLE_SUFFIX "${COBS_EXECUTABLE_SUFFIX}")
endif()
option(COBS_USE_GCOV
"Compile and run tests with gcov for coverage analysis." OFF)
################################################################################
### Compiler Flags
# variables to collect compile-time definitions, include dirs, and libraries
set(COBS_DEFINITIONS "")
set(COBS_INCLUDE_DIRS "")
set(COBS_LINK_LIBRARIES "")
# enable more warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpedantic")
# use C++17
set(CMAKE_CXX_STANDARD 17)
# enable warnings
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -march=native -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -march=native -fPIC")
# with run-time STL checks
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_DEBUG")
# with AddressSanitizer
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
# enable ThreadSanitizer
if(OFF)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread -pie -fPIC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOBS_HAVE_THREAD_SANITIZER=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -pie -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOBS_HAVE_THREAD_SANITIZER=1")
endif()
message(STATUS "COBS CMAKE_CXX_FLAGS:" ${CMAKE_CXX_FLAGS})
###############################################################################
# enable gcov coverage analysis with gcc
if(COBS_USE_GCOV)
# find programs
find_program(GENHTML genhtml)
find_program(LCOV lcov)
if(NOT LCOV OR NOT GENHTML)
message(SEND_ERROR "Coverage analysis requires lcov and genhtml programs.")
else()
message(STATUS "Found lcov: ${LCOV}")
message(STATUS "Found genhtml: ${GENHTML}")
endif()
# add coverage anaylsis compile and link flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov")
# add cached variable containing parameters for lcov/genhtml
set(LCOV_FLAGS "" CACHE STRING "parameters for lcov")
set(GENHTML_FLAGS --legend --no-branch-coverage
CACHE STRING "parameters for genhtml")
# custom target to run before tests
add_custom_target(lcov-reset
COMMAND ${LCOV} -q --directory ${CMAKE_BINARY_DIR} --zerocounters
COMMENT "Resetting code coverage counters")
# custom lcov target to run tests
add_custom_target(lcov-runtests
COMMAND ${CMAKE_CTEST_COMMAND} \${ARGS} || true
DEPENDS lcov-reset
COMMENT "Running all unit tests")
# get git version description
execute_process(COMMAND git describe --tags
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE GITDESC
OUTPUT_STRIP_TRAILING_WHITESPACE)
# command sequence to gather, clean and generate HTML coverage report
add_custom_target(lcov-html
COMMAND ${LCOV} -q --directory . --capture --output-file lcov.info
COMMAND ${LCOV} -q --remove lcov.info '/usr/*' '*/extlib/*' ${LCOV_FLAGS} --output-file lcov-clean.info
COMMAND ${GENHTML} -q -o coverage --title "cobs ${GITDESC}" --prefix ${PROJECT_SOURCE_DIR} ${GENHTML_FLAGS} lcov-clean.info
DEPENDS lcov-runtests
COMMENT "Capturing code coverage counters and create HTML coverage report"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# top-level target to run tests and generate coverage report
add_custom_target(test-coverage
COMMENT "Generate HTML coverage report "
DEPENDS lcov-html)
endif(COBS_USE_GCOV)
################################################################################
### Find Required Libraries
### find pthreads ###
find_package(Threads REQUIRED)
set(COBS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} ${COBS_LINK_LIBRARIES})
if(CMAKE_USE_PTHREADS_INIT)
set(COBS_LINK_LIBRARIES pthread ${COBS_LINK_LIBRARIES})
endif()
### use Google Test ###
add_subdirectory(extlib/googletest)
enable_testing()
include(GoogleTest)
### use xxHash ###
add_subdirectory(extlib/xxhash/cmake_unofficial)
set(COBS_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/extlib/xxhash ${COBS_INCLUDE_DIRS})
set(COBS_LINK_LIBRARIES xxhash ${COBS_LINK_LIBRARIES})
### use ZLIB ###
find_package(ZLIB REQUIRED)
set(COBS_INCLUDE_DIRS ${ZLIB_INCLUDE_DIRS} ${COBS_INCLUDE_DIRS})
set(COBS_LINK_LIBRARIES ${ZLIB_LIBRARIES} ${COBS_LINK_LIBRARIES})
### use Boost filesystem ###
find_package(Boost 1.42.0 COMPONENTS system filesystem)
if(${Boost_FOUND})
set(COBS_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} ${COBS_INCLUDE_DIRS})
set(COBS_LINK_LIBRARIES ${Boost_LIBRARIES} ${COBS_LINK_LIBRARIES})
endif()
set(COBS_LINK_LIBRARIES stdc++fs ${COBS_LINK_LIBRARIES})
### use TLX ###
add_subdirectory(extlib/tlx)
set(COBS_LINK_LIBRARIES tlx ${COBS_LINK_LIBRARIES})
################################################################################
### Descend into Subdirectories
# descend into library source
add_subdirectory(cobs)
# descend into programs
add_subdirectory(src)
# descend into tests
add_subdirectory(tests)
# descend into python
add_subdirectory(python)
################################################################################