[wasm] Standup EnableAgressiveTrimming for library tests (#46367)
authorMitchell Hwang <mitchhwang1418@gmail.com>
Mon, 4 Jan 2021 21:21:55 +0000 (16:21 -0500)
committerGitHub <noreply@github.com>
Mon, 4 Jan 2021 21:21:55 +0000 (16:21 -0500)
* Trim wasm library tests

* Add SystemBuffers HelloTest and only test that

* Import linker from nuget to override sdk linker

* Remove redundant assembly inclusions

* Add WasmTestRunner referenced assemblies

* Add properties to reduce library size

* Updates

* Configure trimming for library tests

* Link assemblies except for test assembly

* Use another path to WasmTestRunner.dll

* Fix typo

* Setup correctly rooted assemblies

* Don't copy WasmTestRunner.dll from wrong patch when using linker

* Don't inflate publish output with packages which are not used for tests runs

* Fix xunit framework files copying to publish location to not overwrite
version copied as part of trimming publish

* Add linker descriptor for xunit code which linker cannot track

* Add tested assembly to the test payload closure

* One more method to preserve

* Preserving some xunit types to make sure we can actually discover the tests

* Put Wasm.targets import in the right spot

* Add a mode where we can run the regular system.buffers tests

* Dont link out Microsoft.DotNet.RemoteExecutor

* Move Microsoft.DotNet.RemoteExecutor inclusion to xml

* Consolidate ConfigureTrimming itemgroups

* Build PrepareForWasmBuildApp before WasmBuildApp

* Remove manual import of WasmTestRunner assemblies

* Remove temporary HelloTest from System.Buffers

* Added flag to not aggressively link by default

* Revert import and build order changes

* Condition trimming related changes on EnableAggressiveTrimming

* Revert "Don't copy WasmTestRunner.dll from wrong patch when using linker" as MainAssembly is back to $(PublishDir)\WasmTestRunner.dll
This reverts commit 79dc240f4c99f61af1a7e4c98d299be25f8f854e.

* Add comment to note temporary RemoteExecutor assembly inclusion

* Move props from general tests to mobile tests

* Revert "Condition trimming related changes on EnableAggressiveTrimming"

This reverts commit a97fb56eeb47522a4af1e7e47acd3595cf48271e.

* Broaden trimming options for all mobile targets

* Enable PublishTrimmed regardless of OS when aggressively trimming

* Move trimming specific property to ConfigureTrimming

* Import NuGet linker in mobile tests by default

* Add file description

* Add detail to ILLink XML descriptor file

* Reformat ConfigureTrimming target tag

* Fix typo

Co-authored-by: Mitchell Hwang <mitchell.hwang@microsoft.com>
Co-authored-by: Steve Pfister <steve.pfister@microsoft.com>
Co-authored-by: Marek Safar <marek.safar@gmail.com>
eng/testing/ILLink.Descriptor.xunit.xml [new file with mode: 0644]
eng/testing/tests.mobile.targets
eng/testing/xunit/xunit.console.targets
eng/testing/xunit/xunit.props

diff --git a/eng/testing/ILLink.Descriptor.xunit.xml b/eng/testing/ILLink.Descriptor.xunit.xml
new file mode 100644 (file)
index 0000000..a3c0369
--- /dev/null
@@ -0,0 +1,21 @@
+<!-- xunit 2.x version is not under development. We make xunit trimming compatible in methods which ILLink cannot track by providing XML descriptor to keep the dependencies -->
+<linker>
+  <assembly fullname="xunit.execution.dotnet">
+    <type fullname="Xunit.Sdk.ReflectionAssemblyInfo">
+      <method signature="System.Void .ctor(System.String)" />
+    </type>
+    <type fullname="Xunit.Sdk.TestFrameworkProxy">
+      <method signature="System.Void .ctor(System.Object,System.Object,System.Object)" />
+    </type>
+    <type fullname="Xunit.Sdk.FactDiscoverer" />
+  </assembly>
+  <assembly fullname="xunit.core">
+    <namespace fullname="Xunit" />
+  </assembly>
+  <!-- Temporary until https://github.com/mono/linker/issues/1713 is resolved -->
+  <assembly fullname="Microsoft.DotNet.RemoteExecutor">
+    <type fullname="Microsoft.DotNet.RemoteExecutor.Program">
+      <method signature="System.Int32 Main(System.String[])" />
+    </type>
+  </assembly>
+</linker>
index a1734bd..60c1465 100644 (file)
@@ -6,11 +6,22 @@
     <RunAOTCompilation Condition="'$(TargetOS)' == 'iOS' and $(TargetArchitecture.StartsWith('arm'))">true</RunAOTCompilation>
     <JSEngine Condition="'$(TargetOS)' == 'Browser' and '$(JSEngine)' == ''">V8</JSEngine>
     <JSEngineArgs Condition="'$(JSEngine)' == 'V8'">$(JSEngineArgs) --engine-arg=--stack-trace-limit=1000</JSEngineArgs>
+
+    <PublishingTestsRun>true</PublishingTestsRun>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(TargetOS)' == 'Browser'">
     <!-- We need to set this in order to get extensibility on xunit category traits and other arguments we pass down to xunit via MSBuild properties -->
     <RunScriptCommand>$HARNESS_RUNNER wasm $XHARNESS_COMMAND  --app=. --engine=$(JSEngine) $(JSEngineArgs) --js-file=runtime.js --output-directory=$XHARNESS_OUT --  $(RunTestsJSArguments) --run WasmTestRunner.dll $(AssemblyName).dll</RunScriptCommand>
+    <EventSourceSupport>false</EventSourceSupport>
+    <UseSystemResourceKeys>true</UseSystemResourceKeys>
+    <EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
+    <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
+    <DebuggerSupport>false</DebuggerSupport>
+  </PropertyGroup>
+
+  <PropertyGroup Condition="'$(EnableAggressiveTrimming)' == 'true'">
+    <PublishTrimmed>true</PublishTrimmed>
   </PropertyGroup>
 
   <!-- Generate a self-contained app bundle for Android with tests. -->
 
   </Target>
 
+  <Target Name="ConfigureTrimming" Condition="'$(EnableAggressiveTrimming)' == 'true'" BeforeTargets="PrepareForILLink">
+    <PropertyGroup>
+      <TrimMode>link</TrimMode>
+    </PropertyGroup>
+
+    <ItemGroup>
+      <ManagedAssemblyToLink Include="$(OutDir)\*xunit*">
+        <IsTrimmable>true</IsTrimmable>
+        <TrimMode>link</TrimMode>
+      </ManagedAssemblyToLink>
+      <ManagedAssemblyToLink Condition="('%(ManagedAssemblyToLink.FileName).dll' != '$(MSBuildProjectName).dll')" >
+        <TrimMode>link</TrimMode>
+      </ManagedAssemblyToLink>
+      <ManagedAssemblyToLink Condition="('%(ManagedAssemblyToLink.FileName).dll' == '$(AssemblyName).dll')" >
+        <TrimMode>copy</TrimMode>
+      </ManagedAssemblyToLink>
+      <TrimmerRootAssembly Condition="'$(TargetOS)' == 'Android'" Include="AndroidTestRunner"/>
+      <TrimmerRootAssembly Condition="'$(TargetOS)' == 'Browser'" Include="WasmTestRunner"/>
+      <TrimmerRootAssembly Condition="'$(TargetOS)' == 'iOS'" Include="AppleTestRunner"/>
+      <TrimmerRootAssembly Include="$(AssemblyName)"/>
+      <TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptor.xunit.xml" />
+    </ItemGroup>
+  </Target>
+
   <Import Project="$(MonoProjectRoot)\wasm\build\WasmApp.targets" Condition="'$(TargetOS)' == 'Browser'" />
   <PropertyGroup>
       <WasmBuildAppDependsOn>PrepareForWasmBuildApp;$(WasmBuildAppDependsOn)</WasmBuildAppDependsOn>
       <_runnerFilesToPublish Include="$(AppleTestRunnerDir)*" Condition="'$(TargetOS)' == 'iOS' or '$(TargetOS)' == 'tvOS'" />
       <_runnerFilesToPublish Include="$(WasmTestRunnerDir)*" Condition="'$(TargetOS)' == 'Browser'" />
 
-      <!-- Exclude xunit assemblies as those should be resolved by our own package references -->
-      <ResolvedFileToPublish Include="@(_runnerFilesToPublish)" Condition="!$([System.String]::Copy('%(Filename)').StartsWith('xunit.'))" RelativePath="%(FileName)%(Extension)" CopyToPublishDirectory="PreserveNewest" PostprocessAssembly="true" />
+      <ResolvedFileToPublish Include="@(_runnerFilesToPublish)" RelativePath="%(FileName)%(Extension)" CopyToPublishDirectory="PreserveNewest" PostprocessAssembly="true" />
     </ItemGroup>
   </Target>
 
           Condition="'$(IsCrossTargetingBuild)' != 'true'"
           AfterTargets="Build"
           DependsOnTargets="Publish;BundleTestAppleApp;BundleTestAndroidApp;BundleTestWasmApp;ArchiveTests" />
+
+  <Import Project="$(RepositoryEngineeringDir)illink.targets" />
 </Project>
index a2a6d88..ee70055 100644 (file)
@@ -48,7 +48,7 @@
 
   <!-- Overwrite the runner config file with the app local one. -->
   <Target Name="OverwriteDesktopTestRunnerConfigs"
-          Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and 
+          Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and
                      '$(GenerateAppConfigurationFile)' == 'true' and
                      '@(AppConfigWithTargetPath)' != ''"
           AfterTargets="CopyFilesToOutputDirectory">
@@ -62,7 +62,7 @@
   </Target>
 
   <!-- ResolveAssemblyReferences is the target that populates ReferenceCopyLocalPaths which is what is copied to output directory. -->
-  <Target Name="CopyRunnerToOutputDirectory" BeforeTargets="ResolveAssemblyReferences">
+  <Target Name="CopyRunnerToOutputDirectory" BeforeTargets="ResolveAssemblyReferences" Condition="'$(TargetsMobile)' != 'true'">
     <ItemGroup>
       <!-- Copy test runner to output directory -->
       <None Include="$([System.IO.Path]::GetDirectoryName('$(XunitConsole472Path)'))\*"
index 69355a4..f63b390 100644 (file)
@@ -13,7 +13,7 @@
     <PackageReference Include="Microsoft.DotNet.XUnitExtensions" Version="$(MicrosoftDotNetXUnitExtensionsVersion)" />
   </ItemGroup>
 
-  <ItemGroup Condition="'$(ArchiveTests)' != 'true'">
+  <ItemGroup Condition="'$(ArchiveTests)' != 'true' AND '$(PublishingTestsRun)' != 'true'">
     <!-- Microsoft.Net.Test.Sdk brings a lot of assemblies with it. To reduce helix payload submission size we disable it on CI. -->
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" />
     <PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioVersion)" GeneratePathProperty="true" />