Skip to content

Commit

Permalink
Fix bootstrap wrapper scripts to tolerate deployment within readonly …
Browse files Browse the repository at this point in the history
…VCPKG_ROOT. (#939)
  • Loading branch information
BillyONeal authored Mar 1, 2023
1 parent b2d59cb commit 538539c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 259 deletions.
4 changes: 2 additions & 2 deletions azure-pipelines/signing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ jobs:
inputs:
pwsh: true
filePath: vcpkg-init/mint-standalone-bundle.ps1
arguments: '-DestinationTarball "$(Build.BinariesDirectory)\vcpkg-standalone-bundle.tar.gz" -TempDir standalone-temp -SignedFilesRoot "$(Build.BinariesDirectory)" -Deployment OneLiner'
arguments: '-DestinationTarball "$(Build.BinariesDirectory)\vcpkg-standalone-bundle.tar.gz" -TempDir standalone-temp -SignedFilesRoot "$(Build.BinariesDirectory)" -Deployment OneLiner -VcpkgBaseVersion "$(VCPKG_INITIAL_BASE_VERSION)"'
- script: npm pack
displayName: Create vcpkg-artifacts Pack
workingDirectory: $(Build.BinariesDirectory)/ce
Expand Down Expand Up @@ -366,7 +366,7 @@ jobs:
inputs:
pwsh: true
filePath: vcpkg-init/mint-standalone-bundle.ps1
arguments: '-DestinationDir "$(Build.ArtifactStagingDirectory)/vs-insertion/staging" -TempDir standalone-temp -SignedFilesRoot "$(Build.ArtifactStagingDirectory)\staging" -Deployment "VisualStudio"'
arguments: '-DestinationDir "$(Build.ArtifactStagingDirectory)/vs-insertion/staging" -TempDir standalone-temp -SignedFilesRoot "$(Build.ArtifactStagingDirectory)\staging" -Deployment "VisualStudio" -VcpkgBaseVersion "$(VCPKG_BASE_VERSION)"'
- task: CmdLine@2
displayName: 'Arrange Drop'
inputs:
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/configure-environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace vcpkg
download_manager.download_file(fs, ce_uri, {}, ce_tarball, nullopt, null_sink);
extract_ce_tarball(paths, ce_tarball, node_path, base_path);
#endif // ^^^ always get latest
#endif // ^^^ !VCPKG_CE_SHA
#endif // ^^^ might need to download

auto temp_directory = fs.create_or_get_temp_directory(VCPKG_LINE_INFO);

Expand Down
8 changes: 4 additions & 4 deletions vcpkg-init/lock-versions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ Param(
$versionUnlockedUri = "https://github.com/microsoft/vcpkg-tool/releases/latest/download/"
$versionLockedUri = "https://github.com/microsoft/vcpkg-tool/releases/download/$VcpkgBaseVersion/"

$pwshInstaller = Get-Content "$PSScriptRoot\vcpkg-init.ps1" -Raw -Encoding ascii
$pwshInstaller = Get-Content "$PSScriptRoot\vcpkg-init.ps1" -Raw -Encoding Ascii
$pwshInstaller = $pwshInstaller.Replace($versionUnlockedUri, $versionLockedUri)
$pwshLatestLine = "`$SCRIPT:VCPKG_INIT_VERSION = 'latest'"
$pwshLockedLine = "`$SCRIPT:VCPKG_INIT_VERSION = '$VcpkgBaseVersion'"
$pwshInstaller = $pwshInstaller.Replace($pwshLatestLine, $pwshLockedLine)
Set-Content -Path "$Destination\vcpkg-init.ps1" -Value $pwshInstaller -NoNewline -Encoding ascii
Set-Content -Path "$Destination\vcpkg-init.ps1" -Value $pwshInstaller -NoNewline -Encoding Ascii

$shInstaller = Get-Content "$PSScriptRoot\vcpkg-init" -Raw -Encoding ascii
$shInstaller = Get-Content "$PSScriptRoot\vcpkg-init" -Raw -Encoding Ascii
$shInstaller = $shInstaller.Replace($versionUnlockedUri, $versionLockedUri)
$shLatestLine = "VCPKG_BASE_VERSION='latest'"
$shLockedLine = "VCPKG_BASE_VERSION='$VcpkgBaseVersion'"
$shInstaller = $shInstaller.Replace($shLatestLine, $shLockedLine)
Set-Content -Path "$Destination\vcpkg-init" -Value $shInstaller -NoNewline -Encoding ascii
Set-Content -Path "$Destination\vcpkg-init" -Value $shInstaller -NoNewline -Encoding Ascii
14 changes: 9 additions & 5 deletions vcpkg-init/mint-standalone-bundle.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ Param(
[string]$DestinationDir,
[Parameter(Mandatory = $True)]
[string]$TempDir,
[Parameter()]
[Parameter(Mandatory = $True)]
[string]$Deployment,
[Parameter(Mandatory = $True)]
[string]$SignedFilesRoot
[string]$SignedFilesRoot,
[Parameter(Mandatory = $true)]
[string]$VcpkgBaseVersion
)

$sha = Get-Content "$PSScriptRoot/vcpkg-scripts-sha.txt" -Raw
$sha = $sha.Trim()

if ($Deployment -eq 'VisualStudio') {
$BundleConfig = @{
'readonly' = $True;
Expand All @@ -29,9 +34,6 @@ if ($Deployment -eq 'VisualStudio') {
}
}

$sha = Get-Content "$PSScriptRoot/vcpkg-scripts-sha.txt" -Raw
$sha = $sha.Trim()

$scripts_dependencies = @(
'build_info.cmake',
'buildsystems',
Expand Down Expand Up @@ -82,6 +84,8 @@ try {
Pop-Location
}

Set-Content -Path "out/vcpkg-version.txt" -Value $VcpkgBaseVersion -NoNewLine -Encoding Ascii
Copy-Item -Path "$PSScriptRoot/vcpkg-cmd.cmd" -Destination 'out/vcpkg-cmd.cmd'
Copy-Item -Path "$SignedFilesRoot/vcpkg-init" -Destination 'out/vcpkg-init'
Copy-Item -Path "$SignedFilesRoot/vcpkg-init.ps1" -Destination 'out/vcpkg-init.ps1'
Copy-Item -Path "$SignedFilesRoot/vcpkg-init.cmd" -Destination 'out/vcpkg-init.cmd'
Expand Down
17 changes: 17 additions & 0 deletions vcpkg-init/vcpkg-cmd.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@echo off
:: Generate 30 bits of randomness, to avoid clashing with concurrent executions.
SET /A Z_VCPKG_POSTSCRIPT=%RANDOM% * 32768 + %RANDOM%
:: Set a temporary postscript path
SET Z_VCPKG_POSTSCRIPT=%TEMP%\VCPKG_tmp_%Z_VCPKG_POSTSCRIPT%.cmd
:: Actually run vcpkg and save its exit code
"%~dp0\vcpkg.exe" %*
SET Z_VCPKG_ERRORLEVEL=%ERRORLEVEL%
:: If vcpkg wanted to make any environment changes, make them
IF EXIST "%Z_VCPKG_POSTSCRIPT%" (
CALL "%Z_VCPKG_POSTSCRIPT%"
DEL "%Z_VCPKG_POSTSCRIPT%"
)

:: Cleanup
SET Z_VCPKG_POSTSCRIPT=
EXIT /B %Z_VCPKG_ERRORLEVEL%
52 changes: 6 additions & 46 deletions vcpkg-init/vcpkg-init
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,13 @@ if [ $sourced -eq 0 ]; then
exit
fi

Z_VCPKG_init() {
# find important cmdline args
Z_VCPKG_ARGS=()
for each in "$@"; do case $each in
--remove-vcpkg) Z_VCPKG_REMOVE=TRUE;;
*) Z_VCPKG_ARGS+=("$each");;
esac ;done
}

Z_VCPKG_bootstrap() {
VCPKG_BASE_VERSION='latest'

if [ $VCPKG_BASE_VERSION != 'latest' ] \
&& [ -f "${VCPKG_ROOT}/vcpkg" ] \
&& [ -f "${VCPKG_ROOT}/vcpkg-one-liner-version.txt" ] \
&& [ "$(cat "${VCPKG_ROOT}/vcpkg-one-liner-version.txt")" = $VCPKG_BASE_VERSION ]; then
&& [ -f "${VCPKG_ROOT}/vcpkg-version.txt" ] \
&& [ "$(cat "${VCPKG_ROOT}/vcpkg-version.txt")" = $VCPKG_BASE_VERSION ]; then
return 0;
fi

Expand All @@ -64,36 +55,18 @@ Z_VCPKG_bootstrap() {

chmod +x "${VCPKG_ROOT}/vcpkg"
"${VCPKG_ROOT}/vcpkg" bootstrap-standalone
echo $VCPKG_BASE_VERSION > "${VCPKG_ROOT}/vcpkg-one-liner-version.txt"
return 0;
}

Z_VCPKG_cleanup() {
# clear things that we're not going to need for the long term
unset Z_VCPKG_REMOVE
unset Z_VCPKG_ARGS
if [ -f "${Z_VCPKG_POSTSCRIPT}" ]; then
command rm "${Z_VCPKG_POSTSCRIPT}"
fi
unset Z_VCPKG_POSTSCRIPT
unset -f Z_VCPKG_bootstrap > /dev/null 2>&1
}

Z_VCPKG_remove() {
Z_VCPKG_cleanup
if [ -d "$VCPKG_ROOT" ]; then
rm -rf "$VCPKG_ROOT"
fi

unset -f Z_VCPKG_init > /dev/null 2>&1
unset -f Z_VCPKG_cleanup > /dev/null 2>&1
unset -f Z_VCPKG_remove > /dev/null 2>&1
unset -f vcpkg > /dev/null 2>&1
}

Z_VCPKG_init "$@"
shift $#

if [ -n "$VCPKG_ROOT" ]; then
export VCPKG_ROOT=$VCPKG_ROOT
else
Expand All @@ -102,12 +75,6 @@ fi;

mkdir -p "$VCPKG_ROOT"

if [ ! -z "$Z_VCPKG_REMOVE" ]; then
Z_VCPKG_remove
return
fi


Z_VCPKG_bootstrap
if [ $? -eq 1 ]; then
Z_VCPKG_cleanup
Expand All @@ -116,20 +83,13 @@ fi

# So, we're the real script then.
vcpkg() {
Z_VCPKG_init "$@"

if [ ! -z "$Z_VCPKG_REMOVE" ]; then
Z_VCPKG_remove
return;
fi

# set the response file
# Generate 32 bits of randomness, to avoid clashing with concurrent executions.
export Z_VCPKG_POSTSCRIPT="${VCPKG_ROOT}/VCPKG_tmp_$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" ").sh"
export Z_VCPKG_POSTSCRIPT="$(mktemp).sh"

# call vcpkg
# it picks up the Z_VCPKG_POSTSCRIPT environment variable to know where to dump the postscript
"${VCPKG_ROOT}/vcpkg" ${Z_VCPKG_ARGS[@]}
"${VCPKG_ROOT}/vcpkg" $@

# Call the post-invocation script if it is present, then delete it.
# This allows the invocation to potentially modify the caller's environment (e.g. PATH)
Expand All @@ -143,8 +103,8 @@ vcpkg() {
}

# did they dotsource and have args go ahead and run it then!
if [ -n "$Z_VCPKG_ARGS" ]; then
vcpkg "${Z_VCPKG_ARGS[@]}"
if [ "$#" -gt "0" ]; then
vcpkg $@
fi

Z_VCPKG_cleanup
Loading

0 comments on commit 538539c

Please sign in to comment.