John Zwinck
John Zwinck

Reputation: 249374

CMake: trigger regeneration of Makefiles when environment variable changes

I am using Linux, CMake, and Conda, which is a package manager that edits $PATH as a way to switch between "environments." This means when I run a special command in Conda, $PATH points to a different compiler. $CONDA_PREFIX is also changed.

By default, CMake is not sensitive to these changes. I would like it to be. Specifically, I want CMake to automatically regenerate all Makefiles when a different GCC is found in $PATH, or alternatively when $CONDA_PREFIX has changed since the last explicit cmake invocation. This regeneration would be similar to what CMake does when you edit the top-level CMakeLists.txt file - the next time you run make it regenerates everything.

How can I do this in a simple way using CMake?

Upvotes: 5

Views: 2218

Answers (2)

Victor Sergienko
Victor Sergienko

Reputation: 13485

I ended up not needing a workaround code today, but here's an idea. It's not general - it requires you to explicitly specify the variables your build depends on.

  • Create a CMake wrapper script that receives the names of environment variables that affect my specific build.
  • echo them all in VARIABLE=value format into a ${CMAKE_CURRENT_BINARY_DIR}/buildvars.environment file. Only do it if the file doesn't exist or if its content is different.
  • When calling cmake, add cmake -DCMAKE_CONFIGURE_DEPENDS=<build_direcotry>/buildvars.environment to trigger reconfiguration if the file is newer than build configuration.

References:

Related question: How to trigger a CMake reconfigure when the output of a command changes

Upvotes: 3

arrowd
arrowd

Reputation: 34411

In short, the generated build directory should no more depend on environment.

if a user accidentally is in the wrong (mismatched) Conda environment vs CMake build tree, the build may be defective.

If you've used find_program() command to determine paths to all tools you are using then this would never happen. Using full path for every tool and having a build dir for each environment setup is the preffered way to handle this problem.

Upvotes: 0

Related Questions