Friday, September 2, 2022

[SOLVED] Make CMake not search for Python components on reconfigure

Issue

I added googletest to my build using FetchContent like this:

FetchContent_Declare(googletest
    GIT_REPOSITORY https://github.com/google/googletest
    GIT_TAG main
)

FetchContent_MakeAvailable(googletest)

But after I did this, each time CMake needs to rerun its configure/generate steps it takes about ~25 seconds. This is really interfering with my workflow when making iterative changes to the build.

C:\NSEW>cmake --build .
[0/2] Re-checking globbed directories...
[1/2] Re-running CMake...

   | It takes about 20 seconds until the following output is shown |

-- Could NOT find Python (missing: Python_EXECUTABLE Interpreter)
-- Found SFML 2.5.1 in C:/install/lib/cmake/SFML
-- Configuring done
-- Generating done
-- Build files have been written to: C:/NSEW
[0/2] Re-checking globbed directories...
ninja: no work to do.

googletest has an optional Python dependency but since Python isn't installed, CMake tries every possible installation path which takes very long. CMake appears to do this search on every reconfigure. How to stop CMake from doing this very slow search for a component I don't need?

I tried to set the cache variable Python_EXECUTABLE to NO / Python-NOTFOUND but that didn't seem to help.


Solution

You can disable any non-REQUIRED find_package(PackageName) command using CMAKE_DISABLE_FIND_PACKAGE_<PackageName>. This variable has existed since at least CMake 3.0, so this will work in general.

You can set it just before FetchContent_MakeAvailable and then unset() it afterward. Otherwise, just set CMAKE_DISABLE_FIND_PACKAGE_Python=TRUE (and/or ...Python3) at the cmake command line.



Answered By - Alex Reinking
Answer Checked By - Senaida (WPSolving Volunteer)