-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCMakeLists.txt
375 lines (330 loc) · 13.4 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# vim:ts=2:sw=2:et
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
project(utxx VERSION 1.4.1)
#===============================================================================
# CMAKE options customization
#===============================================================================
option(WITH_THRIFT "Enable to compile UTXX with Thrift" OFF)
option(VERBOSE "Turn verbosity on|off" OFF)
option(WITH_ENUM_SERIALIZATION "Turn enum serialization support on|off" OFF)
if(VERBOSE)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif()
if(WITH_ENUM_SERIALIZATION)
set(UTXX_ENUM_SUPPORT_SERIALIZATION ON)
endif()
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE)
string(TOLOWER ${TOOLCHAIN} toolchain)
# Custom extensions
include(${CMAKE_CURRENT_SOURCE_DIR}/build-aux/CMakeEx.txt)
if (DEFINED ENV{CMAKE_CXX_STANDARD})
set(CMAKE_CXX_STANDARD $ENV{CMAKE_CXX_STANDARD})
endif()
#-------------------------------------------------------------------------------
# Toolchain
#-------------------------------------------------------------------------------
# See also build/CMakeInit.txt
if("${toolchain}" STREQUAL "gcc")
if (NOT CMAKE_C_COMPILER)
set(CMAKE_C_COMPILER "gcc")
endif()
if (NOT CMAKE_CXX_COMPILER)
set(CMAKE_CXX_COMPILER "g++")
endif()
add_definitions(-Wno-strict-aliasing -fopenmp -Wall)
if("${CMAKE_BUILD_TYPE}" STREQUAL "release")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "5.0")
# When this option is on, compiler versions below 5.0 cause errors with
# boost:
# undefined reference to
# `virtual thunk to boost::exception_detail::clone_impl<...'
# undefined reference to
# `boost::exception_detail::error_info_injector<...'
add_definitions(-flto)
endif()
add_definitions(-funroll-loops -fomit-frame-pointer)
# The following will omit all symbol information from the build:
#add_definitions(-Wl,-s)
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s")
endif()
elseif("${toolchain}" STREQUAL "intel")
set(CMAKE_C_COMPILER "icc")
set(CMAKE_CXX_COMPILER "icpc")
add_definitions(-openmp)
elseif("${toolchain}" STREQUAL "clang")
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
add_definitions(-Wall)
else()
message(FATAL_ERROR "Invalid toolchain: ${TOOLCHAIN}")
endif()
# Append "_d" to the library when doing debug build
if (CMAKE_BUILD_TYPE STREQUAL "debug")
set(LIB_SUFFIX "_d")
endif()
# Note: explicit c++14 definitions done in CMakeInit.txt.
# Alternative is to set for each target:
# target_compile_features(${PROJECT_NAME} PRIVATE cxx_lambda_init_captures)
add_definitions(
-D_REENTRANT
-Wno-unused-local-typedefs
-Wno-deprecated-declarations
-DBOOST_SYSTEM_NO_DEPRECATED
-DBOOST_ALLOW_DEPRECATED_HEADERS
)
message(STATUS "Configuring for the "
"${BoldMagenta}${TOOLCHAIN}${ClrReset} toolchain version "
"${CMAKE_CXX_COMPILER_VERSION} "
"${BoldMagenta}${CMAKE_BUILD_TYPE}${ClrReset} build")
#-------------------------------------------------------------------------------
# Policies
#-------------------------------------------------------------------------------
# Don't curse at non-existing dependencies (since we use code generation)
#cmake_policy(SET CMP0046 OLD)
# RPATH configuration
# ===================
# Don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH FALSE)
# When building, don't use the install RPATH already
# (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_BINARY_DIR}/src:${CMAKE_INSTALL_PREFIX}/lib")
# Add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
#-------------------------------------------------------------------------------
# Platform-specific checks
#-------------------------------------------------------------------------------
#include(${CMAKE_ROOT}/Modules/CheckTypeSize.cmake)
#include(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
include(${CMAKE_ROOT}/Modules/FindZLIB.cmake)
include(${CMAKE_ROOT}/Modules/CheckStructHasMember.cmake)
include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
if(ZLIB_FOUND)
set(UTXX_HAVE_LIBZ 1)
list(APPEND UTXX_LIBS ${ZLIB_LIBRARIES})
endif()
execute_process(COMMAND getconf LEVEL1_DCACHE_LINESIZE OUTPUT_VARIABLE UTXX_CL_SIZE)
#check_type_size(size_t SIZE_T)
# Needed for Thrift
CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H)
CHECK_INCLUDE_FILE(netinet/in.h HAVE_NETINET_IN_H)
# Needed for pcap.hpp tests
#CHECK_STRUCT_HAS_MEMBER("struct tcphdr" th_flags netinet/tcp.h UTXX_HAVE_TCPHDR_TH_FLAGS_H)
message("TCP: ${UTXX_HAVE_TCPHDR_TH_FLAGS_H}")
#-------------------------------------------------------------------------------
# Dependent packages and their directory locations
#-------------------------------------------------------------------------------
find_package(PkgConfig)
set(PKG_ROOT_DIR "/opt/pkg" CACHE STRING "Package root directory")
# Python
if(NOT DEFINED ENV{WITH_PYTHON2} OR "$ENV{WITH_PYTHON2}" STREQUAL "0")
find_package(Python3 COMPONENTS Interpreter)
endif()
if(Python3_FOUND)
set(Python_EXECUTABLE ${Python3_EXECUTABLE})
set(Python_LXML "python3-lxml")
configure_file(${CMAKE_SOURCE_DIR}/bin/config_validator_codegen.py3
${CMAKE_BINARY_DIR}/bin/config_validator_codegen.py
COPYONLY)
else()
find_package(Python2 COMPONENTS Interpreter)
set(Python_LXML "python-lxml")
if(NOT Python2_FOUND)
message(FATAL_ERROR "Missing Python version 3.x or 2.7 or later!")
elseif("${Python2_VERSION_MAJOR}.${Python2_VERSION_MINOR}" VERSION_LESS 2.7)
message(FATAL_ERROR "Python version >= 2.7 is required!")
endif()
set(Python_EXECUTABLE ${Python2_EXECUTABLE})
configure_file(${CMAKE_SOURCE_DIR}/bin/config_validator_codegen.py2
${CMAKE_BINARY_DIR}/bin/config_validator_codegen.py
COPYONLY)
endif()
# Find python-lxml
execute_process(COMMAND ${Python_EXECUTABLE} -c
"from distutils.sysconfig import *;print(get_python_lib(plat_specific=True))"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT EXISTS "${PYTHON_SITE_PACKAGES}/lxml")
message(FATAL_ERROR "${Python_LXML} is not installed!")
else()
message(STATUS "Found ${Python_LXML}: ${PYTHON_SITE_PACKAGES}/lxml")
endif()
if(WITH_THRIFT)
if (NOT Thrift_DIR)
set(Thrift_DIR "${PKG_ROOT_DIR}/thrift/current")
endif()
# Determine thrift include/libs
set(ENV{PKG_CONFIG_PATH} "${Thrift_DIR}/gcc/lib/pkgconfig:${Thrift_DIR}/lib/pkgconfig")
pkg_search_module(Thrift thrift)
if (Thrift_FOUND)
message(STATUS "Found thrift: ${Thrift_DIR}")
set(UTXX_HAVE_THRIFT_H 1)
endif()
endif()
# Boost (with local modifications):
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREAD ON)
set(Boost_NO_SYSTEM_PATHS ON)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/build")
find_package(Boost 1.58.0 REQUIRED COMPONENTS
system filesystem date_time program_options thread regex
unit_test_framework timer)
if(Boost_FOUND)
#include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
#link_directories(${Boost_LIBRARY_DIRS})
set(UTXX_HAVE_BOOST_TIMER_TIMER_HPP 1)
message(STATUS "Found boost: ${Boost_LIBRARY_DIRS}")
endif()
set(Boost_LIBRARIES
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_REGEX_LIBRARY}
${Boost_DATE_TIME_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_IOSTREAMS_LIBRARY}
)
set(AddCleanFiles) # Additional clean files
#===============================================================================
# Code generation
#===============================================================================
macro(XML_CFG TargetSrcList File)
get_filename_component(FileName ${File} NAME)
get_filename_component(BaseName ${File} NAME_WE) # Name without extension
get_filename_component(DirName ${File} DIRECTORY)
if (DirName MATCHES "^/")
set(SrcFile "${File}")
else()
if (DirName STREQUAL "")
set(DirName "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
set(SrcFile "${DirName}/${File}")
endif()
string(REGEX REPLACE "^${CMAKE_SOURCE_DIR}/?" "" RelDirName ${DirName})
string(REGEX REPLACE "^([^/].+)" "/\\1" RelDirName ${RelDirName})
set(GenFileName "${CMAKE_BINARY_DIR}${RelDirName}/generated/${BaseName}.generated.hpp")
get_filename_component(GenDirName ${GenFileName} DIRECTORY)
set(Deps ${ARGN})
PREFIX(Deps "${CMAKE_CURRENT_SOURCE_DIR}/" "")
message(STATUS " >> ${SrcFile} -> ${GenFileName}")
if (Deps STREQUAL "")
unset(DepComment)
else()
set(DepComment "\n Dependencies: ${Deps}")
endif()
set(GenIncDir ${CMAKE_CURRENT_SOURCE_DIR}:${CMAKE_SOURCE_DIR}/include:${CMAKE_BINARY_DIR}/include)
file(MAKE_DIRECTORY "${GenDirName}")
add_custom_command(
OUTPUT "${GenFileName}"
COMMAND "${CMAKE_BINARY_DIR}/bin/config_validator_codegen.py"
-f "${SrcFile}" -o "${GenFileName}" --overwrite
-d ${GenIncDir}
MAIN_DEPENDENCY "${SrcFile}"
# The following dependency may be extraneous due to some .hpp file
# depending on a specific .xml file which "includes" the following file:
DEPENDS ${Deps}
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
COMMENT ">>> ${BoldBlue}Generating: ${FileName} -> ${GenFileName}${DepComment}${ClrReset}"
)
# TargetSrcList contains the list name and therefore ${TargetSrcList} is
# the list itself, and ${${TargetSrcList}} is the expanded list
set(${TargetSrcList} ${${TargetSrcList}} ${GenFileName})
endmacro(XML_CFG)
#-------------------------------------------------------------------------------
# MAKE options
#-------------------------------------------------------------------------------
add_custom_target(build-version
ALL
COMMAND ${CMAKE_SOURCE_DIR}/build-aux/make-version.sh -d ${REBUILD_VERSION}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT ">>> Verifying include/utxx/version.hpp"
)
#add_custom_target(vars
# COMMAND ${CMAKE_COMMAND} -H${CMAKE_SOURCE_DIR} -B${CMAKE_BINARY_DIR} -LA
#)
include_directories(
SYSTEM
${Boost_INCLUDE_DIRS}
${Thrift_INCLUDE_DIRS}
)
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
)
link_directories(
${Boost_LIBRARY_DIRS}
${Thrift_LIBRARY_DIRS}
)
#-------------------------------------------------------------------------------
# Configure files
#-------------------------------------------------------------------------------
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/config.h"
@ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY)
#-------------------------------------------------------------------------------
# Srcs and Targets:
#-------------------------------------------------------------------------------
list(APPEND UTXX_LIBS ${Thrift_LIBRARIES} aio pthread)
add_subdirectory(src)
add_subdirectory(test)
#===============================================================================
# Installation
#===============================================================================
install(
DIRECTORY ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "*.hxx" PATTERN "*.ipp" PATTERN "*.x??"
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME}/config.h
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}
)
install(
PROGRAMS ${CMAKE_BINARY_DIR}/bin/config_validator_codegen.py
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
)
install(
FILES ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig
)
install(
FILES ${CMAKE_SOURCE_DIR}/LICENSE
${CMAKE_SOURCE_DIR}/README.md
DESTINATION ${CMAKE_INSTALL_PREFIX}/share
)
#===============================================================================
# Uninstallation
# Prereq: copy the uninstall.cmake file to the appropriate CMAKE_MODULE_PATH.
#===============================================================================
set_directory_properties(
PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${AddCleanFiles}"
)
#add_custom_target(
# uninstall "${CMAKE_COMMAND}" -P "${CMAKE_MODULE_PATH}/uninstall.cmake"
#)
#===============================================================================
# CTEST options
#===============================================================================
enable_testing()
add_test(test-utxx test/test_utxx -l message)
#===============================================================================
# Documentation options
#===============================================================================
# add a target to generate API documentation with Doxygen
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/build-aux/Doxyfile.in
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
@ONLY)
add_custom_target(doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "${ClrBold}Generating API documentation with Doxygen${ClrReset}"
VERBATIM
)
endif()
# Post-install script (installation of symlinks):
install(SCRIPT ${CMAKE_SOURCE_DIR}/build-aux/install-symlinks.cmake)