Skip to content

Commit

Permalink
build.ps1 - Nuget Package only a subset of architectures
Browse files Browse the repository at this point in the history
- Can now build/package only a subset of architectures
- Remove file elements from nuspec if arch wasn't specified
- Reorder and simplify script

.\build.ps1 -BuildArches "x64"

Issue cefsharp#3706
  • Loading branch information
amaitland committed Oct 13, 2021
1 parent 7c2226c commit c72e21c
Showing 1 changed file with 126 additions and 66 deletions.
192 changes: 126 additions & 66 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function Warn
Write-Host
}

function Msvs
function BuildSolution
{
param(
[ValidateSet('v142','v143')]
Expand All @@ -108,73 +108,22 @@ function Msvs

[Parameter(Position = 2, ValueFromPipeline = $true)]
[ValidateSet('x86', 'x64', 'arm64')]
[string] $Platform
)

Write-Diagnostic "Targeting $Toolchain using configuration $Configuration on platform $Platform"

$VisualStudioVersion = $null
$VXXCommonTools = $null
$VS_VER = -1
$VS_OFFICIAL_VER = -1
$VS_PRE = ""

switch -Exact ($Toolchain)
{
'v142'
{
$VS_VER = 16;
$VS_OFFICIAL_VER = 2019;
}
'v143'
{
$VS_VER = 17;
$VS_OFFICIAL_VER = 2022;
$VS_PRE = "-prerelease";
}
}

$versionSearchStr = "[$VS_VER.0," + ($VS_VER+1) + ".0)"
[string] $Platform,

$VSInstallPath = & $VSWherePath -version $versionSearchStr -property installationPath $VS_PRE

Write-Diagnostic "$($VS_OFFICIAL_VER)InstallPath: $VSInstallPath"

if( -not $VSInstallPath -or -not (Test-Path $VSInstallPath))
{
Die "Visual Studio $VS_OFFICIAL_VER is not installed on your development machine, unable to continue, ran command: $VSWherePath -version $versionSearchStr -property installationPath"
}

$MSBuildExe = "msbuild.exe"
$VisualStudioVersion = "$VS_VER.0"
$VXXCommonTools = Join-Path $VSInstallPath VC\Auxiliary\Build

if ($VXXCommonTools -eq $null -or (-not (Test-Path($VXXCommonTools))))
{
Die 'Error unable to find any visual studio environment'
}

$VCVarsAll = Join-Path $VXXCommonTools vcvarsall.bat
if (-not (Test-Path $VCVarsAll))
{
Die "Unable to find $VCVarsAll"
}
[Parameter(Position = 3, ValueFromPipeline = $true)]
[string] $VisualStudioVersion
)

# Only configure build environment once
if($env:CEFSHARP_BUILD_IS_BOOTSTRAPPED -eq $null)
{
Invoke-BatchFile $VCVarsAll $Platform
$env:CEFSHARP_BUILD_IS_BOOTSTRAPPED = $true
}
Write-Diagnostic "Begin compiling targeting $Toolchain using configuration $Configuration for platform $Platform"

$Arch = $Platform
if ($Arch -eq "x86")
if (!$IsNetCoreBuild -and $Arch -eq "x86")
{
$Arch="win32";
}

# Restore Nuget packages
&msbuild /t:restore /p:Platform=$Arch /p:Configuration=Release $CefSln
&msbuild /nologo /verbosity:minimal /t:restore /p:Platform=$Arch /p:Configuration=Release $CefSln

$Arguments = @(
"$CefSln",
Expand All @@ -186,7 +135,7 @@ function Msvs
)

$StartInfo = New-Object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = $MSBuildExe
$StartInfo.FileName = "msbuild.exe"
$StartInfo.Arguments = $Arguments

$StartInfo.EnvironmentVariables.Clear()
Expand Down Expand Up @@ -215,6 +164,8 @@ function Msvs
Write-Host "stderr: $stderr"
Die "Build failed"
}

