Cleanup, and little improvement for aggressive trimming (#46587)
authorAnkit Jain <radical@gmail.com>
Tue, 5 Jan 2021 21:48:00 +0000 (16:48 -0500)
committerGitHub <noreply@github.com>
Tue, 5 Jan 2021 21:48:00 +0000 (16:48 -0500)
* Cleanup, and little improvement for aggressive trimming

- The `ConfigureTrimming` target adds xunit assemblies explicitly, and
  sets `TriggerRootAssembly` too.

- Looking at the files being linked, some issues noticed:
    - Multiple copies of xunit assemblies, from various locations are
      included. Eg. from `System.Buffer.Tests`, `WasmTestRunner`, and
      from the nuget
    - `WasmTestRunner` is explicitly added as `TriggerRootAssembly`, but
      it is also passed as an arg for linking, `-p link ...`
    - `xunit.runner.json` is incorrectly added to the files to be linked

- Instead:
    - all the files to be published are already in
      `ResolvedFileToPublish` after `AddTestRunnersToPublishedFiles` target
    - so, we mark everything for linking,
    - *except*:
        - main test assembly
        - and the test runner assembly

    - This way we are explicitly specifying which assemblies to link,
      and which ones to `copy`

- This reduces the size:
    - 3.1M to 2.6M `artifacts/bin/System.Buffers.Tests/net6.0-Release/browser-wasm/AppBundle/managed/`
    - 46M to 31M `artifacts/bin/System.Buffers.Tests/net6.0-Release/browser-wasm/AppBundle/publish/`

* cleanup

* Trim the test runner also. based on @mareks' feedback

eng/testing/tests.mobile.targets

index e6e140d..9ac8dcc 100644 (file)
 
   </Target>
 
-  <Target Name="ConfigureTrimming" Condition="'$(EnableAggressiveTrimming)' == 'true'" BeforeTargets="PrepareForILLink">
+  <Target Name="ConfigureTrimming" Condition="'$(EnableAggressiveTrimming)' == 'true'" AfterTargets="AddTestRunnersToPublishedFiles">
     <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)"/>
+      <!-- Mark all the assemblies for link. We will explicitly mark the non-trimmable ones -->
+      <ResolvedFileToPublish TrimMode="link" />
+
+      <!-- Don't trim the main assembly.
+           TrimMode="" is needed so the root assemblies are correctly identified -->
+      <ResolvedFileToPublish TrimMode="" Condition="'%(FileName)' == '$(AssemblyName)'" />
+
+      <!-- Even though we are trimming the test runner assembly, we want it to be treated
+           as a root -->
+      <TrimmerRootAssembly
+          Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('TestRunner.dll'))"
+          Include="%(ResolvedFileToPublish.FileName)" />
+    </ItemGroup>
+
+    <ItemGroup>
       <TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptor.xunit.xml" />
     </ItemGroup>
   </Target>