Skip to content

Conversation

@black-desk
Copy link
Contributor

If I have a project, which has:

  1. /CMakeLists.txt:

    cmake_minimum_required(VERSION 3.29)
    project(test)
    include(./cmake/CPM.cmake)
    add_subdirectory(subdir)
    cpmfindpackage(
      NAME
      nlohmann_json
      VERSION
      3.11.2
      URL
      "https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz"
      EXCLUDE_FROM_ALL
      ON)
    
    if(NOT TARGET nlohmann_json::nlohmann_json)
      message(FATAL_ERROR "BUG")
    endif()
  2. /subdir/CMakeLists.txt

    cpmfindpackage(
      NAME
      nlohmann_json
      VERSION
      3.11.2
      URL
      "https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz"
      EXCLUDE_FROM_ALL
      ON)

When nlohmann_json is first founded by find_package,
CPM.cmake register this package and later call to CPMFindPackage will
not actually perform a find_package again, which leads to this buggy
behavior.

As CPM write a FindXXX.cmake file for packages added by CPM to pervert
find_package to get local package, I think directly use cpm_find_package
to check if a package is already added is good enough.

If I have a project, which has:

1. /CMakeLists.txt:

   ```cmake
   cmake_minimum_required(VERSION 3.29)
   project(test)
   include(./cmake/CPM.cmake)
   add_subdirectory(subdir)
   cpmfindpackage(
     NAME
     nlohmann_json
     VERSION
     3.11.2
     URL
     "https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz"
     EXCLUDE_FROM_ALL
     ON)

   if(NOT TARGET nlohmann_json::nlohmann_json)
     message(FATAL_ERROR "BUG")
   endif()
   ```
2. /subdir/CMakeLists.txt

   ```cmake
   cpmfindpackage(
     NAME
     nlohmann_json
     VERSION
     3.11.2
     URL
     "https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz"
     EXCLUDE_FROM_ALL
     ON)
   ```

When nlohmann_json is first founded by find_package,
CPM.cmake register this package and later call to CPMFindPackage will
not actually perform a find_package again, which leads to this buggy
behavior.

As CPM write a FindXXX.cmake file for packages added by CPM to pervert
find_package to get local package, I think directly use cpm_find_package
to check if a package is already added is good enough.
@black-desk
Copy link
Contributor Author

Copy link
Member

@TheLartians TheLartians left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I can't quite follow all the implications atm, but your reasoning seems sound and tests pass, so I'm happy with merging this.

@TheLartians TheLartians merged commit 7e81149 into cpm-cmake:master Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants