Skip to content

Commit b636396

Browse files
authored
Build self-contained minimal size package for Guest Config team (#14976)
1 parent c9d6506 commit b636396

File tree

11 files changed

+268
-110
lines changed

11 files changed

+268
-110
lines changed

build.psm1

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ function Start-PSBuild {
271271
[Parameter(ParameterSetName="Default")]
272272
[switch]$NoPSModuleRestore,
273273
[switch]$CI,
274+
[switch]$ForMinimalSize,
274275

275276
# Skips the step where the pwsh that's been built is used to create a configuration
276277
# Useful when changing parsing/compilation, since bugs there can mean we can't get past this step
@@ -321,6 +322,17 @@ function Start-PSBuild {
321322
if ("win-arm","win-arm64" -contains $Runtime -and -not $environment.IsWindows) {
322323
throw "Cross compiling for win-arm or win-arm64 is only supported on Windows environment"
323324
}
325+
326+
if ($ForMinimalSize) {
327+
if ($CrossGen) {
328+
throw "Build for the minimal size requires the minimal disk footprint, so `CrossGen` is not allowed"
329+
}
330+
331+
if ($Runtime -and "linux-x64", "win7-x64", "osx-x64" -notcontains $Runtime) {
332+
throw "Build for the minimal size is enabled only for following runtimes: 'linux-x64', 'win7-x64', 'osx-x64'"
333+
}
334+
}
335+
324336
function Stop-DevPowerShell {
325337
Get-Process pwsh* |
326338
Where-Object {
@@ -390,6 +402,7 @@ Fix steps:
390402
Verbose=$true
391403
SMAOnly=[bool]$SMAOnly
392404
PSModuleRestore=$PSModuleRestore
405+
ForMinimalSize=$ForMinimalSize
393406
}
394407
$script:Options = New-PSOptions @OptionsArguments
395408

@@ -414,7 +427,7 @@ Fix steps:
414427
# Framework Dependent builds do not support ReadyToRun as it needs a specific runtime to optimize for.
415428
# The property is set in Powershell.Common.props file.
416429
# We override the property through the build command line.
417-
if($Options.Runtime -like 'fxdependent*') {
430+
if($Options.Runtime -like 'fxdependent*' -or $ForMinimalSize) {
418431
$Arguments += "/property:PublishReadyToRun=false"
419432
}
420433

@@ -467,18 +480,21 @@ Fix steps:
467480
Push-Location $Options.Top
468481

469482
if ($Options.Runtime -notlike 'fxdependent*') {
470-
if ($Options.Runtime -like 'win-arm*') {
471-
$Arguments += "/property:SDKToUse=Microsoft.NET.Sdk"
472-
} else {
473-
$Arguments += "/property:SDKToUse=Microsoft.NET.Sdk.WindowsDesktop"
483+
$sdkToUse = 'Microsoft.NET.Sdk'
484+
if ($Options.Runtime -like 'win7-*' -and !$ForMinimalSize) {
485+
## WPF/WinForm and the PowerShell GraphicalHost assemblies are included
486+
## when 'Microsoft.NET.Sdk.WindowsDesktop' is used.
487+
$sdkToUse = 'Microsoft.NET.Sdk.WindowsDesktop'
474488
}
475489

490+
$Arguments += "/property:SDKToUse=$sdkToUse"
491+
476492
Write-Log -message "Run dotnet $Arguments from $PWD"
477493
Start-NativeExecution { dotnet $Arguments }
478494
Write-Log -message "PowerShell output: $($Options.Output)"
479495

480496
if ($CrossGen) {
481-
## fxdependent package cannot be CrossGen'ed
497+
# fxdependent package cannot be CrossGen'ed
482498
Start-CrossGen -PublishPath $publishPath -Runtime $script:Options.Runtime
483499
Write-Log -message "pwsh.exe with ngen binaries is available at: $($Options.Output)"
484500
}
@@ -651,14 +667,14 @@ function Restore-PSPackage
651667

652668
if ($Force -or (-not (Test-Path "$($Options.Top)/obj/project.assets.json"))) {
653669

654-
$sdkToUse = if (($Options.Runtime -eq 'fxdependent-win-desktop' -or $Options.Runtime -like 'win*')) { # this is fxd or some windows runtime
655-
if ($Options.Runtime -like 'win-arm*') {
656-
'Microsoft.NET.Sdk'
657-
} else {
658-
'Microsoft.NET.Sdk.WindowsDesktop'
670+
if ($Options.Runtime -eq 'fxdependent-win-desktop') {
671+
$sdkToUse = 'Microsoft.NET.Sdk.WindowsDesktop'
672+
}
673+
else {
674+
$sdkToUse = 'Microsoft.NET.Sdk'
675+
if ($Options.Runtime -like 'win7-*' -and !$Options.ForMinimalSize) {
676+
$sdkToUse = 'Microsoft.NET.Sdk.WindowsDesktop'
659677
}
660-
} else {
661-
'Microsoft.NET.Sdk'
662678
}
663679

664680
if ($PSModule.IsPresent) {
@@ -784,7 +800,9 @@ function New-PSOptions {
784800

785801
[switch]$SMAOnly,
786802

787-
[switch]$PSModuleRestore
803+
[switch]$PSModuleRestore,
804+
805+
[switch]$ForMinimalSize
788806
)
789807

790808
# Add .NET CLI tools to PATH
@@ -883,7 +901,8 @@ function New-PSOptions {
883901
-Configuration $Configuration `
884902
-PSModuleRestore $PSModuleRestore.IsPresent `
885903
-Framework $Framework `
886-
-Output $Output
904+
-Output $Output `
905+
-ForMinimalSize $ForMinimalSize
887906
}
888907

889908
# Get the Options of the last build
@@ -3018,7 +3037,8 @@ function Restore-PSOptions {
30183037
-Configuration $options.Configuration `
30193038
-PSModuleRestore $options.PSModuleRestore `
30203039
-Framework $options.Framework `
3021-
-Output $options.Output
3040+
-Output $options.Output `
3041+
-ForMinimalSize $options.ForMinimalSize
30223042

30233043
Set-PSOptions -Options $newOptions
30243044
}
@@ -3055,7 +3075,11 @@ function New-PSOptionsObject
30553075

30563076
[Parameter(Mandatory)]
30573077
[String]
3058-
$Output
3078+
$Output,
3079+
3080+
[Parameter(Mandatory)]
3081+
[Bool]
3082+
$ForMinimalSize
30593083
)
30603084

30613085
return @{
@@ -3067,6 +3091,7 @@ function New-PSOptionsObject
30673091
Output = $Output
30683092
CrossGen = $CrossGen
30693093
PSModuleRestore = $PSModuleRestore
3094+
ForMinimalSize = $ForMinimalSize
30703095
}
30713096
}
30723097

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "6.0.100-preview.1.21104.4"
3+
"version": "6.0.100-preview.1.21103.13"
44
}
55
}

tools/packaging/packaging.psm1

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function Start-PSPackage {
2929
[string]$Name = "powershell",
3030

3131
# Ubuntu, CentOS, Fedora, macOS, and Windows packages are supported
32-
[ValidateSet("msix", "deb", "osxpkg", "rpm", "msi", "zip", "zip-pdb", "nupkg", "tar", "tar-arm", "tar-arm64", "tar-alpine", "fxdependent", "fxdependent-win-desktop")]
32+
[ValidateSet("msix", "deb", "osxpkg", "rpm", "msi", "zip", "zip-pdb", "nupkg", "tar", "tar-arm", "tar-arm64", "tar-alpine", "fxdependent", "fxdependent-win-desktop", "min-size")]
3333
[string[]]$Type,
3434

3535
# Generate windows downlevel package
@@ -47,7 +47,7 @@ function Start-PSPackage {
4747
)
4848

4949
DynamicParam {
50-
if ("zip" -eq $Type -or "fxdependent" -eq $Type -or "fxdependent-win-desktop" -eq $Type) {
50+
if ($Type -in ('zip', 'min-size') -or $Type -like 'fxdependent*') {
5151
# Add a dynamic parameter '-IncludeSymbols' when the specified package type is 'zip' only.
5252
# The '-IncludeSymbols' parameter can be used to indicate that the package should only contain powershell binaries and symbols.
5353
$ParameterAttr = New-Object "System.Management.Automation.ParameterAttribute"
@@ -105,8 +105,9 @@ function Start-PSPackage {
105105
$actualParams = @()
106106

107107
$crossGenCorrect = $false
108-
if ($Runtime -match "arm") {
109-
# crossgen doesn't support arm32/64
108+
if ($Runtime -match "arm" -or $Type -eq 'min-size') {
109+
## crossgen doesn't support arm32/64;
110+
## For the min-size package, we intentionally avoid crossgen.
110111
$crossGenCorrect = $true
111112
}
112113
elseif ($Script:Options.CrossGen) {
@@ -299,7 +300,37 @@ function Start-PSPackage {
299300
New-PdbZipPackage @Arguments
300301
}
301302
}
303+
"min-size" {
304+
# Remove symbol files, xml document files.
305+
Remove-Item "$Source\*.pdb", "$Source\*.xml" -Force
302306

307+
# Add suffix '-gc' because this package is for the Guest Config team.
308+
if ($Environment.IsWindows) {
309+
$Arguments = @{
310+
PackageNameSuffix = "$NameSuffix-gc"
311+
PackageSourcePath = $Source
312+
PackageVersion = $Version
313+
Force = $Force
314+
}
315+
316+
if ($PSCmdlet.ShouldProcess("Create Zip Package")) {
317+
New-ZipPackage @Arguments
318+
}
319+
}
320+
elseif ($Environment.IsLinux) {
321+
$Arguments = @{
322+
PackageSourcePath = $Source
323+
Name = $Name
324+
PackageNameSuffix = 'gc'
325+
Version = $Version
326+
Force = $Force
327+
}
328+
329+
if ($PSCmdlet.ShouldProcess("Create tar.gz Package")) {
330+
New-TarballPackage @Arguments
331+
}
332+
}
333+
}
303334
{ $_ -like "fxdependent*" } {
304335
## Remove PDBs from package to reduce size.
305336
if(-not $IncludeSymbols.IsPresent) {
@@ -317,7 +348,7 @@ function Start-PSPackage {
317348
if ($PSCmdlet.ShouldProcess("Create Zip Package")) {
318349
New-ZipPackage @Arguments
319350
}
320-
} elseif ($IsLinux) {
351+
} elseif ($Environment.IsLinux) {
321352
$Arguments = @{
322353
PackageSourcePath = $Source
323354
Name = $Name

tools/releaseBuild/Images/GenericLinuxFiles/PowerShellPackage.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ param (
1818
[switch]$TarX64,
1919
[switch]$TarArm,
2020
[switch]$TarArm64,
21+
[switch]$TarMinSize,
2122
[switch]$FxDependent,
2223
[switch]$Alpine
2324
)
@@ -66,6 +67,7 @@ function BuildPackages {
6667
}
6768

6869
Start-PSBuild @buildParams @releaseTagParam
70+
$options = Get-PSOptions
6971

7072
if ($FxDependent) {
7173
Start-PSPackage -Type 'fxdependent' @releaseTagParam -LTS:$LTS
@@ -77,6 +79,22 @@ function BuildPackages {
7779

7880
if ($TarX64) { Start-PSPackage -Type tar @releaseTagParam -LTS:$LTS }
7981

82+
if ($TarMinSize) {
83+
Write-Verbose -Verbose "---- Min-Size ----"
84+
Write-Verbose -Verbose "options.Output: $($options.Output)"
85+
Write-Verbose -Verbose "options.Top $($options.Top)"
86+
87+
$binDir = Join-Path -Path $options.Top -ChildPath 'bin'
88+
Write-Verbose -Verbose "Remove $binDir, to get a clean build for min-size package"
89+
Remove-Item -Path $binDir -Recurse -Force
90+
91+
## Build 'min-size' and create 'tar.gz' package for it.
92+
$buildParams['Crossgen'] = $false
93+
$buildParams['ForMinimalSize'] = $true
94+
Start-PSBuild @buildParams @releaseTagParam
95+
Start-PSPackage -Type min-size @releaseTagParam -LTS:$LTS
96+
}
97+
8098
if ($TarArm) {
8199
## Build 'linux-arm' and create 'tar.gz' package for it.
82100
## Note that 'linux-arm' can only be built on Ubuntu environment.

0 commit comments

Comments
 (0)