Skip to content

Commit fe74201

Browse files
committedJan 27, 2025
PYCBC-1643: Update build system mechanism to find Python3
Changes ======= * Add function to attempt to determine the local Python3 version * Use local Python3 if found, otherwise default to version CMake is able to find Change-Id: Ibcaca99b723fa9e5ecf4e36bd11ab7603393dda1 Reviewed-on: https://review.couchbase.org/c/couchbase-python-client/+/222172 Reviewed-by: Dimitris Christodoulou <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent 4ed27c7 commit fe74201

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed
 

‎CMakeLists.txt

+24-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ include(FetchContent)
2020

2121
set(CMAKE_CXX_STANDARD 17)
2222

23+
function(get_python_version)
24+
if(DEFINED ENV{PYCBC_PYTHON3_EXECUTABLE})
25+
message(STATUS "Using python3 executable from environment: $ENV{PYCBC_PYTHON3_EXECUTABLE}")
26+
set(PYTHON3_EXE $ENV{PYCBC_PYTHON3_EXECUTABLE})
27+
else()
28+
message(STATUS "Using default python3 executable")
29+
set(PYTHON3_EXE "python3")
30+
endif()
31+
32+
execute_process(COMMAND ${PYTHON3_EXE} -c "import platform;print(platform.python_version())"
33+
OUTPUT_VARIABLE local_python_version)
34+
string(STRIP "${local_python_version}" PYTHON_VERSION)
35+
set(LOCAL_PYTHON_VERSION "${PYTHON_VERSION}" PARENT_SCOPE)
36+
endfunction()
37+
2338
if(WIN32)
2439
# cmake-format: off
2540
# MultiThreaded$<$<CONFIG:Debug>:Debug>DLL for /MD compile flag
@@ -33,13 +48,17 @@ endif()
3348
project(couchbase_client)
3449
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3550

36-
if(PYTHON_VERSION_EXACT)
37-
set(Python_FIND_VIRTUALENV FIRST)
38-
message("finding python version ${PYTHON_VERSION_EXACT}")
51+
get_python_version()
52+
message(STATUS "LOCAL_PYTHON_VERSION=${LOCAL_PYTHON_VERSION}")
53+
54+
if(LOCAL_PYTHON_VERSION)
55+
set(Python3_FIND_VIRTUALENV FIRST)
56+
message("finding python version ${LOCAL_PYTHON_VERSION}")
57+
find_package(Python3 ${LOCAL_PYTHON_VERSION} EXACT COMPONENTS Interpreter Development.Module)
3958
else()
40-
set(PYTHON_VERSION_EXACT 3.6)
59+
find_package(Python3 COMPONENTS Interpreter Development.Module)
4160
endif()
42-
find_package(Python3 ${PYTHON_VERSION_EXACT} COMPONENTS Interpreter Development.Module)
61+
4362

4463
if(WIN32)
4564
set(PYCBC_C_MOD_SUFFIX ".pyd")

‎pycbc_build_setup.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,23 @@ def create_cmake_config(cls, # noqa: C901
146146
os.environ.get('CMAKE_COMMON_VARIABLES', '').split(' ')
147147
if x])
148148

149-
python3_executable = env.pop('PYCBC_PYTHON3_EXECUTABLE', None)
149+
python3_find_strategy = env.get('PYCBC_PYTHON3_FIND_STRATEGY', 'LOCATION')
150+
cmake_config_args += [f'-DPython3_FIND_STRATEGY={python3_find_strategy}']
151+
152+
python3_rootdir = env.get('PYCBC_PYTHON3_ROOT_DIR', None)
153+
if python3_rootdir:
154+
cmake_config_args += [f'-DPython3_ROOT_DIR={python3_rootdir}']
155+
156+
python3_executable = env.get('PYCBC_PYTHON3_EXECUTABLE', None)
157+
if python3_executable is None and sys.executable:
158+
# if sys.executable determines the path we want to use that to determine the
159+
# Python version in our get_python_version CMake function.
160+
python3_executable = sys.executable
161+
env['PYCBC_PYTHON3_EXECUTABLE'] = python3_executable
150162
if python3_executable:
151163
cmake_config_args += [f'-DPython3_EXECUTABLE={python3_executable}']
152164

153-
python3_include = env.pop('PYCBC_PYTHON3_INCLUDE_DIR', None)
165+
python3_include = env.get('PYCBC_PYTHON3_INCLUDE_DIR', None)
154166
if python3_include:
155167
cmake_config_args += [f'-DPython3_INCLUDE_DIR={python3_include}']
156168

0 commit comments

Comments
 (0)