Skip to content

FindPython: invalidate cache when virtualenv is (de)activated

Alexander Krotov requested to merge kva/cmake:krotov/python-virtualenv into master

When Python3_FIND_VIRTUALENV variable is not set by the user, internal _Python3_FIND_VIRTUALENV variable is set to ethier FIRST or STANDARD depending on whether virtualenv is activated.

User may activate or deactivate the environment without changing the Python3_FIND_VIRTUALENV variable. Therefore, prefixed vairable _Python3_FIND_VIRTUALENV should be part of the signature.

Example of incorrect behaviour:

$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.23)
project(foo LANGUAGES NONE)
find_package(Python3 COMPONENTS Interpreter)

message(STATUS "Python ${Python3_EXECUTABLE}")
$ cmake -B build .
-- Found Python3: /usr/bin/python3.10 (found version "3.10.5") found components: Interpreter
-- Python /usr/bin/python3.10
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/cmake-test/build
$ python3 -m venv env
$ . env/bin/activate
(env) $ cmake -B build .
-- Python /usr/bin/python3.10
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/cmake-test/build

With this fix cache is invalidated correctly when activating an environment:

(env) $ cmake -B build .
-- Found Python3: /home/user/cmake-test/env/bin/python3.10 (found version "3.10.5") found components: Interpreter
-- Python /home/user/cmake-test/env/bin/python3.10
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/cmake-test/build

Fixes #23850 (closed)

Edited by Alexander Krotov

Merge request reports