From the documentation of
The top-level CMakeLists.txt file for a project must contain a
literal, direct call to the project() command; loading one through the
include() command is not sufficient. If no such call exists CMake will
implicitly add one to the top that enables the default languages (C
So, those variables are already there and propagate scopes down the layout.
project command should normally be used once in project, since it sets up various flags. If your other sub-projects are also required to be built by themselves as standalone, then a common idiom is to have a check like this:
if(CURRENT_CMAKE_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# other stuff here
Please note that the
PARENT_SCOPE is useful in the context of functions and their call sites.