What is the difference between cmake_policy specifying only min or specifying min and max at the same time?

I tried the following two ways of writing:

cmake_policy(VERSION 3.24)
cmake_policy(VERSION 3.20…3.24)

the other test code is:

cmake_policy(GET CMP0139 v)
message("CMP0143=${v}   ")  

#CMake 3.22 (CMP0127 ~ CMP0128) 
cmake_policy(GET CMP0127 v)
message("CMP0127=${v}   ")  

#CMake 3.21 (CMP0121 ~ CMP0126) 
cmake_policy(GET CMP0126 v)
message("CMP0126=${v}   ")  

#CMake 3.20 (CMP0115 ~ CMP0120) 
cmake_policy(GET CMP0120 v)
message("CMP0120=${v}   ")  

#CMake 2.6 (CMP0000 ~ CMP0011)
cmake_policy(GET CMP0000 v)
message("CMP0000=${v}   ")  

The results of the two tests are the same
cmake_policy(VERSION 3.24 )
QQ截图20240517145320

cmake_policy(VERSION 3.20…3.24 )
the same as above

So I am very confused whether the max here is necessary. If so, then I would like to ask how to test the difference. I am eager for the advice of the experts!

To see the difference, try running your tests with CMake 3.20 (and with 3.19).

How cmake_policy(VERION min...max) works:

  • CMake of version less than min will error out and not process the project.
  • CMake of version min or later will process the code. All policies which it knows and which were introduced in max or earlier will be set to NEW, later ones will be unset.
  • If max is not specified, it behaves the same as if it was equal to min.

In other words: set the minimum version based on what you require to process your project. If your project requires at least CMake 3.15, that’s your min.

As CMake introduces new versions with new policies, these policies may affect behaviour which you’re using in your project. When that happens, processing your project with this newer CMake produces a developer warning “Policy X is unset, treating as OLD for now. Please update your project.”

What you as project author should do in this case is indeed update the project. However, if you don’t want to force your users to upgrade their CMake, you can make the update such that it works with both the old version and the new one. Doing that is the proper time to use the min...max version range. What such a version range says:

I, project author, rely on CMake min being used for processing. I have adapted my project to processing with CMake max and guarantee that it behaves correctly. If you use a version newer than max, there may be behaviour I wasn’t expecting and you may get “using OLD policy behaviour” warnings.

1 Like

I I Thank you very much for your help and advice!
I changed my cmake to CMake 3.20. The following are the two running results. There seems to be no difference between setting or not setting max here? Who can tell me how the max parameter can work?

#设置策略最小版本
cmake_policy( VERSION 3.20)
#cmake_policy( VERSION 3.19...3.20)

cmake_policy(GET CMP0139 v)
message("CMP0139=${v}   ")  

#CMake 3.22 (CMP0127 ~ CMP0128) 
cmake_policy(GET CMP0127 v)
message("CMP0127=${v}   ")  

#CMake 3.21 (CMP0121 ~ CMP0126) 
cmake_policy(GET CMP0126 v)
message("CMP0126=${v}   ")  

#CMake 3.20 (CMP0115 ~ CMP0120) 
cmake_policy(GET CMP0120 v)
message("CMP0120=${v}   ")  

#CMake 2.6 (CMP0000 ~ CMP0011)
cmake_policy(GET CMP0000 v)
message("CMP0000=${v}   ")  

when I use
cmake_policy( VERSION 3.20)

the result is

when I use
#cmake_policy( VERSION 3.19...3.20)

the result is same too !