Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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})
Expand Down