Skip to content

FetchContent failure when using CPM on multiple projects with cmake < 3.17 #192

Closed
@xmuller

Description

@xmuller

To reproduce the bug with at least CPM >= 0.27.5:

Minimal structure:

.
├── CMakeLists.txt
├── libA
│   ├── CMakeLists.txt
│   └── CPM.cmake
└── libB
    ├── CMakeLists.txt
    └── CPM.cmake

CMakeLists.txt:

cmake_minimum_required(VERSION 3.16.3)
project(minimal_bug_cpm)

add_subdirectory(libA)
add_subdirectory(libB)

libA/CMakeLists.txt:

cmake_minimum_required(VERSION 3.16.3)
project(libA)

include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake)
CPMFindPackage(
  NAME doctest
  GITHUB_REPOSITORY onqtam/doctest
  GIT_TAG 2.4.4
)

libB/CMakeLists.txt:

cmake_minimum_required(VERSION 3.16.3)
project(libB)

include(${CMAKE_CURRENT_LIST_DIR}/CPM.cmake)
CPMFindPackage(
  NAME fmt
  GITHUB_REPOSITORY fmtlib/fmt
  GIT_TAG 6.2.1
)

ERROR:

-- Detecting CXX compile features - done
-- CPM: adding package [email protected] (2.4.4)
--  adding package [email protected] (6.2.1)
CMake Error: File /CMakeLists.cmake.in does not exist.
CMake Error at /usr/share/cmake-3.16/Modules/FetchContent.cmake:891 (configure_file):
  configure_file Problem configuring file
Call Stack (most recent call first):
  /usr/share/cmake-3.16/Modules/FetchContent.cmake:1006 (__FetchContent_directPopulate)
  /usr/share/cmake-3.16/Modules/FetchContent.cmake:1047 (FetchContent_Populate)
  libA/CPM.cmake:561 (FetchContent_MakeAvailable)
  libA/CPM.cmake:425 (cpm_fetch_package)
  libA/CPM.cmake:196 (CPMAddPackage)
  libB/CMakeLists.txt:6 (CPMFindPackage)


-- Configuring incomplete, errors occurred!

Solution

include(FetchContent) need to be call inside each subdirectory (cmake < 3.17) else it's not working.
So before the return() in CPM.cmake line 44 we can add include(FetchContent) which fix the issue.

Also, CPM_INDENT is locally defined that why there is no CPM: in front of adding package [email protected] (6.2.1) in the given example.
An (ugly ?) way to keep the prefix is to also add the following line:

if(NOT CPM_INDENT)
  set(CPM_INDENT "CPM:")
endif()

Unless I missed something or something seems wrong to you, if the fix sound nice to you I can create a pull request.
Best regards,
Xavier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions