summaryrefslogtreecommitdiff
path: root/tests/src/CLRTest.Execute.Bash.targets
blob: f95408f5128629194101f6049865d7bc332a214e (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
<!--
***********************************************************************************************
CLRTest.Execute.Bash.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">

  <!-- This is here because of this bug: http://blogs.msdn.com/b/msbuild/archive/2006/01/03/508629.aspx-->
  <Target Name="FetchExternalPropertiesForXpalt">
    <!--Call GetExecuteShFullPath to get ToRunProject cmd file Path  -->
    <MSBuild Projects="$(CLRTestProjectToRun)" 
             Targets="GetExecuteShFullPath"
             Properties="GenerateRunScript=True"
             Condition="'$(_CLRTestNeedsProjectToRun)' == 'True'">
      <Output TaskParameter="TargetOutputs" PropertyName="_CLRTestToRunFileFullPath"/>
    </MSBuild>
  </Target>
  
  <!--
    Target: GetExecuteShFullPath
    Return Executed Sh Relative Full Path
    We can use this target get its toRun Project's Sh Full Path to avoid hard-code
  -->
  <Target
    Name="GetExecuteShFullPath"
    Returns="$(ExecuteShFullPath)">
    <PropertyGroup Condition="$(GenerateRunScript)">
      <ExecuteShFullPath>$(OutputPath)$(MSBuildProjectName).sh</ExecuteShFullPath>
    </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)">
      <ExecuteShFullPath>$(OutputPath)$([System.IO.Path]::GetFilenameWithoutExtension(&quot;$(CLRTestProjectToRun)&quot;)).exe</ExecuteShFullPath>
    </PropertyGroup>
    <PropertyGroup Condition="!$(GenerateRunScript)">
      <ExecuteShFullPath>$(OutputPath)$(MSBuildProjectName).$(OutputType.ToLower())</ExecuteShFullPath>
    </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="GenerateBashExecutionScript"
    Inputs="$(MSBuildProjectFullPath)"
    Outputs="$(OutputPath)\$(MSBuildProjectName).sh"
    DependsOnTargets="FetchExternalPropertiesForXpalt;$(BashScriptSnippetGen);GetJitDisasmBashScript;GetIlasmRoundTripBashScript">

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

    <PropertyGroup>
      <!-- An exit code of 2 indicates "Skipped" for regular non-windows runs, but "Failed" in Helix -->
      <GCBashScriptExitCode Condition="'$(BuildTestsAgainstPackages)' != 'true'">2</GCBashScriptExitCode>
      <GCBashScriptExitCode Condition="'$(BuildTestsAgainstPackages)' == 'true'">0</GCBashScriptExitCode>

      <BashCLRTestEnvironmentCompatibilityCheck Condition="'$(GCStressIncompatible)' == 'true'"><![CDATA[
$(BashCLRTestEnvironmentCompatibilityCheck)
if [ ! -z "$COMPlus_GCStress" ]
then
  echo SKIPPING EXECUTION BECAUSE COMPlus_GCStress IS SET
  exit $(GCBashScriptExitCode)
fi
      ]]></BashCLRTestEnvironmentCompatibilityCheck>
      <BashCLRTestEnvironmentCompatibilityCheck Condition="'$(JitOptimizationSensitive)' == 'true'"><![CDATA[
$(BashCLRTestEnvironmentCompatibilityCheck)
if [ \( ! -z "$COMPlus_JitStress" \) -o \( ! -z "$COMPlus_JitStressRegs" \) -o \( ! -z "$COMPlus_JITMinOpts" \) -o \( ! -z "$COMPlus_TailcallStress" \) ]
then
  echo "SKIPPING EXECUTION BECAUSE ONE OR MORE OF (COMPlus_JitStress, COMPlus_JitStressRegs, COMPlus_JITMinOpts, COMPlus_TailcallStress) IS SET"
  exit $(GCBashScriptExitCode)
fi
      ]]></BashCLRTestEnvironmentCompatibilityCheck>
      <BashCLRTestEnvironmentCompatibilityCheck Condition="'$(HeapVerifyIncompatible)' == 'true'"><![CDATA[
$(BashCLRTestEnvironmentCompatibilityCheck)
if [ ! -z "$COMPlus_HeapVerify" ]
then
  echo SKIPPING EXECUTION BECAUSE COMPlus_HeapVerify IS SET
  exit $(GCBashScriptExitCode)
fi
      ]]></BashCLRTestEnvironmentCompatibilityCheck>

      <BashCLRTestExitCodePrep Condition="$(_CLRTestNeedsToRun)">
<![CDATA[
if [ -z ${CLRTestExpectedExitCode+x} ]%3B then export CLRTestExpectedExitCode=$(CLRTestExitCode)%3B fi
echo BEGIN EXECUTION]]>
      </BashCLRTestExitCodePrep>
    
      <BashCLRTestArgPrep Condition=" '$(CLRTestExecutionArguments)'!='' ">
