forked from rpclib/rpclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
260 lines (222 loc) · 7 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
cmake_minimum_required(VERSION 3.9.0)
project(rpc VERSION 2.2.1)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
include(policies)
include(msvc_support)
include(coverage)
include(check_warning_flag)
#
# Options
#
option(RPCLIB_BUILD_TESTS
"Build unit RPCLIB_BUILD_TESTS."
OFF)
option(RPCLIB_GENERATE_COMPDB
"Generate compilation database. Useful for YCM."
OFF)
option(RPCLIB_BUILD_EXAMPLES
"Build examples."
OFF)
option(RPCLIB_ENABLE_LOGGING
"ALlow logging in the library for debug purposes."
OFF)
option(RPCLIB_ENABLE_COVERAGE
"Generate coverage information"
OFF)
option(RPCLIB_MSVC_STATIC_RUNTIME
"MSVC only: build with /MT instead of /MD"
OFF)
#
# Other configuration values
#
set(RPCLIB_DEFAULT_PORT 8080
CACHE STRING "Default port used for running tests and examples")
set(RPCLIB_DEFAULT_BUFFER_SIZE "1024 << 10"
CACHE STRING "Default buffer size")
set(RPCLIB_CXX_STANDARD 11 CACHE STRING
"C++ version used to build rpclib (Currently: Only 11 and 14 supported)")
if(RPCLIB_GENERATE_COMPDB)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON") # for YCM
add_custom_command(PROJECT_NAME ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy ${CMAKE_BINARY_DIR}/compile_commands.json ${CMAKE_BINARY_DIR}/../compile_commands.json)
endif()
if(NOT ${RPCLIB_CXX_STANDARD} EQUAL 14 AND
NOT ${RPCLIB_CXX_STANDARD} EQUAL 11)
message(fatal_error "Unsupported C++ standard: ${RPCLIB_CXX_STANDARD}")
endif()
set(CMAKE_CXX_STANDARD ${RPCLIB_CXX_STANDARD})
#
# Compile & install the library
#
if (WIN32)
set(RPCLIB_OS_DEF "RPCLIB_WIN32")
elseif (LINUX)
set(RPCLIB_OS_DEF "RPCLIB_LINUX")
elseif (APPLE)
set(RPCLIB_OS_DEF "RPCLIB_MAC")
endif()
set(RPCLIB_DEPENDENCIES "${CMAKE_CURRENT_LIST_DIR}/dependencies")
configure_file(
"${PROJECT_SOURCE_DIR}/include/rpc/version.h.in"
"${PROJECT_SOURCE_DIR}/include/rpc/version.h")
configure_file(
"${PROJECT_SOURCE_DIR}/include/rpc/config.h.in"
"${PROJECT_SOURCE_DIR}/include/rpc/config.h")
configure_file(
"${PROJECT_SOURCE_DIR}/doc/pages/versions.md.in"
"${PROJECT_SOURCE_DIR}/doc/pages/versions.md")
file(GLOB_RECURSE RPCLIB_HEADERS
include/rpc/*.h
include/msgpack/*.hpp)
file(GLOB_RECURSE DEP_HEADERS
${RPCLIB_DEPENDENCIES}/include/*.h
${RPCLIB_DEP_LIBRARIDEPENDENCIES}/include/*.hpp)
set(DEP_SOURCES
${RPCLIB_DEPENDENCIES}/src/format.cc
${RPCLIB_DEPENDENCIES}/src/posix.cc)
add_library(${PROJECT_NAME}
lib/rpc/dispatcher.cc
lib/rpc/server.cc
lib/rpc/client.cc
lib/rpc/this_handler.cc
lib/rpc/this_session.cc
lib/rpc/this_server.cc
lib/rpc/rpc_error.cc
lib/rpc/detail/server_session.cc
lib/rpc/detail/response.cc
lib/rpc/detail/client_error.cc
lib/rpc/nonstd/optional.cc
${DEP_SOURCES}
${DEP_HEADERS}
${RPCLIB_HEADERS})
# Perform steps and checks required for MSVC support
rpclib_msvc_support()
set(RPCLIB_BUILD_FLAGS "") # reset flags
if(RPCLIB_ENABLE_COVERAGE)
enable_coverage(${PROJECT_NAME})
endif()
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# clang is the compiler used for developing mainly, so
# this is where I set the highest warning level
list(APPEND RPCLIB_BUILD_FLAGS
-Wall -pedantic -Weverything -Wno-c++98-compat
-Wno-c++98-compat-pedantic -Wno-padded -Wno-missing-prototypes
-Wno-undef -pthread)
set(UNUSED_LAMBDA_CAPTURE_WARN_SUPPORTED)
check_warning_flag("unused-lambda-capture" UNUSED_LAMBDA_CAPTURE_WARN_SUPPORTED)
if(${UNUSED_LAMBDA_CAPTURE_WARN_SUPPORTED})
list(APPEND RPCLIB_BUILD_FLAGS -Wno-no-unused-lambda-capture)
endif()
check_warning_flag("zero-as-null-pointer-constant" ZERO_AS_NULL_POINTER_CONSTANT_WARN_SUPPORTED)
set(ZERO_AS_NULL_POINTER_CONSTANT_WARN_SUPPORTED)
if(${ZERO_AS_NULL_POINTER_CONSTANT_WARN_SUPPORTED})
list(APPEND RPCLIB_BUILD_FLAGS -Wno-zero-as-null-pointer-constant)
endif()
endif()
if (RPCLIB_EXTRA_BUILD_FLAGS)
list(APPEND RPCLIB_BUILD_FLAGS ${RPCLIB_EXTRA_BUILD_FLAGS})
endif()
target_compile_definitions(${PROJECT_NAME}
PRIVATE
"${RPCLIB_COMPILE_DEFINITIONS}"
"${RPCLIB_ARCH_DEF}"
"ASIO_STANDALONE"
"RPCLIB_ASIO=clmdep_asio"
"RPCLIB_FMT=clmdep_fmt"
PUBLIC
"${RPCLIB_OS_DEF}"
"RPCLIB_MSGPACK=clmdep_msgpack"
)
if(RPCLIB_ENABLE_LOGGING)
target_compile_definitions(${PROJECT_NAME} PRIVATE "RPCLIB_ENABLE_LOGGING")
endif()
if(RPCLIB_BUILD_FLAGS)
target_compile_options(${PROJECT_NAME} PRIVATE ${RPCLIB_BUILD_FLAGS})
endif()
if(RPCLIB_COMPILE_DEFINITIONS)
set_target_properties(${PROJECT_NAME}
PROPERTIES
COMPILE_DEFINITIONS "${RPCLIB_COMPILE_DEFINITIONS}")
endif()
target_link_libraries(${PROJECT_NAME} ${RPCLIB_DEP_LIBRARIES})
target_include_directories(
${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_include_directories(
${PROJECT_NAME} SYSTEM
PRIVATE ${RPCLIB_DEPENDENCIES}/include
)
install(TARGETS ${PROJECT_NAME} DESTINATION lib EXPORT rpclibTargets)
install(DIRECTORY include/
DESTINATION include
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.hpp"
PATTERN "*.inl"
PATTERN "*.in" EXCLUDE)
#
# Unit tests
#
if(RPCLIB_BUILD_TESTS)
add_subdirectory(tests)
endif()
#
# Example programs
#
if(RPCLIB_BUILD_EXAMPLES)
set(RPCLIB_ROOT_DIR "${PROJECT_SOURCE_DIR}")
set(RPCLIB_PROJECT_NAME "${CMAKE_PROJECT_NAME}")
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/localbuild")
set(ENV{RPCLIB_DEFAULT_PORT} "${RPCLIB_DEFAULT_PORT}")
add_subdirectory(examples/echo)
add_subdirectory(examples/mandelbrot)
add_subdirectory(examples/calculator)
endif()
#
# Cmake Package
#
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/cmake/rpclibConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
set(CONFIG_PACKAGE_LOCATION lib/cmake/rpclib)
set(INCLUDE_INSTALL_DIR include/ )
configure_package_config_file(cmake/rpclibConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/rpclibConfig.cmake
INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION}
PATH_VARS INCLUDE_INSTALL_DIR
)
export(EXPORT rpclibTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/rpclibTargets.cmake"
NAMESPACE rpclib::
)
install(EXPORT rpclibTargets
FILE rpclibTargets.cmake
NAMESPACE rpclib::
DESTINATION ${CONFIG_PACKAGE_LOCATION}
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/cmake/rpclibConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake/rpclibConfigVersion.cmake
DESTINATION ${CONFIG_PACKAGE_LOCATION}
)
#
# Pkg-config
#
if(NOT MSVC) # Don't install pkg-config files when building with MSVC
# Variables for pkg-config files
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "")
set(libdir "${CMAKE_INSTALL_PREFIX}/lib")
set(includedir "${CMAKE_INSTALL_PREFIX}/include")
set(rpclib_version ${PROJECT_VERSION})
get_target_property(rpclib_cflags ${PROJECT_NAME} COMPILE_OPTIONS)
string(REPLACE ";" " " rpclib_cflags "${rpclib_cflags}") # Convert list to string
configure_file(rpclib.pc.in "${CMAKE_CURRENT_BINARY_DIR}/rpclib.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/rpclib.pc" DESTINATION "${libdir}/pkgconfig")
endif()