CPM.cmake icon indicating copy to clipboard operation
CPM.cmake copied to clipboard

Recently added `find_package` CONFIG mode override files (redicts) looks for `[pkg name]-extra.cmake` in locations which differ from CMake FetchContent's overrides

Open christopherbate opened this issue 10 months ago • 1 comments

A behavior recently added in https://github.com/cpm-cmake/CPM.cmake/pull/604 attempts to emulate a FetchContent behavior for allowing find_package in CONFIG mode to be redirected to some CMake files.

In this section added in the PR's changelist:


   if(DEFINED CMAKE_FIND_PACKAGE_REDIRECTS_DIR)
      # Redirect find_package calls to the CPM package. This is what FetchContent does when you set
      # OVERRIDE_FIND_PACKAGE. The CMAKE_FIND_PACKAGE_REDIRECTS_DIR works for find_package in CONFIG
      # mode, unlike the Find${Name}.cmake fallback. CMAKE_FIND_PACKAGE_REDIRECTS_DIR is not defined
      # in script mode, or in CMake < 3.24.
      # https://cmake.org/cmake/help/latest/module/FetchContent.html#fetchcontent-find-package-integration-examples
      string(TOLOWER ${Name} NameLower)
      file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-config.cmake
           "include(\"${CMAKE_CURRENT_LIST_DIR}/${NameLower}-extra.cmake\" OPTIONAL)\n"
           "include(\"${CMAKE_CURRENT_LIST_DIR}/${Name}Extra.cmake\" OPTIONAL)\n"
      )
      file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-version.cmake
           "set(PACKAGE_VERSION_COMPATIBLE TRUE)\n" "set(PACKAGE_VERSION_EXACT TRUE)\n"
      )
    else()
      file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake
           "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)"
      )
    endif()

These two lines:

           "include(\"${CMAKE_CURRENT_LIST_DIR}/${NameLower}-extra.cmake\" OPTIONAL)\n"
           "include(\"${CMAKE_CURRENT_LIST_DIR}/${Name}Extra.cmake\" OPTIONAL)\n"

should be

     "include(\"${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-extra.cmake\" OPTIONAL)\n"
      "include(\"${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${Name}Extra.cmake\" OPTIONAL)\n"

To match cmake's behavior. I believe this was actually the intent of @patstew's change, but the ${CMAKE_CURRENT_LIST_DIR} is being evaluated prior to writing out the file.

christopherbate avatar Feb 06 '25 19:02 christopherbate

Yea, sorry about that. The fix is in #630 , it's just waiting for approval.

patstew avatar Feb 06 '25 19:02 patstew