Is there any way to detect if the current scope has a parent?
I have a project that can either be a standalone project or a sub-project of another. To allow the sub project case, I use the PARENT_SCOPE flag to set() to push things up to the parent. However, when build as a standalone project I get a "current scope has no parent" warning. I would like to avoid that error by detecting if there is a parent and enclosing the set() calls in an if statement. Or is there another way to set a variable at parent scope only if there is a parent?
Expanding a bit on @ruslo idea I would not take the
PROJECT_SOURCE_DIR
but theCMAKE_PROJECT_NAME
variable (contains the name of the first defined project) and thePROJECT_NAME
(contains the name of the current project), so you could do something like this in theCMakeLists.txt
of the Subproject:I think the most robust approach is to use the
PARENT_DIRECTORY
directory property.This will yield the correct answer regardless of whether it's called before or after the
project
command, and regardless of whether the parent and child both have the same project name.