-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
406 lines (374 loc) · 15.5 KB
/
Copy pathCMakeLists.txt
File metadata and controls
406 lines (374 loc) · 15.5 KB
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
# Copyright 2016-2023. Couchbase, Inc.
# All Rights Reserved.
#
# Licensed 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.19)
# needed for CMAKE_MSVC_RUNTIME_LIBRARY
cmake_policy(SET CMP0091 NEW)
include(FetchContent)
set(CMAKE_CXX_STANDARD 17)
function(get_python_version)
if(DEFINED ENV{PYCBC_PYTHON3_EXECUTABLE})
message(STATUS "Using python3 executable from environment: $ENV{PYCBC_PYTHON3_EXECUTABLE}")
set(PYTHON3_EXE $ENV{PYCBC_PYTHON3_EXECUTABLE})
else()
message(STATUS "Using default python3 executable")
set(PYTHON3_EXE "python3")
endif()
# Be careful with whitespace
execute_process(COMMAND ${PYTHON3_EXE} -c
"import platform
import re
version = platform.python_version()
match = re.search(r'\\d+\\.\\d+\\.\\d+', version)
print(match.group())"
OUTPUT_VARIABLE local_python_version
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(LOCAL_PYTHON_VERSION "${local_python_version}" PARENT_SCOPE)
endfunction()
if(WIN32)
# cmake-format: off
# MultiThreaded$<$<CONFIG:Debug>:Debug>DLL for /MD compile flag
# MultiThreaded$<$<CONFIG:Debug>:Debug> for /MT compile flag
# cmake-format: on
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
add_definitions(/bigobj)
add_definitions(-D_WIN32_WINNT=0x0601)
endif()
project(couchbase_client)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
get_python_version()
message(STATUS "LOCAL_PYTHON_VERSION=${LOCAL_PYTHON_VERSION}")
if(LOCAL_PYTHON_VERSION)
set(Python3_FIND_VIRTUALENV FIRST)
message(STATUS "Attempting to find python version: ${LOCAL_PYTHON_VERSION}")
find_package(Python3 ${LOCAL_PYTHON_VERSION} EXACT COMPONENTS Interpreter Development.Module)
else()
find_package(Python3 COMPONENTS Interpreter Development.Module)
endif()
if(WIN32)
set(PYCBC_C_MOD_SUFFIX_DEFAULT ".pyd")
else()
set(PYCBC_C_MOD_SUFFIX_DEFAULT ".so")
endif()
# Normally set by pycbc_build_setup.py to the building interpreter's own
# sysconfig EXT_SUFFIX (e.g. .cpython-310-darwin.so). This is what makes a
# version-specific (non-abi3) build loadable only by a matching interpreter;
# the plain fallback below is for a raw `cmake` invocation outside setup.py.
set(PYCBC_C_MOD_SUFFIX "${PYCBC_C_MOD_SUFFIX_DEFAULT}" CACHE STRING "Compiled extension module suffix, including any interpreter tag")
file(READ "${PROJECT_SOURCE_DIR}/couchbase/_version.py" PYCBC_VERSION_CONTENTS)
string(REGEX MATCH "__version__.*([0-9]+\\.[0-9]+\\.[0-9]+)(\\.?[+a-z0-9]*)" PYCBC_FOUND_VERSION ${PYCBC_VERSION_CONTENTS})
set(PYCBC_VERSION "${CMAKE_MATCH_1}")
if(NOT CMAKE_MATCH_2 STREQUAL "")
set(PYCBC_VERSION "${PYCBC_VERSION}${CMAKE_MATCH_2}")
endif()
message(STATUS "PYCBC_VERSION=${PYCBC_VERSION}")
option(USE_STATIC_BORINGSSL "Statically link BoringSSL instead of dynamically linking OpenSSL" FALSE)
message(STATUS "USE_STATIC_BORINGSSL=${USE_STATIC_BORINGSSL}")
if(NOT USE_STATIC_BORINGSSL)
set(COUCHBASE_CXX_CLIENT_POST_LINKED_OPENSSL
ON
CACHE BOOL "" FORCE)
if(OPENSSL_ROOT_DIR)
message(STATUS "OPENSSL_ROOT_DIR set to ${OPENSSL_ROOT_DIR}, calling finder...")
find_package(OpenSSL REQUIRED)
endif()
if(OPENSSL_FOUND)
message(STATUS "OpenSSL found, OPENSSL_ROOT_DIR set to ${OPENSSL_ROOT_DIR}")
else()
if(WIN32)
if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
message(STATUS "++ 64 bit architecture")
set(PKGARCH "amd64")
else()
message(STATUS "++ 32 bit architecture")
set(PKGARCH "win32")
endif()
if(NOT OPENSSL_VERSION)
message(STATUS "No OpenSSL version set...cannot attempt to download.")
else()
# see pycbc_build_setup.py for default OpenSSL versions
FetchContent_Declare(openssl
URL https://github.com/python/cpython-bin-deps/archive/openssl-bin-${OPENSSL_VERSION}.zip)
message(STATUS "fetching OpenSSL version: ${OPENSSL_VERSION}")
FetchContent_Populate(openssl)
message(STATUS "Downloaded OpenSSL: ${openssl_SOURCE_DIR}/${PKGARCH}")
set(OPENSSL_ROOT_DIR ${openssl_SOURCE_DIR}/${PKGARCH})
endif()
elseif(APPLE)
# we were not supplied an OPENSSL_ROOT_DIR, so for macos assume brew is how it is installed, if it is...
find_program(BREW_COMMAND brew)
if(BREW_COMMAND)
message(STATUS "brew command: ${BREW_COMMAND}")
set(BREW_OPENSSL "openssl@3")
if(USE_OPENSSLV1_1)
set(BREW_OPENSSL "openssl@1.1")
message(STATUS "Using OpenSSL v1.1 from homebrew")
else()
message(STATUS "Using OpenSSL v3 from homebrew")
endif()
execute_process(
COMMAND ${BREW_COMMAND} --prefix ${BREW_OPENSSL}
OUTPUT_VARIABLE BREW_OPENSSL_PREFIX
RESULT_VARIABLE BREW_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "brew result: ${BREW_RESULT}, prefix: ${BREW_OPENSSL_PREFIX}")
if(BREW_RESULT EQUAL 0)
set(OPENSSL_ROOT_DIR
${BREW_OPENSSL_PREFIX}
CACHE INTERNAL "" FORCE)
message(STATUS "brew set OPENSSL_ROOT_DIR to ${OPENSSL_ROOT_DIR}, finding OpenSSL...")
endif()
endif()
else()
message(STATUS "Not mac or windows, so assuming OpenSSL v1.1 is installed and findable...")
endif()
find_package(OpenSSL REQUIRED)
endif()
message(STATUS "Adding ${OPENSSL_INCLUDE_DIR} to include dirs...")
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
else()
set(COUCHBASE_CXX_CLIENT_POST_LINKED_OPENSSL
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_STATIC_BORINGSSL
ON
CACHE BOOL "" FORCE)
endif()
set(COUCHBASE_CXX_CLIENT_WRAPPER_UNIFIED_ID
"python/${PYCBC_VERSION}"
CACHE STRING "" FORCE)
set(COUCHBASE_CXX_CLIENT_PYTHON_WARNINGS
ON
CACHE INTERNAL "")
set(COUCHBASE_CXX_CLIENT_BUILD_STATIC
ON
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_SHARED
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_INSTALL
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_DOCS
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_EXAMPLES
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_TESTS
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_TOOLS
OFF
CACHE BOOL "" FORCE)
set(COUCHBASE_CXX_CLIENT_BUILD_OPENTELEMETRY
OFF
CACHE BOOL "" FORCE)
# cmake-format: off
# PYCBC-1374 + PYCBC-1495: Move to dynamically link against static stdlib to avoid issues with:
# - other packages that also link against stdlibs (grpc)
# - building SDK on RHEL >= RHEL8 as static stdlibs are not available.
# cmake-format: on
option(USE_STATIC_STDLIB "Statically link C++ standard library" FALSE)
if(USE_STATIC_STDLIB)
set(COUCHBASE_CXX_CLIENT_STATIC_STDLIB
ON
CACHE BOOL "" FORCE)
else()
set(COUCHBASE_CXX_CLIENT_STATIC_STDLIB
OFF
CACHE BOOL "" FORCE)
endif()
message(STATUS "USE_STATIC_STDLIB=${USE_STATIC_STDLIB}")
option(DOWNLOAD_MOZILLA_CA_BUNDLE "Allow C++ client to download and embed Mozilla certificates" TRUE)
if(DOWNLOAD_MOZILLA_CA_BUNDLE)
set(COUCHBASE_CXX_CLIENT_EMBED_MOZILLA_CA_BUNDLE
ON
CACHE BOOL "" FORCE)
else()
set(COUCHBASE_CXX_CLIENT_EMBED_MOZILLA_CA_BUNDLE
OFF
CACHE BOOL "" FORCE)
endif()
message(STATUS "DOWNLOAD_MOZILLA_CA_BUNDLE=${DOWNLOAD_MOZILLA_CA_BUNDLE}")
# handle CPM cache dir
if(DEFINED COUCHBASE_CXX_CPM_CACHE_DIR AND NOT COUCHBASE_CXX_CPM_CACHE_DIR STREQUAL "")
set(CPM_SOURCE_CACHE "${COUCHBASE_CXX_CPM_CACHE_DIR}")
endif()
if(DEFINED CPM_SOURCE_CACHE)
message(STATUS "CPM_SOURCE_CACHE=${CPM_SOURCE_CACHE}")
endif()
add_subdirectory(deps/couchbase-cxx-client)
set(COUCHBASE_CXX_BINARY_DIR "${CMAKE_BINARY_DIR}/deps/couchbase-cxx-client")
set(COUCHBASE_CXX_SOURCE_DIR "${PROJECT_SOURCE_DIR}/deps/couchbase-cxx-client")
message(STATUS "COUCHBASE_CXX_BINARY_DIR=${COUCHBASE_CXX_BINARY_DIR}")
message(STATUS "COUCHBASE_CXX_SOURCE_DIR=${COUCHBASE_CXX_SOURCE_DIR}")
if(DEFINED COUCHBASE_CXX_CPM_CACHE_DIR AND NOT COUCHBASE_CXX_CPM_CACHE_DIR STREQUAL "")
file(COPY "${COUCHBASE_CXX_BINARY_DIR}/mozilla-ca-bundle.crt" "${COUCHBASE_CXX_BINARY_DIR}/mozilla-ca-bundle.sha256"
DESTINATION "${COUCHBASE_CXX_CPM_CACHE_DIR}")
message(STATUS "Copied Mozilla cert bundle to cache. COUCHBASE_CXX_CPM_CACHE_DIR=${COUCHBASE_CXX_CPM_CACHE_DIR}")
endif()
if(Python3_FOUND)
message(STATUS "Python executable: ${Python3_EXECUTABLE}")
message(STATUS "Python include dir: ${Python3_INCLUDE_DIRS}")
message(STATUS "Python libs: ${Python3_LIBRARIES}")
else()
message(FATAL_ERROR "Python3 not found.")
endif()
include_directories(SYSTEM ${Python3_INCLUDE_DIRS})
file(
GLOB
SOURCE_FILES
"src/*.cxx"
"src/transactions/*.cxx")
add_library(pycbc_core SHARED ${SOURCE_FILES})
target_compile_definitions(pycbc_core PRIVATE COUCHBASE_CXX_CLIENT_IGNORE_CORE_DEPRECATIONS)
# PYCBC-1854: Build against the Python Stable ABI (abi3) by default so a single
# wheel can target every supported CPython. Power users can opt out via the
# PYCBC_PY_LIMITED_API env var (handled in pycbc_build_setup.py) which passes
# -DPYCBC_PY_LIMITED_API:BOOL=OFF here, producing a version-specific build.
option(PYCBC_PY_LIMITED_API "Build the C extension against Py_LIMITED_API (abi3)" ON)
# PYCBC_PY_LIMITED_API_HEX is the lowest CPython release this binary will load on.
# Driven by PYCBC_PY_LIMITED_API_VERSION in pycbc_build_setup.py so it stays in
# lockstep with the bdist_wheel --py-limited-api tag. 0x030A0000 == CPython 3.10,
# the floor required by the limited-API symbols pycbc uses.
set(PYCBC_PY_LIMITED_API_HEX "0x030A0000" CACHE STRING "Py_LIMITED_API hex (e.g. 0x030A0000 for CPython 3.10)")
message(STATUS "PYCBC_PY_LIMITED_API=${PYCBC_PY_LIMITED_API}")
set(PYCBC_C_MOD_OUTPUT_NAME "_core")
if(PYCBC_PY_LIMITED_API)
message(STATUS "PYCBC_PY_LIMITED_API_HEX=${PYCBC_PY_LIMITED_API_HEX}")
target_compile_definitions(pycbc_core PRIVATE Py_LIMITED_API=${PYCBC_PY_LIMITED_API_HEX})
# abi3 tag goes on the suffix (_core.abi3.so), not the output name. The
# interpreter tag in PYCBC_C_MOD_SUFFIX is dropped here so the filename matches
# CMakeBuildExt.get_ext_filename(), which pairs ".abi3" with the bare .so.
string(REGEX REPLACE "^.*(\\.[^.]+)$" "\\1" PYCBC_C_MOD_SHARED_LIB_EXT "${PYCBC_C_MOD_SUFFIX}")
if(WIN32)
# Windows has no ".abi3" import suffix: importlib only recognizes
# ".cp3XY-win_amd64.pyd" and a bare ".pyd", so the stable-ABI module is _core.pyd.
set(PYCBC_C_MOD_FULL_SUFFIX "${PYCBC_C_MOD_SHARED_LIB_EXT}")
else()
set(PYCBC_C_MOD_FULL_SUFFIX ".abi3${PYCBC_C_MOD_SHARED_LIB_EXT}")
endif()
else()
set(PYCBC_C_MOD_FULL_SUFFIX "${PYCBC_C_MOD_SUFFIX}")
endif()
target_include_directories(
pycbc_core PRIVATE SYSTEM
"${COUCHBASE_CXX_BINARY_DIR}/generated"
"${COUCHBASE_CXX_SOURCE_DIR}"
"${COUCHBASE_CXX_SOURCE_DIR}/third_party/cxx_function"
"${COUCHBASE_CXX_SOURCE_DIR}/third_party/expected/include")
set(COUCHBASE_CXX_CLIENT_TARGET couchbase_cxx_client_static_intermediate)
if(WIN32)
# Python3_LIBRARIES is the version-specific python3XY.lib. Linking it in a stable-ABI
# build takes precedence over the python3.lib that pyconfig.h's MSVC auto-link pragma
# selects under Py_LIMITED_API, which would bind an abi3-tagged .pyd to one interpreter
# version. Prefer python3.lib explicitly, and fall back to the pragma if it isn't found.
if(PYCBC_PY_LIMITED_API)
find_library(
PYCBC_PYTHON3_SABI_LIBRARY
NAMES python3
HINTS ${Python3_LIBRARY_DIRS}
NO_DEFAULT_PATH)
set(PYCBC_PYTHON_LINK_LIBRARIES "")
if(PYCBC_PYTHON3_SABI_LIBRARY)
set(PYCBC_PYTHON_LINK_LIBRARIES ${PYCBC_PYTHON3_SABI_LIBRARY})
endif()
message(STATUS "PYCBC_PYTHON3_SABI_LIBRARY=${PYCBC_PYTHON3_SABI_LIBRARY}")
else()
set(PYCBC_PYTHON_LINK_LIBRARIES ${Python3_LIBRARIES})
endif()
target_link_libraries(
pycbc_core PRIVATE
${COUCHBASE_CXX_CLIENT_TARGET}
${PYCBC_PYTHON_LINK_LIBRARIES}
asio
Microsoft.GSL::GSL
taocpp::json
spdlog::spdlog
hdr_histogram_static)
else()
target_link_libraries(
pycbc_core PRIVATE
${COUCHBASE_CXX_CLIENT_TARGET}
asio
Microsoft.GSL::GSL
taocpp::json
spdlog::spdlog
hdr_histogram_static)
if(APPLE)
target_link_options(
pycbc_core
PRIVATE
-undefined
dynamic_lookup)
endif()
endif()
if(CMAKE_VERBOSE_MAKEFILE)
target_link_options(pycbc_core PRIVATE -v)
endif()
if(NOT USE_STATIC_BORINGSSL)
target_link_libraries(pycbc_core PUBLIC ${OPENSSL_LIBRARIES})
endif()
# PyInit__core carries its own visibility("default") via PyMODINIT_FUNC, so hiding
# everything else keeps the module's exported surface to the one symbol CPython needs.
# No-op on MSVC, which exports nothing without __declspec(dllexport).
set_target_properties(
pycbc_core
PROPERTIES PREFIX ""
OUTPUT_NAME ${PYCBC_C_MOD_OUTPUT_NAME}
SUFFIX ${PYCBC_C_MOD_FULL_SUFFIX}
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)
# The extension module is the only build artifact that belongs in the Python package
# directory, so the directory is set on the target rather than through a global
# CMAKE_<category>_OUTPUT_DIRECTORY, which applies to every target in the build tree
# (PYCBC-1878). The category differs by platform: on Windows a DLL is RUNTIME, which also
# covers executables, and everywhere else the module is LIBRARY. PDB_OUTPUT_DIRECTORY
# otherwise follows the target's own output directory, which would put the symbols back into
# the package directory and therefore into the wheel.
#
# The path is used as passed. These properties accept native separators, and
# file(TO_CMAKE_PATH) splits on the host's path-list separator, so it is not a safe
# normalizer for a single path.
if(PYCBC_MODULE_OUTPUT_DIRECTORY)
message(STATUS "PYCBC_MODULE_OUTPUT_DIRECTORY=${PYCBC_MODULE_OUTPUT_DIRECTORY}")
if(WIN32)
set(PYCBC_MOD_DIR_CATEGORY RUNTIME)
else()
set(PYCBC_MOD_DIR_CATEGORY LIBRARY)
endif()
set_target_properties(
pycbc_core
PROPERTIES ${PYCBC_MOD_DIR_CATEGORY}_OUTPUT_DIRECTORY "${PYCBC_MODULE_OUTPUT_DIRECTORY}"
PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
# Multi-config generators (Visual Studio, Xcode) append the configuration name unless the
# per-config property is set. Single-config generators leave CMAKE_CONFIGURATION_TYPES
# empty, so this loop is a no-op there and the plain property above applies.
foreach(pycbc_config IN LISTS CMAKE_CONFIGURATION_TYPES)
string(TOUPPER "${pycbc_config}" pycbc_config_uc)
set_target_properties(
pycbc_core
PROPERTIES ${PYCBC_MOD_DIR_CATEGORY}_OUTPUT_DIRECTORY_${pycbc_config_uc} "${PYCBC_MODULE_OUTPUT_DIRECTORY}"
PDB_OUTPUT_DIRECTORY_${pycbc_config_uc} "${CMAKE_BINARY_DIR}")
endforeach()
else()
# Nothing else places the module, and the CMake-driven build_ext does not check, so the
# wheel would be packed without an extension and fail at import instead of here.
message(
WARNING
"PYCBC_MODULE_OUTPUT_DIRECTORY is not set: ${PYCBC_C_MOD_OUTPUT_NAME}${PYCBC_C_MOD_FULL_SUFFIX} will be left in the build tree, not the package directory"
)
endif()