Setting CMake variables in the parent scope

Tags: programming

Published on
« Previous post: Interesting LaTeX packages — Next post: Solving an installation error for the … »

A few weeks ago, I faced an interesting CMake problem: Our group’s software contains several submodules such as kernel, 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 PARENT_SCOPE. 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.

Hence, typing

SET(TOPOLOGY_MODULE_AVAILABLE true PARENT_SCOPE)

in the CMakeLists.txt file of the subordinate module solved the problem.