Write-Diagnostic "Compile succeeded targeting $Toolchain using configuration $Configuration for platform $Platform"
}

function VSX
Expand All @@ -227,9 +178,71 @@ function VSX

Write-Diagnostic "Starting to build targeting toolchain $Toolchain"

$VisualStudioVersion = $null
$VXXCommonTools = $null
$VS_VER = -1
$VS_OFFICIAL_VER = -1
$VS_PRE = ""

switch -Exact ($Toolchain)
{
'v142'
{
$VS_VER = 16;
$VS_OFFICIAL_VER = 2019;
}
'v143'
{
$VS_VER = 17;
$VS_OFFICIAL_VER = 2022;
$VS_PRE = "-prerelease";
}
}

$versionSearchStr = "[$VS_VER.0," + ($VS_VER+1) + ".0)"

$VSInstallPath = & $VSWherePath -version $versionSearchStr -property installationPath $VS_PRE

Write-Diagnostic "$($VS_OFFICIAL_VER)InstallPath: $VSInstallPath"

if( -not $VSInstallPath -or -not (Test-Path $VSInstallPath))
{
Die "Visual Studio $VS_OFFICIAL_VER is not installed on your development machine, unable to continue, ran command: $VSWherePath -version $versionSearchStr -property installationPath"
}

$VisualStudioVersion = "$VS_VER.0"
$VXXCommonTools = Join-Path $VSInstallPath VC\Auxiliary\Build

if ($null -eq $VXXCommonTools -or (-not (Test-Path($VXXCommonTools))))
{
Die 'Error unable to find any visual studio environment'
}

$VCVarsAll = Join-Path $VXXCommonTools vcvarsall.bat
if (-not (Test-Path $VCVarsAll))
{
Die "Unable to find $VCVarsAll"
}

# Only configure build environment once
if($null -eq $env:CEFSHARP_BUILD_IS_BOOTSTRAPPED)
{
$VCVarsAllArch = $ARCHES[0]
if ($VCVarsAllArch -eq "arm64")
{
#TODO: Add support for compiling from an arm64 host
# Detect host and determine if we are native or cross compile
# currently only cross compiling arm64 from x64 host
$VCVarsAllArch = 'x64_arm64'
}

Invoke-BatchFile $VCVarsAll $VCVarsAllArch
$env:CEFSHARP_BUILD_IS_BOOTSTRAPPED = $true
}

foreach ($arch in $ARCHES)
{
Msvs "$Toolchain" 'Release' $arch
BuildSolution "$Toolchain" 'Release' $arch $VisualStudioVersion
}

Write-Diagnostic "Finished build targeting toolchain $Toolchain"
Expand Down Expand Up @@ -269,7 +282,51 @@ function Nupkg
# Build packages
foreach($file in $Files)
{
. $nuget pack "$NugetPackagePath\$file" -Version $Version -OutputDirectory $NugetPackagePath -Properties "RedistVersion=$RedistVersion;"
$filePath = Join-Path $WorkingDir "$NugetPackagePath\$file"
$tempFile = $filePath + ".backup"
try
{
# We need to rewrite the CefSharp.Common nupkg file if we are building a subset of architectures
if($file.StartsWith("CefSharp.Common") -and $ARCHES.Count -lt $SupportedArches.Count)
{
Copy-Item $filePath $tempFile
$removeArches = $SupportedArches | Where-Object {$_ -notin $ARCHES}
$NupkgXml = [xml](Get-Content ($filePath) -Encoding UTF8)

foreach($a in $removeArches)
{
$targetFolder = "CefSharp\$a"
if($IsNetCoreBuild)
{
$targetFolder = "runtimes\win-$a"
}

$nodes = $NupkgXml.package.files.file | Where-Object {$_.Attributes["target"].Value.StartsWith($targetFolder) };

$nodes | ForEach-Object { $_.ParentNode.RemoveChild($_) } | Out-Null
}

$NupkgXml.Save($filePath)
}

#Only show package analysis for newer packages
if($IsNetCoreBuild)
{
. $nuget pack $filePath -Version $Version -OutputDirectory $NugetPackagePath -Properties "RedistVersion=$RedistVersion;"
}
else
{
. $nuget pack $filePath -NoPackageAnalysis -Version $Version -OutputDirectory $NugetPackagePath -Properties "RedistVersion=$RedistVersion;"
}
}
finally
{
if(Test-Path($tempFile))
{
Copy-Item $tempFile $filePath
Remove-Item $tempFile
}
}
}

