forked from tgockel/zookeeper-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
282 lines (239 loc) · 12.5 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
cmake_minimum_required(VERSION 3.5)
file(READ src/zk/config.hpp CONFIG_HPP_STR)
string(REGEX REPLACE ".*# *define +ZKPP_VERSION_MAJOR +([0-9]+).*" "\\1" ZKPP_VERSION_MAJOR "${CONFIG_HPP_STR}")
string(REGEX REPLACE ".*# *define +ZKPP_VERSION_MINOR +([0-9]+).*" "\\1" ZKPP_VERSION_MINOR "${CONFIG_HPP_STR}")
string(REGEX REPLACE ".*# *define +ZKPP_VERSION_PATCH +([0-9]+).*" "\\1" ZKPP_VERSION_PATCH "${CONFIG_HPP_STR}")
set(ZKPP_VERSION "${ZKPP_VERSION_MAJOR}.${ZKPP_VERSION_MINOR}.${ZKPP_VERSION_PATCH}")
project(zookeeper-cpp
LANGUAGES CXX
VERSION "${ZKPP_VERSION}"
)
set(PROJECT_SO_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
message(STATUS "Software Version: ${ZKPP_VERSION}")
################################################################################
# CMake #
################################################################################
cmake_policy(VERSION 3.5)
cmake_policy(SET CMP0037 OLD) # allow generation of "test" target
set(CMAKE_REQUIRED_QUIET YES) # tell check_include_file_cxx to keep quiet
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/")
include(BuildFunctions)
include(CheckIncludeFileCXX)
include(ConfigurationSetting)
include(ListSplit)
include(ZooKeeper)
################################################################################
# Build Configuration #
################################################################################
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
message(STATUS "No build type selected, default to ${CMAKE_BUILD_TYPE}")
endif()
set(VALID_BUILD_TYPES Debug Release)
if(NOT ${CMAKE_BUILD_TYPE} IN_LIST VALID_BUILD_TYPES)
message(FATAL_ERROR "Invalid CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}\nValid build types are: ${VALID_BUILD_TYPES}")
endif()
message(STATUS "Configuration: ${CMAKE_BUILD_TYPE}")
set(ZKPP_SERVER_VERSIONS "3.5.3-beta;3.4.10"
CACHE STRING "The ZooKeeper server versions to run tests against. The first in the list is the default."
)
message(STATUS "Features:")
build_option(NAME CODE_COVERAGE
DOC "Enable code coverage (turns on the test-coverage target)"
DEFAULT OFF
CONFIGS_ON Debug
)
configuration_setting(NAME BUFFER
DOC "Type to use for zk::buffer"
DEFAULT STD_VECTOR
OPTIONS
STD_VECTOR
CUSTOM
)
configuration_setting(NAME FUTURE
DOC "Type to use for zk::future<T> and zk::promise<T>"
DEFAULT STD
OPTIONS
STD
STD_EXPERIMENTAL
CUSTOM
)
set(CXX_STANDARD c++17
CACHE STRING "The language standard to target for C++."
)
set(CXX_WARNINGS "-Wall -Wextra -Wconversion -Werror")
set(CXX_EXTRA_FLAGS "-Wl,--no-as-needed")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=${CXX_STANDARD} ${CXX_WARNINGS} -ggdb3 ${CXX_EXTRA_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DZKPP_DEBUG=1")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
################################################################################
# External Libraries #
################################################################################
find_library(zookeeper_LIBRARIES zookeeper_mt)
include_directories("${PROJECT_SOURCE_DIR}/src")
################################################################################
# GTest #
################################################################################
find_package(GTest)
if(GTest_FOUND)
# Unclear why GTest packaging behaves differently than every other find_package
set(gtest_LIBRARIES GTest::GTest)
elseif(EXISTS "/usr/src/gtest/src/")
# GTest's packaging on Ubuntu (libgtest-dev) contains all the source files instead of a library and headers. CMake has
# a package discovery for GTest, but it does not pick up on this for you, so we'll build it manually here.
message(STATUS "GTest is not installed, but the sources were found...adding them to the build")
if(EXISTS "/usr/src/gtest/src/gtest-all.cc")
set(gtest_lib_cpps "/usr/src/gtest/src/gtest-all.cc")
message(STATUS "Building with gtest-all.cc")
else()
file(GLOB gtest_lib_cpps "/usr/src/gtest/src/gtest-*.cc")
message(STATUS "Building with gtest_lib_cpps=${gtest_lib_cpps}")
endif()
add_library(gtest SHARED ${gtest_lib_cpps})
# GTest uses relative imports incorrectly, so make sure to add it to the include path.
target_include_directories(gtest PRIVATE "/usr/src/gtest")
# Also disable -Werror
target_compile_options(gtest PRIVATE "-Wno-error")
set(gtest_LIBRARIES gtest)
else()
message(SEND_ERROR "GTest was not found")
endif()
################################################################################
# Building #
################################################################################
build_module(NAME zkpp-tests
PATH src/zk/tests
LINK_LIBRARIES
${gtest_LIBRARIES}
)
build_module(NAME zkpp
PATH src/zk
NO_RECURSE
LINK_LIBRARIES
${zookeeper_LIBRARIES}
)
build_module(NAME zkpp-server
PATH src/zk/server
LINK_LIBRARIES
zkpp
)
target_link_libraries(zkpp_tests zkpp-server zkpp-server_tests)
################################################################################
# ZooKeeper Server Testing #
################################################################################
foreach(server_version IN LISTS ZKPP_SERVER_VERSIONS)
find_zookeeper_server(VERSION "${server_version}"
OUTPUT_CLASSPATH server_classpath
)
set(generated_cpp "${CMAKE_CURRENT_BINARY_DIR}/generated/src/zk/server/classpath_registration_${server_version}.cpp")
configure_file(src/zk/server/classpath_registration_template.cpp.in "${generated_cpp}" @ONLY)
target_sources(zkpp-server_tests PRIVATE "${generated_cpp}")
endforeach()
################################################################################
# Targets #
################################################################################
add_custom_target(test
COMMAND $<TARGET_FILE:zkpp-tests_prog>
"--gtest_output=xml:test-results.xml"
"--gtest_death_test_style=threadsafe"
DEPENDS zkpp-tests_prog
BYPRODUCTS test-results.xml
USES_TERMINAL
)
# Similar to test, but run it inside of GDB with GTest options one would want when running in GDB.
add_custom_target(gdbtest
COMMAND "gdb"
"-args"
$<TARGET_FILE:zkpp-tests_prog>
"--gtest_output=xml:test-results-gdb.xml"
"--gtest_death_test_style=threadsafe"
"--gtest_break_on_failure=1"
"--gtest_catch_exceptions=0"
DEPENDS zkpp-tests_prog
BYPRODUCTS test-results-gdb.xml
USES_TERMINAL
)
if(ZKPP_BUILD_OPTION_CODE_COVERAGE)
include(CodeCoverage)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
setup_target_for_coverage(test-coverage zkpp-tests_prog coverage "--gtest_death_test_style=threadsafe")
endif()
################################################################################
# Packaging #
################################################################################
set(ZKPP_PACKAGE_SYSTEM ""
CACHE STRING "The packaging system to generate a package build file for (leave blank to not build a package)"
)
if(ZKPP_PACKAGE_SYSTEM)
message(STATUS "Packaging system ${ZKPP_PACKAGE_SYSTEM}:")
set(PROJECT_PACKAGE_VERSION "${PROJECT_VERSION}-1") # TODO: Allow arbitrary tags here.
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(PROJECT_BUILD_ARCHITECTURE "amd64")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
set(PROJECT_BUILD_ARCHITECTURE "i386")
else()
set(PROJECT_BUILD_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR})
endif()
set(all_packages)
foreach(module zkpp zkpp-server)
message(STATUS " ${module}:")
set(package_name ${module})
string(REGEX REPLACE "-" "/" header_path "${module}")
string(REGEX REPLACE "pp" "" header_path "${header_path}")
if(ZKPP_PACKAGE_SYSTEM STREQUAL "DEB")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/install/deb/lib${package_name}")
message(STATUS " lib${package_name}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/install/deb/lib${package_name}/control.in"
"${CMAKE_CURRENT_BINARY_DIR}/install/lib${package_name}${PROJECT_SO_VERSION}/DEBIAN/control"
@ONLY IMMEDIATE
)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/install/deb/lib${package_name}/postinst.in")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/install/deb/lib${package_name}/postinst.in"
"${CMAKE_CURRENT_BINARY_DIR}/intermediate/lib${package_name}${PROJECT_SO_VERSION}/DEBIAN/postinst"
@ONLY IMMEDIATE
)
file(COPY "${CMAKE_CURRENT_BINARY_DIR}/intermediate/lib${package_name}${PROJECT_SO_VERSION}/DEBIAN/postinst"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/install/lib${package_name}${PROJECT_SO_VERSION}/DEBIAN"
FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)
endif()
add_custom_target("lib${package_name}.deb"
BYPRODUCTS "install/lib${package_name}${PROJECT_SO_VERSION}.deb"
COMMAND rm -rf "install/lib${package_name}${PROJECT_SO_VERSION}/usr/local/lib"
COMMAND mkdir -p "install/lib${package_name}${PROJECT_SO_VERSION}/usr/local/lib"
COMMAND cp "$<TARGET_FILE:${module}>" "install/lib${package_name}${PROJECT_SO_VERSION}/usr/local/lib"
COMMAND dpkg --build "install/lib${package_name}${PROJECT_SO_VERSION}"
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/install/lib${package_name}${PROJECT_SO_VERSION}/DEBIAN/control"
${module}
)
list(APPEND all_packages "lib${package_name}.deb")
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/install/deb/lib${package_name}-dev")
message(STATUS " lib${package_name}-dev")
set(staging_dir "install/lib${package_name}${PROJECT_SO_VERSION}-dev/usr/local/include/${header_path}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/install/deb/lib${package_name}-dev/control.in"
"${CMAKE_CURRENT_BINARY_DIR}/install/lib${package_name}${PROJECT_SO_VERSION}-dev/DEBIAN/control"
@ONLY IMMEDIATE
)
add_custom_target("lib${package_name}-dev.deb"
BYPRODUCTS "install/lib${package_name}${PROJECT_SO_VERSION}-dev.deb"
COMMAND rm -rf "${staging_dir}"
COMMAND mkdir -p "${staging_dir}"
COMMAND cp -t "${staging_dir}" ${${module}_LIBRARY_HEADERS}
COMMAND dpkg --build "install/lib${package_name}${PROJECT_SO_VERSION}-dev"
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/install/lib${package_name}${PROJECT_SO_VERSION}-dev/DEBIAN/control"
${${module}_LIBRARY_HEADERS}
)
list(APPEND all_packages "lib${package_name}-dev.deb")
endif()
else()
message(FATAL_ERROR "Unknown ZKPP_PACKAGE_SYSTEM=${ZKPP_PACKAGE_SYSTEM}")
endif()
endforeach()
add_custom_target(package
DEPENDS ${all_packages}
COMMENT "Built all packages"
)
endif()