Skip to content
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

Make default rooting behavior consistent #3124

Merged
merged 3 commits into from
Nov 21, 2022
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
7 changes: 2 additions & 5 deletions src/ILLink.Tasks/LinkTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public class ILLink : ToolTask
/// <summary>
/// The names of the assemblies to root. This should contain
/// assembly names without an extension, not file names or
/// paths. Exactly which parts of the assemblies get rooted
/// is subject to change. Currently these get passed to
/// illink with "-a", which roots the entry point for
/// executables, and everything for libraries. To control
/// the behaviour explicitly, set RootMode metadata.
/// paths. The default is to root everything in these assemblies.
// For more fine-grained control, set RootMode metadata.
/// </summary>
[Required]
public ITaskItem[] RootAssemblyNames { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ Copyright (c) .NET Foundation. All rights reserved.
<ManagedAssemblyToLink Include="@(_TrimmableManagedAssemblyToLink)" IsTrimmable="true" />
</ItemGroup>

<!-- Root the main assembly, whether or not it has IsTrimmable set. -->
<!-- Root the main assembly entry point. -->
<ItemGroup>
<TrimmerRootAssembly Include="@(IntermediateAssembly)" />
<TrimmerRootAssembly Include="@(IntermediateAssembly)" RootMode="EntryPoint" />
Copy link
Member

@jonathanpeppers jonathanpeppers Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sbomer Android apps are actually class libraries, there is no static void Main() because Android's entry point is either an Android.App.Application or Android.App.Activity.

I think this change causes:

ILLink error IL1034: Root assembly 'MyAndroidApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have entry point.

Should I do this in the Android workload?

<ItemGroup>
  <TrimmerRootAssembly Update="@(TrimmerRootAssembly)" RootMode="Library" />
</ItemGroup>

</ItemGroup>

<!-- For .NET < 6, root and copy assemblies without IsTrimmable=true MSBuild metadata. -->
Expand Down
5 changes: 0 additions & 5 deletions src/linker/Linker.Steps/RootAssemblyInputStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ protected override void Process ()
}

switch (rootMode) {
case AssemblyRootMode.Default:
if (assembly.MainModule.Kind == ModuleKind.Dll)
goto case AssemblyRootMode.AllMembers;
else
goto case AssemblyRootMode.EntryPoint;
case AssemblyRootMode.EntryPoint:
var ep = assembly.MainModule.EntryPoint;
if (ep == null) {
Expand Down
3 changes: 1 addition & 2 deletions src/linker/Linker/AssemblyRootMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ namespace Mono.Linker
{
public enum AssemblyRootMode
{
Default = 0,
AllMembers = 0,
EntryPoint,
AllMembers,
VisibleMembers,
Library
}
Expand Down
8 changes: 3 additions & 5 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,10 @@ protected int SetupContext (ILogger? customLogger = null)
return -1;
}

AssemblyRootMode rmode = AssemblyRootMode.Default;
AssemblyRootMode rmode = AssemblyRootMode.AllMembers;
var rootMode = GetNextStringValue ();
if (rootMode != null) {
var parsed_rmode = ParseAssemblyRootsMode (rootMode);
var parsed_rmode = ParseAssemblyRootMode (rootMode);
if (parsed_rmode is null)
return -1;

Expand Down Expand Up @@ -1115,11 +1115,9 @@ static string[] ReadLines (string file)
return null;
}

AssemblyRootMode? ParseAssemblyRootsMode (string s)
AssemblyRootMode? ParseAssemblyRootMode (string s)
{
switch (s.ToLowerInvariant ()) {
case "default":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance we get this on the command line?
Just thinking about backward compat.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely possible - someone could have set RootMode=default on ManagedAssemblyToLink - but we never documented that and I hope nobody is doing it. My preference is to get rid of it, but if this is a real concern I can keep support for it. It would just be a bit unfortunate that "default" is no longer the default. @marek-safar, thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's OK to break this as well - especially since this is 8.0 only and we will have TFM-specific linker then.

return AssemblyRootMode.Default;
case "all":
return AssemblyRootMode.AllMembers;
case "visible":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace ILLink.RoslynAnalyzer.Tests.Inheritance.Interfaces
{
public sealed partial class BaseProvidesInterfaceEdgeCaseTests : LinkerTestBase
{

protected override string TestSuiteName => "Inheritance.Interfaces.BaseProvidesInterfaceEdgeCase";

[Fact]
public Task BaseProvidesInterfaceMethodCircularReference ()
{
return RunTest (allowMissingWarnings: true);
}

}
}
2 changes: 1 addition & 1 deletion test/ILLink.Tasks.Tests/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Dictionary<string, string> GetCustomData ()
protected override List<BaseStep> CreateDefaultResolvers ()
{
return new List<BaseStep> () {
new RootAssemblyInput (null, AssemblyRootMode.Default)
new RootAssemblyInput (null, AssemblyRootMode.EntryPoint)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand All @@ -15,7 +15,6 @@ namespace Mono.Linker.Tests.Cases.Inheritance.Interfaces.StaticInterfaceMethods
{
[SetupCompileBefore ("library.dll", new[] { "Dependencies/Library.cs" })]
[SetupLinkerAction ("skip", "library")]
[SetupLinkerArgument ("-a", "test.exe")]
public static class StaticInterfaceMethodsInPreservedScope
{
[Kept]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand All @@ -15,7 +15,6 @@ namespace Mono.Linker.Tests.Cases.Inheritance.Interfaces.StaticInterfaceMethods
{
[SetupCompileBefore ("library.dll", new[] { "Dependencies/Library.cs" })]
[SetupLinkerAction ("skip", "library")]
[SetupLinkerArgument ("-a", "test.exe")]
public static class StaticVirtualInterfaceMethodsInPreservedScope
{
[Kept]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Libraries
{
[SetupCompileAsLibrary]
[SetupLinkerArgument ("-a", "test.dll")]
[Kept]
[KeptMember (".ctor()")]
public class DefaultLibraryLinkBehavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
Expand Down Expand Up @@ -61,6 +61,7 @@ public virtual void LinkFromAssembly (string fileName)
{
Append ("-a");
Append (fileName);
Append ("entrypoint");
}

public virtual void LinkFromPublicAndFamily (string fileName)
Expand Down