2017-08-30 06:12:23 +08:00
## # vcpkg_from_bitbucket
##
## Download and extract a project from Bitbucket.
## Enables support for installing HEAD `vcpkg.exe install --head <port>`.
##
## ## Usage:
## ```cmake
## vcpkg_from_bitbucket(
## OUT_SOURCE_PATH <SOURCE_PATH>
## REPO <Microsoft/cpprestsdk>
## [REF <v2.0.0>]
## [SHA512 <45d0d7f8cc350...>]
## [HEAD_REF <master>]
## )
## ```
##
## ## 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) 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://bitbucket.com/${REPO}/get/${REF}.tar.gz).
##
## This is most easily determined by first setting it to `1`, then trying to build the port. The error message will contain the full hash, which can be copied back into the portfile.
##
## ### 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.
##
## ## 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:
##
## * [blaze](https://github.com/Microsoft/vcpkg/blob/master/ports/blaze/portfile.cmake)
function ( vcpkg_from_bitbucket )
set ( oneValueArgs OUT_SOURCE_PATH REPO REF SHA512 HEAD_REF )
set ( multipleValuesArgs )
cmake_parse_arguments ( _vdud "" "${oneValueArgs}" "${multipleValuesArgs}" ${ ARGN } )
if ( NOT _vdud_OUT_SOURCE_PATH )
message ( FATAL_ERROR "OUT_SOURCE_PATH must be specified." )
endif ( )
if ( ( _vdud_REF AND NOT _vdud_SHA512 ) OR ( NOT _vdud_REF AND _vdud_SHA512 ) )
message ( FATAL_ERROR "SHA512 must be specified if REF is specified." )
endif ( )
if ( NOT _vdud_REPO )
message ( FATAL_ERROR "The Bitbucket repository must be specified." )
endif ( )
if ( NOT _vdud_REF AND NOT _vdud_HEAD_REF )
message ( FATAL_ERROR "At least one of REF and HEAD_REF must be specified." )
endif ( )
string ( REGEX REPLACE ".*/" "" REPO_NAME ${ _vdud_REPO } )
string ( REGEX REPLACE "/.*" "" ORG_NAME ${ _vdud_REPO } )
macro ( set_SOURCE_PATH BASE BASEREF )
set ( SOURCE_PATH "${BASE}/${ORG_NAME}-${REPO_NAME}-${BASEREF}" )
if ( EXISTS ${ SOURCE_PATH } )
set ( ${ _vdud_OUT_SOURCE_PATH } "${SOURCE_PATH}" PARENT_SCOPE )
else ( )
# Sometimes GitHub strips a leading 'v' off the REF.
string ( REGEX REPLACE "^v" "" REF ${ BASEREF } )
set ( SOURCE_PATH "${BASE}/${ORG_NAME}-${REPO_NAME}-${REF}" )
if ( EXISTS ${ SOURCE_PATH } )
set ( ${ _vdud_OUT_SOURCE_PATH } "${SOURCE_PATH}" PARENT_SCOPE )
else ( )
message ( FATAL_ERROR "Could not determine source path: '${BASE}/${ORG_NAME}-${REPO_NAME}-${BASEREF}' does not exist" )
endif ( )
endif ( )
endmacro ( )
if ( VCPKG_USE_HEAD_VERSION AND NOT _vdud_HEAD_REF )
message ( STATUS "Package does not specify HEAD_REF. Falling back to non-HEAD version." )
set ( VCPKG_USE_HEAD_VERSION OFF )
endif ( )
2017-11-04 05:50:16 +08:00
2017-08-30 06:12:23 +08:00
# Handle --no-head scenarios
if ( NOT VCPKG_USE_HEAD_VERSION )
if ( NOT _vdud_REF )
message ( FATAL_ERROR "Package does not specify REF. It must built using --head." )
endif ( )
2017-11-04 05:50:16 +08:00
2017-08-30 06:12:23 +08:00
set ( URL "https://bitbucket.com/${ORG_NAME}/${REPO_NAME}/get/${_vdud_REF}.tar.gz" )
set ( downloaded_file_path "${DOWNLOADS}/${ORG_NAME}-${REPO_NAME}-${_vdud_REF}.tar.gz" )
file ( DOWNLOAD "https://api.bitbucket.com/2.0/repositories/${ORG_NAME}/${REPO_NAME}/refs/tags/${_vdud_REF}"
$ { d o w n l o a d e d _ f i l e _ p a t h } . v e r s i o n
S T A T U S d o w n l o a d _ s t a t u s
)
list ( GET download_status 0 status_code )
if ( "${status_code}" STREQUAL "0" )
# Parse the github refs response with regex.
# TODO: use some JSON swiss-army-knife utility instead.
file ( READ "${downloaded_file_path}.version" _contents )
string ( REGEX MATCH "\" hash\ ": \" [a-f0-9]+\ "" x "${_contents}" )
string ( REGEX REPLACE "\" hash\ ": \" ( [a-f0-9]+ ) \"" "\\1" _version ${ x } )
string ( SUBSTRING ${ _version } 0 12 _version ) # Get the 12 first numbers from commit hash
else ( )
set ( _version ${ _vdud_REF } )
endif ( )
2017-11-04 05:50:16 +08:00
2017-08-30 06:12:23 +08:00
vcpkg_download_distfile ( ARCHIVE
U R L S " h t t p s : / / b i t b u c k e t . c o m / $ { O R G _ N A M E } / $ { R E P O _ N A M E } / g e t / $ { _ v d u d _ R E F } . t a r . g z "
S H A 5 1 2 " $ { _ v d u d _ S H A 5 1 2 } "
F I L E N A M E " $ { O R G _ N A M E } - $ { R E P O _ N A M E } - $ { _ v d u d _ R E F } . t a r . g z "
)
vcpkg_extract_source_archive_ex ( ARCHIVE "${ARCHIVE}" )
set_SOURCE_PATH ( ${ CURRENT_BUILDTREES_DIR } /src ${ _version } )
return ( )
endif ( )
# The following is for --head scenarios
set ( URL "https://bitbucket.com/${ORG_NAME}/${REPO_NAME}/get/${_vdud_HEAD_REF}.tar.gz" )
set ( downloaded_file_path "${DOWNLOADS}/${ORG_NAME}-${REPO_NAME}-${_vdud_HEAD_REF}.tar.gz" )
if ( _VCPKG_NO_DOWNLOADS )
if ( NOT EXISTS ${ downloaded_file_path } OR NOT EXISTS ${ downloaded_file_path } .version )
message ( FATAL_ERROR "Downloads are disabled, but '${downloaded_file_path}' does not exist." )
endif ( )
message ( STATUS "Using cached ${downloaded_file_path}" )
else ( )
if ( EXISTS ${ downloaded_file_path } )
message ( STATUS "Purging cached ${downloaded_file_path} to fetch latest (use --no-downloads to suppress)" )
file ( REMOVE ${ downloaded_file_path } )
endif ( )
if ( EXISTS ${ downloaded_file_path } .version )
file ( REMOVE ${ downloaded_file_path } .version )
endif ( )
if ( EXISTS ${ CURRENT_BUILDTREES_DIR } /src/head )
file ( REMOVE_RECURSE ${ CURRENT_BUILDTREES_DIR } /src/head )
endif ( )
# Try to download the file and version information from bitbucket.
2017-11-04 05:50:16 +08:00
set ( _VCPKG_INTERNAL_NO_HASH_CHECK "TRUE" )
vcpkg_download_distfile ( ARCHIVE_VERSION
U R L S " h t t p s : / / a p i . b i t b u c k e t . c o m / 2 . 0 / r e p o s i t o r i e s / $ { O R G _ N A M E } / $ { R E P O _ N A M E } / r e f s / b r a n c h e s / $ { _ v d u d _ H E A D _ R E F } "
F I L E N A M E $ { d o w n l o a d e d _ f i l e _ n a m e } . v e r s i o n
2017-08-30 06:12:23 +08:00
)
2017-11-04 05:50:16 +08:00
vcpkg_download_distfile ( ARCHIVE
U R L S $ { U R L }
F I L E N A M E $ { d o w n l o a d e d _ f i l e _ n a m e }
)
set ( _VCPKG_INTERNAL_NO_HASH_CHECK "FALSE" )
2017-08-30 06:12:23 +08:00
endif ( )
vcpkg_extract_source_archive_ex (
2017-11-04 05:50:16 +08:00
A R C H I V E " $ { A R C H I V E } "
2017-08-30 06:12:23 +08:00
W O R K I N G _ D I R E C T O R Y " $ { C U R R E N T _ B U I L D T R E E S _ D I R } / s r c / h e a d "
)
# Parse the github refs response with regex.
# TODO: use some JSON swiss-army-knife utility instead.
2017-11-04 05:50:16 +08:00
file ( READ "${ARCHIVE_VERSION}" _contents )
2017-08-30 06:12:23 +08:00
string ( REGEX MATCH "\" hash\ ": \" [a-f0-9]+\ "" x "${_contents}" )
string ( REGEX REPLACE "\" hash\ ": \" ( [a-f0-9]+ ) \"" "\\1" _version ${ x } )
string ( SUBSTRING ${ _version } 0 12 _vdud_HEAD_REF ) # Get the 12 first numbers from commit hash
2017-11-04 05:50:16 +08:00
2017-08-30 06:12:23 +08:00
# exports VCPKG_HEAD_VERSION to the caller. This will get picked up by ports.cmake after the build.
set ( VCPKG_HEAD_VERSION ${ _version } PARENT_SCOPE )
set_SOURCE_PATH ( ${ CURRENT_BUILDTREES_DIR } /src/head ${ _vdud_HEAD_REF } )
endfunction ( )