Policy Stack within Macros/Functions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Policy Stack within Macros/Functions

Sérgio Agostinho

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?

Best regards,

Sérgio


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Policy Stack within Macros/Functions

CMake mailing list
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
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Policy Stack within Macros/Functions

Sérgio Agostinho

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() and find_package() commands except when invoked with the NO_POLICY_SCOPE option (see also 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


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Policy Stack within Macros/Functions

CMake mailing list
On Mon, 2019-10-21 at 21:39 +0200, Sérgio Agostinho wrote:
> 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.

This has the caveat that CMP0011 must be set to NEW for it to work
(which should be the case with any sane project - CMP0011 was
introduced in 2.6, the oldest version recognized by
cmake_minimum_required().)

Kyle
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake