Setting CMake variables in the parent scope
A few weeks ago, I faced an interesting
CMake problem: Our group’s software contains several
submodules such as
math, etc. The
topology module, for which I am responsible,
obtained a new dependency. I now wanted to notify all other modules about the inclusion status of
this module. If the dependency was not met, the module should not be included. Furthermore, all
algorithms depending on the
topology module shall not be built, either.
I expected this to be difficult, nay impossible, because CMake does not seem to have the concept of
global variables. Instead, I was pleasantly surprised to find out about the option
This option can be used in
SET() statements of subordinate modules to set a variable in the scope
of the calling module, i.e. the parent.
SET(TOPOLOGY_MODULE_AVAILABLE true PARENT_SCOPE)
CMakeLists.txt file of the subordinate module solved the problem.