-
Notifications
You must be signed in to change notification settings - Fork 569
[assembly-preparer] Create a new tool to replace pre-mark custom linker steps. #25652
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
Open
rolfbjarne
wants to merge
13
commits into
main
Choose a base branch
from
dev/rolf/assembly-preparer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
656beee
[tools] Move RegistrarMode to its own file.
rolfbjarne 08b6734
[tools] Move NormalizedStringComparer into its own file.
rolfbjarne a29a1ac
[tools] Simplify a little bit of code.
rolfbjarne 542e99d
[tests] Clean up Configure.cs a bit.
rolfbjarne 2d9925c
[xharness] Run the new assembly processing tests.
rolfbjarne 9cac08c
[tools] Remove some dead code
rolfbjarne 00274d7
[tools] Extract OptimizeGeneratedCode into its own class.
rolfbjarne 01d6ef4
[dotnet-linker] Refactor LinkerConfiguration to support read/write.
rolfbjarne 8a10ef2
[tools] VSCode tasks.
rolfbjarne 3d3b7d2
[tools] Add link to NoWarn issue
rolfbjarne 9e1532c
[tests] Create an opt-in test to capture build performance
rolfbjarne 97d66d0
[dotnet-linker] Move methods from ManagedRegistrarLookupTablesStep to…
rolfbjarne 2fbe855
[assembly-preparer] Create a new tool to replace pre-mark custom link…
rolfbjarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "type": "shell", | ||
| "command": "make", | ||
| "group": { | ||
| "kind": "build", | ||
| "isDefault": true | ||
| }, | ||
| "problemMatcher": [ | ||
| "$msCompile" | ||
| ], | ||
| "label": "make" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.IO; | ||
| using System.Text; | ||
|
|
||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.Utils; | ||
|
|
||
| class ConsoleToTaskWriter : TextWriter { | ||
| TaskLoggingHelper helper; | ||
| bool errorShown; | ||
|
|
||
| public ConsoleToTaskWriter (TaskLoggingHelper helper) | ||
| { | ||
| this.helper = helper; | ||
| } | ||
|
|
||
| public override Encoding Encoding => Encoding.UTF8; | ||
|
|
||
| public override void Write (char value) | ||
| { | ||
| ShowError (); | ||
| helper.LogMessage (MessageImportance.Low, value.ToString ()); | ||
| } | ||
|
|
||
| public override void Write (char [] buffer, int index, int count) | ||
| { | ||
| ShowError (); | ||
| helper.LogMessage (MessageImportance.Low, new string (buffer, index, count)); | ||
| } | ||
|
|
||
| public override void Write (string? value) | ||
| { | ||
| ShowError (); | ||
| helper.LogMessage (MessageImportance.Low, value ?? string.Empty); | ||
| } | ||
|
|
||
| public override void WriteLine () | ||
| { | ||
| ShowError (); | ||
| } | ||
|
|
||
| public override void WriteLine (string? value) | ||
| { | ||
| ShowError (); | ||
| helper.LogMessage (MessageImportance.Low, value ?? string.Empty); | ||
| } | ||
|
|
||
| void ShowError () | ||
| { | ||
| if (errorShown) | ||
| return; | ||
| errorShown = true; | ||
|
|
||
| helper.LogError (null, "MT7178" /* Console.StandardOutput or Console.StandardError was accessed during a build task. This should not happen, use the MSBuild logging infrastructure instead. Stack trace: {0} */, null, null, 0, 0, 0, 0, Xamarin.Localization.MSBuild.MSBStrings.E7178, Environment.StackTrace); | ||
| } | ||
|
|
||
| public static IDisposable EnsureNoConsoleUsage (TaskLoggingHelper log) | ||
| { | ||
| return new NoConsoleUsage (new ConsoleToTaskWriter (log)); | ||
| } | ||
|
|
||
| class NoConsoleUsage : IDisposable { | ||
| TextWriter? originalStdout; | ||
| TextWriter? originalStderr; | ||
|
|
||
| public NoConsoleUsage (ConsoleToTaskWriter redirector) | ||
| { | ||
| originalStdout = Console.Out; | ||
| originalStderr = Console.Error; | ||
| Console.SetOut (redirector); | ||
| Console.SetError (redirector); | ||
| } | ||
|
|
||
| ~NoConsoleUsage () | ||
| { | ||
| Restore (); | ||
| } | ||
|
|
||
| void IDisposable.Dispose () | ||
| { | ||
| Restore (); | ||
| GC.SuppressFinalize (this); | ||
| } | ||
|
|
||
| void Restore () | ||
| { | ||
| if (originalStdout is not null) { | ||
| Console.SetOut (originalStdout); | ||
| originalStdout = null; | ||
| } | ||
| if (originalStderr is not null) { | ||
| Console.SetError (originalStderr); | ||
| originalStderr = null; | ||
| } | ||
| } | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
msbuild/Xamarin.MacDev.Tasks/Tasks/PrepareAssemblies.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Configuration; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| using Xamarin.Build; | ||
| using Xamarin.Bundler; | ||
| using Xamarin.Utils; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.MacDev.Tasks { | ||
| public class PrepareAssemblies : XamarinTask { | ||
| const string ErrorPrefix = "MX"; | ||
|
|
||
| #region Inputs | ||
| [Required] | ||
| public ITaskItem [] InputAssemblies { get; set; } = []; | ||
|
|
||
| public string MakeReproPath { get; set; } = ""; | ||
|
|
||
| public string OutputDirectory { get; set; } = ""; | ||
|
|
||
| [Required] | ||
| public ITaskItem? OptionsFile { get; set; } | ||
| #endregion | ||
|
|
||
| #region Outputs | ||
| [Output] | ||
| public ITaskItem [] OutputAssemblies { get; set; } = []; | ||
| #endregion | ||
|
|
||
| Dictionary<AssemblyPreparerInfo, ITaskItem> map = new (); | ||
|
|
||
| AssemblyPreparerInfo GetAssemblyInfo (ITaskItem item) | ||
| { | ||
| var inputPath = item.ItemSpec; | ||
| var outputPath = Path.Combine (OutputDirectory, Path.GetFileName (inputPath)); | ||
| var isTrimmableString = item.GetMetadata ("IsTrimmable"); | ||
| var isTrimmable = string.IsNullOrEmpty (isTrimmableString) ? (bool?) null : string.Equals (isTrimmableString, "true", StringComparison.OrdinalIgnoreCase); | ||
| var trimMode = item.GetMetadata ("TrimMode"); | ||
| var rv = new AssemblyPreparerInfo (inputPath, outputPath, isTrimmable, trimMode); | ||
| map [rv] = item; | ||
| return rv; | ||
| } | ||
|
|
||
| public override bool Execute () | ||
| { | ||
| // Capture Console usage and show an error if anything uses Console.[Error.]Write* | ||
| using var consoleToLog = ConsoleToTaskWriter.EnsureNoConsoleUsage (Log); | ||
|
|
||
| try { | ||
| var infos = InputAssemblies.Select (GetAssemblyInfo).ToArray (); | ||
| using var preparer = new AssemblyPreparer (this, infos, OptionsFile?.ItemSpec ?? ""); | ||
| preparer.MakeReproPath = MakeReproPath; | ||
| var rv = preparer.Prepare (out var exceptions); | ||
|
Comment on lines
+61
to
+65
|
||
|
|
||
| foreach (var pe in exceptions) { | ||
| if (pe.IsError (this)) { | ||
| ((IToolLog) this).LogError (pe); | ||
| } else { | ||
| ((IToolLog) this).LogWarning (pe); | ||
| } | ||
| } | ||
|
|
||
| var outputAssemblies = preparer.Assemblies.Select (v => { | ||
| var item = new TaskItem (v.OutputPath); | ||
| map [v].CopyMetadataTo (item); | ||
| item.SetMetadata ("BeforePrepareAssembliesPath", v.InputPath); | ||
| return (ITaskItem) item; | ||
| }).ToList (); | ||
|
|
||
| outputAssemblies.AddRange (preparer.AddedAssemblies.Select (v => { | ||
| var rv = new TaskItem (v.Path); | ||
| rv.SetMetadata ("PostprocessAssembly", "true"); | ||
| rv.SetMetadata ("RelativePath", preparer.Configuration.AssemblyPublishDir + Path.GetFileName (v.Path)); | ||
| if (v.OriginatingAssembly is not null) { | ||
| var originatingItem = map.SingleOrDefault (kvp => Path.GetFileName (kvp.Key.InputPath) == Path.GetFileName (v.OriginatingAssembly)).Value; | ||
| if (originatingItem is null) { | ||
| Log.LogMessage (MessageImportance.Low, $"Could not find originating assembly for {v.Path} with originating assembly name {v.OriginatingAssembly}"); | ||
| } else { | ||
| var metadata = originatingItem.MetadataNames.Cast<string> ().ToList (); | ||
| if (metadata.Contains ("TrimMode")) | ||
| rv.SetMetadata ("TrimMode", originatingItem.GetMetadata ("TrimMode")); | ||
| if (metadata.Contains ("IsTrimmable")) | ||
| rv.SetMetadata ("IsTrimmable", originatingItem.GetMetadata ("IsTrimmable")); | ||
| } | ||
| } | ||
| return rv; | ||
| })); | ||
|
|
||
| OutputAssemblies = outputAssemblies.ToArray (); | ||
| return rv && !Log.HasLoggedErrors; | ||
| } catch (Exception e) { | ||
| ((IToolLog) this).LogException (e); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 💡 MSBuild tasks — Consider whether this task needs
SessionIdandIsMacEnabledproperties for remote execution support when building from Windows.Most MSBuild tasks in this repository that perform Mac-specific operations include:
Since this task processes assemblies and may eventually need to run on the Mac (or already does for certain operations), verify whether remote execution support is needed. If assembly preparation must always happen on the Mac, add these properties and conditions.
Rule: SessionId for remote execution