# Invoke `AfterBuild` script if available (ie. upload packages to myget)
Expand Down Expand Up @@ -405,19 +462,21 @@ $WorkingDir = split-path -parent $MyInvocation.MyCommand.Definition
Write-Diagnostic "pushd $WorkingDir"
Push-Location $WorkingDir

$IsNetCoreBuild = $TargetFramework.Contains("NetCore")
$ARCHES = [System.Collections.ArrayList]$BuildArches.Split(" ");
$IsNetCoreBuild = $TargetFramework.ToLower().Contains("netcore")
$ARCHES = [System.Collections.ArrayList]$BuildArches.ToLower().Split(" ");
$CefSln = $null
$NugetPackagePath = $null
$NupkgFiles = $null
$VCXProjPackageConfigFiles = $null
$SupportedArches = [System.Collections.ArrayList]@();

if($IsNetCoreBuild)
{
$CefSln = Join-Path $WorkingDir 'CefSharp3.netcore.sln'
$NugetPackagePath = "nuget\PackageReference";
$NupkgFiles = @('CefSharp.Common.NETCore.nuspec', 'CefSharp.WinForms.NETCore.nuspec', 'CefSharp.Wpf.NETCore.nuspec','CefSharp.OffScreen.NETCore.nuspec')
$VCXProjPackageConfigFiles = @('CefSharp.Core.Runtime\packages.CefSharp.Core.Runtime.netcore.config', 'CefSharp.BrowserSubprocess.Core\packages.CefSharp.BrowserSubprocess.Core.netcore.config');
$SupportedArches.AddRange(@("x86", "x64", "arm64"));
}
else
{
Expand All @@ -426,6 +485,7 @@ else
$NugetPackagePath = "nuget";
$NupkgFiles = @('CefSharp.Common.nuspec', 'CefSharp.WinForms.nuspec', 'CefSharp.Wpf.nuspec', 'CefSharp.OffScreen.nuspec')
$VCXProjPackageConfigFiles = @('CefSharp.Core.Runtime\packages.CefSharp.Core.Runtime.config', 'CefSharp.BrowserSubprocess.Core\packages.CefSharp.BrowserSubprocess.Core.config');
$SupportedArches.AddRange(@("x86", "x64"));
}

# Extract the current CEF Redist version from the CefSharp.Core.Runtime\packages.CefSharp.Core.Runtime.config file
Expand Down Expand Up @@ -458,9 +518,9 @@ DownloadNuget

NugetPackageRestore $VCXProjPackageConfigFiles

$programFilesDir = (${env:ProgramFiles(x86)}, ${env:ProgramFiles} -ne $null)[0]
$ProgramFilesDir = (${env:ProgramFiles(x86)}, ${env:ProgramFiles} -ne $null)[0]

$VSWherePath = Join-Path $programFilesDir 'Microsoft Visual Studio\Installer\vswhere.exe'
$VSWherePath = Join-Path $ProgramFilesDir 'Microsoft Visual Studio\Installer\vswhere.exe'
#Check if we already have vswhere which is included in newer versions of VS2017/VS2019
if(-not (Test-Path $VSWherePath))
{
Expand Down

0 comments on commit c72e21c

Please sign in to comment.