/
build.sh
executable file
·531 lines (446 loc) · 12.9 KB
/
build.sh
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/bin/bash
if [ "$1" == "-fromScript" ]; then
otherScript=$2
shift 2
echo Executed from $otherScript with arguments: $*
fi
build=false
ci=false
configuration="Debug"
help=false
nolog=false
pack=false
prepareMachine=false
rebuild=false
norestore=false
sign=false
skipTests=false
bootstrap=false
verbosity="minimal"
hostType="core"
properties=""
dotnetBuildFromSource=false
dotnetCoreSdkDir=""
function Help() {
echo "Common settings:"
echo " -configuration <value> Build configuration Debug, Release"
echo " -hostType <value> core (default), mono"
echo " -verbosity <value> Msbuild verbosity (q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic])"
echo " -help Print help and exit"
echo ""
echo "Actions:"
echo " -norestore Don't automatically run restore"
echo " -build Build solution"
echo " -rebuild Rebuild solution"
echo " -skipTests Don't run tests"
echo " -bootstrap Run build again with bootstrapped MSBuild"
echo " -sign Sign build outputs"
echo " -pack Package build outputs into NuGet packages and Willow components"
echo ""
echo "Advanced settings:"
echo " -ci Set when running on CI server"
echo " -nolog Disable logging"
echo " -prepareMachine Prepare machine for CI run"
echo " -useSystemMSBuild [mono] Use system msbuild instead of downloading a copy for use with mono"
echo ""
echo "Command line arguments not listed above are passed through to MSBuild."
}
while [[ $# -gt 0 ]]; do
lowerI="$(echo $1 | awk '{print tolower($0)}')"
case $lowerI in
build)
build=true
shift 1
;;
-build)
build=true
shift 1
;;
-ci)
ci=true
shift 1
;;
-configuration)
configuration=$2
shift 2
;;
-h | -help)
Help
exit 0
;;
-hosttype)
hostType=$2
shift 2
;;
-nolog)
nolog=true
shift 1
;;
-pack)
pack=true
shift 1
;;
-preparemachine)
prepareMachine=true
shift 1
;;
-rebuild)
rebuild=true
shift 1
;;
-norestore)
norestore=true
shift 1
;;
-sign)
sign=true
shift 1
;;
-skiptests)
skipTests=true
shift 1
;;
-bootstrap)
bootstrap=true
shift 1
;;
-usesystemmsbuild)
useSystemMSBuild=true
shift 1
;;
-verbosity)
verbosity=$2
shift 2
;;
-dotnetbuildfromsource)
dotnetBuildFromSource=true
shift 1
;;
-dotnetcoresdkdir)
dotnetCoreSdkDir=$2
shift 2
;;
*)
properties="$properties $1"
shift 1
;;
esac
done
if [ "$hostType" = "mono" ]; then
configuration="$configuration-MONO"
fi
function KillProcessWithName {
echo "Killing processes containing \"$1\""
kill $(ps ax | grep -i "$1" | awk '{ print $1 }')
}
function StopProcesses {
echo "Killing running build processes..."
KillProcessWithName "dotnet"
KillProcessWithName "vbcscompiler"
}
function ExitIfError {
if [ $1 != 0 ]
then
echo "$2"
if [[ "$ci" == "true" ]];
then
# Log VSTS error for each line in the .err files.
find "$LogDir" -name "*.err" | xargs sed -e 's/^/##vso[task.logissue type=error] /'
if [[ "$dotnetBuildFromSource" != "true" ]]; # kill command not permitted on CI machines or in source-build]]
then
StopProcesses
fi
fi
exit $1
fi
}
# copied from https://github.com/dotnet/metadata-tools/blob/0c2c58c79edc2085f97b208afb8980cfed33b857/eng/common/build.sh#L122-L138
# ReadJson [filename] [json key]
# Result: Sets 'readjsonvalue' to the value of the provided json key
# Note: this method may return unexpected results if there are duplicate
# keys in the json
function ReadJson {
local unamestr="$(uname)"
local sedextended='-r'
if [[ "$unamestr" == 'Darwin' ]]; then
sedextended='-E'
fi;
readjsonvalue="$(grep -m 1 "\"${2}\"" ${1} | sed ${sedextended} 's/^ *//;s/.*: *"//;s/",?//')"
if [[ ! "$readjsonvalue" ]]; then
ExitIfError 1 "Error: Cannot find \"${2}\" in ${1}"
fi;
}
function CreateDirectory {
if [ ! -d "$1" ]
then
mkdir -p "$1"
fi
}
function QQ {
echo '"'$*'"'
}
function GetLogCmd {
if $ci || $log
then
CreateDirectory $LogDir
local logCmd="/bl:$(QQ $LogDir/$1.binlog)"
# When running under CI, also create a text log, so it can be viewed in the Jenkins UI
if $ci
then
logCmd="$logCmd /fl /flp:ErrorsOnly\;LogFile=$(QQ $LogDir/$1.err)"
fi
else
logCmd=""
fi
echo $logCmd
}
function downloadMSBuildForMono {
if [[ -z $useSystemMSBuild && ! -e "$MONO_MSBUILD_DIR/MSBuild.dll" ]]
then
echo "** Downloading MSBUILD from $MSBUILD_DOWNLOAD_URL"
curl -sL -o "$MSBUILD_ZIP" "$MSBUILD_DOWNLOAD_URL"
unzip -q "$MSBUILD_ZIP" -d "$ArtifactsDir"
# rename just to make it obvious when reading logs!
mv $ArtifactsDir/msbuild $MONO_MSBUILD_DIR
chmod +x $ArtifactsDir/mono-msbuild/MSBuild.dll
rm "$MSBUILD_ZIP"
fi
}
function CallMSBuild {
local commandLine=""
if [ -z "$msbuildHost" ]
then
commandLine="$msbuildToUse $*"
else
commandLine="$msbuildHost $msbuildToUse $*"
fi
echo ============= MSBuild command =============
echo "$commandLine"
echo ===========================================
eval $commandLine
ExitIfError $? "Failed to run MSBuild: $commandLine"
}
function GetVersionsPropsVersion {
echo "$( awk -F'[<>]' "/<$1>/{print \$3}" "$VersionsProps" )"
}
function DownloadDotnetCli {
DotNetCliVersion="$( GetVersionsPropsVersion DotNetCliVersion )"
DotNetInstallVerbosity=""
if [ -z "$DOTNET_INSTALL_DIR" ]
then
export DOTNET_INSTALL_DIR="$RepoRoot/artifacts/.dotnet/$DotNetCliVersion"
fi
DotNetRoot=$DOTNET_INSTALL_DIR
DotNetInstallScript="$DotNetRoot/dotnet-install.sh"
if [ ! -a "$DotNetInstallScript" ]
then
CreateDirectory "$DotNetRoot"
curl "https://dot.net/v1/dotnet-install.sh" -sSL -o "$DotNetInstallScript"
fi
if [[ "$(echo $verbosity | awk '{print tolower($0)}')" == "diagnostic" ]]
then
DotNetInstallVerbosity="--verbose"
fi
# Install a stage 0
SdkInstallDir="$DotNetRoot/sdk/$DotNetCliVersion"
if [ ! -d "$SdkInstallDir" ]
then
bash "$DotNetInstallScript" --version $DotNetCliVersion $DotNetInstallVerbosity
LASTEXITCODE=$?
if [ $LASTEXITCODE != 0 ]
then
echo "Failed to install stage0"
return $LASTEXITCODE
fi
fi
}
function InstallDotNetCli {
if [ "$dotnetCoreSdkDir" = "" ]
then
DownloadDotnetCli
else
export DOTNET_INSTALL_DIR=$dotnetCoreSdkDir
fi
# don't double quote this otherwise the csc tooltask will fail with double double-quotting
export DOTNET_HOST_PATH="$DOTNET_INSTALL_DIR/dotnet"
# Put the stage 0 on the path
export PATH="$DOTNET_INSTALL_DIR:$PATH"
# Disable first run since we want to control all package sources
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
# Don't resolve runtime, shared framework, or SDK from other locations
export DOTNET_MULTILEVEL_LOOKUP=0
}
function InstallRepoToolset {
CreateDirectory "$RepoToolsetDir"
ReadJson "$RepoRoot/global.json" "RoslynTools.RepoToolset"
RepoToolsetVersion=$readjsonvalue
RepoToolsetLocationFile="$RepoToolsetDir/$RepoToolsetVersion.txt"
echo "Using repo tools version: $readjsonvalue"
if [[ -a "$RepoToolsetLocationFile" ]]; then
local path=`cat $RepoToolsetLocationFile`
if [[ -a "$path" ]]; then
RepoToolsetBuildProj=$path
return
fi
fi
if [[ "$restore" != true ]]; then
ExitIfError 2 "Toolset version $RepoToolsetVersion has not been restored."
fi
local proj="$RepoToolsetDir/restore.proj"
echo '<Project Sdk="RoslynTools.RepoToolset"/>' > $proj
local logCmd=$(GetLogCmd Toolset)
CallMSBuild $(QQ $proj) /t:__WriteToolsetLocation /m /nologo /clp:None /warnaserror $logCmd /p:__ToolsetLocationOutputFile=$RepoToolsetLocationFile
local lastexitcode=$?
ExitIfError $lastexitcode "Failed to restore toolset (exit code '$lastexitcode')."
RepoToolsetBuildProj=`cat $RepoToolsetLocationFile`
if [[ ! -a "$RepoToolsetBuildProj" ]]; then
ExitIfError 3 "Invalid toolset path: $RepoToolsetBuildProj"
fi
}
function ErrorHostType {
echo "Unsupported hostType ($hostType)"
exit 1
}
function Build {
InstallDotNetCli
echo "Using dotnet from: $DOTNET_INSTALL_DIR"
if $prepareMachine
then
CreateDirectory "$NuGetPackageRoot"
eval "$(QQ $DOTNET_HOST_PATH) nuget locals all --clear"
ExitIfError $? "Failed to clear NuGet cache"
fi
if [ "$hostType" = "core" ]
then
msbuildHost=$(QQ $DOTNET_HOST_PATH)
elif [ "$hostType" = "mono" ]
then
if [ -z $useSystemMSBuild ]; then
downloadMSBuildForMono
msbuildHost=""
msbuildToUse="$MONO_MSBUILD_DIR/msbuild"
else
msbuildHost=""
msbuildToUse="msbuild"
fi
else
ErrorHostType
fi
InstallRepoToolset
local logCmd=$(GetLogCmd Build)
commonMSBuildArgs="/m /clp:Summary /v:$verbosity /p:Configuration=$configuration /p:RepoRoot=$(QQ $RepoRoot) /p:Projects=$(QQ $MSBuildSolution) /p:CIBuild=$ci /p:DisableNerdbankVersioning=$dotnetBuildFromSource"
# Only enable warnaserror on CI runs.
if $ci
then
commonMSBuildArgs="$commonMSBuildArgs /warnaserror"
fi
# Only test using stage 0 MSBuild if -bootstrap is not specified
local testStage0=false
if ! $bootstrap
then
testStage0=$test
fi
CallMSBuild $(QQ $RepoToolsetBuildProj) $commonMSBuildArgs $logCmd /p:Restore=$restore /p:Build=$build /p:Rebuild=$rebuild /p:Test=$testStage0 /p:Sign=$sign /p:Pack=$pack /p:CreateBootstrap=true $properties
if $bootstrap
then
bootstrapRoot="$ArtifactsConfigurationDir/bootstrap"
if [ $hostType = "core" ]
then
msbuildToUse=$(QQ "$bootstrapRoot/netcoreapp2.1/MSBuild/MSBuild.dll")
elif [ "$hostType" = "mono" ]
then
msbuildToUse="$bootstrapRoot/net461/MSBuild/15.0/Bin/MSBuild.dll"
msbuildHost="mono"
properties="$properties /p:MSBuildExtensionsPath=$bootstrapRoot/net461/MSBuild/"
else
ErrorHostType
fi
# forcing the slashes here or they get normalized and lost later
export ArtifactsDir="$ArtifactsDir\\2\\"
local logCmd=$(GetLogCmd BuildWithBootstrap)
# When using bootstrapped MSBuild:
# - Turn off node reuse (so that bootstrapped MSBuild processes don't stay running and lock files)
# - Don't sign
# - Don't pack
# - Do run tests (if not skipped)
# - Don't try to create a bootstrap deployment
CallMSBuild $(QQ $RepoToolsetBuildProj) $commonMSBuildArgs /nr:false $logCmd /p:Restore=$restore /p:Build=$build /p:Rebuild=$rebuild /p:Test=$test /p:Sign=false /p:Pack=false /p:CreateBootstrap=false $properties
fi
}
function AssertNugetPackages {
if $pack || $dotnetBuildFromSource
then
packageCount=$(find $PackagesDir -type f | wc -l)
if [ $packageCount -ne 5 ]
then
ExitIfError 1 "Did not find 5 packages in $PackagesDir"
fi
echo "Ensured that 5 nuget packages were created"
fi
}
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$ScriptRoot/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
ScriptRoot="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
RepoRoot="$ScriptRoot/.."
ArtifactsDir="$RepoRoot/artifacts"
ArtifactsConfigurationDir="$ArtifactsDir/$configuration"
PackagesDir="$ArtifactsConfigurationDir/packages"
LogDir="$ArtifactsConfigurationDir/log"
VersionsProps="$ScriptRoot/Versions.props"
RepoToolsetDir="$ArtifactsDir/toolset"
#https://github.com/dotnet/source-build
if $dotnetBuildFromSource
then
MSBuildSolution="$RepoRoot/MSBuild.SourceBuild.sln"
else
MSBuildSolution="$RepoRoot/MSBuild.sln"
fi
msbuildToUse="msbuild"
MONO_MSBUILD_DIR="$ArtifactsDir/mono-msbuild"
MSBUILD_DOWNLOAD_URL="https://github.com/mono/msbuild/releases/download/0.06/mono_msbuild_xplat-master-3c930fa8.zip"
MSBUILD_ZIP="$ArtifactsDir/msbuild.zip"
log=false
if ! $nolog
then
log=true
fi
restore=false
if ! $norestore
then
restore=true
fi
test=false
if ! $skipTests
then
test=true
fi
if [ "$hostType" != "core" -a "$hostType" != "mono" ]; then
ErrorHostType
fi
# HOME may not be defined in some scenarios, but it is required by NuGet
if [ -z $HOME ]
then
export HOME="$RepoRoot/artifacts/.home/"
CreateDirectory "$HOME"
fi
if $ci
then
TempDir="$ArtifactsConfigurationDir/tmp"
CreateDirectory "$TempDir"
export TEMP="$TempDir"
export TMP="$TempDir"
export MSBUILDDEBUGPATH="$TempDir"
fi
if [ -z $NUGET_PACKAGES ]
then
export NUGET_PACKAGES="$HOME/.nuget/packages"
fi
NuGetPackageRoot=$NUGET_PACKAGES
Build
ExitIfError $? "Build failed"
AssertNugetPackages
ExitIfError $? "AssertNugetPackages failed"