What is the scope of CMake policies?

2019-02-23 23:57发布

问题:

I've got a CMake project with several subdirectories, like this one:

dir1
    subdir11
    subdir12
dir2
    subdir21
    subdir22

Root CMakeLists.txt:

add_subdirectory(dir1)
add_subdirectory(dir2)

CMakeLists in dir1 and dir2 are similar:

add_subdirectory(subdir11)
add_subdirectory(subdir12)

and

add_subdirectory(subdir21)
add_subdirectory(subdir22)

And CMakeLists in subdirs do actual work.

The file dir1/subdir12/CMakeLists.txt sets the CMP0046 policy to OLD:

cmake_policy(SET CMP0046 OLD) #silently ignore missing dependencies

My question is - will this setting of CMP0046 propagate to subdir21 and subdir22 ?

回答1:

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.



标签: cmake