2020-12-01 13:37:26 -08:00
|
|
|
#[===[.md:
|
|
|
|
# vcpkg_from_github
|
|
|
|
|
|
|
|
Download and extract a project from GitHub. Enables support for `install --head`.
|
|
|
|
|
2021-11-22 17:14:23 -06:00
|
|
|
This also works with Gitea by specifying the Gitea server with the `GITHUB_HOST` option.
|
|
|
|
|
2020-12-01 13:37:26 -08:00
|
|
|
## Usage:
|
|
|
|
```cmake
|
|
|
|
vcpkg_from_github(
|
|
|
|
OUT_SOURCE_PATH <SOURCE_PATH>
|
|
|
|
REPO <Microsoft/cpprestsdk>
|
|
|
|
[REF <v2.0.0>]
|
|
|
|
[SHA512 <45d0d7f8cc350...>]
|
|
|
|
[HEAD_REF <master>]
|
|
|
|
[PATCHES <patch1.patch> <patch2.patch>...]
|
|
|
|
[GITHUB_HOST <https://github.com>]
|
|
|
|
[AUTHORIZATION_TOKEN <${SECRET_FROM_FILE}>]
|
2020-12-03 13:24:26 -08:00
|
|
|
[FILE_DISAMBIGUATOR <N>]
|
2020-12-01 13:37:26 -08:00
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
## Parameters:
|
|
|
|
### OUT_SOURCE_PATH
|
|
|
|
Specifies the out-variable that will contain the extracted location.
|
|
|
|
|
|
|
|
This should be set to `SOURCE_PATH` by convention.
|
|
|
|
|
|
|
|
### REPO
|
|
|
|
The organization or user and repository on GitHub.
|
|
|
|
|
|
|
|
### REF
|
|
|
|
A stable git commit-ish (ideally a tag or commit) that will not change contents. **This should not be a branch.**
|
|
|
|
|
|
|
|
For repositories without official releases, this can be set to the full commit id of the current latest master.
|
|
|
|
|
|
|
|
If `REF` is specified, `SHA512` must also be specified.
|
|
|
|
|
|
|
|
### SHA512
|
|
|
|
The SHA512 hash that should match the archive (https://github.com/${REPO}/archive/${REF}.tar.gz).
|
|
|
|
|
2021-07-16 00:17:19 -07:00
|
|
|
This is most easily determined by first setting it to `0`, then trying to build the port. The error message will contain the full hash, which can be copied back into the portfile.
|
2020-12-01 13:37:26 -08:00
|
|
|
|
|
|
|
### HEAD_REF
|
|
|
|
The unstable git commit-ish (ideally a branch) to pull for `--head` builds.
|
|
|
|
|
|
|
|
For most projects, this should be `master`. The chosen branch should be one that is expected to be always buildable on all supported platforms.
|
|
|
|
|
|
|
|
### PATCHES
|
|
|
|
A list of patches to be applied to the extracted sources.
|
|
|
|
|
|
|
|
Relative paths are based on the port directory.
|
|
|
|
|
|
|
|
### GITHUB_HOST
|
|
|
|
A replacement host for enterprise GitHub instances.
|
|
|
|
|
|
|
|
This field should contain the scheme, host, and port of the desired URL without a trailing slash.
|
|
|
|
|
|
|
|
### AUTHORIZATION_TOKEN
|
|
|
|
A token to be passed via the Authorization HTTP header as "token ${AUTHORIZATION_TOKEN}".
|
|
|
|
|
|
|
|
### FILE_DISAMBIGUATOR
|
|
|
|
A token to uniquely identify the resulting filename if the SHA512 changes even though a git ref does not, to avoid stepping on the same file name.
|
|
|
|
|
|
|
|
## Notes:
|
|
|
|
At least one of `REF` and `HEAD_REF` must be specified, however it is preferable for both to be present.
|
|
|
|
|
|
|
|
This exports the `VCPKG_HEAD_VERSION` variable during head builds.
|
|
|
|
|
|
|
|
## Examples:
|
|
|
|
|
|
|
|
* [cpprestsdk](https://github.com/Microsoft/vcpkg/blob/master/ports/cpprestsdk/portfile.cmake)
|
|
|
|
* [ms-gsl](https://github.com/Microsoft/vcpkg/blob/master/ports/ms-gsl/portfile.cmake)
|
2021-06-05 05:18:54 +05:30
|
|
|
* [boost-beast](https://github.com/Microsoft/vcpkg/blob/master/ports/boost-beast/portfile.cmake)
|
2020-12-01 13:37:26 -08:00
|
|
|
#]===]
|
|
|
|
|
2017-05-02 20:34:11 -07:00
|
|
|
function(vcpkg_from_github)
|
2021-07-14 14:45:18 -05:00
|
|
|
cmake_parse_arguments(PARSE_ARGV 0 "arg"
|
|
|
|
""
|
|
|
|
"OUT_SOURCE_PATH;REPO;REF;SHA512;HEAD_REF;GITHUB_HOST;AUTHORIZATION_TOKEN;FILE_DISAMBIGUATOR"
|
|
|
|
"PATCHES")
|
2017-05-02 20:34:11 -07:00
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
if(DEFINED arg_UNPARSED_ARGUMENTS)
|
|
|
|
message(WARNING "vcpkg_from_github was passed extra arguments: ${arg_UNPARSED_ARGUMENTS}")
|
2017-05-02 20:34:11 -07:00
|
|
|
endif()
|
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
if(DEFINED arg_REF AND NOT DEFINED arg_SHA512)
|
2017-05-02 20:34:11 -07:00
|
|
|
message(FATAL_ERROR "SHA512 must be specified if REF is specified.")
|
|
|
|
endif()
|
2021-07-14 14:45:18 -05:00
|
|
|
if(NOT DEFINED arg_REF AND DEFINED arg_SHA512)
|
|
|
|
message(FATAL_ERROR "REF must be specified if SHA512 is specified.")
|
2017-05-02 20:34:11 -07:00
|
|
|
endif()
|
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
if(NOT DEFINED arg_OUT_SOURCE_PATH)
|
|
|
|
message(FATAL_ERROR "OUT_SOURCE_PATH must be specified.")
|
2017-05-02 20:34:11 -07:00
|
|
|
endif()
|
2021-07-14 14:45:18 -05:00
|
|
|
if(NOT DEFINED arg_REPO)
|
|
|
|
message(FATAL_ERROR "The GitHub repository must be specified.")
|
2019-03-18 11:32:22 -07:00
|
|
|
endif()
|
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
if(NOT DEFINED arg_GITHUB_HOST)
|
|
|
|
set(github_host "https://github.com")
|
|
|
|
set(github_api_url "https://api.github.com")
|
2019-11-22 10:05:39 -08:00
|
|
|
else()
|
2021-07-14 14:45:18 -05:00
|
|
|
set(github_host "${arg_GITHUB_HOST}")
|
|
|
|
set(github_api_url "${arg_GITHUB_HOST}/api/v3")
|
2019-03-18 11:32:22 -07:00
|
|
|
endif()
|
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
set(headers_param "")
|
|
|
|
if(DEFINED arg_AUTHORIZATION_TOKEN)
|
|
|
|
set(headers_param "HEADERS" "Authorization: token ${arg_AUTHORIZATION_TOKEN}")
|
2017-05-02 20:34:11 -07:00
|
|
|
endif()
|
|
|
|
|
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
if(NOT DEFINED arg_REF AND NOT DEFINED arg_HEAD_REF)
|
|
|
|
message(FATAL_ERROR "At least one of REF or HEAD_REF must be specified.")
|
|
|
|
endif()
|
2017-11-07 09:06:54 -08:00
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
if(NOT arg_REPO MATCHES "^([^/]*)/([^/]*)$")
|
|
|
|
message(FATAL_ERROR "REPO (${arg_REPO}) is not a valid repo name:
|
|
|
|
must be an organization name followed by a repository name separated by a single slash.")
|
|
|
|
endif()
|
|
|
|
set(org_name "${CMAKE_MATCH_1}")
|
|
|
|
set(repo_name "${CMAKE_MATCH_2}")
|
|
|
|
|
2022-03-18 09:59:45 -07:00
|
|
|
if(VCPKG_USE_HEAD_VERSION AND NOT DEFINED arg_HEAD_REF)
|
|
|
|
message(STATUS "Package does not specify HEAD_REF. Falling back to non-HEAD version.")
|
|
|
|
set(VCPKG_USE_HEAD_VERSION OFF)
|
|
|
|
elseif(NOT VCPKG_USE_HEAD_VERSION AND NOT DEFINED arg_REF)
|
2021-07-14 14:45:18 -05:00
|
|
|
message(FATAL_ERROR "Package does not specify REF. It must be built using --head.")
|
2017-05-02 20:34:11 -07:00
|
|
|
endif()
|
|
|
|
|
2021-07-14 14:45:18 -05:00
|
|
|
# exports VCPKG_HEAD_VERSION to the caller. This will get picked up by ports.cmake after the build.
|
|
|
|
if(VCPKG_USE_HEAD_VERSION)
|
2022-03-18 09:59:45 -07:00
|
|
|
string(REPLACE "/" "_-" sanitized_head_ref "${arg_HEAD_REF}")
|
2021-07-14 14:45:18 -05:00
|
|
|
vcpkg_download_distfile(archive_version
|
|
|
|
URLS "${github_api_url}/repos/${org_name}/${repo_name}/git/refs/heads/${arg_HEAD_REF}"
|
2022-03-18 09:59:45 -07:00
|
|
|
FILENAME "${org_name}-${repo_name}-${sanitized_head_ref}.version"
|
2021-07-28 03:04:22 +03:00
|
|
|
${headers_param}
|
2017-12-30 01:21:18 +03:00
|
|
|
SKIP_SHA512
|
2021-07-14 14:45:18 -05:00
|
|
|
ALWAYS_REDOWNLOAD
|
2017-05-02 20:34:11 -07:00
|
|
|
)
|
2021-07-14 14:45:18 -05:00
|
|
|
# Parse the github refs response with regex.
|
|
|
|
file(READ "${archive_version}" version_contents)
|
2022-03-18 09:59:45 -07:00
|
|
|
string(JSON head_version
|
|
|
|
ERROR_VARIABLE head_version_err
|
|
|
|
GET "${version_contents}"
|
|
|
|
"object"
|
|
|
|
"sha"
|
|
|
|
)
|
|
|
|
if(NOT "${head_version_err}" STREQUAL "NOTFOUND")
|
2021-07-14 14:45:18 -05:00
|
|
|
message(FATAL_ERROR "Failed to parse API response from '${version_url}':
|
|
|
|
${version_contents}
|
2022-03-18 09:59:45 -07:00
|
|
|
|
|
|
|
Error was: ${head_version_err}
|
2021-07-14 14:45:18 -05:00
|
|
|
")
|
|
|
|
endif()
|
2022-03-18 09:59:45 -07:00
|
|
|
|
|
|
|
set(VCPKG_HEAD_VERSION "${head_version}" PARENT_SCOPE)
|
|
|
|
set(ref_to_use "${head_version}")
|
|
|
|
|
|
|
|
vcpkg_list(SET redownload_param ALWAYS_REDOWNLOAD)
|
|
|
|
vcpkg_list(SET sha512_param SKIP_SHA512)
|
|
|
|
vcpkg_list(SET working_directory_param WORKING_DIRECTORY "${CURRENT_BUILDTREES_DIR}/src/head")
|
|
|
|
vcpkg_list(SET skip_patch_check_param SKIP_PATCH_CHECK)
|
|
|
|
else()
|
|
|
|
set(ref_to_use "${arg_REF}")
|
|
|
|
|
|
|
|
vcpkg_list(SET redownload_param)
|
|
|
|
vcpkg_list(SET working_directory_param)
|
|
|
|
vcpkg_list(SET skip_patch_check_param)
|
|
|
|
vcpkg_list(SET sha512_param SHA512 "${arg_SHA512}")
|
2018-01-05 16:16:08 -08:00
|
|
|
endif()
|
2017-05-02 20:34:11 -07:00
|
|
|
|
2022-03-18 09:59:45 -07:00
|
|
|
string(REPLACE "/" "_-" sanitized_ref "${ref_to_use}")
|
|
|
|
if(DEFINED arg_FILE_DISAMBIGUATOR AND NOT VCPKG_USE_HEAD_REF)
|
|
|
|
set(downloaded_file_name "${org_name}-${repo_name}-${sanitized_ref}-${arg_FILE_DISAMBIGUATOR}.tar.gz")
|
|
|
|
else()
|
|
|
|
set(downloaded_file_name "${org_name}-${repo_name}-${sanitized_ref}.tar.gz")
|
|
|
|
endif()
|
2021-07-14 14:45:18 -05:00
|
|
|
# Try to download the file information from github
|
|
|
|
vcpkg_download_distfile(archive
|
|
|
|
URLS "${github_host}/${org_name}/${repo_name}/archive/${ref_to_use}.tar.gz"
|
|
|
|
FILENAME "${downloaded_file_name}"
|
|
|
|
${headers_param}
|
|
|
|
${sha512_param}
|
|
|
|
${redownload_param}
|
|
|
|
)
|
2018-09-04 19:20:46 -04:00
|
|
|
vcpkg_extract_source_archive_ex(
|
|
|
|
OUT_SOURCE_PATH SOURCE_PATH
|
2021-07-14 14:45:18 -05:00
|
|
|
ARCHIVE "${archive}"
|
|
|
|
REF "${sanitized_ref}"
|
|
|
|
PATCHES ${arg_PATCHES}
|
|
|
|
${working_directory_param}
|
2022-03-18 09:59:45 -07:00
|
|
|
${skip_patch_check_param}
|
2018-02-21 21:17:14 -08:00
|
|
|
)
|
2021-07-14 14:45:18 -05:00
|
|
|
set("${arg_OUT_SOURCE_PATH}" "${SOURCE_PATH}" PARENT_SCOPE)
|
2017-05-02 20:34:11 -07:00
|
|
|
endfunction()
|