xamarin-macios/msbuild/Xamarin.MacDev.Tasks.Core/Tasks/UnpackLibraryResourcesTaskB...

226 строки
5.9 KiB
C#
Исходник Обычный вид История

2016-04-21 16:40:25 +03:00
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
2020-03-14 00:46:28 +03:00
using Xamarin.Localization.MSBuild;
2016-04-21 16:40:25 +03:00
namespace Xamarin.MacDev.Tasks
{
public abstract class UnpackLibraryResourcesTaskBase : XamarinTask
2016-04-21 16:40:25 +03:00
{
List<ITaskItem> unpackedResources = new List<ITaskItem> ();
2016-04-21 16:40:25 +03:00
#region Inputs
[Required]
public string Prefix { get; set; }
[Required]
public ITaskItem[] NoOverwrite { get; set; }
[Required]
public string IntermediateOutputPath { get; set; }
[Required]
public ITaskItem[] ReferencedLibraries { get; set; }
[msbuild] Fix how UnpackLibraryResources handles mscorlib.dll (and potentially other framework assemblies) (#1011) Target _UnpackLibraryResources: Task "UnpackLibraryResources" Using task UnpackLibraryResources from Xamarin.MacDev.Tasks.UnpackLibraryResources, Xamarin.MacDev.Tasks, Version=1.0.6128.15885, Culture=neutral, PublicKeyToken=null UnpackLibraryResources Task Prefix: monotouch IntermediateOutputPath: obj/iPhone/Debug/ NoOverwrite: obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/01J-lp-oVM-view-Ze5-6b-2t3.nib obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/Info.plist obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/UIViewController-01J-lp-oVM.nib obj/iPhone/Debug/ibtool-link/Main.storyboardc/BYZ-38-t0r-view-8bC-Xf-vdC.nib obj/iPhone/Debug/ibtool-link/Main.storyboardc/Info.plist obj/iPhone/Debug/ibtool-link/Main.storyboardc/UIViewController-BYZ-38-t0r.nib ReferencedLibraries: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Xml.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Core.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll /Users/poupou/Downloads/LinkingTest-2/RMSDKWrapper/bin/Debug//RMSDKWrapper.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Xml.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Core.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll Inspecting assembly: /Users/poupou/Downloads/LinkingTest-2/RMSDKWrapper/bin/Debug//RMSDKWrapper.dll Inspecting assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Inspecting assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Done executing task "UnpackLibraryResources" Done building target "_UnpackLibraryResources" in project "/Users/poupou/Downloads/LinkingTest-2/LinkingTest/LinkingTest.csproj". The above log excerpt shows two issues: 1. mscorlib.dll is needlessly inspected as it's **not** considered a "framework" assembly. The current check was checking *how* it was resolved and not *where* it was resolved to. The later is the most important as it's possible for other assemblies to have direct paths references and we do not want to process them. This is fixed by comparing each assembly path with the (now) provided `TargetFrameworkDirectory` 2. mscorlib.dll is inspected twice That's because it's present two times in the task's input. That issue is upstream (not sure why) of the current task but it makes #1 twice as costly. The fix for #1 indirectly fix that too. Future ------ It's worth investigating to move that logic into `mtouch`. The later must already load all assemblies and is in charge of removing other embedded data (e.g. native code from bindings) from the assemblies (so they are not shipped both inside and outside the .dll in the final .app). This makes this task seems extraneous work. Considering that my current test case, `RMSDKWrapper.dll`, is 1.3GB in size it's easy to see that the extra load (which has nothing to be extracted wrt resources*) is quite visible in build time. > 3268.201 ms UnpackLibraryResources 1 calls * it has for bindings but that's already handled by mtouch
2017-01-10 23:03:00 +03:00
[Required]
public ITaskItem[] TargetFrameworkDirectory { get; set; }
2016-04-21 16:40:25 +03:00
#endregion
#region Outputs
[Output]
public ITaskItem[] BundleResourcesWithLogicalNames { get; set; }
[Output]
public ITaskItem[] UnpackedResources { get; set; }
2016-04-21 16:40:25 +03:00
#endregion
public override bool Execute ()
{
// TODO: give each assembly its own intermediate output directory
// TODO: use list file to avoid re-extracting assemblies but allow FileWrites to work
var results = new List<ITaskItem> ();
HashSet<string> ignore = null;
foreach (var asm in ReferencedLibraries) {
[msbuild] Fix how UnpackLibraryResources handles mscorlib.dll (and potentially other framework assemblies) (#1011) Target _UnpackLibraryResources: Task "UnpackLibraryResources" Using task UnpackLibraryResources from Xamarin.MacDev.Tasks.UnpackLibraryResources, Xamarin.MacDev.Tasks, Version=1.0.6128.15885, Culture=neutral, PublicKeyToken=null UnpackLibraryResources Task Prefix: monotouch IntermediateOutputPath: obj/iPhone/Debug/ NoOverwrite: obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/01J-lp-oVM-view-Ze5-6b-2t3.nib obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/Info.plist obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/UIViewController-01J-lp-oVM.nib obj/iPhone/Debug/ibtool-link/Main.storyboardc/BYZ-38-t0r-view-8bC-Xf-vdC.nib obj/iPhone/Debug/ibtool-link/Main.storyboardc/Info.plist obj/iPhone/Debug/ibtool-link/Main.storyboardc/UIViewController-BYZ-38-t0r.nib ReferencedLibraries: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Xml.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Core.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll /Users/poupou/Downloads/LinkingTest-2/RMSDKWrapper/bin/Debug//RMSDKWrapper.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Xml.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Core.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll Inspecting assembly: /Users/poupou/Downloads/LinkingTest-2/RMSDKWrapper/bin/Debug//RMSDKWrapper.dll Inspecting assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Inspecting assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Done executing task "UnpackLibraryResources" Done building target "_UnpackLibraryResources" in project "/Users/poupou/Downloads/LinkingTest-2/LinkingTest/LinkingTest.csproj". The above log excerpt shows two issues: 1. mscorlib.dll is needlessly inspected as it's **not** considered a "framework" assembly. The current check was checking *how* it was resolved and not *where* it was resolved to. The later is the most important as it's possible for other assemblies to have direct paths references and we do not want to process them. This is fixed by comparing each assembly path with the (now) provided `TargetFrameworkDirectory` 2. mscorlib.dll is inspected twice That's because it's present two times in the task's input. That issue is upstream (not sure why) of the current task but it makes #1 twice as costly. The fix for #1 indirectly fix that too. Future ------ It's worth investigating to move that logic into `mtouch`. The later must already load all assemblies and is in charge of removing other embedded data (e.g. native code from bindings) from the assemblies (so they are not shipped both inside and outside the .dll in the final .app). This makes this task seems extraneous work. Considering that my current test case, `RMSDKWrapper.dll`, is 1.3GB in size it's easy to see that the extra load (which has nothing to be extracted wrt resources*) is quite visible in build time. > 3268.201 ms UnpackLibraryResources 1 calls * it has for bindings but that's already handled by mtouch
2017-01-10 23:03:00 +03:00
// mscorlib.dll was not coming out with ResolvedFrom == {TargetFrameworkDirectory}
// and what we really care is where it comes from, not how it was resolved
if (IsFrameworkAssembly (asm)) {
2020-03-14 00:46:28 +03:00
Log.LogMessage (MessageImportance.Low, MSBStrings.M0168, asm.ItemSpec);
2016-04-21 16:40:25 +03:00
} else {
var extracted = ExtractContentAssembly (asm.ItemSpec, IntermediateOutputPath);
foreach (var bundleResource in extracted) {
string logicalName;
if (ignore == null) {
// Create a hashset of the bundle resources that should not be overwritten by extracted resources
// from the referenced assemblies.
//
// See https://bugzilla.xamarin.com/show_bug.cgi?id=8409 for details.
ignore = new HashSet<string> ();
foreach (var item in NoOverwrite) {
logicalName = item.GetMetadata ("LogicalName");
if (string.IsNullOrEmpty (logicalName))
ignore.Add (logicalName);
}
}
logicalName = bundleResource.GetMetadata ("LogicalName");
if (!ignore.Contains (logicalName))
results.Add (bundleResource);
}
}
}
BundleResourcesWithLogicalNames = results.ToArray ();
UnpackedResources = unpackedResources.ToArray ();
2016-04-21 16:40:25 +03:00
return !Log.HasLoggedErrors;
}
[msbuild] Fix how UnpackLibraryResources handles mscorlib.dll (and potentially other framework assemblies) (#1011) Target _UnpackLibraryResources: Task "UnpackLibraryResources" Using task UnpackLibraryResources from Xamarin.MacDev.Tasks.UnpackLibraryResources, Xamarin.MacDev.Tasks, Version=1.0.6128.15885, Culture=neutral, PublicKeyToken=null UnpackLibraryResources Task Prefix: monotouch IntermediateOutputPath: obj/iPhone/Debug/ NoOverwrite: obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/01J-lp-oVM-view-Ze5-6b-2t3.nib obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/Info.plist obj/iPhone/Debug/ibtool-link/LaunchScreen.storyboardc/UIViewController-01J-lp-oVM.nib obj/iPhone/Debug/ibtool-link/Main.storyboardc/BYZ-38-t0r-view-8bC-Xf-vdC.nib obj/iPhone/Debug/ibtool-link/Main.storyboardc/Info.plist obj/iPhone/Debug/ibtool-link/Main.storyboardc/UIViewController-BYZ-38-t0r.nib ReferencedLibraries: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Xml.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Core.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll /Users/poupou/Downloads/LinkingTest-2/RMSDKWrapper/bin/Debug//RMSDKWrapper.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Xml.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/System.Core.dll Skipping framework assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Xamarin.iOS.dll Inspecting assembly: /Users/poupou/Downloads/LinkingTest-2/RMSDKWrapper/bin/Debug//RMSDKWrapper.dll Inspecting assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Inspecting assembly: /Users/poupou/git/xamarin/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS//mscorlib.dll Done executing task "UnpackLibraryResources" Done building target "_UnpackLibraryResources" in project "/Users/poupou/Downloads/LinkingTest-2/LinkingTest/LinkingTest.csproj". The above log excerpt shows two issues: 1. mscorlib.dll is needlessly inspected as it's **not** considered a "framework" assembly. The current check was checking *how* it was resolved and not *where* it was resolved to. The later is the most important as it's possible for other assemblies to have direct paths references and we do not want to process them. This is fixed by comparing each assembly path with the (now) provided `TargetFrameworkDirectory` 2. mscorlib.dll is inspected twice That's because it's present two times in the task's input. That issue is upstream (not sure why) of the current task but it makes #1 twice as costly. The fix for #1 indirectly fix that too. Future ------ It's worth investigating to move that logic into `mtouch`. The later must already load all assemblies and is in charge of removing other embedded data (e.g. native code from bindings) from the assemblies (so they are not shipped both inside and outside the .dll in the final .app). This makes this task seems extraneous work. Considering that my current test case, `RMSDKWrapper.dll`, is 1.3GB in size it's easy to see that the extra load (which has nothing to be extracted wrt resources*) is quite visible in build time. > 3268.201 ms UnpackLibraryResources 1 calls * it has for bindings but that's already handled by mtouch
2017-01-10 23:03:00 +03:00
bool IsFrameworkAssembly (ITaskItem asm)
{
var asm_path = asm.GetMetadata ("FullPath");
foreach (var dir in TargetFrameworkDirectory) {
if (asm_path.StartsWith (dir.GetMetadata ("FullPath"), StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
2016-04-21 16:40:25 +03:00
IEnumerable<ITaskItem> ExtractContentAssembly (string assembly, string intermediatePath)
{
Log.LogMessage (MessageImportance.Low, " Inspecting assembly: {0}", assembly);
if (!File.Exists (assembly))
yield break;
var asmWriteTime = File.GetLastWriteTimeUtc (assembly);
2016-04-21 16:40:25 +03:00
foreach (var embedded in GetAssemblyManifestResources (assembly)) {
string rpath;
if (embedded.Name.StartsWith ("__" + Prefix + "_content_", StringComparison.Ordinal)) {
var mangled = embedded.Name.Substring (("__" + Prefix + "_content_").Length);
rpath = UnmangleResource (mangled);
} else if (embedded.Name.StartsWith ("__" + Prefix + "_page_", StringComparison.Ordinal)) {
var mangled = embedded.Name.Substring (("__" + Prefix + "_page_").Length);
rpath = UnmangleResource (mangled);
} else {
continue;
}
var path = Path.Combine (intermediatePath, rpath);
var file = new FileInfo (path);
var item = new TaskItem (path);
item.SetMetadata ("LogicalName", rpath);
item.SetMetadata ("Optimize", "false");
if (file.Exists && file.LastWriteTimeUtc >= asmWriteTime) {
2016-04-21 16:40:25 +03:00
Log.LogMessage (" Up to date: {0}", rpath);
} else {
Log.LogMessage (" Unpacking: {0}", rpath);
Directory.CreateDirectory (Path.GetDirectoryName (path));
using (var stream = File.Open (path, FileMode.Create)) {
using (var resource = embedded.Open ())
resource.CopyTo (stream);
}
unpackedResources.Add (item);
}
2016-04-21 16:40:25 +03:00
yield return item;
}
yield break;
}
// FIXME: Using cecil for now, due to not having IKVM available in the mtbserver build.
// Eventually, we will want to prefer IKVM over cecil if we can work out the build in a sane way.
/*
static IEnumerable<ManifestResource> GetAssemblyManifestResources (string fileName)
{
using (var universe = new IKVM.Reflection.Universe ()) {
IKVM.Reflection.Assembly assembly;
try {
assembly = universe.LoadFile (fileName);
} catch {
yield break;
}
foreach (var _r in assembly.GetManifestResourceNames ()) {
var r = _r;
yield return new ManifestResource (r, () => assembly.GetManifestResourceStream (r));
}
}
}
*/
protected abstract IEnumerable<ManifestResource> GetAssemblyManifestResources (string fileName);
static string UnmangleResource (string mangled)
{
var unmangled = new StringBuilder (mangled.Length);
bool escaped = false;
for (int i = 0; i < mangled.Length; i++) {
char c = mangled[i];
if (c == '_' && !escaped) {
escaped = true;
continue;
}
if (escaped) {
switch (c) {
case 'b': c = '\\'; break;
case 'f': c = '/'; break;
case '_': c = '_'; break;
default: throw new FormatException ("Invalid resource name: " + mangled);
}
escaped = false;
}
unmangled.Append (c);
}
if (escaped)
throw new FormatException ("Invalid resource name: " + mangled);
return unmangled.ToString ();
}
public class ManifestResource
{
readonly Func<Stream> callback;
public ManifestResource (string name, Func<Stream> streamCallback)
{
callback = streamCallback;
Name = name;
}
public string Name {
get; private set;
}
public Stream Open ()
{
return callback ();
}
}
}
}