Skip to content

Commit

Permalink
Add find_package overrides that also work in CONFIG mode (cpm-cmake#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
patstew committed Dec 18, 2024
1 parent 0bc73f4 commit b9499c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ In the case that `find_package` requires additional arguments, the parameter `FI

Note that this does not apply to dependencies that have been defined with a truthy `FORCE` parameter. These will be added as defined.

### CPM_DONT_UPDATE_MODULE_PATH

By default, CPM will override any `find_package` commands to use the CPM downloaded version.
This is equivalent to the `OVERRIDE_FIND_PACKAGE` FetchContent option, which has no effect in CPM.
To disable this behaviour set the `CPM_DONT_UPDATE_MODULE_PATH` option.
This will not work for `find_package(CONFIG)` in CMake versions before 3.24.

### CPM_USE_NAMED_CACHE_DIRECTORIES

If set, CPM use additional directory level in cache to improve readability of packages names in IDEs like CLion. It changes cache structure, so all dependencies are downloaded again. There is no problem to mix both structures in one cache directory but then there may be 2 copies of some dependencies.
Expand Down
24 changes: 19 additions & 5 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ set(CPM_SOURCE_CACHE
CACHE PATH "Directory to download CPM dependencies"
)

if(NOT CPM_DONT_UPDATE_MODULE_PATH)
if(${CMAKE_VERSION} VERSION_LESS "3.24.0" AND NOT CPM_DONT_UPDATE_MODULE_PATH)
set(CPM_MODULE_PATH
"${CMAKE_BINARY_DIR}/CPM_modules"
CACHE INTERNAL ""
Expand Down Expand Up @@ -269,10 +269,24 @@ endfunction()
# finding the system library
function(cpm_create_module_file Name)
if(NOT CPM_DONT_UPDATE_MODULE_PATH)
# erase any previous modules
file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake
"include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)"
)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0")
# 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.
# 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()
endif()
endfunction()

Expand Down

0 comments on commit b9499c0

Please sign in to comment.