|
| 1 | +# !/usr/bin/env powershell |
| 2 | +# Copyright 2020 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Stop on errors. This is similar to `set -e` on Unix shells. |
| 17 | +$ErrorActionPreference = "Stop" |
| 18 | + |
| 19 | +# First check the required environment variables. |
| 20 | +if (-not (Test-Path env:CONFIG)) { |
| 21 | + throw "Aborting build because the CONFIG environment variable is not set." |
| 22 | +} |
| 23 | +$CONFIG = $env:CONFIG |
| 24 | + |
| 25 | +$project_root = (Get-Item -Path ".\" -Verbose).FullName |
| 26 | + |
| 27 | +# Update or clone the 'vcpkg' package manager, this is a bit overly complicated, |
| 28 | +# but it works well on your workstation where you may want to run this script |
| 29 | +# multiple times while debugging vcpkg installs. It also works on AppVeyor |
| 30 | +# where we cache the vcpkg installation, but it might be empty on the first |
| 31 | +# build. |
| 32 | +Set-Location .. |
| 33 | +if (Test-Path env:RUNNING_CI) { |
| 34 | + Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Cloning vcpkg repository." |
| 35 | + if (Test-Path "vcpkg") { |
| 36 | + Remove-Item -LiteralPath "vcpkg" -Force -Recurse |
| 37 | + } |
| 38 | + git clone --depth 10 https://github.com/Microsoft/vcpkg.git |
| 39 | + if ($LastExitCode) { |
| 40 | + throw "vcpkg git setup failed with exit code $LastExitCode" |
| 41 | + } |
| 42 | +} |
| 43 | +if (-not (Test-Path "vcpkg")) { |
| 44 | + throw "Missing vcpkg directory." |
| 45 | +} |
| 46 | +Set-Location vcpkg |
| 47 | + |
| 48 | +# If BUILD_CACHE is set (which typically is on Kokoro builds), try |
| 49 | +# to download and extract the build cache. |
| 50 | +if (Test-Path env:BUILD_CACHE) { |
| 51 | + gcloud auth activate-service-account --key-file "${env:KOKORO_GFILE_DIR}/build-results-service-account.json" |
| 52 | + Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Downloading build cache." |
| 53 | + gsutil cp $env:BUILD_CACHE vcpkg-installed.zip |
| 54 | + if ($LastExitCode) { |
| 55 | + # Ignore errors, caching failures should not break the build. |
| 56 | + Write-Host "gsutil download failed with exit code $LastExitCode" |
| 57 | + } |
| 58 | + Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Extracting build cache." |
| 59 | + 7z x vcpkg-installed.zip -aoa |
| 60 | + if ($LastExitCode) { |
| 61 | + # Ignore errors, caching failures should not break the build. |
| 62 | + Write-Host "extracting build cache failed with exit code $LastExitCode" |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Bootstrap vcpkg." |
| 67 | +powershell -exec bypass scripts\bootstrap.ps1 |
| 68 | +if ($LastExitCode) { |
| 69 | + throw "Error bootstrapping vcpkg: $LastExitCode" |
| 70 | + Write-Host -ForegroundColor Red "bootstrap[1] failed" |
| 71 | +} |
| 72 | + |
| 73 | +# Only compile the release version of the packages we need, for Debug builds |
| 74 | +# this trick does not work, so we need to compile all versions (yuck). |
| 75 | +if ($CONFIG -eq "Release") { |
| 76 | +# Add-Content triplets\x64-windows-static.cmake "set(VCPKG_BUILD_TYPE release)" |
| 77 | +} |
| 78 | + |
| 79 | +# Integrate installed packages into the build environment. |
| 80 | +.\vcpkg.exe integrate install |
| 81 | +if ($LastExitCode) { |
| 82 | + throw "vcpkg integrate failed with exit code $LastExitCode" |
| 83 | +} |
| 84 | + |
| 85 | +$vcpkg_flags=@( |
| 86 | + "--triplet", "${env:VCPKG_TRIPLET}", |
| 87 | + "--overlay-ports=${project_root}/ci/kokoro/windows/vcpkg-ports") |
| 88 | + |
| 89 | +# Remove old versions of the packages. |
| 90 | +Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Cleanup old vcpkg package versions." |
| 91 | +.\vcpkg.exe remove ${vcpkg_flags} --outdated --recurse |
| 92 | +if ($LastExitCode) { |
| 93 | + throw "vcpkg remove --outdated failed with exit code $LastExitCode" |
| 94 | +} |
| 95 | + |
| 96 | +Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Building vcpkg package versions." |
| 97 | +$packages = @("zlib", "openssl", |
| 98 | + "protobuf", "c-ares", |
| 99 | + "grpc", "gtest", "crc32c", "curl", |
| 100 | + "googleapis", "google-cloud-cpp-common[test]") |
| 101 | +foreach ($pkg in $packages) { |
| 102 | + .\vcpkg.exe install ${vcpkg_flags} "${pkg}" |
| 103 | + if ($LastExitCode) { |
| 104 | + throw "vcpkg install $pkg failed with exit code $LastExitCode" |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) vcpkg list" |
| 109 | +.\vcpkg.exe list |
| 110 | + |
| 111 | +if (Test-Path env:BUILD_CACHE) { |
| 112 | + Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Create cache zip file." |
| 113 | + 7z a vcpkg-installed.zip installed\ |
| 114 | + if ($LastExitCode) { |
| 115 | + # Ignore errors, caching failures should not break the build. |
| 116 | + Write-Host "zip build cache failed with exit code $LastExitCode" |
| 117 | + } |
| 118 | + |
| 119 | + Write-Host -ForegroundColor Yellow "`n$(Get-Date -Format o) Upload cache zip file." |
| 120 | + gsutil cp vcpkg-installed.zip $env:BUILD_CACHE |
| 121 | + if ($LastExitCode) { |
| 122 | + # Ignore errors, caching failures should not break the build. |
| 123 | + Write-Host "gsutil upload failed with exit code $LastExitCode" |
| 124 | + } |
| 125 | +} |
0 commit comments