Skip to content

Refactoring of CPM policies causes a regression in policy propagation #387

@robertmaynard

Description

@robertmaynard

After deploying CPM 0.35.4 I found a regression in policy propagation.

Here is a small reproducer of the issue:

Root CMakeLists.txt

cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(cpm_reproducer)

function(cpm_init)
  include(${CMAKE_CURRENT_SOURCE_DIR}/cpm.cmake)
endfunction()
cpm_init()

add_cpm_subdir(src)

cpm.cmake:

macro(set_policies )
  # the policy allows us to change options without caching
  cmake_policy(SET CMP0077 NEW)
  set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

  # the policy allows us to change set(CACHE) without caching
  if(POLICY CMP0118)
    cmake_policy(SET CMP0118 NEW)
    set(CMAKE_POLICY_DEFAULT_CMP0118 NEW)
  endif()
endmacro()
set_policies()

function(add_cpm_subdir dir)
  # set_policies()
  add_subdirectory(${dir})
endfunction()

and the src/CMakeLists.txt

message(STATUS "before src cmake_minimum_required")
cmake_policy(GET CMP0077 77_state)
cmake_policy(GET CMP0118 126_state)
message(STATUS "CMP0077 ${77_state}")
message(STATUS "CMP0118 ${126_state}")

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

message(STATUS "after src cmake_minimum_required")
cmake_policy(GET CMP0077 77_state)
cmake_policy(GET CMP0118 126_state)
message(STATUS "CMP0077 ${77_state}")
message(STATUS "CMP0118 ${126_state}")

The behavior we see is that src/CMakeLists.txt will reset to 3.1.0 policy settings since the variables CMAKE_POLICY_DEFAULT_CMP0077 and CMAKE_POLICY_DEFAULT_CMP0126 are not set in the scope the are called from.

So while CPM.cmake behaves as expected the consuming project doesn't which means that options and set(CACHE) variables will ignore the provided values we propagate.

Proposed Solution:

We refactor the CPM policies to set in a macro, which we call when CPM.cmake is included and also when CPMAddPackage is called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions