diff --git a/README.md b/README.md index 13e31ed2..1776284c 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,8 @@ After calling `CPMAddPackage`, the following variables are defined in the local 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. +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. + 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. ## Updating CPM diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 2d151bf9..e6a4c2ea 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -197,6 +197,10 @@ function(cpm_package_name_from_git_uri URI RESULT) ${CMAKE_MATCH_1} PARENT_SCOPE ) + # Also export org-prefixed variant if org can be extracted + if("${URI}" MATCHES "[/:]([^/:]+)/([^/:]+)(/?.git/?)?$") + set(${RESULT}_WITH_ORG "${CMAKE_MATCH_1}-${CMAKE_MATCH_2}" PARENT_SCOPE) + endif() else() unset(${RESULT} PARENT_SCOPE) endif() @@ -305,6 +309,14 @@ endfunction() function(cpm_find_package NAME VERSION) string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) + + if(NOT ${CPM_ARGS_NAME}_FOUND AND DEFINED ${CPM_ARGS_NAME}_WITH_ORG) + find_package(${${CPM_ARGS_NAME}_WITH_ORG} ${CPM_ARGS_VERSION} QUIET ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + if(${${CPM_ARGS_NAME}_WITH_ORG}_FOUND) + set(${CPM_ARGS_NAME}_FOUND TRUE) + endif() + endif() + if(${CPM_ARGS_NAME}_FOUND) if(DEFINED ${CPM_ARGS_NAME}_VERSION) set(VERSION ${${CPM_ARGS_NAME}_VERSION})