-
Notifications
You must be signed in to change notification settings - Fork 567
[TrimmableTypeMap] Extract TrimmableTypeMapGenerator from MSBuild task #11034
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
jonathanpeppers
merged 15 commits into
main
from
dev/simonrozsival/extract-trimmable-typemap-generator
Apr 1, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a399675
[TrimmableTypeMap] Extract TrimmableTypeMapGenerator from MSBuild task
simonrozsival bcde9df
Keep per-assembly acw-map generation in MSBuild task
simonrozsival 24d340c
Address review: restore assembly filtering, add input validation
simonrozsival e3553d5
Split tests: generator logic → TrimmableTypeMap.Tests, MSBuild → Buil…
simonrozsival 6b6ff3d
Restore AcwMapDirectory doc comment, scan all assemblies
simonrozsival b9ce5c6
Move ParseTargetFrameworkVersion to GenerateTrimmableTypeMap task
simonrozsival 7a66696
Address review fixes
simonrozsival dc478cf
Remove all IO from TrimmableTypeMapGenerator
simonrozsival 4342ba8
Drop file-path scanner overloads, simplify null checks
simonrozsival 8aaf126
Remove file-path writing overloads from generator library
simonrozsival fab1cc2
Address review: dispose MemoryStreams, add doc-comments, fix formatting
simonrozsival 6791efb
Restore dropped test coverage: incrementality, TFV parsing, no-peers
simonrozsival 5d596ea
Fix CS0246: CecilTypeDefinitionCache -> TypeDefinitionCache
jonathanpeppers ba00b01
Fix CS0104: disambiguate AssemblyDefinition between Cecil and SRM
simonrozsival 7d93aff
Address PR review: remove dead code, add TODO, fix PEReader disposal
simonrozsival 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
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
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
18 changes: 18 additions & 0 deletions
18
src/Microsoft.Android.Sdk.TrimmableTypeMap/NullableExtensions.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,18 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| // The static methods in System.String are not NRT annotated in netstandard2.0, | ||
| // so we need our own extension methods to make them nullable aware. | ||
| static class NullableExtensions | ||
| { | ||
| public static bool IsNullOrEmpty ([NotNullWhen (false)] this string? str) | ||
| { | ||
| return string.IsNullOrEmpty (str); | ||
| } | ||
|
|
||
| public static bool IsNullOrWhiteSpace ([NotNullWhen (false)] this string? str) | ||
| { | ||
| return string.IsNullOrWhiteSpace (str); | ||
| } | ||
| } |
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
83 changes: 83 additions & 0 deletions
83
src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapGenerator.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,83 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection.PortableExecutable; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| public class TrimmableTypeMapGenerator | ||
| { | ||
| readonly Action<string> log; | ||
|
|
||
| public TrimmableTypeMapGenerator (Action<string> log) | ||
| { | ||
| this.log = log ?? throw new ArgumentNullException (nameof (log)); | ||
| } | ||
|
|
||
| public TrimmableTypeMapResult Execute ( | ||
| IReadOnlyList<(string Name, PEReader Reader)> assemblies, | ||
| Version systemRuntimeVersion, | ||
| HashSet<string> frameworkAssemblyNames) | ||
| { | ||
simonrozsival marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ = assemblies ?? throw new ArgumentNullException (nameof (assemblies)); | ||
| _ = systemRuntimeVersion ?? throw new ArgumentNullException (nameof (systemRuntimeVersion)); | ||
| _ = frameworkAssemblyNames ?? throw new ArgumentNullException (nameof (frameworkAssemblyNames)); | ||
|
|
||
| var allPeers = ScanAssemblies (assemblies); | ||
| if (allPeers.Count == 0) { | ||
| log ("No Java peer types found, skipping typemap generation."); | ||
| return new TrimmableTypeMapResult ([], [], allPeers); | ||
| } | ||
|
|
||
| var generatedAssemblies = GenerateTypeMapAssemblies (allPeers, systemRuntimeVersion); | ||
| var jcwPeers = allPeers.Where (p => | ||
| !frameworkAssemblyNames.Contains (p.AssemblyName) | ||
| || p.JavaName.StartsWith ("mono/", StringComparison.Ordinal)).ToList (); | ||
| log ($"Generating JCW files for {jcwPeers.Count} types (filtered from {allPeers.Count} total)."); | ||
| var generatedJavaSources = GenerateJcwJavaSources (jcwPeers); | ||
| return new TrimmableTypeMapResult (generatedAssemblies, generatedJavaSources, allPeers); | ||
| } | ||
|
|
||
| List<JavaPeerInfo> ScanAssemblies (IReadOnlyList<(string Name, PEReader Reader)> assemblies) | ||
| { | ||
| using var scanner = new JavaPeerScanner (); | ||
| var peers = scanner.Scan (assemblies); | ||
| log ($"Scanned {assemblies.Count} assemblies, found {peers.Count} Java peer types."); | ||
| return peers; | ||
| } | ||
|
|
||
| List<GeneratedAssembly> GenerateTypeMapAssemblies (List<JavaPeerInfo> allPeers, Version systemRuntimeVersion) | ||
| { | ||
| var peersByAssembly = allPeers.GroupBy (p => p.AssemblyName, StringComparer.Ordinal).OrderBy (g => g.Key, StringComparer.Ordinal); | ||
| var generatedAssemblies = new List<GeneratedAssembly> (); | ||
| var perAssemblyNames = new List<string> (); | ||
| var generator = new TypeMapAssemblyGenerator (systemRuntimeVersion); | ||
| foreach (var group in peersByAssembly) { | ||
| string assemblyName = $"_{group.Key}.TypeMap"; | ||
| perAssemblyNames.Add (assemblyName); | ||
| var peers = group.ToList (); | ||
| var stream = new MemoryStream (); | ||
| generator.Generate (peers, stream, assemblyName); | ||
| stream.Position = 0; | ||
| generatedAssemblies.Add (new GeneratedAssembly (assemblyName, stream)); | ||
| log ($" {assemblyName}: {peers.Count} types"); | ||
simonrozsival marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| var rootStream = new MemoryStream (); | ||
| var rootGenerator = new RootTypeMapAssemblyGenerator (systemRuntimeVersion); | ||
| rootGenerator.Generate (perAssemblyNames, rootStream); | ||
| rootStream.Position = 0; | ||
| generatedAssemblies.Add (new GeneratedAssembly ("_Microsoft.Android.TypeMaps", rootStream)); | ||
| log ($" Root: {perAssemblyNames.Count} per-assembly refs"); | ||
| log ($"Generated {generatedAssemblies.Count} typemap assemblies."); | ||
| return generatedAssemblies; | ||
| } | ||
|
|
||
| List<GeneratedJavaSource> GenerateJcwJavaSources (List<JavaPeerInfo> allPeers) | ||
| { | ||
| var jcwGenerator = new JcwJavaSourceGenerator (); | ||
| var sources = jcwGenerator.GenerateContent (allPeers); | ||
| log ($"Generated {sources.Count} JCW Java source files."); | ||
| return sources.ToList (); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/Microsoft.Android.Sdk.TrimmableTypeMap/TrimmableTypeMapTypes.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,13 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| namespace Microsoft.Android.Sdk.TrimmableTypeMap; | ||
|
|
||
| public record TrimmableTypeMapResult ( | ||
| IReadOnlyList<GeneratedAssembly> GeneratedAssemblies, | ||
| IReadOnlyList<GeneratedJavaSource> GeneratedJavaSources, | ||
| IReadOnlyList<JavaPeerInfo> AllPeers); | ||
|
|
||
| public record GeneratedAssembly (string Name, MemoryStream Content); | ||
|
|
||
| public record GeneratedJavaSource (string RelativePath, string Content); |
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.
Uh oh!
There was an error while loading. Please reload this page.