mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-21 01:47:28 -05:00
If a name isn't provided, try to infer it from the git repo (#202)
* Added assert_not_defined check * Function to get package name form git uri and tests * Autofix format * If name is not provided, try to infer it from the git repo * Unset result of cpm_package_name_from_git_uri if there is no match - Also reordered tests to ensure that the result is actually unset when needed * Removed trailing spaces in README * Updated the main example with the new minimal syntax * Well... autofix format again * Update error message for missing name to reflect the possible auto-infer step * Autofix format... yet again :)
This commit is contained in:
committed by
GitHub
parent
2744b87f07
commit
4cbf443363
@@ -71,7 +71,6 @@ add_executable(tests tests.cpp)
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
CPMAddPackage(
|
||||
NAME Catch2
|
||||
GITHUB_REPOSITORY catchorg/Catch2
|
||||
VERSION 2.5.0
|
||||
)
|
||||
|
||||
@@ -134,6 +134,19 @@ endif()
|
||||
include(FetchContent)
|
||||
include(CMakeParseArguments)
|
||||
|
||||
# Infer package name from git repository uri (path or url)
|
||||
function(cpm_package_name_from_git_uri URI RESULT)
|
||||
string(REGEX MATCH "([^/:]+)/?.git/?$" cpmGitUriMatch "${URI}")
|
||||
if(DEFINED cpmGitUriMatch)
|
||||
set(${RESULT}
|
||||
${CMAKE_MATCH_1}
|
||||
PARENT_SCOPE
|
||||
)
|
||||
else()
|
||||
unset(${RESULT} PARENT_SCOPE)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Initialize logging prefix
|
||||
if(NOT CPM_INDENT)
|
||||
set(CPM_INDENT
|
||||
@@ -264,12 +277,6 @@ function(CPMAddPackage)
|
||||
|
||||
cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}")
|
||||
|
||||
# Check for required arguments
|
||||
|
||||
if(NOT DEFINED CPM_ARGS_NAME)
|
||||
message(FATAL_ERROR "CPM: 'NAME' was not provided for package added with arguments: '${ARGN}'")
|
||||
endif()
|
||||
|
||||
# Set default values for arguments
|
||||
|
||||
if(NOT DEFINED CPM_ARGS_VERSION)
|
||||
@@ -297,6 +304,11 @@ function(CPMAddPackage)
|
||||
if(NOT DEFINED CPM_ARGS_GIT_TAG)
|
||||
set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION})
|
||||
endif()
|
||||
|
||||
# If a name wasn't provided, try to infer it from the git repo
|
||||
if(NOT DEFINED CPM_ARGS_NAME)
|
||||
cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(CPM_SKIP_FETCH FALSE)
|
||||
@@ -309,6 +321,15 @@ function(CPMAddPackage)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Check for required arguments
|
||||
|
||||
if(NOT DEFINED CPM_ARGS_NAME)
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"CPM: 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'"
|
||||
)
|
||||
endif()
|
||||
|
||||
# Check if package has been added before
|
||||
cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" "${CPM_ARGS_OPTIONS}")
|
||||
if(CPM_PACKAGE_ALREADY_ADDED)
|
||||
|
||||
@@ -36,6 +36,14 @@ function(ASSERT_DEFINED KEY)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(ASSERT_NOT_DEFINED KEY)
|
||||
if(DEFINED ${KEY})
|
||||
message(FATAL_ERROR "assertion failed: '${KEY}' is defiend")
|
||||
else()
|
||||
message(STATUS "test passed: '${KEY}' is not defined")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(ASSERT_TRUTHY KEY)
|
||||
if(${${KEY}})
|
||||
message(STATUS "test passed: '${KEY}' is set truthy")
|
||||
|
||||
28
test/unit/package_name_from_git_uri.cmake
Normal file
28
test/unit/package_name_from_git_uri.cmake
Normal file
@@ -0,0 +1,28 @@
|
||||
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
|
||||
|
||||
include(${CPM_PATH}/CPM.cmake)
|
||||
include(${CPM_PATH}/testing.cmake)
|
||||
|
||||
cpm_package_name_from_git_uri("https://github.com/cpm-cmake/CPM.cmake.git" name)
|
||||
assert_equal("CPM.cmake" ${name})
|
||||
|
||||
cpm_package_name_from_git_uri("ssh://user@host.xz:123/path/to/pkg.git/" name)
|
||||
assert_equal("pkg" ${name})
|
||||
|
||||
cpm_package_name_from_git_uri("git://host.xz/path/to/pkg.git" name)
|
||||
assert_equal("pkg" ${name})
|
||||
|
||||
cpm_package_name_from_git_uri("git@host.xz:cool-pkg.git" name)
|
||||
assert_equal("cool-pkg" ${name})
|
||||
|
||||
cpm_package_name_from_git_uri("file:///path/to/pkg33.git" name)
|
||||
assert_equal("pkg33" ${name})
|
||||
|
||||
cpm_package_name_from_git_uri("../local-repo/.git" name)
|
||||
assert_equal("local-repo" ${name})
|
||||
|
||||
cpm_package_name_from_git_uri("asdf" name)
|
||||
assert_not_defined(name)
|
||||
|
||||
cpm_package_name_from_git_uri("/something.git/stuff" name)
|
||||
assert_not_defined(name)
|
||||
Reference in New Issue
Block a user