From 75deb425c238d6c794a6a97b010f6fdf119a2dd7 Mon Sep 17 00:00:00 2001 From: Samuel Felton Date: Fri, 26 Jan 2024 12:49:34 +0100 Subject: [PATCH] tentative at improving system python rejection (avoid caching, still some issues with a base conda environment --- CMakeLists.txt | 37 ++++++++++++++++++++++++++++++++++- modules/python/CMakeLists.txt | 17 ---------------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8d05d0775..465c151ef4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,27 @@ endif() # --- Python Support --- if(NOT IOS) + # Make sure to refresh the python interpreter every time we rerun cmake + # If we don't do this, we may use an old or invalid python when installing the bindings + # that was cached by a previous attempt at building + + + # # this avoids non-active conda from getting picked anyway on Windows + # set(Python_FIND_REGISTRY LAST) + # set(Python_FIND_VIRTUALENV FIRST) + # set(Python_FIND_STRATEGY LOCATION) + unset(PYTHON3INTERP_FOUND CACHE) + unset(PYTHONINTERP_FOUND CACHE) + unset(PYTHON3INTERP_FOUND) + unset(PYTHONLIBS_FOUND CACHE) + unset(PYTHONINTERP_FOUND) + unset(PYTHON_FOUND CACHE) + unset(PYTHON3_EXECUTABLE CACHE) + unset(PYTHON_EXECUTABLE CACHE) + unset(PYTHON3_EXECUTABLE) + unset(PYTHON_EXECUTABLE) + unset(VISP_PYTHON_IS_SYSTEM_WIDE CACHE) + unset(VISP_PYTHON3_VERSION CACHE) include(cmake/VISPDetectPython.cmake) endif() @@ -279,10 +300,24 @@ else() endif() if(CMAKE_NOT_OK_FOR_BINDINGS) - status("${CMAKE_NOT_OK_FOR_BINDINGS}") status("CMake version required for Python bindings is 3.19.0, but you have ${CMAKE_VERSION}. Python bindings generation will be deactivated") endif() +if(DEFINED ENV{VIRTUAL_ENV} OR DEFINED ENV{CONDA_PREFIX}) + set(_pip_args) + set(VISP_PYTHON_IS_SYSTEM_WIDE FALSE) +else() + # First solution: raise an error when cmake will call pip install + # set(_pip_args "--require-virtualenv") # If this is a system python, throw an error + message(WARNING "\ + The python version that you are using (${PYTHON3_EXECUTABLE}) is the system interpreter. + pip packages should not be installed system-wide! + Python bindings targets will be deactivated! + To reenable them, install conda or virtualenv, delete the CMakeCache file then rerun cmake when inside the virtual environment. + ") + set(VISP_PYTHON_IS_SYSTEM_WIDE TRUE) +endif() + # --- Python Bindings requirements --- # this avoids non-active conda from getting picked anyway on Windows diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index 90e497a064..dd2035c473 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -44,23 +44,6 @@ find_package(VISP REQUIRED) # TODO: check for pip -# Set pip args -if(DEFINED ENV{VIRTUAL_ENV} OR DEFINED ENV{CONDA_PREFIX}) - set(_pip_args) - set(VISP_PYTHON_IS_SYSTEM_WIDE FALSE PARENT_SCOPE) -else() - # First solution: raise an error when cmake will call pip install - # set(_pip_args "--require-virtualenv") # If this is a system python, throw an error - message(WARNING "\ - The python version that you are using (${PYTHON3_EXECUTABLE}) is the system interpreter. - pip packages should not be installed system-wide! - Python bindings targets will be deactivated! - To reenable them, install conda or virtualenv, delete the CMakeCache file then rerun cmake when inside the virtual environment. - ") - set(VISP_PYTHON_IS_SYSTEM_WIDE TRUE PARENT_SCOPE) - return() -endif() - # Step 1: Generate configuration file # Define modules for which to generate python bindings set(python_ignored_modules "visp_python" "visp_java_bindings_generator" "visp_java" )