forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
423 lines (367 loc) · 13.8 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
cmake_minimum_required(VERSION 3.13)
project (pulsar-cpp)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake_modules")
execute_process(COMMAND cat ${PROJECT_SOURCE_DIR}/version.txt OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE PULSAR_CLIENT_VERSION)
message(STATUS "Pulsar Client version: ${PULSAR_CLIENT_VERSION}")
execute_process(COMMAND ${PROJECT_SOURCE_DIR}/build-support/gen-pulsar-version-macro.py OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE PULSAR_CLIENT_VERSION_MACRO)
message(STATUS "Pulsar Client version macro: ${PULSAR_CLIENT_VERSION_MACRO}")
set(PVM_COMMENT "This is generated from Version.h.in by CMAKE. DO NOT EDIT DIRECTLY")
configure_file(templates/Version.h.in include/pulsar/Version.h @ONLY)
option(LINK_STATIC "Link against static libraries" OFF)
if (VCPKG_TRIPLET)
message(STATUS "Use vcpkg, triplet is ${VCPKG_TRIPLET}")
set(CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}")
message(STATUS "Use CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
set(PROTOC_PATH "${CMAKE_PREFIX_PATH}/tools/protobuf/protoc")
message(STATUS "Use protoc: ${PROTOC_PATH}")
set(VCPKG_DEBUG_ROOT "${PROJECT_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}/debug")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ZLIB_ROOT ${VCPKG_DEBUG_ROOT})
set(OPENSSL_ROOT_DIR ${VCPKG_DEBUG_ROOT})
set(CMAKE_PREFIX_PATH ${VCPKG_DEBUG_ROOT} ${CMAKE_PREFIX_PATH})
endif ()
if (VCPKG_TRIPLET MATCHES ".*-static")
set(LINK_STATIC ON)
else ()
set(LINK_STATIC OFF)
endif ()
endif()
MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC})
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "ccache")
MESSAGE(STATUS "Using CCache")
endif(CCACHE_PROGRAM)
MESSAGE(STATUS "ARCHITECTURE: ${CMAKE_SYSTEM_PROCESSOR}")
option(BUILD_DYNAMIC_LIB "Build dynamic lib" ON)
MESSAGE(STATUS "BUILD_DYNAMIC_LIB: " ${BUILD_DYNAMIC_LIB})
option(BUILD_STATIC_LIB "Build static lib" ON)
MESSAGE(STATUS "BUILD_STATIC_LIB: " ${BUILD_STATIC_LIB})
option(BUILD_TESTS "Build tests" ON)
MESSAGE(STATUS "BUILD_TESTS: " ${BUILD_TESTS})
option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF)
MESSAGE(STATUS "BUILD_PERF_TOOLS: " ${BUILD_PERF_TOOLS})
IF (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
ENDIF ()
MESSAGE(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE})
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
MESSAGE(STATUS "Threads library: " ${CMAKE_THREAD_LIBS_INIT})
set(Boost_NO_BOOST_CMAKE ON)
if (NOT CMAKE_CXX_STANDARD)
if (APPLE)
# The latest Protobuf dependency on macOS requires the C++17 support
set(CMAKE_CXX_STANDARD 17)
else ()
set(CMAKE_CXX_STANDARD 11)
endif ()
endif ()
set(CMAKE_C_STANDARD 11)
# Compiler specific configuration:
# https://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_definitions(-DWIN32_LEAN_AND_MEAN -DNOGDI -D_WIN32_WINNT=0x0501 -D_CRT_SECURE_NO_WARNINGS)
add_compile_options(/wd4244 /wd4267 /wd4018 /wd4715 /wd4251 /wd4275 /wd4819)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# ?? Don't have this to test with
else() # GCC or Clang are mostly compatible:
# Turn on warnings and enable warnings-as-errors:
add_compile_options(-Wall -Wformat-security -Wvla -Werror)
# Turn off certain warnings that are too much pain for too little gain:
add_compile_options(-Wno-sign-compare -Wno-deprecated-declarations -Wno-error=cpp)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR APPLE)
add_compile_options(-msse4.2 -mpclmul)
endif()
# Options unique to Clang or GCC:
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Qunused-arguments)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.1))
add_compile_options(-Wno-stringop-truncation)
endif()
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_definitions(-DBUILDING_PULSAR -DBOOST_ALL_NO_LIB -DBOOST_ALLOW_DEPRECATED_HEADERS)
# For dependencies other than OpenSSL, dynamic libraries are forbidden to link when LINK_STATIC is ON
if (LINK_STATIC)
if (NOT MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
endif()
endif ()
find_package(Boost REQUIRED)
message("Boost_INCLUDE_DIRS: " ${Boost_INCLUDE_DIRS})
set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/lib64/)
if (APPLE)
set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/local/opt/openssl/ /opt/homebrew/opt/openssl)
endif ()
find_package(OpenSSL REQUIRED)
message("OPENSSL_INCLUDE_DIR: " ${OPENSSL_INCLUDE_DIR})
message("OPENSSL_LIBRARIES: " ${OPENSSL_LIBRARIES})
if (APPLE)
# See https://github.com/apache/arrow/issues/35987
add_definitions(-DPROTOBUF_USE_DLLS)
# Use Config mode to avoid FindProtobuf.cmake does not find the Abseil library
find_package(Protobuf REQUIRED CONFIG)
else ()
find_package(Protobuf REQUIRED)
endif ()
message("Protobuf_INCLUDE_DIRS: " ${Protobuf_INCLUDE_DIRS})
message("Protobuf_LIBRARIES: " ${Protobuf_LIBRARIES})
# NOTE: CMake might not find curl and zlib on some platforms like Ubuntu, in this case, find them manually
set(CURL_NO_CURL_CMAKE ON)
find_package(curl QUIET)
if (NOT CURL_FOUND)
find_path(CURL_INCLUDE_DIRS NAMES curl/curl.h)
find_library(CURL_LIBRARIES NAMES curl curllib libcurl_imp curllib_static libcurl)
endif ()
message("CURL_INCLUDE_DIRS: " ${CURL_INCLUDE_DIRS})
message("CURL_LIBRARIES: " ${CURL_LIBRARIES})
if (NOT CURL_INCLUDE_DIRS OR NOT CURL_LIBRARIES)
message(FATAL_ERROR "Could not find libcurl")
endif ()
find_package(zlib QUIET)
if (NOT ZLIB_FOUND)
find_path(ZLIB_INCLUDE_DIRS NAMES zlib.h)
find_library(ZLIB_LIBRARIES NAMES z zlib zdll zlib1 zlibstatic)
endif ()
message("ZLIB_INCLUDE_DIRS: " ${ZLIB_INCLUDE_DIRS})
message("ZLIB_LIBRARIES: " ${ZLIB_LIBRARIES})
if (NOT ZLIB_INCLUDE_DIRS OR NOT ZLIB_LIBRARIES)
message(FATAL_ERROR "Could not find zlib")
endif ()
if (LINK_STATIC AND NOT VCPKG_TRIPLET)
find_library(LIB_ZSTD NAMES libzstd.a)
message(STATUS "ZStd: ${LIB_ZSTD}")
find_library(LIB_SNAPPY NAMES libsnappy.a)
message(STATUS "LIB_SNAPPY: ${LIB_SNAPPY}")
if (MSVC)
add_definitions(-DCURL_STATICLIB)
endif()
elseif (LINK_STATIC AND VCPKG_TRIPLET)
find_package(Protobuf REQUIRED)
message(STATUS "Found protobuf static library: " ${Protobuf_LIBRARIES})
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
find_library(ZLIB_LIBRARIES NAMES zlibd)
else ()
find_library(ZLIB_LIBRARIES NAMES zlib z)
endif ()
if (ZLIB_LIBRARIES)
message(STATUS "Found zlib static library: " ${ZLIB_LIBRARIES})
else ()
message(FATAL_ERROR "Failed to find zlib static library")
endif ()
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
find_library(CURL_LIBRARIES NAMES libcurl-d)
else ()
find_library(CURL_LIBRARIES NAMES libcurl)
endif ()
if (CURL_LIBRARIES)
message(STATUS "Found libcurl: ${CURL_LIBRARIES}")
else ()
message(FATAL_ERROR "Cannot find libcurl")
endif ()
find_library(LIB_ZSTD zstd)
if (LIB_ZSTD)
message(STATUS "Found ZSTD library: ${LIB_ZSTD}")
endif ()
find_library(LIB_SNAPPY NAMES snappy)
if (LIB_SNAPPY)
message(STATUS "Found Snappy library: ${LIB_SNAPPY}")
endif ()
else()
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
find_library(LIB_ZSTD zstdd HINTS "${VCPKG_DEBUG_ROOT}/lib")
else ()
find_library(LIB_ZSTD zstd)
endif ()
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
find_library(LIB_SNAPPY NAMES snappyd HINTS "${VCPKG_DEBUG_ROOT}/lib")
else ()
find_library(LIB_SNAPPY NAMES snappy libsnappy)
endif ()
endif ()
if (Boost_MAJOR_VERSION EQUAL 1 AND Boost_MINOR_VERSION LESS 69)
# Boost System does not require linking since 1.69
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} system)
MESSAGE(STATUS "Linking with Boost:System")
endif()
if (MSVC)
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} date_time)
endif()
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
# GCC 4.8.2 implementation of std::regex is buggy
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex)
set(CMAKE_CXX_FLAGS " -DPULSAR_USE_BOOST_REGEX")
MESSAGE(STATUS "Using Boost::Regex")
elseif (CMAKE_COMPILER_IS_GNUCC)
MESSAGE(STATUS "Using std::regex")
# Turn on color error messages and show additional help with errors (only available in GCC v4.9+):
add_compile_options(-fdiagnostics-show-option -fdiagnostics-color)
endif()
if(BUILD_PERF_TOOLS)
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} program_options)
endif()
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
if (BUILD_TESTS)
find_path(GTEST_INCLUDE_PATH gtest/gtest.h)
find_path(GMOCK_INCLUDE_PATH gmock/gmock.h)
endif ()
if (NOT APPLE AND NOT MSVC)
# Hide all non-exported symbols to avoid conflicts
add_compile_options(-fvisibility=hidden)
if (CMAKE_COMPILER_IS_GNUCC)
add_link_options(-Wl,--exclude-libs=ALL)
endif ()
endif ()
if (LIB_ZSTD)
set(HAS_ZSTD 1)
else ()
set(HAS_ZSTD 0)
endif ()
MESSAGE(STATUS "HAS_ZSTD: ${HAS_ZSTD}")
if (LIB_SNAPPY)
set(HAS_SNAPPY 1)
else ()
set(HAS_SNAPPY 0)
endif ()
MESSAGE(STATUS "HAS_SNAPPY: ${HAS_SNAPPY}")
set(ADDITIONAL_LIBRARIES $ENV{PULSAR_ADDITIONAL_LIBRARIES})
link_directories( $ENV{PULSAR_ADDITIONAL_LIBRARY_PATH} )
set(AUTOGEN_DIR ${PROJECT_BINARY_DIR}/generated)
file(MAKE_DIRECTORY ${AUTOGEN_DIR})
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${AUTOGEN_DIR}
${Boost_INCLUDE_DIRS}
${OPENSSL_INCLUDE_DIR}
${ZLIB_INCLUDE_DIRS}
${CURL_INCLUDE_DIRS}
${Protobuf_INCLUDE_DIRS}
${GTEST_INCLUDE_PATH}
${GMOCK_INCLUDE_PATH}
)
set(COMMON_LIBS
${COMMON_LIBS}
${CMAKE_THREAD_LIBS_INIT}
${Boost_REGEX_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_DATE_TIME_LIBRARY}
${CURL_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZLIB_LIBRARIES}
${ADDITIONAL_LIBRARIES}
${CMAKE_DL_LIBS}
)
if (APPLE)
# Protobuf_LIBRARIES is empty when finding Protobuf in Config mode
set(COMMON_LIBS ${COMMON_LIBS} protobuf::libprotobuf)
else ()
set(COMMON_LIBS ${COMMON_LIBS} ${Protobuf_LIBRARIES})
endif ()
if (MSVC)
set(COMMON_LIBS
${COMMON_LIBS}
${Boost_DATE_TIME_LIBRARY}
wldap32.lib
Normaliz.lib)
if (LINK_STATIC)
# add external dependencies of libcurl
set(COMMON_LIBS ${COMMON_LIBS} ws2_32.lib crypt32.lib)
# the default compile options have /MD, which cannot be used to build DLLs that link static libraries
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_CXX_FLAGS_DEBUG: " ${CMAKE_CXX_FLAGS_DEBUG})
message(STATUS "CMAKE_CXX_FLAGS_RELEASE: " ${CMAKE_CXX_FLAGS_RELEASE})
message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: " ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
endif ()
else()
set(COMMON_LIBS ${COMMON_LIBS} m)
endif()
if (USE_LOG4CXX)
set(COMMON_LIBS
${COMMON_LIBS}
${LOG4CXX_LIBRARY_PATH}
${APR_LIBRARY_PATH}
${APR_UTIL_LIBRARY_PATH}
${EXPAT_LIBRARY_PATH}
${ICONV_LIBRARY_PATH}
)
endif ()
if (HAS_ZSTD)
set(COMMON_LIBS ${COMMON_LIBS} ${LIB_ZSTD} )
endif ()
add_definitions(-DHAS_ZSTD=${HAS_ZSTD})
if (HAS_SNAPPY)
set(COMMON_LIBS ${COMMON_LIBS} ${LIB_SNAPPY} )
endif ()
add_definitions(-DHAS_SNAPPY=${HAS_SNAPPY})
if(NOT APPLE AND NOT MSVC)
set(COMMON_LIBS ${COMMON_LIBS} rt)
endif ()
link_directories(${PROJECT_BINARY_DIR}/lib)
set(LIB_NAME $ENV{PULSAR_LIBRARY_NAME})
if (NOT LIB_NAME)
set(LIB_NAME pulsar)
endif(NOT LIB_NAME)
set(CLIENT_LIBS
${COMMON_LIBS}
${LIB_NAME}
)
add_subdirectory(lib)
if(BUILD_PERF_TOOLS)
add_subdirectory(perf)
endif(BUILD_PERF_TOOLS)
if (BUILD_DYNAMIC_LIB)
add_subdirectory(examples)
endif()
if (BUILD_TESTS)
add_subdirectory(tests)
endif()
find_package(ClangTools)
set(BUILD_SUPPORT_DIR "${PROJECT_SOURCE_DIR}/build-support")
add_custom_target(format ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
0
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
${PROJECT_SOURCE_DIR}/lib
${PROJECT_SOURCE_DIR}/perf
${PROJECT_SOURCE_DIR}/examples
${PROJECT_SOURCE_DIR}/tests
${PROJECT_SOURCE_DIR}/tests/unix
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/wireshark)
# `make check-format` option (for CI test)
add_custom_target(check-format ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
1
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
${PROJECT_SOURCE_DIR}/lib
${PROJECT_SOURCE_DIR}/perf
${PROJECT_SOURCE_DIR}/examples
${PROJECT_SOURCE_DIR}/tests
${PROJECT_SOURCE_DIR}/tests/unix
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/wireshark)