Merge pull request #11054 from wtgodbe/spacing
[platform/upstream/coreclr.git] / tests / runtest.cmd
1 @if not defined _echo @echo off
2 setlocal EnableDelayedExpansion
3
4 :: Set the default arguments
5 set __BuildArch=x64
6 set __BuildType=Debug
7 set __BuildOS=Windows_NT
8 set __MSBuildBuildArch=x64
9
10 :: Default to highest Visual Studio version available
11 ::
12 :: For VS2015 (and prior), only a single instance is allowed to be installed on a box
13 :: and VS140COMNTOOLS is set as a global environment variable by the installer. This
14 :: allows users to locate where the instance of VS2015 is installed.
15 ::
16 :: For VS2017, multiple instances can be installed on the same box SxS and VS150COMNTOOLS
17 :: is no longer set as a global environment variable and is instead only set if the user
18 :: has launched the VS2017 Developer Command Prompt.
19 ::
20 :: Following this logic, we will default to the VS2017 toolset if VS150COMNTOOLS tools is
21 :: set, as this indicates the user is running from the VS2017 Developer Command Prompt and
22 :: is already configured to use that toolset. Otherwise, we will fallback to using the VS2015
23 :: toolset if it is installed. Finally, we will fail the script if no supported VS instance
24 :: can be found.
25 set __VSVersion=vs2017
26
27 if defined VS140COMNTOOLS set __VSVersion=vs2015
28 if defined VS150COMNTOOLS set __VSVersion=vs2017
29
30 :: Define a prefix for most output progress messages that come from this script. That makes
31 :: it easier to see where these are coming from. Note that there is a trailing space here.
32 set __MsgPrefix=RUNTEST: 
33
34 set __ProjectDir=%~dp0
35 :: remove trailing slash
36 if %__ProjectDir:~-1%==\ set "__ProjectDir=%__ProjectDir:~0,-1%"
37 set "__ProjectFilesDir=%__ProjectDir%"
38 set "__RootBinDir=%__ProjectDir%\..\bin"
39 set "__LogsDir=%__RootBinDir%\Logs"
40
41 set __Sequential=
42 set __msbuildExtraArgs=
43 set __LongGCTests=
44 set __GCSimulatorTests=
45 set __AgainstPackages=
46 set __JitDisasm=
47 set __CollectDumps=
48
49 :Arg_Loop
50 if "%1" == "" goto ArgsDone
51
52 if /i "%1" == "/?"    goto Usage
53 if /i "%1" == "-?"    goto Usage
54 if /i "%1" == "/h"    goto Usage
55 if /i "%1" == "-h"    goto Usage
56 if /i "%1" == "/help" goto Usage
57 if /i "%1" == "-help" goto Usage
58
59 if /i "%1" == "x64"                   (set __BuildArch=x64&set __MSBuildBuildArch=x64&shift&goto Arg_Loop)
60 if /i "%1" == "x86"                   (set __BuildArch=x86&set __MSBuildBuildArch=x86&shift&goto Arg_Loop)
61 if /i "%1" == "arm"                   (set __BuildArch=arm&set __MSBuildBuildArch=arm&shift&goto Arg_Loop)
62
63 if /i "%1" == "debug"                 (set __BuildType=Debug&shift&goto Arg_Loop)
64 if /i "%1" == "release"               (set __BuildType=Release&shift&goto Arg_Loop)
65 if /i "%1" == "checked"               (set __BuildType=Checked&shift&goto Arg_Loop)
66
67 if /i "%1" == "vs2015"                (set __VSVersion=%1&shift&goto Arg_Loop)
68 if /i "%1" == "vs2017"                (set __VSVersion=%1&shift&goto Arg_Loop)
69
70 if /i "%1" == "TestEnv"               (set __TestEnv=%2&shift&shift&goto Arg_Loop)
71 if /i "%1" == "AgainstPackages"       (set __AgainstPackages=1&shift&goto Arg_Loop)
72 if /i "%1" == "sequential"            (set __Sequential=1&shift&goto Arg_Loop)
73 if /i "%1" == "crossgen"              (set __DoCrossgen=1&shift&goto Arg_Loop)
74 if /i "%1" == "longgc"                (set __LongGCTests=1&shift&goto Arg_Loop)
75 if /i "%1" == "gcsimulator"           (set __GCSimulatorTests=1&shift&goto Arg_Loop)
76 if /i "%1" == "jitstress"             (set COMPlus_JitStress=%2&shift&shift&goto Arg_Loop)
77 if /i "%1" == "jitstressregs"         (set COMPlus_JitStressRegs=%2&shift&shift&goto Arg_Loop)
78 if /i "%1" == "jitminopts"            (set COMPlus_JITMinOpts=1&shift&shift&goto Arg_Loop)
79 if /i "%1" == "jitforcerelocs"        (set COMPlus_ForceRelocs=1&shift&shift&goto Arg_Loop)
80 if /i "%1" == "jitdisasm"             (set __JitDisasm=1&shift&goto Arg_Loop)
81 if /i "%1" == "GenerateLayoutOnly"    (set __GenerateLayoutOnly=1&shift&goto Arg_Loop)
82 if /i "%1" == "PerfTests"             (set __PerfTests=true&shift&goto Arg_Loop)
83 if /i "%1" == "runcrossgentests"      (set RunCrossGen=true&shift&goto Arg_Loop)
84 if /i "%1" == "link"                  (set DoLink=true&set ILLINK=%2&shift&shift&goto Arg_Loop)
85
86 REM change it to COMPlus_GCStress when we stop using xunit harness
87 if /i "%1" == "gcstresslevel"         (set __GCSTRESSLEVEL=%2&set __TestTimeout=1800000&shift&shift&goto Arg_Loop)
88 if /i "%1" == "collectdumps"          (set __CollectDumps=true&shift&goto Arg_Loop)
89
90 if /i not "%1" == "msbuildargs" goto SkipMsbuildArgs
91 :: All the rest of the args will be collected and passed directly to msbuild.
92 :CollectMsbuildArgs
93 shift
94 if "%1"=="" goto ArgsDone
95 set __msbuildExtraArgs=%__msbuildExtraArgs% %1
96 goto CollectMsbuildArgs
97 :SkipMsbuildArgs
98
99 set CORE_ROOT=%1
100 echo %__MsgPrefix%CORE_ROOT is initially set to: "%CORE_ROOT%"
101 shift 
102 :ArgsDone
103
104 :: Set the remaining variables based upon the determined configuration
105 set "__BinDir=%__RootBinDir%\Product\%__BuildOS%.%__BuildArch%.%__BuildType%"
106 set "__TestWorkingDir=%__RootBinDir%\tests\%__BuildOS%.%__BuildArch%.%__BuildType%"
107
108 :: Default global test environment variables
109 :: REVIEW: are these ever expected to be defined on entry to this script? Why? By whom?
110 :: REVIEW: XunitTestReportDirBase is not used in this script. Who needs to have it set?
111 if not defined XunitTestBinBase       set  XunitTestBinBase=%__TestWorkingDir%
112 if not defined XunitTestReportDirBase set  XunitTestReportDirBase=%XunitTestBinBase%\Reports\
113
114 if not exist %__LogsDir% md %__LogsDir%
115
116 set _msbuildexe=
117 if /i "%__VSVersion%" == "vs2017" (
118   set "__VSToolsRoot=%VS150COMNTOOLS%"
119   set "__VCToolsRoot=%VS150COMNTOOLS%\..\..\VC\Auxiliary\Build"
120
121   set _msbuildexe="%VS150COMNTOOLS%\..\..\MSBuild\15.0\Bin\MSBuild.exe"
122 ) else if /i "%__VSVersion%" == "vs2015" (
123   set "__VSToolsRoot=%VS140COMNTOOLS%"
124   set "__VCToolsRoot=%VS140COMNTOOLS%\..\..\VC"
125
126   set _msbuildexe="%ProgramFiles(x86)%\MSBuild\14.0\Bin\MSBuild.exe"
127   if not exist !_msbuildexe! set _msbuildexe="%ProgramFiles%\MSBuild\14.0\Bin\MSBuild.exe"
128 )
129
130 :: Does VS really exist?
131 if not exist "%__VSToolsRoot%\..\IDE\devenv.exe"      goto NoVS
132 if not exist "%__VCToolsRoot%\vcvarsall.bat"          goto NoVS
133 if not exist "%__VSToolsRoot%\VsDevCmd.bat"           goto NoVS
134
135 :: Does MSBuild really exist?
136 if not exist %_msbuildexe% echo Error: Could not find MSBuild.exe.  Please see https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md for build instructions. && exit /b 1
137
138 :: Set the environment for the  build- VS cmd prompt
139 echo %__MsgPrefix%Using environment: "%__VSToolsRoot%\VsDevCmd.bat"
140 call                                 "%__VSToolsRoot%\VsDevCmd.bat"
141
142 if not defined VSINSTALLDIR (
143     echo %__MsgPrefix%Error: runtest.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.
144     exit /b 1
145 )
146
147 :: Note: We've disabled node reuse because it causes file locking issues.
148 ::       The issue is that we extend the build with our own targets which
149 ::       means that that rebuilding cannot successfully delete the task
150 ::       assembly. 
151 set __msbuildCommonArgs=/nologo /nodeReuse:false %__msbuildExtraArgs% /p:Platform=%__MSBuildBuildArch%
152
153 if not defined __Sequential (
154     set __msbuildCommonArgs=%__msbuildCommonArgs% /maxcpucount
155 ) else (
156     set __msbuildCommonArgs=%__msbuildCommonArgs% /p:ParallelRun=false
157 )
158
159 if defined __AgainstPackages (
160     set __msbuildCommonArgs=%__msbuildCommonArgs% /p:BuildTestsAgainstPackages=true
161 )
162
163 if defined DoLink (
164     set __msbuildCommonArgs=%__msbuildCommonArgs% /p:RunTestsViaIllink=true
165 )
166
167 REM Prepare the Test Drop
168 REM Cleans any NI from the last run
169 powershell "Get-ChildItem -path %__TestWorkingDir% -Include '*.ni.*' -Recurse -Force | Remove-Item -force"
170 REM Cleans up any lock folder used for synchronization from last run
171 powershell "Get-ChildItem -path %__TestWorkingDir% -Include 'lock' -Recurse -Force |  where {$_.Attributes -eq 'Directory'}| Remove-Item -force -Recurse"
172
173 if defined CORE_ROOT goto SkipCoreRootSetup
174
175 set "CORE_ROOT=%XunitTestBinBase%\Tests\Core_Root"
176 echo %__MsgPrefix%Using Default CORE_ROOT as %CORE_ROOT%
177 echo %__MsgPrefix%Copying Built binaries from %__BinDir% to %CORE_ROOT%
178 if exist "%CORE_ROOT%" rd /s /q "%CORE_ROOT%"
179 md "%CORE_ROOT%"
180 xcopy /s "%__BinDir%" "%CORE_ROOT%"
181
182 :SkipCoreRootSetup
183
184
185 if defined __TestEnv (if not exist %__TestEnv% echo %__MsgPrefix%Error: Test Environment script %__TestEnv% not found && exit /b 1)
186
187 REM These log files are created automatically by the test run process. Q: what do they depend on being set?
188 set __TestRunHtmlLog=%__LogsDir%\TestRun_%__BuildOS%__%__BuildArch%__%__BuildType%.html
189 set __TestRunXmlLog=%__LogsDir%\TestRun_%__BuildOS%__%__BuildArch%__%__BuildType%.xml
190
191
192 if "%__PerfTests%"=="true" goto RunPerfTests
193
194 call :ResolveDependecies
195
196 if not defined __DoCrossgen goto :SkipPrecompileFX
197 call :PrecompileFX
198
199 :SkipPrecompileFX
200
201 if  defined __GenerateLayoutOnly (
202     exit /b 0
203 )
204
205 if not exist %CORE_ROOT%\coreclr.dll (
206     echo %__MsgPrefix%Error: Ensure you have done a successful build of the Product and %CORE_ROOT% contains runtime binaries.
207     exit /b 1
208 )
209
210 ::Check if the test Binaries are built
211 if not exist %XunitTestBinBase% (
212     echo %__MsgPrefix%Error: Ensure the Test Binaries are built and are present at %XunitTestBinBase%.
213     echo %__MsgPrefix%Run "buildtest.cmd %__BuildArch% %__BuildType%" to build the tests first.
214     exit /b 1
215 )
216
217 if "%__CollectDumps%"=="true" (
218     :: Install dumpling
219     set "__DumplingHelperPath=%__ProjectDir%\..\Tools\DumplingHelper.py"
220     python "!__DumplingHelperPath!" install_dumpling
221
222     :: Create the crash dump folder if necessary
223     set "__CrashDumpFolder=%tmp%\CoreCLRTestCrashDumps"
224     if not exist "!__CrashDumpFolder!" (
225         mkdir "!__CrashDumpFolder!"
226     )
227
228     :: Grab the current time before execution begins. This will be used to determine which crash dumps
229     :: will be uploaded.
230     for /f "delims=" %%a in ('python !__DumplingHelperPath! get_timestamp') do @set __StartTime=%%a
231 )
232
233 echo %__MsgPrefix%CORE_ROOT that will be used is: %CORE_ROOT%
234 echo %__MsgPrefix%Starting the test run ...
235
236 del %CORE_ROOT%\mscorlib.ni.dll
237
238 set __BuildLogRootName=TestRunResults
239 call :msbuild "%__ProjectFilesDir%\runtest.proj" /p:Runtests=true /clp:showcommandline
240 set __errorlevel=%errorlevel%
241
242 if "%__CollectDumps%"=="true" (
243     python "%__DumplingHelperPath%" collect_dump %errorlevel% "%__CrashDumpFolder%" %__StartTime% "CoreCLR_Tests"
244 )
245
246 if %__errorlevel% GEQ 1 (
247     echo Test Run failed. Refer to the following:
248     echo     Html report: %__TestRunHtmlLog%
249     exit /b 1
250 )
251
252 if not defined __PerfTests goto :SkipRunPerfTests
253
254 :RunPerfTests 
255 echo %__MsgPrefix%CORE_ROOT that will be used is: %CORE_ROOT%  
256 echo %__MsgPrefix%Starting the test run ...  
257
258 set __BuildLogRootName=PerfTestRunResults  
259 echo Running perf tests  
260 call :msbuild "%__ProjectFilesDir%\runtest.proj" /t:RunPerfTests /clp:showcommandline  
261
262 if errorlevel 1 (  
263    echo Test Run failed. Refer to the following:  
264    echo     Html report: %__TestRunHtmlLog%  
265 )  
266
267 :SkipRunPerfTests
268
269 REM =========================================================================================
270 REM ===
271 REM === All tests complete!
272 REM ===
273 REM =========================================================================================
274
275 echo %__MsgPrefix%Test run successful. Refer to the log files for details:
276 echo     %__TestRunHtmlLog%
277 echo     %__TestRunXmlLog%
278 exit /b 0
279
280 REM =========================================================================================
281 REM ===
282 REM === Helper routines
283 REM ===
284 REM =========================================================================================
285
286 REM Compile the managed assemblies in Core_ROOT before running the tests
287 :PrecompileAssembly
288
289 if defined __JitDisasm goto :jitdisasm
290
291 REM Skip mscorlib since it is already precompiled.
292 if /I "%3" == "mscorlib.dll" exit /b 0
293 if /I "%3" == "mscorlib.ni.dll" exit /b 0
294
295 "%1\crossgen.exe" /Platform_Assemblies_Paths "%CORE_ROOT%" "%2" >nul 2>nul
296 set /a __exitCode = %errorlevel%
297 if "%__exitCode%" == "-2146230517" (
298     echo %2 is not a managed assembly.
299     exit /b 0
300 )
301
302 if %__exitCode% neq 0 (
303     echo Unable to precompile %2
304     exit /b 0
305 )
306     
307 echo Successfully precompiled %2
308 exit /b 0
309
310 :jitdisasm
311
312 if /I "%3" == "mscorlib.ni.dll" exit /b 0
313
314 echo "%1\corerun" "%1\jit-dasm.dll" --crossgen %1\crossgen.exe --platform %CORE_ROOT% --output %__TestWorkingDir%\dasm "%2"
315 "%1\corerun" "%1\jit-dasm.dll" --crossgen %1\crossgen.exe --platform %CORE_ROOT% --output %__TestWorkingDir%\dasm "%2"
316 set /a __exitCode = %errorlevel%
317
318 if "%__exitCode%" == "-2146230517" (
319     echo %2 is not a managed assembly.
320     exit /b 0
321 )
322
323 if %__exitCode% neq 0 (
324     echo Unable to precompile %2
325     exit /b 0
326 )
327
328 echo Successfully precompiled and generated dasm for %2
329 exit /b 0
330
331 :PrecompileFX
332 for %%F in (%CORE_ROOT%\*.dll) do call :PrecompileAssembly "%CORE_ROOT%" "%%F" %%~nF%%~xF
333 exit /b 0
334
335 :msbuild
336 @REM Subroutine to invoke msbuild. All arguments are passed to msbuild. The first argument should be the
337 @REM .proj file to invoke.
338 @REM
339 @REM On entry, __BuildLogRootName must be set to a file name prefix for the generated log file.
340 @REM All the "standard" environment variables that aren't expected to change per invocation must also be set,
341 @REM like __msbuildCommonArgs.
342 @REM
343 @REM The build log files will be overwritten, not appended to.
344
345 echo %__MsgPrefix%Invoking msbuild
346
347 set "__BuildLog=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.log"
348 set "__BuildWrn=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.wrn"
349 set "__BuildErr=%__LogsDir%\%__BuildLogRootName%_%__BuildOS%__%__BuildArch%__%__BuildType%.err"
350
351 set __msbuildLogArgs=^
352 /fileloggerparameters:Verbosity=normal;LogFile="%__BuildLog%";Append ^
353 /fileloggerparameters1:WarningsOnly;LogFile="%__BuildWrn%" ^
354 /fileloggerparameters2:ErrorsOnly;LogFile="%__BuildErr%" ^
355 /consoleloggerparameters:Summary ^
356 /verbosity:minimal
357
358 set __msbuildArgs=%* %__msbuildCommonArgs% %__msbuildLogArgs%
359
360 @REM The next line will overwrite the existing log file, if any.
361 echo %_msbuildexe% %__msbuildArgs%
362 echo Invoking: %_msbuildexe% %__msbuildArgs% > "%__BuildLog%"
363
364 %_msbuildexe% %__msbuildArgs%
365 if errorlevel 1 (
366     echo %__MsgPrefix%Error: msbuild failed. Refer to the log files for details:
367     echo     %__BuildLog%
368     echo     %__BuildWrn%
369     echo     %__BuildErr%
370     exit /b 1
371 )
372
373 exit /b 0
374
375 :ResolveDependecies:
376
377 if "%CORE_ROOT%" == "" (
378     echo %__MsgPrefix%Error: Ensure you have done a successful build of the Product and Run - runtest BuildArch BuildType {path to product binaries}.
379     exit /b 1
380 )
381
382 :: Long GC tests take about 10 minutes per test on average, so
383 :: they often bump up against the default 10 minute timeout.
384 :: 20 minutes is more than enough time for a test to complete successfully.
385 if defined __LongGCTests (
386     echo Running Long GC tests, extending timeout to 20 minutes
387     set __TestTimeout=1200000
388     set RunningLongGCTests=1
389 )
390
391 :: GCSimulator tests can take up to an hour to complete. They are run twice a week in the
392 :: CI, so it's fine if they take a long time.
393 if defined __GCSimulatorTests (
394     echo Running GCSimulator tests, extending timeout to one hour
395     set __TestTimeout=3600000
396     set RunningGCSimulatorTests=1
397 )
398
399 if defined __JitDisasm (
400     if defined __DoCrossgen (
401         echo Running jit disasm on framework and test assemblies
402     )
403     if not defined __DoCrossgen (
404        echo Running jit disasm on test assemblies only
405     )
406     set RunningJitDisasm=1
407 )
408
409 set __BuildLogRootName=Tests_GenerateRuntimeLayout
410 call :msbuild "%__ProjectFilesDir%\runtest.proj" /p:GenerateRuntimeLayout=true 
411 if errorlevel 1 (
412     echo Test Dependency Resolution Failed
413     exit /b 1
414 )
415 echo %__MsgPrefix%Created the runtime layout with all dependencies in %CORE_ROOT%
416
417 exit /b 0
418
419
420
421 :Usage
422 echo.
423 echo Usage:
424 echo   %0 BuildArch BuildType [TestEnv TEST_ENV_SCRIPT] [VSVersion] CORE_ROOT
425 echo where:
426 echo.
427 echo./? -? /h -h /help -help: view this message.
428 echo BuildArch- Optional parameter - x64 or x86 ^(default: x64^).
429 echo BuildType- Optional parameter - Debug, Release, or Checked ^(default: Debug^).
430 echo TestEnv- Optional parameter - this will run a custom script to set custom test environment settings.
431 echo VSVersion- Optional parameter - VS2015 or VS2017 ^(default: VS2017^)
432 echo AgainstPackages - Optional parameter - this indicates that we are running tests that were built against packages
433 echo GenerateLayoutOnly - If specified will not run the tests and will only create the Runtime Dependency Layout
434 echo link "ILlink"      - Runs the tests after linking via ILlink
435 echo RunCrossgenTests   - Runs ReadytoRun tests
436 echo jitstress n        - Runs the tests with COMPlus_JitStress=n
437 echo jitstressregs n    - Runs the tests with COMPlus_JitStressRegs=n
438 echo jitminopts         - Runs the tests with COMPlus_JITMinOpts=1
439 echo jitforcerelocs     - Runs the tests with COMPlus_ForceRelocs=1
440 echo jitdisasm          - Runs jit-dasm on the tests
441 echo gcstresslevel n    - Runs the tests with COMPlus_GCStress=n
442 echo     0: None                                1: GC on all allocs and 'easy' places
443 echo     2: GC on transitions to preemptive GC  4: GC on every allowable JITed instr
444 echo     8: GC on every allowable NGEN instr   16: GC only on a unique stack trace
445 echo CORE_ROOT The path to the runtime  
446 exit /b 1
447
448 :NoVS
449 echo Visual Studio 2015 or 2017 (Community is free) is a prerequisite to build this repository.
450 echo See: https://github.com/dotnet/coreclr/blob/master/Documentation/project-docs/developer-guide.md#prerequisites
451 exit /b 1