[CMake] Policy Stack within Macros/Functions
Kyle Edwards
kyle.edwards at kitware.com
Mon Oct 21 09:45:09 EDT 2019
On Sun, 2019-10-20 at 19:25 +0200, Sérgio Agostinho wrote:
> Hey everyone,
>
> I’m shipping a config file for my library and inside I set push/pop
> specific policies so that me and the consumers of my library can
> target different policies without us clashing against each other.
> However I’m struggling with cmake_policy(PUSH)/cmake_policy(POP) once
> they are invoked from macro/functions.
>
> Here’s a minimal failure example illustrating my problem
>
>
> project(pcl-config-test)
> cmake_minimum_required(VERSION 3.5)
>
> cmake_policy(PUSH)
>
> macro(return_early)
> # clean up code
> cmake_policy(POP)
> return()
> endmacro()
>
> # 1. do a bunch of things. invoke a couple of macros/functions
> # from within other macros/functions, etc...
>
> # 2. some error occurs
> if(ERROR)
> return_early()
> endif()
>
> # 3. Everything went well
> cmake_policy(POP)
> This produces the following output
>
>
> CMake Error at CMakeLists.txt:27 (my_macro):
> cmake_policy POP without matching PUSH
>
> CMake Error in CMakeLists.txt:
> cmake_policy PUSH without matching POP
>
> -- Configuring incomplete, errors occurred!
> I was counting on the policy stack being preserved for at least
> macros, but that is not the case.
>
> Is there a way for me to return early from my config file from within
> macros?
If you're trying to set policies within a function, set them at the
module level rather than at the function level. The module-level policy
settings will automatically be pushed when you call the function. That
way, there will be no need to do cmake_policy(POP) within
return_early().
Kyle
More information about the CMake
mailing list