[tools] Add nuget-hash-extractor tool to help produce the runtime ignored assemblies...
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 17 Jan 2017 22:31:32 +0000 (14:31 -0800)
committerRodrigo Kumpera <kumpera@gmail.com>
Tue, 17 Jan 2017 22:35:57 +0000 (14:35 -0800)
This tool takes a folder full of nupkg file and print macro invocations to add to image.c:ignored_assemblies.

Usage:

make download
make run

The download.sh script downloads all nugets we want to ignore. So update it before running if you have more you want to add.

tools/nuget-hash-extractor/Makefile [new file with mode: 0644]
tools/nuget-hash-extractor/download.sh [new file with mode: 0755]
tools/nuget-hash-extractor/nuget-hash-extractor.cs [new file with mode: 0644]

diff --git a/tools/nuget-hash-extractor/Makefile b/tools/nuget-hash-extractor/Makefile
new file mode 100644 (file)
index 0000000..1d52b77
--- /dev/null
@@ -0,0 +1,13 @@
+SOURCES = \
+       nuget-hash-extractor.cs
+
+nuget-hash-extractor.exe: $(SOURCES)
+        mcs /r:System.Xml.Linq  /r:System.IO.Compression -o:nuget-hash-extractor.exe $(SOURCES)
+
+download:
+       echo "Downloading all the nugets";      \
+       ./download.sh
+
+run: nuget-hash-extractor.exe
+       mono nuget-hash-extractor.exe nugets
+.PHONY: download run
diff --git a/tools/nuget-hash-extractor/download.sh b/tools/nuget-hash-extractor/download.sh
new file mode 100755 (executable)
index 0000000..978ce89
--- /dev/null
@@ -0,0 +1,27 @@
+mkdir nugets
+
+#System.Runtime.InteropServices.RuntimeInformation
+wget https://www.nuget.org/api/v2/package/System.Runtime.InteropServices.RuntimeInformation/4.3.0 -O nugets/system.runtime.interopservices.runtimeinformation.4.3.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.Runtime.InteropServices.RuntimeInformation/4.0.0 -O nugets/system.runtime.interopservices.runtimeinformation.4.0.0.nupkg
+
+#System.Globalization.Extensions
+wget https://www.nuget.org/api/v2/package/System.Globalization.Extensions/4.3.0 -O nugets/system.globalization.extensions.4.3.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.Globalization.Extensions/4.0.1 -O nugets/system.globalization.extensions.4.0.1.nupkg
+wget https://www.nuget.org/api/v2/package/System.Globalization.Extensions/4.0.0 -O nugets/system.globalization.extensions.4.0.0.nupkg
+
+#System.IO.Compression
+wget https://www.nuget.org/api/v2/package/System.IO.Compression/4.3.0 -O nugets/system.io.compression.4.3.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.IO.Compression/4.1.0 -O nugets/system.io.compression.4.1.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.IO.Compression/4.0.0 -O nugets/system.io.compression.4.0.0.nupkg
+
+#System.Net.Http
+wget https://www.nuget.org/api/v2/package/System.Net.Http/4.3.0 -O nugets/system.net.http.4.3.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.Net.Http/4.1.1 -O nugets/system.net.http.4.1.1.nupkg
+wget https://www.nuget.org/api/v2/package/System.Net.Http/4.1.0 -O nugets/system.net.http.4.1.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.Net.Http/4.0.0 -O nugets/system.net.http.4.0.0.nupkg
+
+#System.Text.Encoding.CodePages
+wget https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.3.0 -O nugets/system.text.encoding.codepages.4.3.0.nupkg
+wget https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.0.1 -O nugets/system.text.encoding.codepages.4.0.1.nupkg
+wget https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.0.0 -O nugets/system.text.encoding.codepages.4.0.0.nupkg
+
diff --git a/tools/nuget-hash-extractor/nuget-hash-extractor.cs b/tools/nuget-hash-extractor/nuget-hash-extractor.cs
new file mode 100644 (file)
index 0000000..310abd2
--- /dev/null
@@ -0,0 +1,85 @@
+using System;
+using System.Xml.Linq;
+using System.Linq;
+using System.IO;
+using System.IO.Compression;
+using System.Reflection;
+
+class Driver {
+       static ZipArchiveEntry FindSpecFile (ZipArchive zip) {
+               foreach (var entry in zip.Entries) {
+                       if (entry.Name.EndsWith (".nuspec"))
+                               return entry;
+               }
+               throw new Exception ("Could not find nuspec file");
+       }
+
+       static void DumpNuget (string nupkg) {
+               var zip = new ZipArchive(new FileStream (nupkg, FileMode.Open));
+
+               var nuspec = FindSpecFile (zip);
+               var l = XElement.Load (new StreamReader (nuspec.Open ()));
+               var version = (from el in l.Descendants() where el.Name.LocalName == "version" select el.Value).FirstOrDefault ();
+
+               foreach (var et in from e in zip.Entries where (e.FullName.StartsWith ("lib/net4") || e.FullName.StartsWith ("lib/netcore")) && e.Name.EndsWith (".dll") select e) {
+                       LoadAndDump (et, version);
+               }
+       }
+       static void Main (string[] args) {
+               foreach (var f in Directory.GetFiles (args [0], "*.nupkg")) {
+                       DumpNuget (f);
+               }
+       }
+
+       static byte[] StreamToArray (Stream s) {
+               using(var ms = new MemoryStream ()) {
+                       s.CopyTo (ms);
+                       return ms.ToArray ();
+               }
+       }
+
+       static int domain_id = 1;
+       static void LoadAndDump (ZipArchiveEntry entry, string version) {
+               // Console.WriteLine ("Dumping {0}", entry);
+               var data = StreamToArray (entry.Open ());
+               AppDomain ad = AppDomain.CreateDomain ("parse_" + ++domain_id);
+               DoParse p = (DoParse)ad.CreateInstanceAndUnwrap (typeof (DoParse).Assembly.FullName, typeof (DoParse).FullName);
+               p.ParseAssembly (data, version, entry.Name, entry.FullName);
+               AppDomain.Unload (ad);
+       }
+}
+
+class DoParse : MarshalByRefObject {
+       static int Hash (string str) {
+               int h = 5381;
+        for (int i = 0;  i < str.Length; ++i)
+            h = ((h << 5) + h) ^ str[i];
+               return h;
+       }
+       static string FileToEnum (string name) {
+               switch (name) {
+               case "System.Runtime.InteropServices.RuntimeInformation.dll": return "SYS_RT_INTEROP_RUNTIME_INFO";
+               case "System.Globalization.Extensions.dll": return "SYS_GLOBALIZATION_EXT";
+               case "System.IO.Compression.dll": return "SYS_IO_COMPRESSION";
+               case "System.Net.Http.dll": return "SYS_NET_HTTP";
+               case "System.Text.Encoding.CodePages.dll": return "SYS_TEXT_ENC_CODEPAGES";
+               default: throw new Exception ($"No idea what to do with {name}");
+               }
+       }
+
+       static string FileToMoniker (string p) {
+               var parts = p.Split (Path.DirectorySeparatorChar);
+               return parts[parts.Length - 2];
+       }
+
+       public void ParseAssembly (byte[] data, string version, string name, string fullname) {
+               var a = Assembly.ReflectionOnlyLoad (data);
+               var m = a.GetModules ()[0];
+               var id = m.ModuleVersionId.ToString ().ToUpper ();
+               var hash_code = Hash (id).ToString ("X");
+               var str = FileToEnum (name);
+
+               string ver_str = version + " " + FileToMoniker (fullname);      
+               Console.WriteLine ($"IGNORED_ASSEMBLY (0x{hash_code}, {str}, \"{id}\", \"{ver_str}\"),");
+       }
+}
\ No newline at end of file