Skip to content

Commit b60aa1c

Browse files
committed
Support org-prefixed package names in find_package
CPM.cmake now attempts to locate packages using organization-prefixed names (e.g., org-library) if the standard name is not found. This helps resolve naming conflicts and improves compatibility with packages installed under organization-qualified names. Documentation has been updated to reflect this behavior.
1 parent d9364ce commit b60aa1c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ After calling `CPMAddPackage`, the following variables are defined in the local
134134

135135
For using CPM.cmake projects with external package managers, such as conan or vcpkg, setting the variable [`CPM_USE_LOCAL_PACKAGES`](#options) will make CPM.cmake try to add a package through `find_package` first, and add it from source if it doesn't succeed.
136136

137+
When using `find_package`, CPM will automatically try to locate installed packages using organization-prefixed names. For example, a package specified as `gh:org/library` will first be searched as `library`, and if not found, will be searched as `org-library`. This allows installed packages to be found using their organization-qualified names to avoid naming conflicts.
138+
137139
In rare cases, this behaviour may be desirable by default. The function `CPMFindPackage` will try to find a local dependency via CMake's `find_package` and fallback to `CPMAddPackage`, if the dependency is not found.
138140

139141
## Updating CPM

cmake/CPM.cmake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ function(cpm_package_name_from_git_uri URI RESULT)
197197
${CMAKE_MATCH_1}
198198
PARENT_SCOPE
199199
)
200+
# Also export org-prefixed variant if org can be extracted
201+
if("${URI}" MATCHES "[/:]([^/:]+)/([^/:]+)(/?.git/?)?$")
202+
set(${RESULT}_WITH_ORG "${CMAKE_MATCH_1}-${CMAKE_MATCH_2}" PARENT_SCOPE)
203+
endif()
200204
else()
201205
unset(${RESULT} PARENT_SCOPE)
202206
endif()
@@ -304,7 +308,14 @@ endfunction()
304308

305309
function(cpm_find_package NAME VERSION)
306310
string(REPLACE " " ";" EXTRA_ARGS "${ARGN}")
307-
find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET)
311+
find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET
312+
313+
if(NOT ${CPM_ARGS_NAME}_FOUND AND DEFINED ${CPM_ARGS_NAME}_WITH_ORG)
314+
find_package(${${CPM_ARGS_NAME}_WITH_ORG} ${CPM_ARGS_VERSION} QUIET ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS})
315+
if(${${CPM_ARGS_NAME}_WITH_ORG}_FOUND)
316+
set(${CPM_ARGS_NAME}_FOUND TRUE)
317+
endif()
318+
endif()
308319
if(${CPM_ARGS_NAME}_FOUND)
309320
if(DEFINED ${CPM_ARGS_NAME}_VERSION)
310321
set(VERSION ${${CPM_ARGS_NAME}_VERSION})

0 commit comments

Comments
 (0)