From 49652b05c2b1d5f48e71484ee8ed15b806cb82ce Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Wed, 26 Jan 2022 11:47:36 +0100 Subject: [PATCH 01/12] Added relative git URL support --- cmake/CPM.cmake | 53 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 933aa721..c834abc9 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -316,6 +316,9 @@ function(cpm_parse_add_package_single_arg arg outArgs) elseif(scheme STREQUAL "bb") set(out "BITBUCKET_REPOSITORY;${uri}") set(packageType "git") + elseif(scheme STREQUAL "rel") + set(out "RELATIVE_REPOSITORY;${uri}") + set(packageType "git") # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine # type elseif(arg MATCHES ".git/?(@|#|$)") @@ -365,11 +368,11 @@ function(cpm_parse_add_package_single_arg arg outArgs) ) endfunction() +find_package(Git REQUIRED) + # Check that the working directory for a git repo is clean function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) - find_package(Git REQUIRED) - if(NOT GIT_EXECUTABLE) # No git executable, assume directory is clean set(${isClean} @@ -427,6 +430,48 @@ function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) endfunction() +# Convert a URI relative to that of the URL of the remote of our current Git repository +# e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git +function(cpm_git_relative_uri_to_url relative_uri absolute_url) + if (NOT Git_FOUND) + message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL") + return() + endif() + + # Get the list of remotes available + execute_process(COMMAND ${GIT_EXECUTABLE} remote + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE remotes_names + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + + string(REPLACE "\n" ";" remotes_names_list ${remotes_names}) + + list(LENGTH remotes_names_list remotes_count) + + if (remotes_count EQUAL 0) + message(ERROR "Repository located at ${CMAKE_CURRENT_SOURCE_DIR} has no remote, cannot use relative URLs") + return() + endif() + + # Get the first remote + list(GET remotes_names_list 0 remote_name) + + # Get the fetch URL of that remote + execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote_name} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE remote_url + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) + + string(CONCAT absolute_url_local "${remote_url}" "/" "${relative_uri}") + + set(${absolute_url} + "${absolute_url_local}" + PARENT_SCOPE + ) +endfunction() + # Download and add a package from source function(CPMAddPackage) list(LENGTH ARGN argnLength) @@ -446,6 +491,7 @@ function(CPMAddPackage) GITHUB_REPOSITORY GITLAB_REPOSITORY BITBUCKET_REPOSITORY + RELATIVE_REPOSITORY GIT_REPOSITORY SOURCE_DIR DOWNLOAD_COMMAND @@ -480,6 +526,9 @@ function(CPMAddPackage) set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_RELATIVE_REPOSITORY) + cpm_git_relative_uri_to_url("${CPM_ARGS_RELATIVE_REPOSITORY}" absolute_git_url) + set(CPM_ARGS_GIT_REPOSITORY "${absolute_git_url}.git") endif() if(DEFINED CPM_ARGS_GIT_REPOSITORY) From 07c9d15bcfcc6262f046da3764ae34cab32700ef Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Thu, 27 Jan 2022 11:34:12 +0100 Subject: [PATCH 02/12] Added a cache system for resolved relative URLs, reduced the amount of calls to git --- cmake/CPM.cmake | 61 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index c834abc9..b3510c0a 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -432,40 +432,65 @@ endfunction() # Convert a URI relative to that of the URL of the remote of our current Git repository # e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git -function(cpm_git_relative_uri_to_url relative_uri absolute_url) +function(cpm_git_relative_uri_to_url relative_uri name absolute_url) + + if (NOT name) + # If no name was provided, get it from the relative uri + cpm_package_name_from_git_uri("${relative_uri}.git" name) + + if (NOT name) + message(SEND_ERROR "Name of the project couldn't be inferred from the relative URI") + return() + endif() + endif() + + # If it has been cached, do not resolve it + if (DEFINED CPM_PACKAGE_${name}_RESOLVED_URL) + set(${absolute_url} + "${CPM_PACKAGE_${name}_RESOLVED_URL}" + PARENT_SCOPE + ) + return() + endif() + if (NOT Git_FOUND) message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL") return() endif() # Get the list of remotes available - execute_process(COMMAND ${GIT_EXECUTABLE} remote + execute_process(COMMAND ${GIT_EXECUTABLE} remote -v WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE remotes_names + OUTPUT_VARIABLE remotes_infos OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) - string(REPLACE "\n" ";" remotes_names_list ${remotes_names}) + string(REPLACE "\n" ";" remotes_infos_list "${remotes_infos}") + + # Since this was a verbose output, the output is in the format (fetch/push) + # Get the first remote fetch URL + foreach(remote_info ${remotes_infos_list}) + string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remote_info}") - list(LENGTH remotes_names_list remotes_count) + if (match) + set(remote_url "${CMAKE_MATCH_1}") + break() + endif() + endforeach() - if (remotes_count EQUAL 0) - message(ERROR "Repository located at ${CMAKE_CURRENT_SOURCE_DIR} has no remote, cannot use relative URLs") + if (NOT DEFINED remote_url) + message(SEND_ERROR "No remote with fetch ability was detected in this repository") return() endif() - # Get the first remote - list(GET remotes_names_list 0 remote_name) - - # Get the fetch URL of that remote - execute_process(COMMAND ${GIT_EXECUTABLE} remote get-url ${remote_name} - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE remote_url - OUTPUT_STRIP_TRAILING_WHITESPACE - ERROR_QUIET) - string(CONCAT absolute_url_local "${remote_url}" "/" "${relative_uri}") + # Save it within the cache + set("CPM_PACKAGE_${name}_RESOLVED_URL" + "${absolute_url_local}" + CACHE INTERNAL "" + ) + set(${absolute_url} "${absolute_url_local}" PARENT_SCOPE @@ -527,7 +552,7 @@ function(CPMAddPackage) elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") elseif(DEFINED CPM_ARGS_RELATIVE_REPOSITORY) - cpm_git_relative_uri_to_url("${CPM_ARGS_RELATIVE_REPOSITORY}" absolute_git_url) + cpm_git_relative_uri_to_url("${CPM_ARGS_RELATIVE_REPOSITORY}" "${CPM_ARGS_NAME}" absolute_git_url) set(CPM_ARGS_GIT_REPOSITORY "${absolute_git_url}.git") endif() From c6fddab3b14f81e8cb896d3d7cc4bc22e5893f9b Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Fri, 28 Jan 2022 09:33:13 +0100 Subject: [PATCH 03/12] Added a way to override the base URL that is used to resolve the relative URIs --- cmake/CPM.cmake | 61 ++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index b3510c0a..c59fcee8 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -430,6 +430,16 @@ function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) endfunction() +# Set a specific URL to be used as a base for all relative URIs, e.g. "https://github.com/myorg" +function(CPMSetRelativeUriBaseUrl base_url) + set(CPM_RELATIVE_URI_BASE_URL "${base_url}" PARENT_SCOPE) +endfunction() + +# Specify that the base URL for relative URIs should be inferred from the current directory (default behavior) +function(CPMSetRelativeUriBaseAuto) + unset(CPM_RELATIVE_URI_BASE_URL PARENT_SCOPE) +endfunction() + # Convert a URI relative to that of the URL of the remote of our current Git repository # e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git function(cpm_git_relative_uri_to_url relative_uri name absolute_url) @@ -453,34 +463,39 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) return() endif() - if (NOT Git_FOUND) - message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL") - return() - endif() + # If a base URL is specified, use that + if (CPM_RELATIVE_URI_BASE_URL) + set(remote_url "${CPM_RELATIVE_URI_BASE_URL}") + else() + if (NOT Git_FOUND) + message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL") + return() + endif() - # Get the list of remotes available - execute_process(COMMAND ${GIT_EXECUTABLE} remote -v - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE remotes_infos - OUTPUT_STRIP_TRAILING_WHITESPACE - ERROR_QUIET) + # Get the list of remotes available + execute_process(COMMAND ${GIT_EXECUTABLE} remote -v + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE remotes_infos + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET) - string(REPLACE "\n" ";" remotes_infos_list "${remotes_infos}") + string(REPLACE "\n" ";" remotes_infos_list "${remotes_infos}") - # Since this was a verbose output, the output is in the format (fetch/push) - # Get the first remote fetch URL - foreach(remote_info ${remotes_infos_list}) - string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remote_info}") + # Since this was a verbose output, the output is in the format (fetch/push) + # Get the first remote fetch URL + foreach(remote_info ${remotes_infos_list}) + string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remote_info}") - if (match) - set(remote_url "${CMAKE_MATCH_1}") - break() - endif() - endforeach() + if (match) + set(remote_url "${CMAKE_MATCH_1}") + break() + endif() + endforeach() - if (NOT DEFINED remote_url) - message(SEND_ERROR "No remote with fetch ability was detected in this repository") - return() + if (NOT DEFINED remote_url) + message(SEND_ERROR "No remote with fetch ability was detected in this repository") + return() + endif() endif() string(CONCAT absolute_url_local "${remote_url}" "/" "${relative_uri}") From 0178177c2b1b9448f48736f8c0a25fe2735a705e Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Wed, 2 Feb 2022 11:11:36 +0100 Subject: [PATCH 04/12] Added built-in URL normalizing, now requires CMake 3.20+ for the feature --- cmake/CPM.cmake | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index c59fcee8..3e96a507 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -440,9 +440,43 @@ function(CPMSetRelativeUriBaseAuto) unset(CPM_RELATIVE_URI_BASE_URL PARENT_SCOPE) endfunction() +# Normalize the URI, mainly for "cd up" (..) directives, as those are not supported by SSH +function(cpm_normalize_uri input output) + # Extract the scheme and the authority (if present) from the path + # Can either be http(s)://authority/ or git@authority: + string(REGEX MATCH "^(https{0,1}:\\/\\/[^\\/]+\\/)(.*)$" match "${input}") + + if(match) + set(schemeAndAuthority "${CMAKE_MATCH_1}") + set(path "${CMAKE_MATCH_2}") + else() + string(REGEX MATCH "^(git@[^:]+:)(.*)$" match "${input}") + + if(match) + set(schemeAndAuthority "${CMAKE_MATCH_1}") + set(path "${CMAKE_MATCH_2}") + else() + set(schemeAndAuthority "") + set(path "${input}") + endif() + endif() + + # Normalize the path, which should get rid of the "cd up" directives + cmake_path(NORMAL_PATH path) + + set(${output} + "${schemeAndAuthority}${path}" + PARENT_SCOPE + ) +endfunction() + # Convert a URI relative to that of the URL of the remote of our current Git repository # e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git function(cpm_git_relative_uri_to_url relative_uri name absolute_url) + if(${CMAKE_VERSION} VERSION_LESS "3.20.0") + message(ERROR "Relative URIs require CMake 3.20+") + return() + endif() if (NOT name) # If no name was provided, get it from the relative uri @@ -463,7 +497,7 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) return() endif() - # If a base URL is specified, use that + # If a base URL is specified, use that if (CPM_RELATIVE_URI_BASE_URL) set(remote_url "${CPM_RELATIVE_URI_BASE_URL}") else() @@ -498,16 +532,18 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) endif() endif() - string(CONCAT absolute_url_local "${remote_url}" "/" "${relative_uri}") + string(CONCAT absoluteUrl "${remote_url}" "/" "${relative_uri}") + + cpm_normalize_uri("${absoluteUrl}" absoluteUrlNormalized) # Save it within the cache set("CPM_PACKAGE_${name}_RESOLVED_URL" - "${absolute_url_local}" + "${absoluteUrlNormalized}" CACHE INTERNAL "" ) set(${absolute_url} - "${absolute_url_local}" + "${absoluteUrlNormalized}" PARENT_SCOPE ) endfunction() From 26e0976366bbd3da17d18cf7d8057b1754fbf77c Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Wed, 2 Feb 2022 11:00:38 +0100 Subject: [PATCH 05/12] Fixed broken http(s) scheme detection in URI normalization --- cmake/CPM.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 3e96a507..883c8da3 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -444,7 +444,7 @@ endfunction() function(cpm_normalize_uri input output) # Extract the scheme and the authority (if present) from the path # Can either be http(s)://authority/ or git@authority: - string(REGEX MATCH "^(https{0,1}:\\/\\/[^\\/]+\\/)(.*)$" match "${input}") + string(REGEX MATCH "^(https?:\\/\\/[^\\/]+\\/)(.*)$" match "${input}") if(match) set(schemeAndAuthority "${CMAKE_MATCH_1}") From b9f9a54749e6cf9aa6477bce09bfddec372e5d17 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Thu, 10 Feb 2022 15:33:16 +0100 Subject: [PATCH 06/12] Brought find_package Git inside the functions to allow use of the functions some scopes above --- cmake/CPM.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 883c8da3..b7b07088 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -368,10 +368,9 @@ function(cpm_parse_add_package_single_arg arg outArgs) ) endfunction() -find_package(Git REQUIRED) - # Check that the working directory for a git repo is clean function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) + find_package(Git REQUIRED) if(NOT GIT_EXECUTABLE) # No git executable, assume directory is clean @@ -501,6 +500,8 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) if (CPM_RELATIVE_URI_BASE_URL) set(remote_url "${CPM_RELATIVE_URI_BASE_URL}") else() + find_package(Git REQUIRED) + if (NOT Git_FOUND) message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL") return() From d2a9b8141cb3caddee439a584a34ed22df33b655 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Sat, 20 Aug 2022 17:54:51 +0200 Subject: [PATCH 07/12] Styling fix --- cmake/CPM.cmake | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index b7b07088..cf072b77 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -430,8 +430,8 @@ function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) endfunction() # Set a specific URL to be used as a base for all relative URIs, e.g. "https://github.com/myorg" -function(CPMSetRelativeUriBaseUrl base_url) - set(CPM_RELATIVE_URI_BASE_URL "${base_url}" PARENT_SCOPE) +function(CPMSetRelativeUriBaseUrl baseUrl) + set(CPM_RELATIVE_URI_BASE_URL "${baseUrl}" PARENT_SCOPE) endfunction() # Specify that the base URL for relative URIs should be inferred from the current directory (default behavior) @@ -471,7 +471,7 @@ endfunction() # Convert a URI relative to that of the URL of the remote of our current Git repository # e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git -function(cpm_git_relative_uri_to_url relative_uri name absolute_url) +function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) if(${CMAKE_VERSION} VERSION_LESS "3.20.0") message(ERROR "Relative URIs require CMake 3.20+") return() @@ -479,7 +479,7 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) if (NOT name) # If no name was provided, get it from the relative uri - cpm_package_name_from_git_uri("${relative_uri}.git" name) + cpm_package_name_from_git_uri("${relativeUri}.git" name) if (NOT name) message(SEND_ERROR "Name of the project couldn't be inferred from the relative URI") @@ -489,7 +489,7 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) # If it has been cached, do not resolve it if (DEFINED CPM_PACKAGE_${name}_RESOLVED_URL) - set(${absolute_url} + set(${absoluteUrl} "${CPM_PACKAGE_${name}_RESOLVED_URL}" PARENT_SCOPE ) @@ -498,7 +498,7 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) # If a base URL is specified, use that if (CPM_RELATIVE_URI_BASE_URL) - set(remote_url "${CPM_RELATIVE_URI_BASE_URL}") + set(remoteUrl "${CPM_RELATIVE_URI_BASE_URL}") else() find_package(Git REQUIRED) @@ -510,30 +510,30 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) # Get the list of remotes available execute_process(COMMAND ${GIT_EXECUTABLE} remote -v WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE remotes_infos + OUTPUT_VARIABLE remotesInfos OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) - string(REPLACE "\n" ";" remotes_infos_list "${remotes_infos}") + string(REPLACE "\n" ";" remotesInfosList "${remotesInfos}") # Since this was a verbose output, the output is in the format (fetch/push) # Get the first remote fetch URL - foreach(remote_info ${remotes_infos_list}) - string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remote_info}") + foreach(remote_info ${remotesInfosList}) + string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remoteInfo}") if (match) - set(remote_url "${CMAKE_MATCH_1}") + set(remoteUrl "${CMAKE_MATCH_1}") break() endif() endforeach() - if (NOT DEFINED remote_url) + if (NOT DEFINED remoteUrl) message(SEND_ERROR "No remote with fetch ability was detected in this repository") return() endif() endif() - string(CONCAT absoluteUrl "${remote_url}" "/" "${relative_uri}") + string(CONCAT absoluteUrl "${remoteUrl}" "/" "${relativeUri}") cpm_normalize_uri("${absoluteUrl}" absoluteUrlNormalized) @@ -543,7 +543,7 @@ function(cpm_git_relative_uri_to_url relative_uri name absolute_url) CACHE INTERNAL "" ) - set(${absolute_url} + set(${absoluteUrl} "${absoluteUrlNormalized}" PARENT_SCOPE ) @@ -604,8 +604,8 @@ function(CPMAddPackage) elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") elseif(DEFINED CPM_ARGS_RELATIVE_REPOSITORY) - cpm_git_relative_uri_to_url("${CPM_ARGS_RELATIVE_REPOSITORY}" "${CPM_ARGS_NAME}" absolute_git_url) - set(CPM_ARGS_GIT_REPOSITORY "${absolute_git_url}.git") + cpm_git_relative_uri_to_url("${CPM_ARGS_RELATIVE_REPOSITORY}" "${CPM_ARGS_NAME}" absoluteGitUrl) + set(CPM_ARGS_GIT_REPOSITORY "${absoluteGitUrl}.git") endif() if(DEFINED CPM_ARGS_GIT_REPOSITORY) From a36e3bf0ef73a37ca35f505aa20ba326bd8fd2a6 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Sat, 20 Aug 2022 18:03:10 +0200 Subject: [PATCH 08/12] Fixed missing case change --- cmake/CPM.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index cf072b77..e7da6bbf 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -518,7 +518,7 @@ function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) # Since this was a verbose output, the output is in the format (fetch/push) # Get the first remote fetch URL - foreach(remote_info ${remotesInfosList}) + foreach(remoteInfo ${remotesInfosList}) string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remoteInfo}") if (match) From 6873962201fdce6afb4c3a737b5ed7f40e14c6c0 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Sun, 21 Aug 2022 12:17:16 +0200 Subject: [PATCH 09/12] Fixed formatting and fixed a typo --- cmake/CPM.cmake | 73 ++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index b6d4d70d..61b66bce 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -458,18 +458,22 @@ endfunction() # Set a specific URL to be used as a base for all relative URIs, e.g. "https://github.com/myorg" function(CPMSetRelativeUriBaseUrl baseUrl) - set(CPM_RELATIVE_URI_BASE_URL "${baseUrl}" PARENT_SCOPE) + set(CPM_RELATIVE_URI_BASE_URL + "${baseUrl}" + PARENT_SCOPE + ) endfunction() -# Specify that the base URL for relative URIs should be inferred from the current directory (default behavior) +# Specify that the base URL for relative URIs should be inferred from the current directory (default +# behavior) function(CPMSetRelativeUriBaseAuto) unset(CPM_RELATIVE_URI_BASE_URL PARENT_SCOPE) endfunction() # Normalize the URI, mainly for "cd up" (..) directives, as those are not supported by SSH function(cpm_normalize_uri input output) - # Extract the scheme and the authority (if present) from the path - # Can either be http(s)://authority/ or git@authority: + # Extract the scheme and the authority (if present) from the path can either be + # http(s)://authority/ or git@authority: string(REGEX MATCH "^(https?:\\/\\/[^\\/]+\\/)(.*)$" match "${input}") if(match) @@ -491,70 +495,71 @@ function(cpm_normalize_uri input output) cmake_path(NORMAL_PATH path) set(${output} - "${schemeAndAuthority}${path}" - PARENT_SCOPE - ) + "${schemeAndAuthority}${path}" + PARENT_SCOPE + ) endfunction() -# Convert a URI relative to that of the URL of the remote of our current Git repository -# e.g. github.com/user/repo.git + ../other.git = github.com/user/other.git +# Convert a URI relative to that of the URL of the remote of our current Git repository e.g. +# github.com/user/repo.git + ../other.git = github.com/user/other.git function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) if(${CMAKE_VERSION} VERSION_LESS "3.20.0") message(ERROR "Relative URIs require CMake 3.20+") return() endif() - if (NOT name) + if(NOT name) # If no name was provided, get it from the relative uri cpm_package_name_from_git_uri("${relativeUri}.git" name) - if (NOT name) + if(NOT name) message(SEND_ERROR "Name of the project couldn't be inferred from the relative URI") return() endif() endif() # If it has been cached, do not resolve it - if (DEFINED CPM_PACKAGE_${name}_RESOLVED_URL) + if(DEFINED CPM_PACKAGE_${name}_RESOLVED_URL) set(${absoluteUrl} - "${CPM_PACKAGE_${name}_RESOLVED_URL}" - PARENT_SCOPE - ) + "${CPM_PACKAGE_${name}_RESOLVED_URL}" + PARENT_SCOPE + ) return() endif() - # If a base URL is specified, use that - if (CPM_RELATIVE_URI_BASE_URL) + # If a base URL is specified, use that + if(CPM_RELATIVE_URI_BASE_URL) set(remoteUrl "${CPM_RELATIVE_URI_BASE_URL}") else() find_package(Git REQUIRED) - if (NOT Git_FOUND) + if(NOT Git_FOUND) message(SEND_ERROR "Git not found, cannot convert relative URI to absolute Git URL") return() endif() # Get the list of remotes available - execute_process(COMMAND ${GIT_EXECUTABLE} remote -v - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE remotesInfos - OUTPUT_STRIP_TRAILING_WHITESPACE - ERROR_QUIET) + execute_process( + COMMAND ${GIT_EXECUTABLE} remote -v + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE remotesInfos + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + ) string(REPLACE "\n" ";" remotesInfosList "${remotesInfos}") - # Since this was a verbose output, the output is in the format (fetch/push) - # Get the first remote fetch URL + # Since this was a verbose output, the output is in the format + # (fetch/push) Get the first remote fetch URL foreach(remoteInfo ${remotesInfosList}) string(REGEX MATCH "^[^ \t]+[ \t]+([^ \t]+) \\(fetch\\)$" match "${remoteInfo}") - if (match) + if(match) set(remoteUrl "${CMAKE_MATCH_1}") break() endif() endforeach() - if (NOT DEFINED remoteUrl) + if(NOT DEFINED remoteUrl) message(SEND_ERROR "No remote with fetch ability was detected in this repository") return() endif() @@ -566,14 +571,14 @@ function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) # Save it within the cache set("CPM_PACKAGE_${name}_RESOLVED_URL" - "${absoluteUrlNormalized}" - CACHE INTERNAL "" - ) + "${absoluteUrlNormalized}" + CACHE INTERNAL "" + ) - set(${absoluteUrl} - "${absoluteUrlNormalized}" - PARENT_SCOPE - ) + set(${absoluteUri} + "${absoluteUrlNormalized}" + PARENT_SCOPE + ) endfunction() # method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload From 4341bbe963cc53488407f82449bb3b31188e7185 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Sun, 21 Aug 2022 14:20:52 +0200 Subject: [PATCH 10/12] Fixed confusing argument name --- cmake/CPM.cmake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 61b66bce..565abeb9 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -502,7 +502,7 @@ endfunction() # Convert a URI relative to that of the URL of the remote of our current Git repository e.g. # github.com/user/repo.git + ../other.git = github.com/user/other.git -function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) +function(cpm_git_relative_uri_to_url relativeUri name url) if(${CMAKE_VERSION} VERSION_LESS "3.20.0") message(ERROR "Relative URIs require CMake 3.20+") return() @@ -520,7 +520,7 @@ function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) # If it has been cached, do not resolve it if(DEFINED CPM_PACKAGE_${name}_RESOLVED_URL) - set(${absoluteUrl} + set(${url} "${CPM_PACKAGE_${name}_RESOLVED_URL}" PARENT_SCOPE ) @@ -575,7 +575,7 @@ function(cpm_git_relative_uri_to_url relativeUri name absoluteUri) CACHE INTERNAL "" ) - set(${absoluteUri} + set(${url} "${absoluteUrlNormalized}" PARENT_SCOPE ) From 1cb66cce06898a46f93d62294062f8591370df26 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Sun, 21 Aug 2022 14:30:12 +0200 Subject: [PATCH 11/12] Relative URLs are now re-resolved when the passed URI changes (no cache clearing needed) --- cmake/CPM.cmake | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 565abeb9..36bbfe19 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -519,7 +519,8 @@ function(cpm_git_relative_uri_to_url relativeUri name url) endif() # If it has been cached, do not resolve it - if(DEFINED CPM_PACKAGE_${name}_RESOLVED_URL) + if(DEFINED CPM_PACKAGE_${name}_RESOLVED_URL + AND "${CPM_PACKAGE_${name}_RELATIVE_URI}" STREQUAL "${relativeUri}") set(${url} "${CPM_PACKAGE_${name}_RESOLVED_URL}" PARENT_SCOPE @@ -527,6 +528,12 @@ function(cpm_git_relative_uri_to_url relativeUri name url) return() endif() + # Save the relative URI that was passed inside of the cache + set("CPM_PACKAGE_${name}_RELATIVE_URI" + "${relativeUri}" + CACHE INTERNAL "" + ) + # If a base URL is specified, use that if(CPM_RELATIVE_URI_BASE_URL) set(remoteUrl "${CPM_RELATIVE_URI_BASE_URL}") From dba6fcbf0b7513cb7eef9f7920b88f1687b9f822 Mon Sep 17 00:00:00 2001 From: Jonathan Vannier Date: Sun, 21 Aug 2022 14:32:22 +0200 Subject: [PATCH 12/12] Fixed format --- cmake/CPM.cmake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 36bbfe19..324e417e 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -519,8 +519,9 @@ function(cpm_git_relative_uri_to_url relativeUri name url) endif() # If it has been cached, do not resolve it - if(DEFINED CPM_PACKAGE_${name}_RESOLVED_URL - AND "${CPM_PACKAGE_${name}_RELATIVE_URI}" STREQUAL "${relativeUri}") + if(DEFINED CPM_PACKAGE_${name}_RESOLVED_URL AND "${CPM_PACKAGE_${name}_RELATIVE_URI}" STREQUAL + "${relativeUri}" + ) set(${url} "${CPM_PACKAGE_${name}_RESOLVED_URL}" PARENT_SCOPE