<![CDATA[if [ -z ${CLRTestExecutionArguments+x} ]%3B then export CLRTestExecutionArguments='$(CLRTestExecutionArguments)'%3B fi]]>
      </BashCLRTestArgPrep>
    
      <!-- By default, be prepared to do a full check -->
      <BashCLRTestExitCodeCheck><![CDATA[
echo Expected: $CLRTestExpectedExitCode
echo Actual: $CLRTestExitCode
if [ $CLRTestExitCode -ne $CLRTestExpectedExitCode ]
then
  echo END EXECUTION - FAILED
  exit 1
else
  echo END EXECUTION - PASSED
  exit 0
fi
      ]]></BashCLRTestExitCodeCheck>
    </PropertyGroup>
  
    <ItemGroup Condition="$(_CLRTestNeedsToRun)">
      <Clean Include="$(OutputPath)\$(MSBuildProjectName).sh"/>

      <BashCLRTestExecutionScriptArgument Include="debug">
        <HasParam>true</HasParam>
        <ParamText>=*</ParamText> <!-- Bash specific -->
        <ParamName>debuggerFullPath</ParamName>
        <Command><![CDATA[        export _DebuggerFullPath="${i#*=}"
        if [ ! -f "$_DebuggerFullPath" ]
        then
            echo The Debugger FullPath \"$_DebuggerFullPath\" doesn\'t exist
            usage
        fi]]></Command>
        <Description>Run testcases under debugger.</Description>
      </BashCLRTestExecutionScriptArgument>

      <BashCLRTestExecutionScriptArgument Include="coreroot">
        <HasParam>true</HasParam>
        <ParamText>=*</ParamText> <!-- Bash specific -->
        <ParamName>coreRootFullPath</ParamName>
        <Command><![CDATA[        CORE_ROOT="${i#*=}"]]></Command>
        <Description>Set CORE_ROOT to the specified value before running the test.</Description>
      </BashCLRTestExecutionScriptArgument>
    </ItemGroup>

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

LinkBin=__Link
Assemblies="-a System.Private.CoreLib"
ReflectionRoots=

shopt -s nullglob

