Skip to content

Commit

Permalink
Add special function to look for a virtualenv Python
Browse files Browse the repository at this point in the history
  • Loading branch information
makaimann committed Feb 16, 2025
1 parent 85617b7 commit 8e20b5f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
11 changes: 3 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,9 @@ option (USE_PYTHON2
# Decide on Python version before setting up googletest
# Otherwise might use wrong version
if (BUILD_PYTHON_BINDINGS)
# We currently use FindPythonInterp even though it is deprecated since 3.12
# This is because the scikit-build files still use this version and it will
# not interact well with the latest Python finding cmake modules
# https://cmake.org/cmake/help/v3.12/module/FindPython.html
# in the future, we can switch to FindPython3 once it has become more standard
# i.e. when the following issue is resolved:
# https://github.com/scikit-build/scikit-build/issues/506
find_package(PythonInterp 3.5 REQUIRED)
# Check for a virtualenv python first
find_package(PythonVirtualEnv)
find_package(Python 3.9 REQUIRED COMPONENTS Interpreter Development)
endif()

if (BUILD_BITWUZLA OR BUILD_CVC5 OR BUILD_MSAT OR BUILD_YICES2 OR BUILD_Z3)
Expand Down
56 changes: 56 additions & 0 deletions cmake/FindPythonVirtualEnv.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# File: FindPythonVirtualEnv.cmake
# Purpose: Detect and prefer Python from a virtual environment

# Use include guard to prevent multiple inclusions
include_guard(GLOBAL)

# Function to detect and set Python from virtual environment
function(find_python_in_virtualenv)
# Debugging: Print initial environment information
message(STATUS "Detecting Python Virtual Environment...")

# List to store potential Python paths
set(VENV_PATHS)

# 1. Standard virtualenv/venv path
if(DEFINED ENV{VIRTUAL_ENV})
list(APPEND VENV_PATHS "$ENV{VIRTUAL_ENV}/bin/python")
message(STATUS "Detected VIRTUAL_ENV: $ENV{VIRTUAL_ENV}")
endif()

# 2. Conda environment path
if(DEFINED ENV{CONDA_PREFIX})
list(APPEND VENV_PATHS "$ENV{CONDA_PREFIX}/bin/python")
message(STATUS "Detected CONDA_PREFIX: $ENV{CONDA_PREFIX}")
endif()

# Iterate through potential paths to find a valid Python interpreter
foreach(POTENTIAL_PYTHON IN LISTS VENV_PATHS)
if(EXISTS "${POTENTIAL_PYTHON}")
# Verify Python version
execute_process(
COMMAND "${POTENTIAL_PYTHON}" -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"
OUTPUT_VARIABLE PYTHON_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE PYTHON_VERSION_RESULT
)

if(PYTHON_VERSION_RESULT EQUAL 0)
# Check if version meets minimum requirements
if(PYTHON_VERSION VERSION_GREATER_EQUAL "3.9")
message(STATUS "Found Virtual Environment Python: ${POTENTIAL_PYTHON}")
message(STATUS "Virtual Environment Python Version: ${PYTHON_VERSION}")

# Set variables for CMake to use
set(Python_EXECUTABLE "${POTENTIAL_PYTHON}" PARENT_SCOPE)
set(PYTHON_EXECUTABLE "${POTENTIAL_PYTHON}" PARENT_SCOPE)
return()
endif()
endif()
endif()
endforeach()

# If no suitable virtual environment Python found, log a message
message(STATUS "No suitable Python virtual environment found")
endfunction()

2 changes: 2 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# First, ensure we have Python 3.9 or newer with all required components
# Check for a virtualenv python first
find_package(PythonVirtualEnv)
find_package(Python 3.9 REQUIRED COMPONENTS Interpreter Development)

# Helper function to check for required Python packages
Expand Down

0 comments on commit 8e20b5f

Please sign in to comment.