[CMake] Policy Stack within Macros/Functions

Sérgio Agostinho sergio.r.agostinho at gmail.com
Mon Oct 21 15:39:21 EDT 2019


Thank you for the tip. After rereading the documentation again, I
noticed the following quote in the docs

    CMake also manages a new entry for scripts loaded by include()
    <https://cmake.org/cmake/help/v3.5/command/include.html#command:include>
    and find_package()
    <https://cmake.org/cmake/help/v3.5/command/find_package.html#command:find_package>
    commands except when invoked with the NO_POLICY_SCOPE option (see
    also policy CMP0011
    <https://cmake.org/cmake/help/v3.5/policy/CMP0011.html#policy:CMP0011>).

Config files are invoked as a result from |find_package()| calls and
indeed, as the documentation states, it automatically creates a new
entry in the stack.

For further users with the same problem, you likely do not need need to
invoke |cmake_policy(PUSH)|/|cmake_policy(POP)| from your shipped Config
file, so just delete them and set whatever policies you need without
concerns.

Sérgio

On 21/10/19 15:45, Kyle Edwards wrote:

> 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

​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191021/11113349/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://cmake.org/pipermail/cmake/attachments/20191021/11113349/attachment.sig>


More information about the CMake mailing list