Merge pull request #22327 from BrianBohe/refactoring_genCodeForBBList
[platform/upstream/coreclr.git] / build-test.cmd
1 @if not defined _echo @echo off
2 setlocal EnableDelayedExpansion EnableExtensions
3
4 :: Define a prefix for most output progress messages that come from this script. That makes
5 :: it easier to see where these are coming from. Note that there is a trailing space here.
6 set "__MsgPrefix=BUILDTEST: "
7
8 echo %__MsgPrefix%Starting Build at %TIME%
9
10 set __ThisScriptDir="%~dp0"
11
12 call "%__ThisScriptDir%"\setup_vs_tools.cmd
13 if NOT '%ERRORLEVEL%' == '0' exit /b 1
14
15 if defined VS160COMNTOOLS (
16     set "__VSToolsRoot=%VS160COMNTOOLS%"
17     set "__VCToolsRoot=%VS160COMNTOOLS%\..\..\VC\Auxiliary\Build"
18     set __VSVersion=vs2019
19 ) else if defined VS150COMNTOOLS (
20     set "__VSToolsRoot=%VS150COMNTOOLS%"
21     set "__VCToolsRoot=%VS150COMNTOOLS%\..\..\VC\Auxiliary\Build"
22     set __VSVersion=vs2017
23 )
24
25 :: Set the default arguments for build
26 set __BuildArch=x64
27 set __BuildType=Debug
28 set __BuildOS=Windows_NT
29
30 set "__ProjectDir=%~dp0"
31 :: remove trailing slash
32 if %__ProjectDir:~-1%==\ set "__ProjectDir=%__ProjectDir:~0,-1%"
33 set "__TestDir=%__ProjectDir%\tests"
34 set "__ProjectFilesDir=%__TestDir%"
35 set "__SourceDir=%__ProjectDir%\src"
36 set "__PackagesDir=%__ProjectDir%\packages"
37 set "__RootBinDir=%__ProjectDir%\bin"
38 set "__LogsDir=%__RootBinDir%\Logs"
39 set "__MsbuildDebugLogsDir=%__LogsDir%\MsbuildDebugLogs"
40
41 :: Default __Exclude to issues.targets
42 set __Exclude=%__TestDir%\issues.targets
43
44 REM __UnprocessedBuildArgs are args that we pass to msbuild (e.g. /p:__BuildArch=x64)
45 set "__args= %*"
46 set processedArgs=
47 set __UnprocessedBuildArgs=
48 set __CommonMSBuildArgs=
49
50 set __BuildAgainstPackagesArg=
51 set __SkipRestorePackages=
52 set __SkipManaged=
53 set __SkipNative=
54 set __RuntimeId=
55 set __ZipTests=
56 set __TargetsWindows=1
57 set __DoCrossgen=
58
59 @REM CMD has a nasty habit of eating "=" on the argument list, so passing:
60 @REM    -priority=1
61 @REM appears to CMD parsing as "-priority 1". Handle -priority specially to avoid problems,
62 @REM and allow the "-priority=1" syntax.
63 set __Priority=0
64 set __PriorityArg=
65
66 :Arg_Loop
67 if "%1" == "" goto ArgsDone
68
69 if /i "%1" == "/?"     goto Usage
70 if /i "%1" == "-?"     goto Usage
71 if /i "%1" == "/h"     goto Usage
72 if /i "%1" == "-h"     goto Usage
73 if /i "%1" == "/help"  goto Usage
74 if /i "%1" == "-help"  goto Usage
75 if /i "%1" == "--help" goto Usage
76
77 if /i "%1" == "x64"                   (set __BuildArch=x64&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
78 if /i "%1" == "x86"                   (set __BuildArch=x86&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
79 if /i "%1" == "arm"                   (set __BuildArch=arm&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
80 if /i "%1" == "arm64"                 (set __BuildArch=arm64&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
81
82 if /i "%1" == "debug"                 (set __BuildType=Debug&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
83 if /i "%1" == "release"               (set __BuildType=Release&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
84 if /i "%1" == "checked"               (set __BuildType=Checked&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
85
86 if /i "%1" == "skipmanaged"           (set __SkipManaged=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
87 if /i "%1" == "skipnative"            (set __SkipNative=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
88 if /i "%1" == "buildtesthostonly"     (set __SkipNative=1&set __SkipManaged=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
89 if /i "%1" == "buildagainstpackages"  (set __ZipTests=1&set __BuildAgainstPackagesArg=/p:BuildTestsAgainstPackages=true&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
90 if /i "%1" == "skiprestorepackages"   (set __SkipRestorePackages=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
91 if /i "%1" == "ziptests"              (set __ZipTests=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
92 if /i "%1" == "crossgen"              (set __DoCrossgen=1&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
93 if /i "%1" == "runtimeid"             (set __RuntimeId=%2&set processedArgs=!processedArgs! %1 %2&shift&shift&goto Arg_Loop)
94 if /i "%1" == "targetsNonWindows"     (set __TargetsWindows=0&set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
95 if /i "%1" == "Exclude"               (set __Exclude=%2&set processedArgs=!processedArgs! %1 %2&shift&shift&goto Arg_Loop)
96 if /i "%1" == "-priority"             (set __Priority=%2&shift&set processedArgs=!processedArgs! %1=%2&shift&goto Arg_Loop)
97 if /i "%1" == "--"                    (set processedArgs=!processedArgs! %1&shift&goto Arg_Loop)
98
99 if [!processedArgs!]==[] (
100     set __UnprocessedBuildArgs=%__args%
101 ) else (
102     set __UnprocessedBuildArgs=%__args%
103     for %%t in (!processedArgs!) do (
104         set __UnprocessedBuildArgs=!__UnprocessedBuildArgs:*%%t=!
105     )
106 )
107
108 :ArgsDone
109
110 @REM Special handling for -priority=N argument.
111 if %__Priority% GTR 0 (
112     set "__PriorityArg=/p:CLRTestPriorityToBuild=%__Priority%"
113 )
114
115 if defined __BuildAgainstPackagesArg (
116     if not defined __RuntimeID (
117         echo %__MsgPrefix%Error: When building against packages, you must supply a target Runtime ID.
118         exit /b 1
119     )
120 )
121
122 set TargetsWindowsArg=
123 set TargetsWindowsMsbuildArg=
124 if "%__TargetsWindows%"=="1" (
125     set TargetsWindowsArg=-TargetsWindows=true
126     set TargetsWindowsMsbuildArg=/p:TargetsWindows=true
127 ) else if "%__TargetsWindows%"=="0" (
128     set TargetsWindowsArg=-TargetsWindows=false
129     set TargetsWindowsMsbuildArg=/p:TargetsWindows=false
130 )
131
132 @if defined _echo @echo on
133
134 set __CommonMSBuildArgs=/p:__BuildOS=%__BuildOS% /p:__BuildType=%__BuildType% /p:__BuildArch=%__BuildArch%
135 REM As we move from buildtools to arcade, __RunArgs should be replaced with __msbuildArgs
136 set __msbuildArgs=/p:__BuildOS=%__BuildOS% /p:__BuildType=%__BuildType% /p:__BuildArch=%__BuildArch% /nologo /verbosity:minimal /clp:Summary /maxcpucount
137
138 echo %__MsgPrefix%Commencing CoreCLR test build
139
140 set "__BinDir=%__RootBinDir%\Product\%__BuildOS%.%__BuildArch%.%__BuildType%"
141 set "__TestRootDir=%__RootBinDir%\tests"
142 set "__TestBinDir=%__TestRootDir%\%__BuildOS%.%__BuildArch%.%__BuildType%"
143
144 REM We have different managed and native intermediate dirs because the managed bits will include
145 REM the configuration information deeper in the intermediates path.
146 REM These variables are used by the msbuild project files.
147
148 if not defined __TestIntermediateDir (
149     set "__TestIntermediateDir=tests\obj\%__BuildOS%.%__BuildArch%.%__BuildType%"
150 )
151 set "__NativeTestIntermediatesDir=%__RootBinDir%\%__TestIntermediateDir%\Native"
152 set "__ManagedTestIntermediatesDir=%__RootBinDir%\%__TestIntermediateDir%\Managed"
153
154 REM Generate path to be set for CMAKE_INSTALL_PREFIX to contain forward slash
155 set "__CMakeBinDir=%__TestBinDir%"
156 set "__CMakeBinDir=%__CMakeBinDir:\=/%"
157
158 if not exist "%__TestBinDir%"                   md "%__TestBinDir%"
159 if not exist "%__NativeTestIntermediatesDir%"   md "%__NativeTestIntermediatesDir%"
160 if not exist "%__ManagedTestIntermediatesDir%"  md "%__ManagedTestIntermediatesDir%"
161 if not exist "%__LogsDir%"                      md "%__LogsDir%"
162 if not exist "%__MsbuildDebugLogsDir%"          md "%__MsbuildDebugLogsDir%"
163
164 REM Set up the directory for MSBuild debug logs.
165 set MSBUILDDEBUGPATH=%__MsbuildDebugLogsDir%
166
167 echo %__MsgPrefix%Checking prerequisites
168
169 REM Eval the output from set-cmake-path.ps1
170 for /f "delims=" %%a in ('powershell -NoProfile -ExecutionPolicy ByPass "& ""%__SourceDir%\pal\tools\set-cmake-path.ps1"""') do %%a
171
172 REM =========================================================================================
173 REM ===
174 REM === Restore Build Tools
175 REM ===
176 REM =========================================================================================
177
178 call "%__ProjectDir%\init-tools.cmd"
179 @if defined _echo @echo on
180
181 set "__ToolsDir=%__ProjectDir%\Tools"
182 set "DotNetCli=%__ToolsDir%\dotnetcli\dotnet.exe"
183 if not exist "%DotNetCli%" (
184     echo %__MsgPrefix%"%DotNetCli%" not found after init-tools.
185     exit /b 1
186 )
187
188 REM =========================================================================================
189 REM ===
190 REM === Resolve runtime dependences
191 REM ===
192 REM =========================================================================================
193
194 call "%__TestDir%\setup-stress-dependencies.cmd" /arch %__BuildArch% /outputdir %__BinDir%
195 @if defined _echo @echo on
196
197 REM =========================================================================================
198 REM ===
199 REM === Native test build section
200 REM ===
201 REM =========================================================================================
202
203 if defined __SkipNative goto skipnative
204
205 echo %__MsgPrefix%Commencing build of native test components for %__BuildArch%/%__BuildType%
206
207 REM Set the environment for the native build
208 set __VCBuildArch=x86_amd64
209 if /i "%__BuildArch%" == "x86" ( set __VCBuildArch=x86 )
210 if /i "%__BuildArch%" == "arm" ( set __VCBuildArch=x86_arm )
211 if /i "%__BuildArch%" == "arm64" ( set __VCBuildArch=x86_arm64 )
212
213 echo %__MsgPrefix%Using environment: "%__VCToolsRoot%\vcvarsall.bat" %__VCBuildArch%
214 call                                 "%__VCToolsRoot%\vcvarsall.bat" %__VCBuildArch%
215 @if defined _echo @echo on
216
217 if not defined VSINSTALLDIR (
218     echo %__MsgPrefix%Error: VSINSTALLDIR variable not defined.
219     exit /b 1
220 )
221 if not exist "%VSINSTALLDIR%DIA SDK" goto NoDIA
222
223 pushd "%__NativeTestIntermediatesDir%"
224 call "%__SourceDir%\pal\tools\gen-buildsys-win.bat" ""%__ProjectFilesDir%"" %__VSVersion% %__BuildArch%
225 @if defined _echo @echo on
226 popd
227
228 if not exist "%__NativeTestIntermediatesDir%\install.vcxproj" (
229     echo %__MsgPrefix%Failed to generate test native component build project!
230     exit /b 1
231 )
232
233 set __BuildLogRootName=Tests_Native
234 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
235 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
236 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
237 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
238 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
239 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
240 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
241
242 call "%__ProjectDir%\msbuild.cmd" /nologo /verbosity:minimal /clp:Summary /nodeReuse:false^
243   /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log^
244   /p:RestoreDefaultOptimizationDataPackage=false /p:PortableBuild=true^
245   /p:UsePartialNGENOptimization=false /maxcpucount^
246   "%__NativeTestIntermediatesDir%\install.vcxproj"^
247   !__Logging! /p:Configuration=%__BuildType% /p:Platform=%__BuildArch% %__CommonMSBuildArgs% %__PriorityArg% %__UnprocessedBuildArgs%
248 if errorlevel 1 (
249     echo %__MsgPrefix%Error: build failed. Refer to the build log files for details:
250     echo     %__BuildLog%
251     echo     %__BuildWrn%
252     echo     %__BuildErr%
253     exit /b 1
254 )
255
256 :skipnative
257
258 REM =========================================================================================
259 REM ===
260 REM === Restore product binaries from packages
261 REM ===
262 REM =========================================================================================
263
264 if "%__SkipRestorePackages%" == 1 goto SkipRestoreProduct
265
266 echo %__MsgPrefix%Restoring CoreCLR product from packages
267
268 if not defined XunitTestBinBase set XunitTestBinBase=%__TestBinDir%
269 set "CORE_ROOT=%XunitTestBinBase%\Tests\Core_Root"
270
271 set __BuildLogRootName=Restore_Product
272 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
273 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
274 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
275 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
276 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
277 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
278 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
279
280 call "%__ProjectDir%\msbuild.cmd" /nologo /verbosity:minimal /clp:Summary /nodeReuse:false^
281   /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log^
282   /p:RestoreDefaultOptimizationDataPackage=false /p:PortableBuild=true^
283   /p:UsePartialNGENOptimization=false /maxcpucount^
284   %__ProjectDir%\tests\build.proj /t:BatchRestorePackages^
285   !__Logging! %__CommonMSBuildArgs% %__BuildAgainstPackagesArg% %__PriorityArg% %__UnprocessedBuildArgs%
286
287 if not defined __BuildAgainstPackagesArg goto SkipRestoreProduct
288
289 echo %__MsgPrefix%BinPlacing CoreLib
290
291 set __BuildLogRootName=Tests_GenerateRuntimeLayout
292 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
293 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
294 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
295 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
296 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
297 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
298 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
299
300 call "%__ProjectDir%\msbuild.cmd" /nologo /verbosity:minimal /clp:Summary /nodeReuse:false^
301   /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log^
302   /p:RestoreDefaultOptimizationDataPackage=false /p:PortableBuild=true^
303   /p:UsePartialNGENOptimization=false /maxcpucount^
304   %__Projectdir%\tests\runtest.proj /t:BinPlaceRef /t:BinPlaceProduct /t:CopyCrossgenToProduct /p:RuntimeId="%__RuntimeId%"^
305   !__Logging! %__CommonMSBuildArgs% %__BuildAgainstPackagesArg% %__PriorityArg% %__UnprocessedBuildArgs%
306 if errorlevel 1 (
307     echo %__MsgPrefix%Error: BinPlace of mscorlib.dll failed. Refer to the build log files for details:
308     echo     %__BuildLog%
309     echo     %__BuildWrn%
310     echo     %__BuildErr%
311     exit /b 1
312 )
313
314 :SkipRestoreProduct
315
316 REM =========================================================================================
317 REM ===
318 REM === Managed test build section
319 REM ===
320 REM =========================================================================================
321
322 if defined __SkipManaged goto SkipManagedBuild
323
324 echo %__MsgPrefix%Starting the Managed Tests Build
325
326 if not defined VSINSTALLDIR (
327     echo %__MsgPrefix%Error: build-test.cmd should be run from a Visual Studio Command Prompt.  Please see https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md for build instructions.
328     exit /b 1
329 )
330 set __AppendToLog=false
331 set __BuildLogRootName=Tests_Managed
332 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
333 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
334 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
335
336 REM Execute msbuild test build in stages - workaround for excessive data retention in MSBuild ConfigCache
337 REM See https://github.com/Microsoft/msbuild/issues/2993
338
339 set __SkipPackageRestore=false
340 set __SkipTargetingPackBuild=false
341 set __BuildLoopCount=2
342 set __TestGroupToBuild=1
343
344 if %__Priority% GTR 0 (set __BuildLoopCount=16&set __TestGroupToBuild=2)
345 echo %__MsgPrefix%Building tests group %__TestGroupToBuild% with %__BuildLoopCount% subgroups
346
347 for /l %%G in (1, 1, %__BuildLoopCount%) do (
348
349     set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%";Append=!__AppendToLog!
350     set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%";Append=!__AppendToLog!
351     set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%";Append=!__AppendToLog!
352
353     set TestBuildSlice=%%G
354     echo Running: msbuild %__ProjectDir%\tests\build.proj !__MsbuildLog! !__MsbuildWrn! !__MsbuildErr! %TargetsWindowsMsbuildArg% %__msbuildArgs% %__BuildAgainstPackagesArg% !__PriorityArg! %__UnprocessedBuildArgs%
355
356     call msbuild %__ProjectDir%\tests\build.proj !__MsbuildLog! !__MsbuildWrn! !__MsbuildErr! %TargetsWindowsMsbuildArg% %__msbuildArgs% %__BuildAgainstPackagesArg% !__PriorityArg! %__UnprocessedBuildArgs%
357
358     if errorlevel 1 (
359         echo %__MsgPrefix%Error: build failed. Refer to the build log files for details:
360         echo     %__BuildLog%
361         echo     %__BuildWrn%
362         echo     %__BuildErr%
363         REM This is necessary because of a(n apparent) bug in the FOR /L command.  Under certain circumstances,
364         REM such as when this script is invoke with CMD /C "build-test.cmd", a non-zero exit directly from
365         REM within the loop body will not propagate to the caller.  For some reason, goto works around it.
366         goto     :Exit_Failure
367     )
368
369     set __SkipPackageRestore=true
370     set __SkipTargetingPackBuild=true
371     set __AppendToLog=true
372 )
373
374 REM Check that we've built about as many tests as we expect. This is primarily intended to prevent accidental changes that cause us to build
375 REM drastically fewer Pri-1 tests than expected.
376 echo %__MsgPrefix%Check the managed tests build
377 echo Running: msbuild %__ProjectDir%\tests\runtest.proj /t:CheckTestBuild /p:CLRTestPriorityToBuild=%__Priority% %__msbuildArgs% %__unprocessedBuildArgs%
378 call msbuild %__ProjectDir%\tests\runtest.proj /t:CheckTestBuild /p:CLRTestPriorityToBuild=%__Priority% %__msbuildArgs% %__unprocessedBuildArgs%
379 if errorlevel 1 (
380     echo %__MsgPrefix%Error: build failed.
381     exit /b 1
382 )
383
384 :SkipManagedBuild
385
386 REM =========================================================================================
387 REM ===
388 REM === Prepare the test drop
389 REM ===
390 REM =========================================================================================
391
392 echo %__MsgPrefix%Removing 'ni' files and 'lock' folders from %__TestBinDir%
393 REM Remove any NI from previous runs.
394 powershell -NoProfile "Get-ChildItem -path %__TestBinDir% -Include '*.ni.*' -Recurse -Force | Remove-Item -force"
395 REM Remove any lock folder used for synchronization from previous runs.
396 powershell -NoProfile "Get-ChildItem -path %__TestBinDir% -Include 'lock' -Recurse -Force |  where {$_.Attributes -eq 'Directory'}| Remove-Item -force -Recurse"
397
398 set CORE_ROOT=%__TestBinDir%\Tests\Core_Root
399 set CORE_ROOT_STAGE=%__TestBinDir%\Tests\Core_Root_Stage
400 if exist "%CORE_ROOT%" rd /s /q "%CORE_ROOT%"
401 if exist "%CORE_ROOT_STAGE%" rd /s /q "%CORE_ROOT_STAGE%"
402 md "%CORE_ROOT%"
403 md "%CORE_ROOT_STAGE%"
404 xcopy "%__BinDir%" "%CORE_ROOT_STAGE%"
405
406 if defined __BuildAgainstPackagesArg ( 
407     if "%__TargetsWindows%"=="0" (
408
409         if not exist %__PackagesDir%\TestNativeBins (
410             echo %__MsgPrefix%Error: Ensure you have run sync.cmd -ab before building a non-Windows test overlay against packages
411             exit /b 1
412         )
413
414         for /R %__PackagesDir%\TestNativeBins\%__RuntimeId%\%__BuildType% %%f in (*.so) do copy %%f %CORE_ROOT_STAGE%
415         for /R %__PackagesDir%\TestNativeBins\%__RuntimeId%\%__BuildType% %%f in (*.dylib) do copy %%f %CORE_ROOT_STAGE%
416     )
417 )
418
419 REM =========================================================================================
420 REM ===
421 REM === Create the test overlay
422 REM ===
423 REM =========================================================================================
424
425 echo %__MsgPrefix%Creating test overlay
426
427 set RuntimeIdArg=
428 if defined __RuntimeId (
429     set RuntimeIdArg=/p:RuntimeId="%__RuntimeId%"
430 )
431
432 set __BuildLogRootName=Tests_Overlay_Managed
433 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
434 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
435 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
436 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
437 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
438 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
439 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
440
441 call %__ProjectDir%\msbuild.cmd /nologo /verbosity:minimal /clp:Summary /nodeReuse:false^
442   /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log^
443   /p:RestoreDefaultOptimizationDataPackage=false /p:PortableBuild=true^
444   /p:UsePartialNGENOptimization=false /maxcpucount^
445   %__ProjectDir%\tests\runtest.proj /t:CreateTestOverlay^
446   !__Logging! %__CommonMSBuildArgs% %RuntimeIdArg% %__PriorityArg% %__UnprocessedBuildArgs%
447 if errorlevel 1 (
448     echo %__MsgPrefix%Error: build failed. Refer to the build log files for details:
449     echo     %__BuildLog%
450     echo     %__BuildWrn%
451     echo     %__BuildErr%
452     exit /b 1
453 )
454
455 xcopy /s /y "%CORE_ROOT_STAGE%" "%CORE_ROOT%"
456
457 REM =========================================================================================
458 REM ===
459 REM === Create the test host necessary for running CoreFX tests.
460 REM === The test host includes a dotnet executable, system libraries and CoreCLR assemblies found in CORE_ROOT.
461 REM ===
462 REM =========================================================================================
463
464 echo %__MsgPrefix%Building CoreFX test host
465
466 set __BuildLogRootName=Tests_CoreFX_Testhost
467 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
468 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
469 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
470 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
471 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
472 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
473 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
474
475 call %__ProjectDir%\msbuild.cmd /nologo /verbosity:minimal /clp:Summary /nodeReuse:false^
476   /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log^
477   /p:RestoreDefaultOptimizationDataPackage=false /p:PortableBuild=true^
478   /p:UsePartialNGENOptimization=false /maxcpucount^
479   %__ProjectDir%\tests\runtest.proj /t:CreateTestHost^
480   !__Logging! %__CommonMSBuildArgs% %RuntimeIdArg% %__PriorityArg% %__UnprocessedBuildArgs%
481 if errorlevel 1 (
482     echo %__MsgPrefix%Error: build failed. Refer to the build log files for details:
483     echo     %__BuildLog%
484     echo     %__BuildWrn%
485     echo     %__BuildErr%
486     exit /b 1
487 )
488
489 REM =========================================================================================
490 REM ===
491 REM === Create test wrappers.
492 REM ===
493 REM =========================================================================================
494
495 if defined __SkipManaged goto SkipBuildingWrappers
496
497 echo %__MsgPrefix%Creating test wrappers
498
499 set __BuildLogRootName=Tests_XunitWrapper
500 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
501 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
502 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
503 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
504 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
505 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
506 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
507
508 REM Build wrappers using the local SDK's msbuild. As we move to arcade, the other builds should be moved away from run.exe as well.
509 call %DotNetCli% msbuild %__ProjectDir%\tests\runtest.proj /p:RestoreAdditionalProjectSources=https://dotnet.myget.org/F/dotnet-core/  /p:BuildWrappers=true !__Logging! %__msbuildArgs% %TargetsWindowsMsbuildArg% %__BuildAgainstPackagesArg% %__UnprocessedBuildArgs%
510 if errorlevel 1 (
511     echo %__MsgPrefix%Error: Xunit wrapper build failed. Refer to the build log files for details:
512     echo     %__BuildLog%
513     echo     %__BuildWrn%
514     echo     %__BuildErr%
515     exit /b 1
516 )
517
518 echo { "build_os": "%__BuildOS%", "build_arch": "%__BuildArch%", "build_type": "%__BuildType%" } > "%__TestBinDir%/build_info.json"
519
520 :SkipBuildingWrappers
521
522 REM =========================================================================================
523 REM ===
524 REM === Crossgen assemblies if needed.
525 REM ===
526 REM =========================================================================================
527
528 set __CrossgenArg = ""
529 if defined __DoCrossgen (
530     set __CrossgenArg="/p:Crossgen=true"
531     if "%__TargetsWindows%" == "1" (
532         echo %__MsgPrefix%Running crossgen on framework assemblies
533         call :PrecompileFX
534     ) else (
535         echo "%__MsgPrefix%Crossgen only supported on Windows, for now"
536     )
537 )
538
539 rd /s /q "%CORE_ROOT_STAGE%"
540
541 REM =========================================================================================
542 REM ===
543 REM === Prep test binaries for Helix publishing
544 REM ===
545 REM =========================================================================================
546
547 if not defined __ZipTests goto SkipPrepForPublish
548
549 echo %__MsgPrefix%Preparing test binaries for Helix publishing
550
551 set __BuildLogRootName=Helix_Prep
552 set __BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log
553 set __BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn
554 set __BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err
555 set __MsbuildLog=/flp:Verbosity=normal;LogFile="%__BuildLog%"
556 set __MsbuildWrn=/flp1:WarningsOnly;LogFile="%__BuildWrn%"
557 set __MsbuildErr=/flp2:ErrorsOnly;LogFile="%__BuildErr%"
558 set __Logging=!__MsbuildLog! !__MsbuildWrn! !__MsbuildErr!
559
560 call %__ProjectDir%\msbuild.cmd /nologo /verbosity:minimal /clp:Summary /nodeReuse:false^
561   /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log^
562   /p:RestoreDefaultOptimizationDataPackage=false /p:PortableBuild=true^
563   /p:UsePartialNGENOptimization=false /maxcpucount^
564   %__ProjectDir%\tests\helixprep.proj^
565   !__Logging! %__CommonMSBuildArgs% %RuntimeIdArg% %TargetsWindowsMSBuildArg% %__CrossgenArg% %__PriorityArg% %__UnprocessedBuildArgs%
566 if errorlevel 1 (
567     echo %__MsgPrefix%Error: build failed. Refer to the build log files for details:
568     echo     %__BuildLog%
569     echo     %__BuildWrn%
570     echo     %__BuildErr%
571     exit /b 1
572 )
573
574 :SkipPrepForPublish
575
576 REM =========================================================================================
577 REM ===
578 REM === All builds complete!
579 REM ===
580 REM =========================================================================================
581
582 echo %__MsgPrefix%Test build succeeded.  Finished at %TIME%
583 echo %__MsgPrefix%Test binaries are available at !__TestBinDir!
584 exit /b 0
585
586 :Usage
587 echo.
588 echo Build the CoreCLR tests.
589 echo.
590 echo Usage:
591 echo     %0 [option1] [option2] ...
592 echo All arguments are optional. Options are case-insensitive. The options are:
593 echo.
594 echo.-? -h -help --help: view this message.
595 echo Build architecture: one of x64, x86, arm, arm64 ^(default: x64^).
596 echo Build type: one of Debug, Checked, Release ^(default: Debug^).
597 echo skipmanaged: skip the managed tests build
598 echo skipnative: skip the native tests build
599 echo buildtesthostonly: build the CoreFX testhost only
600 echo buildagainstpackages: builds tests against restored packages, instead of against a built product.
601 echo skiprestorepackages: skip package restore
602 echo runtimeid ^<ID^>: Builds a test overlay for the specified OS ^(Only supported when building against packages^). Supported IDs are:
603 echo     alpine.3.4.3-x64: Builds overlay for Alpine 3.4.3
604 echo     debian.8-x64: Builds overlay for Debian 8
605 echo     fedora.24-x64: Builds overlay for Fedora 24
606 echo     linux-x64: Builds overlay for portable linux
607 echo     opensuse.42.1-x64: Builds overlay for OpenSUSE 42.1
608 echo     osx.10.12-x64: Builds overlay for OSX 10.12
609 echo     osx-x64: Builds overlay for portable OSX
610 echo     rhel.7-x64: Builds overlay for RHEL 7 or CentOS
611 echo     ubuntu.14.04-x64: Builds overlay for Ubuntu 14.04
612 echo     ubuntu.16.04-x64: Builds overlay for Ubuntu 16.04
613 echo     ubuntu.16.10-x64: Builds overlay for Ubuntu 16.10
614 echo     win-x64: Builds overlay for portable Windows
615 echo     win7-x64: Builds overlay for Windows 7
616 echo ziptests: zips CoreCLR tests and Core_Root for a Helix run
617 echo crossgen: Precompiles the framework managed assemblies
618 echo targetsNonWindows:
619 echo Exclude- Optional parameter - specify location of default exclusion file ^(defaults to tests\issues.targets if not specified^)
620 echo     Set to "" to disable default exclusion file.
621 echo -- ... : all arguments following this tag will be passed directly to msbuild.
622 echo -priority=^<N^> : specify a set of tests that will be built and run, with priority N.
623 echo     0: Build only priority 0 cases as essential testcases (default)
624 echo     1: Build all tests with priority 0 and 1
625 echo     666: Build all tests with priority 0, 1 ... 666
626 echo -verbose: enables detailed file logging for the msbuild tasks into the msbuild log file.
627 exit /b 1
628
629 :NoDIA
630 echo Error: DIA SDK is missing at "%VSINSTALLDIR%DIA SDK". ^
631 This is due to a bug in the Visual Studio installer. It does not install DIA SDK at "%VSINSTALLDIR%" but rather ^
632 at the install location of previous Visual Studio version. The workaround is to copy the DIA SDK folder from the Visual Studio install location ^
633 of the previous version to "%VSINSTALLDIR%" and then build.
634 REM DIA SDK not included in Express editions
635 echo Visual Studio Express does not include the DIA SDK. ^
636 You need Visual Studio 2017 or 2019 (Community is free).
637 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
638 exit /b 1
639
640 :PrecompileFX
641 for %%F in (%CORE_ROOT%\*.dll) do call :PrecompileAssembly "%%F" %%~nF%%~xF
642 exit /b 0
643
644 REM Compile the managed assemblies in Core_ROOT before running the tests
645 :PrecompileAssembly
646
647 REM Skip mscorlib since it is already precompiled.
648 if /I "%2" == "mscorlib.dll" exit /b 0
649 if /I "%2" == "mscorlib.ni.dll" exit /b 0
650 REM don't precompile anything from CoreCLR
651 if /I exist %CORE_ROOT_STAGE%\%2 exit /b 0
652
653 "%CORE_ROOT_STAGE%\crossgen.exe" /Platform_Assemblies_Paths "%CORE_ROOT%" /in "%1" /out "%CORE_ROOT%/temp.ni.dll" >nul 2>nul
654 set /a __exitCode = %errorlevel%
655 if "%__exitCode%" == "-2146230517" (
656     echo %2 is not a managed assembly.
657     exit /b 0
658 )
659
660 if %__exitCode% neq 0 (
661     echo Unable to precompile %2
662     exit /b 0
663 )
664
665 REM Delete original .dll & replace it with the Crossgened .dll
666 del %1
667 ren "%CORE_ROOT%\temp.ni.dll" %2
668     
669 echo Successfully precompiled %2
670 exit /b 0
671
672 :Exit_Failure
673 exit /b 1