Skip to content

Commit b9499c0

Browse files
committed
Add find_package overrides that also work in CONFIG mode (#498)
1 parent 0bc73f4 commit b9499c0

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ In the case that `find_package` requires additional arguments, the parameter `FI
214214

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

217+
### CPM_DONT_UPDATE_MODULE_PATH
218+
219+
By default, CPM will override any `find_package` commands to use the CPM downloaded version.
220+
This is equivalent to the `OVERRIDE_FIND_PACKAGE` FetchContent option, which has no effect in CPM.
221+
To disable this behaviour set the `CPM_DONT_UPDATE_MODULE_PATH` option.
222+
This will not work for `find_package(CONFIG)` in CMake versions before 3.24.
223+
217224
### CPM_USE_NAMED_CACHE_DIRECTORIES
218225

219226
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.

cmake/CPM.cmake

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ set(CPM_SOURCE_CACHE
162162
CACHE PATH "Directory to download CPM dependencies"
163163
)
164164

165-
if(NOT CPM_DONT_UPDATE_MODULE_PATH)
165+
if(${CMAKE_VERSION} VERSION_LESS "3.24.0" AND NOT CPM_DONT_UPDATE_MODULE_PATH)
166166
set(CPM_MODULE_PATH
167167
"${CMAKE_BINARY_DIR}/CPM_modules"
168168
CACHE INTERNAL ""
@@ -269,10 +269,24 @@ endfunction()
269269
# finding the system library
270270
function(cpm_create_module_file Name)
271271
if(NOT CPM_DONT_UPDATE_MODULE_PATH)
272-
# erase any previous modules
273-
file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake
274-
"include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)"
275-
)
272+
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0")
273+
# Redirect find_package calls to the CPM package. This is what FetchContent does when you set OVERRIDE_FIND_PACKAGE.
274+
# The CMAKE_FIND_PACKAGE_REDIRECTS_DIR works for find_package in CONFIG mode, unlike the Find${Name}.cmake fallback.
275+
# https://cmake.org/cmake/help/latest/module/FetchContent.html#fetchcontent-find-package-integration-examples
276+
string(TOLOWER ${Name} NameLower)
277+
file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-config.cmake
278+
"include(\"${CMAKE_CURRENT_LIST_DIR}/${NameLower}-extra.cmake\" OPTIONAL)\n"
279+
"include(\"${CMAKE_CURRENT_LIST_DIR}/${Name}Extra.cmake\" OPTIONAL)\n"
280+
)
281+
file(WRITE ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/${NameLower}-version.cmake
282+
"set(PACKAGE_VERSION_COMPATIBLE TRUE)\n"
283+
"set(PACKAGE_VERSION_EXACT TRUE)\n"
284+
)
285+
else()
286+
file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake
287+
"include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)"
288+
)
289+
endif()
276290
endif()
277291
endfunction()
278292

0 commit comments

Comments
 (0)