Skip to content

Commit

Permalink
build: use python from venv if available
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
willcl-ark committed Dec 3, 2024
1 parent ebe4cac commit 0ad4461
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit 0ad4461

Please sign in to comment.