From 0ad44615899d8898c38e04181f3d0d5b37521134 Mon Sep 17 00:00:00 2001 From: willcl-ark Date: Tue, 3 Dec 2024 13:06:28 +0000 Subject: [PATCH] build: use python from venv if available First check if $VIRTUAL_ENV is set. If it is, use the python binary from this venv. Fall back to system python otherwise. This helps run various targets which use python, when system python is not being used. --- CMakeLists.txt | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b21646ca18f5..0930a4f0f8353 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -534,13 +534,31 @@ if(WERROR) unset(werror_flag) endif() -find_package(Python3 3.10 COMPONENTS Interpreter) -if(Python3_EXECUTABLE) - set(PYTHON_COMMAND ${Python3_EXECUTABLE}) -else() - list(APPEND configure_warnings - "Minimum required Python not found. Utils and rpcauth tests are disabled." - ) +# First check for active virtual environment +if(DEFINED ENV{VIRTUAL_ENV}) + if(WIN32) + if(EXISTS "$ENV{VIRTUAL_ENV}/Scripts/python.exe") + set(Python3_EXECUTABLE "$ENV{VIRTUAL_ENV}/Scripts/python.exe") + set(PYTHON_COMMAND ${Python3_EXECUTABLE}) + endif() + else() + if(EXISTS "$ENV{VIRTUAL_ENV}/bin/python3") + set(Python3_EXECUTABLE "$ENV{VIRTUAL_ENV}/bin/python3") + set(PYTHON_COMMAND ${Python3_EXECUTABLE}) + endif() + endif() +endif() + +if(NOT DEFINED PYTHON_COMMAND) + # Fall back to system Python if no venv is active or found + find_package(Python3 3.10 COMPONENTS Interpreter) + if(Python3_EXECUTABLE) + set(PYTHON_COMMAND ${Python3_EXECUTABLE}) + else() + list(APPEND configure_warnings + "Minimum required Python not found. Utils and rpcauth tests are disabled." + ) + endif() endif() target_compile_definitions(core_interface INTERFACE ${DEPENDS_COMPILE_DEFINITIONS})