E.g. if project has cmake_minimum_required(VERSION 3.10), but introduced cmake_policy(SET CMP0177 OLD).
Since CMP0177 was added in 3.31, anyone using it cmake_policy with it, will get Policy "CMP0177" is not known to this version of CMake. error. So cmake_minimum_required effectively becomes 3.31.
Would be great if cmake can warn about this kind of mistakes.
Since the language is imperative there is no way for cmake_policy(SET) to warn in one case and not the other. If a project mistakenly leaves out the if guard, it’s a bug in that project.
Have been noticing this pattern, but it seems it’s better to avoid it in general to keep behaviour consistent across supported cmake versions and keep code easier to reason about. Nevermind, for some reason I’ve assumed cmake_policy(SET CMP0177 OLD) doesn’t trigger errors on older cmakes, thought it’s only on NEW.
Sorry, if it’s a dumb question and I’m just missing something, but doesn’t cmake know when it’s executing cmake_policy(SET CMP0177 NEW) that minimum required version is set to 3.10 and it would be able to compare version associated with CMP0177 with minimum required version and show a warning if cmake_policy contradicts it?
Though I don’t know if cmake is currently aware of versions associated with each policy or it’s just a note in the documentation.
Agree, that it is a bug in the project in the first place, it just might go under the radar until someone with older cmake version stumbles upon it.
Thinking about it more, I think you’re right, this is probably not possible, since then cmake_policy would need to somehow figure that it’s not inside if(POLICY) branch to issue a warning. And this sounds like too much of complexity to fix this kind of bugs.
Perhaps (I don’t know of the feasibility), we have cmake_minimulm_required(VERSION … ACCEPT_POLICIES CMP0XYZ CMP0IJK). Then we can warn when the upper-range of the version given makes a policy acceptance redundant.