What is the scope of CMake policies?

Per the comment for this answer https://unix.stackexchange.com/a/512695/48776

It's possible to set policy globally with set(CMAKE_POLICY_DEFAULT_CMP0046 OLD)

I have tried on different 3.x versions, it works.


No. This question is best answered straight from the documentation...

Policy settings are scoped using a stack. A new level of the stack is pushed when entering a new subdirectory of the project (with add_subdirectory) and popped when leaving it. Therefore setting a policy in one directory of a project will not affect parent or sibling directories but will affect subdirectories.

For making temporary changes to a specific level, without including sub_directories, you can use

cmake_policy(PUSH)
cmake_policy(POP)

If you want the policy applied in subdir21 and subdir22 you either need to add it there explicitly or consider adding it to the common parent.

Tags:

Cmake