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?


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.


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

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.

Need Your Help

how to pass value to function with ajax

javascript jquery ajax

I would like to get ajax return value. please help my code does not return value false or true.

2 selectboxes one for updating the other onclick cakephp

ajax cakephp drop-down-menu

I have a form callled "Project", it contains a selectbox with the names of the companies , the other selectbox is hidden and is called "Staff" and is to be made visible and filled with the names an...