Two scopes #
- Function scope
For when custom functions defined with function() are executed
- Directory scope
For when a CMakeLists.txt listfile in a nested directory is executed from the add_subdirectory() command
Nested scope #
When a nested scope is created, CMake simply fills it with copies of all the variables from the current scope. Subsequent commands will affect these copies. But as soon as the ececution of the nested scope is completed, all copies are deleted and the original, parent scope is restored.
If you unset a variable created in the parent scope while executing in a nested scope, it will disappear, but only in the nested scope. When the nested scope is completed, the variable is restored to its previous value.