Inconsistent behaviour of <PROJECT-NAME>_* Variables defined on PROJECT Command

When a Project is defined some project specific variables are set:

  • <PROJECT-NAME>_SOURCE_DIR
  • <PROJECT-NAME>_BINARY_DIR
  • <PROJECT-NAME>_VERSION
  • <PROJECT-NAME>_DESCRIPTION

The scope of this variables seems to be inconsistent.
<PROJECT-NAME>_SOURCE_DIR and <PROJECT-NAME>_BINARY_DIR have global scope where the other variables only are defined within the current directory and its sub-directories.

Does someone know, if this is the intended behavior?
I want to use <PROJECT-NAME>_DESCRIPTION and others in another top-level CMake-Directory, where it isn’t available anymore.

In contrast the CMAKE_PROJECT_DESCRIPTION (which is linked to the very first project-call) is available globally.

Regards Thomas

These variables are set by the first project() call. Since this is usually at the top-level, they just end up being global.

As for the others, it’s likely that there wasn’t thought behind it. I’m not sure which is preferred for the other metadata.

It is very important that the PROJECT_... variables are limited to the scope they currently have. It ensures you can use them and have confidence that you are referring to the most recent project() call in the current directory stack. There are techniques that rely on this.

If you need the top level details, that’s what the CMAKE_PROJECT_... variables are for. I think only a couple of those are defined though, not the full set equivalents of PROJECT_... variables.

FYI you can retrieve the value of a variable from a different directory scope as follows:

get_directory_property(<result-var> DIRECTORY <dir> DEFINITION <var-name>)