How to know the scope of variables

When I see a variable like CMAKE_ARCHIVE_OUTPUT_DIRECTORY being set I always wonder whether it affects just the current project or directory, or whether it is truly global, and I’m not sure how to tell other than by running experiments. I realize there are PROJECT_… and CURRENT_… variables for which the answers are clear based on both names and documentation. Is that pattern upheld consistently?

Thanks in advance

You can use get_property(CACHE varname) to query the cache directly. If the VALUE property doesn’t match the current state, you have a local variable shadowing the cache.

I think you explained how to know whether the variable is in the cache, but that’s not what I asked—though my question probably reflects a misunderstanding.

I thought some variables I might set in a CMakeLists.txt file are global and could affect dependent projects, some apply to a project and could affect ancestor or sibling directories in the same project, and some only affect subdirectories. Now that I write it down it sounds wrong…

I think the cmake-language manual clears this up (and I know I’ve read this before): variables are dynamically scoped subdirectory scopes are nested. No need to reply if I’ve got that right, thanks.

Ah! No variables are “special” to the cache or local scope. Which one any given documented variable is tends to be down to convention. There are some that CMake looks at that only matter in the top-level scope (e.g., why include(CTest) must be done there). Those should be documented as such (and we’d need to survey which ones these are).

1 Like