if [ ! -z "$DoLink" ]; 
then
  if [ ! -x "$ILLINK" ]; 
  then
    echo "Illink executable [$ILLINK] Invalid"
    exit 1
  fi
  
  # Clean up old Linked binaries, if any
  rm -rf $LinkBin
    
  # Remove Native images, since the goal is to run from Linked binaries
  rm -f *.ni.*

  # Use hints for reflection roots, if provided in $(ReflectionRootsXml)
  if [ -f $(ReflectionRootsXml) ]; 
  then
    ReflectionRoots="-x $(ReflectionRootsXml)"
  fi

  # Include all .exe files in this directory as entry points (some tests had multiple .exe file modules)
  for bin in *.exe *.dll; 
  do 
    Assemblies="$Assemblies -a ${bin%.*}"
  done

  # Run dotnet-linker
  # Run the Linker such that all assemblies except System.Private.Corlib.dll are linked
  # Debug symbol generation needs some fixes, and is currently omitted.
  # 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
  ERRORLEVEL=$?
  if [  $ERRORLEVEL -ne 0 ]
  then
    echo ILLINK FAILED $ERRORLEVEL
    if [ -z "$KeepLinkedBinaries" ]; 
    then
      rm -rf $LinkBin
    fi
    exit 1
  fi
  
  # Copy CORECLR native binaries to $LinkBin, 
  # so that we can run the test based on that directory
  cp $CORE_ROOT/*.so $LinkBin/
  cp $CORE_ROOT/corerun $LinkBin/

  # Copy some files that may be arguments
  for f in *.txt;
  do
    [ -e "$f" ] && cp $f $LinkBin
  done

  ExePath=$LinkBin/$(InputAssemblyName)
  export CORE_ROOT=$PWD/$LinkBin
fi
]]>
          </BashLinkerTestLaunchCmds>
          <BashLinkerTestCleanupCmds>
              <![CDATA[
# Clean up the LinkBin directories after test execution.
# Otherwise, RunTests may run out of disk space.

if [ ! -z "$DoLink" ]; 
then
  if [ -z "$KeepLinkedBinaries" ]; 
  then
    rm -rf $LinkBin
  fi
fi
]]>
          </BashLinkerTestCleanupCmds>
      </PropertyGroup>
      <PropertyGroup>
      <_CLRTestRunFile Condition="'$(CLRTestIsHosted)'=='true'">"$CORE_ROOT/corerun"</_CLRTestRunFile>
      <BashCLRTestLaunchCmds><![CDATA[
if [ -z "$DoLink" -a ! -z "$RunningIlasmRoundTrip" ];
then
  echo $(_CLRTestRunFile) $(TargetAssemblyName) $CLRTestExecutionArguments
  $(_CLRTestRunFile) $(TargetAssemblyName) $CLRTestExecutionArguments
  if [  $? -ne $CLRTestExpectedExitCode ]
  then
    echo END EXECUTION OF IL{D}ASM BINARY - FAILED $? vs $CLRTestExpectedExitCode
    echo FAILED
    exit 1
  fi
fi
      ]]></BashCLRTestLaunchCmds>

      <BashCLRTestLaunchCmds Condition="'$(CLRTestKind)' == 'BuildAndRun'">
    <![CDATA[
ExePath=$(InputAssemblyName)
$(BashLinkerTestLaunchCmds)
$(BashCLRTestLaunchCmds)
echo $_DebuggerFullPath $(_CLRTestRunFile) $ExePath $CLRTestExecutionArguments 
$_DebuggerFullPath $(_CLRTestRunFile) $ExePath $CLRTestExecutionArguments 
CLRTestExitCode=$?
$(BashLinkerTestCleanupCmds)
      ]]></BashCLRTestLaunchCmds>
      <BashCLRTestLaunchCmds Condition="'$(CLRTestKind)' == 'RunOnly'"><![CDATA[
$(BashCLRTestLaunchCmds)
echo export CDPATH="$%28dirname "$0")"
export CDPATH="$%28dirname "$0")"
echo /bin/sh -c  $(InputAssemblyName) 
/bin/sh -c $(InputAssemblyName)
CLRTestExitCode=$?
CLRTestExpectedExitCode=0
      ]]></BashCLRTestLaunchCmds>
    </PropertyGroup>

    <PropertyGroup>
      <BashEnvironmentVariables>
        @(CLRTestBashEnvironmentVariable -> '%(Identity)', '%0d%0a')
      </BashEnvironmentVariables>
    </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="@(BashCLRTestExecutionScriptArgument)">
      <Output TaskParameter="ParamList" PropertyName="_CLRTestParamList"/>
    </GenerateParamList>
    <PropertyGroup>
      <!--
      This generates the script portion to parse all of the command line arguments.
      The Command metadata for BashCLRTestExecutionScriptArgument items is executed
      when the argument is found.
      -->
      <BashCLRTestArgPrep><![CDATA[
usage()
{
    echo "Usage: $0  $(_CLRTestParamList)"
    echo 
    echo "Arguments:"
@(BashCLRTestExecutionScriptArgument -> '    echo "-%(Identity)=%(ParamName)"
    echo      "%(Description)"', '
')
    echo "-?,-h,--help    show this message"
    exit 1
}

# Parse Command Line

for i in "$@"
    do
        case $i in
        -?|-h|--help)
        usage
        %3B%3B
@(BashCLRTestExecutionScriptArgument -> '        -%(Identity)%(ParamText)|/%(Identity)%(ParamText))
%(Command)
        %3B%3B')
        *)
        CLRTestExecutionArguments="$CLRTestExecutionArguments $i"
    esac
done

$(BashCLRTestArgPrep)
      ]]></BashCLRTestArgPrep>

      <!-- NOTE! semicolons must be escaped with %3B boooo -->
      <_CLRTestExecutionScriptText>
        <![CDATA[
%23%21/usr/bin/env bash
TakeLock()
{
    echo "in takeLock"
    if mkdir $LockFile%3B then
        return 2
    else
        sleep 10
        TakeLock
    fi
    echo "exiting takelock"
}

ReleaseLock()
{
    echo "in ReleaseLock"
    if [ -d "$LockFile" ]%3B then
        rm -rf "$LockFile"
        return 3
    fi
}
cd "$%28dirname "$0")"
LockFile="lock"


# The __TestEnv variable may be used to specify a script to source before the test.
if [ -n "$__TestEnv" ]%3B then
    source $__TestEnv
fi

$(BashEnvironmentVariables)
$(BashCLRTestEnvironmentCompatibilityCheck)
$(BashCLRTestArgPrep)
$(BashCLRTestExitCodePrep)
$(JitDisasmBashScript)
$(IlasmRoundTripBashScript)
# PreCommands
$(BashCLRTestPreCommands)
# Launch
$(BashCLRTestLaunchCmds)
# PostCommands
$(BashCLRTestPostCommands)
$(BashCLRTestExitCodeCheck)
      ]]>
      </_CLRTestExecutionScriptText>

    </PropertyGroup>

    <!-- Write the file.
         Note: under the hood, this will rely on Environment.NewLine for line 
         endings. This means that if the scripts are being generated on Windows,
         the line endings will need to be changed from CR-LF to Unix (LF) line 
         endings before running the scripts on Unix platforms. -->
    <WriteLinesToFile
      File="$(OutputPath)\$(MSBuildProjectName).sh"
      Lines="$(_CLRTestExecutionScriptText)"
      Overwrite="true" />
  </Target>
  
</Project>