forked from microsoft/CLRInstrumentationEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
297 lines (248 loc) · 10.3 KB
/
build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.SYNOPSIS
This script conducts a local build of CLRIE's binaries and package.
.PARAMETER BuildVersion
Optional, the build version associated with this script invocation. Will be generated if not set.
This parameter allows rerunning failed builds (for example, testing packaging changes with -SkipBuild parameter)
.PARAMETER IncludeTests
Optional, run unit tests after build steps are finished. NOTE, this requires the binaries to be built already.
.PARAMETER SkipBuild
Optional, skips the binary build phase.
.PARAMETER SkipPackaging
Optional, skips the package build phase.
.PARAMETER SkipCleanAndRestore
Optional, the binary build phase will delete the bin & obj folders and reruns dotnet restore. This flag skips those steps during binary build.
.PARAMETER Release
Optional, by default the build will run with Debug configuration. This flag switches to using Release configuration.
.PARAMETER VerboseMsbuild
Optional, enables more verbose output for msbuild (great for investigating build failures). We recommend piping the output to a temp.log file:
.\build.ps1 -Verbose > temp.log
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]
$BuildVersion,
[Parameter(Mandatory=$false)]
[switch]
$IncludeTests,
[Parameter(Mandatory=$false)]
[switch]
$SkipBuild,
[Parameter(Mandatory=$false)]
[switch]
$SkipPackaging,
[Parameter(Mandatory=$false)]
[switch]
$SkipCleanAndRestore,
[Parameter(Mandatory=$false)]
[switch]
$Release,
[Parameter(Mandatory=$false)]
[switch]
$VerboseMsbuild
)
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'
function Invoke-ExpressionHelper
{
[CmdletBinding()]
param(
[string] $Executable,
[string[]] $Arguments,
[string] $Activity
)
Write-Host "Running '$Activity'..." -ForegroundColor Green -BackgroundColor Black
$progress = 1.0
foreach ($arg in $Arguments)
{
Write-Progress -Activity $Activity -Status "Percent Complete: $([Math]::Round($progress/$Arguments.Count, 2) * 100)%" -PercentComplete ($progress/$Arguments.Count * 100)
Write-Verbose "[ $Executable $arg ]"
Invoke-Expression "& $Executable $arg"
if ($LASTEXITCODE -ne 0)
{
Write-Error "Terminating..."
}
$progress++
}
}
function Verify-DotnetExists
{
$dotnetCommand = Get-Command 'dotnet' -ErrorAction SilentlyContinue
if (!$dotnetCommand)
{
Write-Error "No .NET Core CLI exists. Please go to https://aka.ms/dotnet-download and install the latest SDK."
}
else
{
Write-Verbose "Verified .NET Core CLI exists."
& 'dotnet' --info
}
}
###
# Set variables
###
if ($VerboseMsbuild)
{
$clParams = "Verbosity=d"
}
else
{
$clParams = "ErrorsOnly"
}
if ($Release)
{
$configuration = "Release"
}
else
{
$configuration = "Debug"
}
if (!$BuildVersion)
{
$BuildVersion = "$([System.DateTime]::Now.ToString('yyyyMMddHHmm'))" # Set the build number so it's constant for the entirety of this build
}
$SignType = 'None' # Used for internal testing of signing.
###
# Attempt to find CLR Instrumentation Engine repo
###
try { $repoRoot = (Get-Item $PSCommandPath).Directory.FullName } catch { } # ignore terminating errors
if ($repoRoot)
{
Write-Verbose "Checking if [$repoRoot] is the CLR Instrumentation Engine repo"
$repoMarker = Join-Path $repoRoot 'EnlistmentRoot.marker'
if (Test-Path $repoMarker -ErrorAction SilentlyContinue)
{
$repoPath = $repoRoot
}
}
if (-not ($repoPath))
{
$repoPath = Read-Host "Please enter the full path to the local repository"
}
###
# Picks up msbuild from vs2019 installation
###
$VsRequirements = @(
'Microsoft.Component.MSBuild'
'Microsoft.VisualStudio.Workload.NativeDesktop'
'Microsoft.VisualStudio.Component.VC.ATL.Spectre'
'Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre'
)
Write-Verbose "Checking for VS installation with these installed components: `n`n$($VsRequirements | Out-String)`n"
$vswhere = "`"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe`""
$filterArgs = "-latest -prerelease -requires $($VsRequirements -join ' ') -property installationPath"
# Restrict the VS version if running from a context that has the VisualStudioVersion env variable (e.g. Developer Command Prompt).
# This will make sure that VisualStudioVersion matches the VS version of MSBuild in order to avoid mismatches (e.g. using a Dev15
# Developer Command Prompt but invoking Dev16's MSBuild).
if ($env:VisualStudioVersion)
{
$vsversion = [version]$($env:VisualStudioVersion)
# This is a version range that looks like "[15.0,16.0)"
$filterArgs = "$filterArgs -version `"[$($vsversion.Major).0,$($vsversion.Major + 1).0)`""
}
$installationPath = Invoke-Expression "& $vswhere $filterArgs"
$msbuild = Join-Path $installationPath 'MSBuild\Current\bin\MSBuild.exe'
if (-not (Test-Path $msbuild))
{
$msbuild = Join-Path (Read-Host "Please enter the full path to your VS installation (eg. 'C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise)'`r`n") 'MSBuild\15.0\Bin\MSBuild.exe'
}
if (-not (Test-Path $msbuild))
{
Write-Error 'Cannot find msbuild.exe. Please check your VS installation.'
}
$msbuild = "`"$msbuild`""
###
# Local Build
###
Verify-DotnetExists
if (!$SkipBuild)
{
if (!$SkipCleanAndRestore)
{
# Clean up bin & obj folder if exists
if (Test-Path "$repoPath\bin\$configuration")
{
Remove-Item -Force -Recurse "$repoPath\bin\$configuration"
}
if (Test-Path "$repoPath\obj\")
{
Remove-Item -Force -Recurse "$repoPath\obj\"
}
# NuGet restore disregards platform/configuration
# dotnet restore defaults to Debug|Any CPU, which requires the /p:platform specification in order to replicate NuGet restore behavior.
$restoreArgsInit = "restore $repoPath\InstrumentationEngine.sln --configfile $repoPath\NuGet.config"
$restoreArgs = @(
"$restoreArgsInit /p:platform=`"x86`""
"$restoreArgsInit /p:platform=`"x64`""
"$restoreArgsInit /p:platform=`"Any CPU`""
)
Invoke-ExpressionHelper -Executable "dotnet" -Arguments $restoreArgs -Activity 'dotnet Restore Solutions'
}
# Build InstrumentationEngine.sln
$buildArgsInit = "$repoPath\InstrumentationEngine.sln /p:configuration=`"$configuration`" /p:SignType=$SignType /p:BuildVersion=$BuildVersion /clp:$($clParams)"
$buildArgs = @(
"$buildArgsInit /p:platform=`"x86`""
"$buildArgsInit /p:platform=`"x64`""
"$buildArgsInit /p:platform=`"Any CPU`" /m"
)
Invoke-ExpressionHelper -Executable "$msbuild" -Arguments $buildArgs -Activity 'Build InstrumentationEngine.sln'
}
if (!$SkipPackaging)
{
# Remove files from obj folders as they can become stale
Remove-Item "$repoPath\obj\InstrumentationEngine.Fragment" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$repoPath\obj\InstrumentationEngine.Module" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$repoPath\obj\InstrumentationEngine.Installer" -Recurse -Force -ErrorAction SilentlyContinue
# The SkipBuild switch prevents bin/obj cleanup and also means we're testing packaging. This will cleanup old/stale packages
if ($SkipBuild)
{
# Remove package artifacts from bin folders
Get-ChildItem "$repoPath\bin\$configuration" -Include *.msi,*.msm,*.wixlib,*.nupkg -Recurse | Remove-Item -Force
}
# NuGet restore disregards platform/configuration
# dotnet restore defaults to Debug|Any CPU, which requires the /p:platform specification in order to replicate NuGet restore behavior.
$restoreArgsInit = "restore $repoPath\src\InstrumentationEngine.Packages.sln --configfile $repoPath\NuGet.config"
$restoreArgs = @(
"$restoreArgsInit /p:platform=`"x86`""
"$restoreArgsInit /p:platform=`"x64`""
"$restoreArgsInit /p:platform=`"Any CPU`""
)
Invoke-ExpressionHelper -Executable "dotnet" -Arguments $restoreArgs -Activity 'dotnet Restore Solutions'
# Build InstrumentationEngine.Packages.sln
$buildArgsInit = "$repoPath\src\InstrumentationEngine.Packages.sln /p:configuration=`"$configuration`" /p:SignType=$SignType /p:BuildVersion=$BuildVersion /clp:$($clParams) /m"
$buildArgs = @(
"$buildArgsInit /p:platform=`"x86`""
"$buildArgsInit /p:platform=`"x64`""
"$buildArgsInit /p:platform=`"Any CPU`""
)
Invoke-ExpressionHelper -Executable "$msbuild" -Arguments $buildArgs -Activity 'Build InstrumentationEngine.Packages.sln'
}
# Run Unit Tests
if ($IncludeTests)
{
$vstest = Join-Path $installationPath 'Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe'
if (-not (Test-Path $vstest))
{
Write-Error "Cannot find $vstest. Please check your VS installation."
}
$vstest = "`"$vstest`""
# Remove Test Results before running. This cleans up old test artifacts
if (Test-Path "$repoPath\TestResults")
{
Remove-Item "$repoPath\TestResults" -Recurse -Force
}
# Release builds contain the RawProfilerHook.Tests_AnyCPU.dll which is not tested in debug configuration.
$x86Tests = (Get-ChildItem -path "$repoPath\bin\$configuration\x86" -Filter *Tests*.dll -Recurse -Exclude *TestAdapter.tests.dll | Select-Object -ExpandProperty FullName | ForEach-Object { "`"$_`""})
$x64Tests = (Get-ChildItem -path "$repoPath\bin\$configuration\x64" -Filter *Tests*.dll -Recurse -Exclude *TestAdapter.tests.dll | Select-Object -ExpandProperty FullName | ForEach-Object { "`"$_`""})
$anyCpuTests = (Get-ChildItem -path "$repoPath\bin\$configuration\anycpu" -Filter *Tests*.dll -Recurse -Exclude *TestAdapter.tests.dll | Select-Object -ExpandProperty FullName | ForEach-Object { "`"$_`""})
$testBuildArgs = @(
($x86Tests -join ' ') + " /Settings:`"$repoPath\tests\TestSettings\x86.runsettings`""
($x64Tests -join ' ') + " /Settings:`"$repoPath\tests\TestSettings\x64.runsettings`""
($anyCpuTests -join ' ') + " /Settings:`"$repoPath\tests\TestSettings\x86.runsettings`"" # AnyCPU
)
Invoke-ExpressionHelper -Executable "$vstest" -Arguments $testBuildArgs -Activity 'Run Tests'
}