Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of CMake policies?

Tags:

cmake

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 ?

like image 716
wl2776 Avatar asked Mar 18 '15 13:03

wl2776


2 Answers

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.

like image 181
StAlphonzo Avatar answered Oct 18 '22 10:10

StAlphonzo


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.

like image 25
CAMOBAP Avatar answered Oct 18 '22 11:10

CAMOBAP