|
| 1 | +# Copyright (c) 2022 by Osyotr |
| 2 | +# https://gist.github.com/Osyotr/e2d415312a10183bc7c614013494bf21 |
| 3 | +# Boost Software License |
| 4 | +# Permission is hereby granted, free of charge, to any person or organization |
| 5 | +# obtaining a copy of the software and accompanying documentation covered by |
| 6 | +# this license (the "Software") to use, reproduce, display, distribute, |
| 7 | +# execute, and transmit the Software, and to prepare derivative works of the |
| 8 | +# Software, and to permit third-parties to whom the Software is furnished to |
| 9 | +# do so, all subject to the following: |
| 10 | +# |
| 11 | +# The copyright notices in the Software and this entire statement, including |
| 12 | +# the above license grant, this restriction and the following disclaimer, |
| 13 | +# must be included in all copies of the Software, in whole or in part, and |
| 14 | +# all derivative works of the Software, unless such copies or derivative |
| 15 | +# works are solely in the form of machine-executable object code generated by |
| 16 | +# a source language processor. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 21 | +# SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 22 | +# FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 23 | +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | +# DEALINGS IN THE SOFTWARE. |
| 25 | + |
| 26 | +include_guard(GLOBAL) |
| 27 | + |
| 28 | +function(x_vcpkg_bootstrap) |
| 29 | + if(NOT GIT_EXECUTABLE) |
| 30 | + find_package(Git REQUIRED) |
| 31 | + endif() |
| 32 | + |
| 33 | + set(X_VCPKG_GIT_REPOSITORY_URL "https://github.com/Microsoft/vcpkg" CACHE STRING "Vcpkg git repository") |
| 34 | + set(X_VCPKG_CLONE_DIR "${CMAKE_SOURCE_DIR}/vcpkg" CACHE PATH "Vcpkg clone directory") |
| 35 | + |
| 36 | + if(NOT EXISTS "${X_VCPKG_CLONE_DIR}/.git") |
| 37 | + message(STATUS "Cloning vcpkg into ${X_VCPKG_CLONE_DIR}") |
| 38 | + execute_process( |
| 39 | + COMMAND "${GIT_EXECUTABLE}" clone --quiet "${X_VCPKG_GIT_REPOSITORY_URL}" "${X_VCPKG_CLONE_DIR}" |
| 40 | + ERROR_VARIABLE _vcpkg_git_clone_error |
| 41 | + ) |
| 42 | + if(_vcpkg_git_clone_error) |
| 43 | + message(FATAL_ERROR "Could not clone vcpkg repository from ${X_VCPKG_GIT_REPOSITORY_URL}\nMake sure you have access rights and the URL is valid.") |
| 44 | + endif() |
| 45 | + message(STATUS "Cloning vcpkg into ${X_VCPKG_CLONE_DIR} - done") |
| 46 | + endif() |
| 47 | + |
| 48 | + execute_process( |
| 49 | + COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD |
| 50 | + WORKING_DIRECTORY "${X_VCPKG_CLONE_DIR}" |
| 51 | + OUTPUT_VARIABLE _vcpkg_current_head_sha |
| 52 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 53 | + COMMAND_ERROR_IS_FATAL ANY |
| 54 | + ) |
| 55 | + set(_baseline_sha "${_vcpkg_current_head_sha}") |
| 56 | + |
| 57 | + file(READ "${CMAKE_SOURCE_DIR}/vcpkg-configuration.json" _vcpkg_configuration_json_contents) |
| 58 | + string(JSON _baseline_sha ERROR_VARIABLE _get_baseline_err GET "${_vcpkg_configuration_json_contents}" "default-registry" "baseline") |
| 59 | + |
| 60 | + set(_should_update_vcpkg FALSE) |
| 61 | + if(NOT "${_vcpkg_current_head_sha}" STREQUAL "${_baseline_sha}") |
| 62 | + set(_should_update_vcpkg TRUE) |
| 63 | + endif() |
| 64 | + |
| 65 | + if(_should_update_vcpkg) |
| 66 | + message(STATUS "Fetching changes from vcpkg upstream") |
| 67 | + execute_process( |
| 68 | + COMMAND "${GIT_EXECUTABLE}" fetch --quiet --prune |
| 69 | + WORKING_DIRECTORY "${X_VCPKG_CLONE_DIR}" |
| 70 | + # No error checking here to allow offline usage |
| 71 | + ) |
| 72 | + message(STATUS "Fetching changes from vcpkg upstream - done") |
| 73 | + |
| 74 | + message(STATUS "Switching vcpkg HEAD to ${_baseline_sha}") |
| 75 | + execute_process( |
| 76 | + COMMAND "${GIT_EXECUTABLE}" reset --quiet --hard "${_baseline_sha}" |
| 77 | + COMMAND "${GIT_EXECUTABLE}" clean -qfd |
| 78 | + WORKING_DIRECTORY "${X_VCPKG_CLONE_DIR}" |
| 79 | + COMMAND_ERROR_IS_FATAL ANY |
| 80 | + ) |
| 81 | + message(STATUS "Switching vcpkg HEAD to ${_baseline_sha} - done") |
| 82 | + |
| 83 | + # Remove vcpkg executable to trigger bootstrap |
| 84 | + if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") |
| 85 | + file(REMOVE "${X_VCPKG_CLONE_DIR}/vcpkg.exe") |
| 86 | + else() |
| 87 | + file(REMOVE "${X_VCPKG_CLONE_DIR}/vcpkg") |
| 88 | + endif() |
| 89 | + endif() |
| 90 | +endfunction() |
0 commit comments