summaryrefslogtreecommitdiff
path: root/tests/src/CLRTest.Execute.Batch.targets
blob: db2a17425dfadd6da5cf32512c427ef45043e0c1 (plain)
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
<!--
***********************************************************************************************
CLRTest.Execute.Batch.targets

WARNING:  DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
          created a backup copy.  Incorrect changes to this file will make it
          impossible to load or build your projects from the command-line or the IDE.

This file contains the logic for providing Execution Script generation.

WARNING:   When setting properties based on their current state (for example:
           <Foo Condition="'$(Foo)'==''>Bar</Foo>).  Be very careful.  Another script generation
           target might be trying to do the same thing.  It's better to avoid this by instead setting a new property.
           
           Additionally, be careful with itemgroups.  Include will propagate outside of the target too!

***********************************************************************************************
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target
    Name="GetIlasmRoundTripBatchScript"
    Returns="$(IlasmRoundTripBatchScript)">
    <PropertyGroup>
      <InputAssemblyName Condition="'$(CLRTestKind)' == 'RunOnly'">$([MSBuild]::MakeRelative($(OutputPath), $(_CLRTestToRunFileFullPath)))</InputAssemblyName>
      <InputAssemblyName Condition="'$(CLRTestKind)' == 'BuildAndRun'">$(MSBuildProjectName).exe</InputAssemblyName>
      <DisassemblyName>$(MSBuildProjectName).dasm.il</DisassemblyName>
      <TargetAssemblyName>$(MSBuildProjectName).asm.exe</TargetAssemblyName>

       <!-- https://github.com/dotnet/coreclr/issues/2481. Delete /raweh to ildasm once it is resolved.-->
      <IlasmRoundTripBatchScript Condition="'$(IlasmRoundTrip)'=='true'">
      <![CDATA[
REM Disable Ilasm round-tripping for Linker tests.
REM Todo: Ilasm round-trip on linked binaries.
IF NOT DEFINED DoLink ( 
  ECHO %CORE_ROOT%\ildasm.exe /raweh /out=$(DisassemblyName) $(InputAssemblyName)
  %CORE_ROOT%\ildasm.exe /raweh /out=$(DisassemblyName) $(InputAssemblyName)
  IF NOT "!ERRORLEVEL!"=="0" (
    ECHO EXECUTION OF ILDASM - FAILED !ERRORLEVEL!
    Exit /b 1
  )
  ECHO %CORE_ROOT%\ilasm.exe /output=$(TargetAssemblyName) $(_IlasmSwitches) $(DisassemblyName)
  %CORE_ROOT%\ilasm.exe /output=$(TargetAssemblyName) $(_IlasmSwitches) $(DisassemblyName)
  IF NOT "!ERRORLEVEL!"=="0" (
    ECHO EXECUTION OF ILASM - FAILED !ERRORLEVEL!
    Exit /b 1
  )
)
      ]]>
      </IlasmRoundTripBatchScript>
    </PropertyGroup>
  </Target>

  <!-- This is here because of this bug: http://blogs.msdn.com/b/msbuild/archive/2006/01/03/508629.aspx-->
  <Target Name="FetchExternalProperties">
    <!--Call GetExecuteCmdFullPath to get ToRunProject cmd file Path  -->
    <MSBuild Projects="$(CLRTestProjectToRun)" 
             Targets="GetExecuteCmdFullPath"
             Properties="GenerateRunScript=True"
             Condition="'$(_CLRTestNeedsProjectToRun)' == 'True'">
      <Output TaskParameter="TargetOutputs" PropertyName="_CLRTestToRunFileFullPath"/>
    </MSBuild>
  </Target>
  
  <!--
    Target: GetExecuteCmdFullPath
    Return Executed Cmd Relative Full Path
    We can use this target get its toRun Project's Cmd Full Path to avoid hard-code
  -->
  <Target
    Name="GetExecuteCmdFullPath"
    Returns="$(ExecuteCmdFullPath)">
    <PropertyGroup Condition="$(GenerateRunScript)">
        <ExecuteCmdFullPath>$(OutputPath)$(MSBuildProjectName).cmd</ExecuteCmdFullPath>
    </PropertyGroup>
    <!-- In order to avoid the overhead of calling MSBuild (as it may result in chains of MSBuild calls) I am assuming the extension in this circumstance. -->
    <PropertyGroup Condition="$(CLRTestProjectToRun) != '' AND $(GenerateRunScript)">
      <ExecuteCmdFullPath>$(OutputPath)$([System.IO.Path]::GetFilenameWithoutExtension(&quot;$(CLRTestProjectToRun)&quot;)).exe</ExecuteCmdFullPath>
    </PropertyGroup>
    <PropertyGroup Condition="!$(GenerateRunScript)">
        <ExecuteCmdFullPath>$(OutputPath)$(MSBuildProjectName).$(OutputType.ToLower())</ExecuteCmdFullPath>
    </PropertyGroup>
  </Target>

  <!--
  *******************************************************************************************
  TARGET: GenerateExecutionScriptInternal
  
  For tests that "run" we will generate an execution script that wraps any arguments or other
  goo.  This allows generated .lst files to be very simple and reusable to invoke any "stage"
  of test execution.
  
  Notice this is hooked up to run after targets that generate the stores that are marked with GenerateScripts metadata.
  Note also that this means it will run after the first of such targets.
  -->
  <Target Name="GenerateBatchExecutionScript"
    Inputs="$(MSBuildProjectFullPath)"
    Outputs="$(OutputPath)\$(MSBuildProjectName).cmd"
    DependsOnTargets="FetchExternalProperties;$(BatchScriptSnippetGen);GetJitDisasmBatchScript;GetIlasmRoundTripBatchScript">

    <Message Text="Project depends on $(_CLRTestToRunFileFullPath)." Condition="'$(_CLRTestNeedsProjectToRun)' == 'True'" />

    <PropertyGroup>
      <BatchCLRTestEnvironmentCompatibilityCheck Condition="'$(GCStressIncompatible)' == 'true'"><![CDATA[
$(BatchCLRTestEnvironmentCompatibilityCheck)
IF NOT "%COMPlus_GCStress%"=="" (
  ECHO SKIPPING EXECUTION BECAUSE COMPlus_GCStress IS SET
  Exit /b 0
)
      ]]></BatchCLRTestEnvironmentCompatibilityCheck>
      <BatchCLRTestEnvironmentCompatibilityCheck Condition="'$(JitOptimizationSensitive)' == 'true'"><![CDATA[
$(BatchCLRTestEnvironmentCompatibilityCheck)
IF "%COMPlus_JitStress%"=="" IF "%COMPlus_JitStressRegs%"=="" IF "%COMPlus_JITMinOpts%"=="" goto :Compatible1
  ECHO SKIPPING EXECUTION BECAUSE ONE OR MORE OF (COMPlus_JitStress, COMPlus_JitStressRegs, COMPlus_JITMinOpts) IS SET
  Exit /b 0
:Compatible1
      ]]></BatchCLRTestEnvironmentCompatibilityCheck>
      <BatchCLRTestEnvironmentCompatibilityCheck Condition="'$(HeapVerifyIncompatible)' == 'true'"><![CDATA[
$(BatchCLRTestEnvironmentCompatibilityCheck)
IF NOT "%COMPlus_HeapVerify%"=="" (
  ECHO SKIPPING EXECUTION BECAUSE COMPlus_HeapVerify IS SET
  Exit /b 0
)
      ]]></BatchCLRTestEnvironmentCompatibilityCheck>
      
      <BatchCLRTestExitCodePrep Condition="$(_CLRTestNeedsToRun)">
        <![CDATA[
if not defined CLRTestExpectedExitCode (set CLRTestExpectedExitCode=$(CLRTestExitCode))
ECHO BEGIN EXECUTION
      ]]>
      </BatchCLRTestExitCodePrep>

      <BatchCLRTestArgPrep Condition=" '$(CLRTestExecutionArguments)'!='' "><![CDATA[
if not defined CLRTestExecutionArguments (set CLRTestExecutionArguments=$(CLRTestExecutionArguments) )
      ]]></BatchCLRTestArgPrep>
    
      <!-- By default, be prepared to do a full check -->
      <BatchCLRTestExitCodeCheck><![CDATA[
ECHO Expected: %CLRTestExpectedExitCode%
ECHO Actual: %CLRTestExitCode%
IF NOT "%CLRTestExitCode%"=="%CLRTestExpectedExitCode%" (
  ECHO END EXECUTION - FAILED
  ECHO FAILED
  Exit /b 1
) ELSE (
  ECHO END EXECUTION - PASSED
  ECHO PASSED
  Exit /b 0
)

:TakeLock
md %lockFolder%
IF NOT "!ERRORLEVEL!"=="0" (
timeout /t 10 /nobreak
goto :TakeLock
)
Exit /b 2


:ReleaseLock
if exist %lockFolder% rd /s /q %lockFolder%
Exit /b 0
      ]]></BatchCLRTestExitCodeCheck>
    </PropertyGroup>
  
    <ItemGroup Condition="$(_CLRTestNeedsToRun)">
      <Clean Include="$(OutputPath)\$(MSBuildProjectName).cmd"/>

      <BatchCLRTestExecutionScriptArgument Include="debug">
        <HasParam>true</HasParam>
        <ParamName>debuggerFullPath</ParamName>
        <Command><![CDATA[
    IF EXIST "%2" (
        set _DebuggerFullPath=%2
    ) ELSE (
        ECHO The Debugger FullPath "%2" doesn't exist
        GOTO :USAGE
    )
        ]]></Command>
        <Description>Run testcases under debugger.</Description>
      </BatchCLRTestExecutionScriptArgument>

      <BatchCLRTestExecutionScriptArgument Include="env">
        <HasParam>true</HasParam>
        <ParamName>envScriptFullPath</ParamName>
        <Command><![CDATA[
    IF EXIST "%2" (
        set __TestEnv=%2
    ) ELSE (
        ECHO The environment script "%2" does not exist
        GOTO :USAGE
    )
        ]]></Command>
        <Description>Run the specified script to set environment variables before running the test.</Description>
      </BatchCLRTestExecutionScriptArgument>

      <BatchCLRTestExecutionScriptArgument Include="coreroot">
        <HasParam>true</HasParam>
        <ParamName>CoreRootFullPath</ParamName>
        <Command><![CDATA[
    set CORE_ROOT=%2
        ]]></Command>
        <Description>Set CORE_ROOT to the specified value before running the test.</Description>
      </BatchCLRTestExecutionScriptArgument>
    </ItemGroup>

      <PropertyGroup>
          <ReflectionRootsXml>$(MSBuildProjectName).reflect.xml</ReflectionRootsXml>
          <BatchLinkerTestLaunchCmds><![CDATA[
REM Linker commands

set LinkBin=__Link
set Assemblies=-a System.Private.CoreLib

IF defined DoLink ( 
    IF NOT EXIST !ILLINK! (
      ECHO ILLink executable [%ILLINK%] Invalid
      Exit /b 1
    )
    
    REM Clean up old Linked binaries, if any
    IF EXIST %LinkBin% rmdir /s /q %LinkBin%
    
    REM Remove Native images, since the goal is to run from Linked binaries
    del /q /f *.ni.* 2> nul

    REM Use hints for reflection roots, if provided in $(ReflectionRootsXml)
    IF EXIST $(ReflectionRootsXml) set ReflectionRoots=-x $(ReflectionRootsXml)

    REM Include all .exe files in this directory as entry points (some tests had multiple .exe file modules)
    FOR /F "delims=" %%E IN ('dir /b *.exe *.dll') DO SET Assemblies=!Assemblies! -a %%~nE

    REM Run dotnet-linker
    REM Run the Linker such that all assemblies except System.Private.Corlib.dll are linked
    REM Debug symbol generation needs some fixes, and is currently omitted.
    REM Once this is fixed, add -b true option.
    ECHO %ILLINK% -out %LinkBin% -d %CORE_ROOT% -c link -l none -t !Assemblies! !ReflectionRoots!
    %ILLINK% -out %LinkBin% -d %CORE_ROOT% -c link -l none -t !Assemblies! !ReflectionRoots!
    IF NOT "!ERRORLEVEL!"=="0" (
      ECHO ILLINK FAILED !ERRORLEVEL!
      IF NOT defined KeepLinkedBinaries (
          IF EXIST %LinkBin% rmdir /s /q %LinkBin%
      )
      Exit /b 1
    )

    REM Copy CORECLR native binaries to %LinkBin%, so that we can run the test based on that directory
    copy %CORE_ROOT%\clrjit.dll %LinkBin% > nul 2> nul
    copy %CORE_ROOT%\coreclr.dll %LinkBin% > nul 2> nul
    copy %CORE_ROOT%\mscorrc.dll %LinkBin% > nul 2> nul
    copy %CORE_ROOT%\CoreRun.exe %LinkBin% > nul 2> nul

    REM Copy some files that may be arguments
    copy *.txt %LinkBin% > nul 2> nul

    set ExePath=%LinkBin%\$(InputAssemblyName)
    set CORE_ROOT=%~dp0%LinkBin%
)
]]>
          </BatchLinkerTestLaunchCmds>
          <BatchLinkerTestCleanupCmds>
              <![CDATA[
REM Clean up the LinkBin directories after test execution.
REM Otherwise, RunTests may run out of disk space.

if defined DoLink (
    if not defined KeepLinkedBinaries (
        IF EXIST %LinkBin% rmdir /s /q %LinkBin%
    )
)              
]]>
          </BatchLinkerTestCleanupCmds>
      </PropertyGroup>      
      <PropertyGroup>      
      <_CLRTestRunFile Condition="'$(CLRTestIsHosted)'=='true'">"%CORE_ROOT%\corerun.exe"</_CLRTestRunFile>
      <BatchCLRTestLaunchCmds><![CDATA[
IF NOT "%CLRCustomTestLauncher%"=="" (
  set LAUNCHER=call %CLRCustomTestLauncher% %~dp0
) ELSE (
  set LAUNCHER=%_DebuggerFullPath% $(_CLRTestRunFile)
)
      ]]></BatchCLRTestLaunchCmds>
      <BatchCLRTestLaunchCmds Condition=" '$(IlasmRoundTrip)'=='true' "><![CDATA[
  $(BatchCLRTestLaunchCmds)
IF NOT DEFINED DoLink ( 
  ECHO %LAUNCHER% $(TargetAssemblyName) %CLRTestExecutionArguments% %Host_Args%
  %LAUNCHER% $(TargetAssemblyName) %CLRTestExecutionArguments% %Host_Args%

  IF NOT "!ERRORLEVEL!"=="%CLRTestExpectedExitCode%" (
    ECHO END EXECUTION OF IL{D}ASM BINARY - FAILED !ERRORLEVEL! vs %CLRTestExpectedExitCode%
    ECHO FAILED
    Exit /b 1
  )
)
      ]]></BatchCLRTestLaunchCmds>
      <BatchCLRTestLaunchCmds Condition="'$(CLRTestKind)' == 'BuildAndRun'">
    <![CDATA[
set ExePath=$(InputAssemblyName)
$(BatchLinkerTestLaunchCmds)
$(BatchCLRTestLaunchCmds)

ECHO %LAUNCHER% %ExePath% %CLRTestExecutionArguments%
%LAUNCHER% %ExePath% %CLRTestExecutionArguments%
set CLRTestExitCode=!ERRORLEVEL!
$(BatchLinkerTestCleanupCmds)
      ]]></BatchCLRTestLaunchCmds>

      <BatchCLRTestLaunchCmds Condition="'$(CLRTestKind)' == 'RunOnly'"><![CDATA[
$(BatchCLRTestLaunchCmds)
ECHO  cmd /c $(InputAssemblyName)
cmd /c $(InputAssemblyName)
set CLRTestExpectedExitCode=0
set CLRTestExitCode=!ERRORLEVEL!
      ]]></BatchCLRTestLaunchCmds>
    </PropertyGroup>
    <PropertyGroup>
      <BatchEnvironmentVariables>
@(CLRTestBatchEnvironmentVariable -> '%(Identity)', '%0d%0a')
      </BatchEnvironmentVariables>
    </PropertyGroup>
    
    <Message Text="MSBuildProjectDirectory:$(MSBuildProjectDirectory)" />
    <Message Text="_CLRTestToRunFileFullPath:$(_CLRTestToRunFileFullPath)"/>
    <Message Text="_CLRTestRunFile:$(_CLRTestRunFile)" />
    
    <ItemGroup>
      <_RequiredProperties Include="_CLRTestRunFile">
        <Value>$(_CLRTestRunFile)</Value>
      </_RequiredProperties>
    </ItemGroup>
    
    <!-- Raise an error if any value in _RequiredProperties is missing  -->
    <Error Condition=" '%(_RequiredProperties.Value)'=='' "
      Text="Missing required test property [%(_RequiredProperties.Identity)]. Something isn't plumbed through correctly.  Contact $(_CLRTestBuildSystemOwner)." />
      <!-- TODO: this is weird.  Consider eliminating it. -->
    <GenerateParamList ArgumentItems="@(BatchCLRTestExecutionScriptArgument)">
      <Output TaskParameter="ParamList" PropertyName="_CLRTestParamList"/>
    </GenerateParamList>
    
    <PropertyGroup>
      <!--
      This generates the script portion to parse all of the command line arguments.
      The Command metadata for BatchCLRTestExecutionScriptArgument items is executed
      when the argument is found.
      -->
      <BatchCLRTestArgPrep><![CDATA[
REM Parse Command Line
:NextArg

IF /I '%1' == '-?'     GOTO :USAGE
IF /I '%1' == '/?'     GOTO :USAGE
IF /I '%1' == '-h'     GOTO :USAGE
IF /I '%1' == '/h'     GOTO :USAGE

@(BatchCLRTestExecutionScriptArgument -> 'set cond=0
IF /I [%1] == [-%(Identity)] set cond=1
IF /I [%1] == [/%(Identity)] set cond=1
IF %cond% EQU 1 (
%(Command)
shift
IF /I [%(HasParam)] == [true] shift
goto NextArg
)','
')

if NOT "%1" == "" (
set CLRTestExecutionArguments=%*
goto :ArgsDone
)

goto ArgsDone

:USAGE
ECHO.Usage
ECHO %0 $(_CLRTestParamList)
ECHO.
ECHO                         - OPTIONS -
@(BatchCLRTestExecutionScriptArgument -> 'ECHO -%(Identity) %(ParamName)
ECHO      %(Description)', '
')
Exit /b 1

:ArgsDone
$(BatchCLRTestArgPrep)
      ]]></BatchCLRTestArgPrep>
      <!-- NOTE! semicolons must be escaped with %3B boooo -->
      <_CLRTestExecutionScriptText>
  <![CDATA[
@ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
set "lockFolder=%~dp0\lock"
pushd %~dp0

$(BatchCLRTestArgPrep)
$(BatchCLRTestExitCodePrep)

REM The __TestEnv variable may be used to specify something to run before the test.
IF NOT "%__TestEnv%"=="" call %__TestEnv%

REM Environment Variables
$(BatchEnvironmentVariables)

$(BatchCLRTestEnvironmentCompatibilityCheck)

$(JitDisasmBatchScript)

REM IlasmRoundTrip Script
$(IlasmRoundTripBatchScript)

REM Precommands
$(CLRTestBatchPreCommands)
REM Launch
$(BatchCLRTestLaunchCmds)
REM PostCommands
$(CLRTestBatchPostCommands)
$(BatchCLRTestExitCodeCheck)
      ]]></_CLRTestExecutionScriptText>
    </PropertyGroup>
    <!-- Write the file -->
    <WriteLinesToFile
      File="$(OutputPath)\$(MSBuildProjectName).cmd"
      Lines="$(_CLRTestExecutionScriptText)"
      Overwrite="true" />
  </Target>
  
</Project>