Skip to content

Link more assemblies (type granularity linking for real this time) #18165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Components/Blazor/Blazor/src/Hosting/WebAssemblyHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ internal class WebAssemblyHost : IWebAssemblyHost

public WebAssemblyHost(IServiceProvider services, IJSRuntime runtime)
{
// To ensure JS-invoked methods don't get linked out, have a reference to their enclosing types
GC.KeepAlive(typeof(EntrypointInvoker));
GC.KeepAlive(typeof(JSInteropMethods));
GC.KeepAlive(typeof(WebAssemblyEventDispatcher));

Services = services ?? throw new ArgumentNullException(nameof(services));
_runtime = runtime ?? throw new ArgumentNullException(nameof(runtime));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Microsoft.AspNetCore.Blazor.Build.Tasks
{
public class GenerateTypeGranularityLinkingConfig : Task
{
[Required]
public ITaskItem[] Assemblies { get; set; }

[Required]
public string OutputPath { get; set; }

public override bool Execute()
{
var linkerElement = new XElement("linker",
new XComment(" THIS IS A GENERATED FILE - DO NOT EDIT MANUALLY "));

foreach (var assembly in Assemblies)
{
var assemblyElement = CreateTypeGranularityConfig(assembly);
linkerElement.Add(assemblyElement);
}

using var fileStream = File.Open(OutputPath, FileMode.Create);
new XDocument(linkerElement).Save(fileStream);

return true;
}

private XElement CreateTypeGranularityConfig(ITaskItem assembly)
{
// We match all types in the assembly, and for each one, tell the linker to preserve all
// its members (preserve=all) but only if there's some reference to the type (required=false)
return new XElement("assembly",
new XAttribute("fullname", Path.GetFileNameWithoutExtension(assembly.ItemSpec)),
new XElement("type",
new XAttribute("fullname", "*"),
new XAttribute("preserve", "all"),
new XAttribute("required", "false")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
</Target>

<UsingTask TaskName="BlazorILLink" AssemblyFile="$(BlazorTasksPath)" />
<UsingTask TaskName="GenerateTypeGranularityLinkingConfig" AssemblyFile="$(BlazorTasksPath)" />

<Target
Name="_LinkBlazorApplication"
Expand All @@ -171,7 +172,10 @@
Outputs="$(_BlazorLinkerOutputCache)">

<ItemGroup>
<_BlazorDependencyAssembly Include="@(_BlazorDependencyInput)" IsLinkable="$([System.String]::Copy('%(FileName)').StartsWith('System.'))" />
<_BlazorDependencyAssembly Include="@(_BlazorDependencyInput)" />
<_BlazorDependencyAssembly IsLinkable="true" Condition="$([System.String]::Copy('%(Filename)').StartsWith('System.'))" />
<_BlazorDependencyAssembly IsLinkable="true" TypeGranularity="true" Condition="$([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.'))" />
<_BlazorDependencyAssembly IsLinkable="true" TypeGranularity="true" Condition="$([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.'))" />

<_BlazorAssemblyToLink Include="@(_WebAssemblyBCLAssembly)" />
<_BlazorAssemblyToLink Include="@(_BlazorDependencyAssembly)" Condition="'%(_BlazorDependencyAssembly.IsLinkable)' == 'true'" />
Expand Down Expand Up @@ -201,6 +205,15 @@
<_DotNetHostFileName Condition=" '$(OS)' == 'Windows_NT' ">dotnet.exe</_DotNetHostFileName>
</PropertyGroup>

<PropertyGroup>
<_TypeGranularityLinkingConfig>$(BlazorIntermediateOutputPath)linker.typegranularityconfig.xml</_TypeGranularityLinkingConfig>
</PropertyGroup>
<GenerateTypeGranularityLinkingConfig Assemblies="@(_BlazorAssemblyToLink->WithMetadataValue('TypeGranularity', 'true'))" OutputPath="$(_TypeGranularityLinkingConfig)" />
<ItemGroup>
<BlazorLinkerDescriptor Include="$(_TypeGranularityLinkingConfig)" />
<FileWrites Include="$(_TypeGranularityLinkingConfig)" />
</ItemGroup>

<BlazorILLink
ILLinkPath="$(MonoLinkerPath)"
AssemblyPaths="@(_BlazorAssemblyToLink)"
Expand Down