From 78f5e8710be34c1c488df87ce481a6532fdeaf49 Mon Sep 17 00:00:00 2001 From: Jeremi Kurdek <59935235+jkurdek@users.noreply.github.com> Date: Mon, 12 Aug 2024 20:02:24 +0200 Subject: [PATCH] Add unmanaged assembly skipping to ILStrip task (#106267) --- .../ILStrip/AssemblyStripper/AssemblyStripper.cs | 14 ++++++++++++++ src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs | 14 +++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/tasks/MonoTargetsTasks/ILStrip/AssemblyStripper/AssemblyStripper.cs b/src/tasks/MonoTargetsTasks/ILStrip/AssemblyStripper/AssemblyStripper.cs index c61cf219f7165..447da99ba52bd 100644 --- a/src/tasks/MonoTargetsTasks/ILStrip/AssemblyStripper/AssemblyStripper.cs +++ b/src/tasks/MonoTargetsTasks/ILStrip/AssemblyStripper/AssemblyStripper.cs @@ -237,5 +237,19 @@ public static void StripAssembly(string assemblyFile, string outputPath) AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyFile); AssemblyStripper.StripAssembly(assembly, outputPath); } + + public static bool TryStripAssembly(string assemblyFile, string outputPath) + { + try + { + StripAssembly(assemblyFile, outputPath); + return true; + } + // Skip unmanged assemblies + catch (ImageFormatException) + { + return false; + } + } } } diff --git a/src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs b/src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs index bdae476c4b068..1814cba54eb18 100644 --- a/src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs +++ b/src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs @@ -96,10 +96,7 @@ public override bool Execute() } }); - if (TrimIndividualMethods) - { - UpdatedAssemblies = ConvertAssembliesDictToOrderedList(_processedAssemblies, Assemblies).ToArray(); - } + UpdatedAssemblies = ConvertAssembliesDictToOrderedList(_processedAssemblies, Assemblies).ToArray(); if (!result.IsCompleted && !Log.HasLoggedErrors) { @@ -130,7 +127,14 @@ private bool StripAssembly(ITaskItem assemblyItem) try { - AssemblyStripper.AssemblyStripper.StripAssembly (assemblyFile, outputPath); + if(!AssemblyStripper.AssemblyStripper.TryStripAssembly(assemblyFile, outputPath)) + { + Log.LogMessage(MessageImportance.Low, $"[ILStrip] Skipping {assemblyFile} because it is not a managed assembly."); + } + else + { + _processedAssemblies.GetOrAdd(assemblyItem.ItemSpec, GetTrimmedAssemblyItem(assemblyItem, outputPath, assemblyFile)); + } } catch (Exception ex) {