Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cpm_find_package to check if a package added #552

Merged
merged 1 commit into from
Apr 7, 2024

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
8 checks passed
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