From 7f1d33f41d06584e9a18843b41f2717910b84266 Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Wed, 18 Apr 2018 13:18:38 +0200 Subject: [PATCH 1/2] [jnimarshalmethod-gen] Update the originating assembly Update the originating assembly instead of creating a *new* one, called `-new.dll` I expected I would need to load the originating assembly in the separate AppDomain, but instead it was enough to load it with cecil with reading parameters having `ReadWrite = true`. Because the referenced assemblies might not be writable, we need to keep loading them as read only, thus with default `ReadWrite = false`. For that I added new method to our cecil assembly resolver, to add existing assembly definition to its cache. We first read the originating assemblies as *ReadWrite* and add them to the resolver cache. --- Makefile | 4 +--- .../DirectoryAssemblyResolver.cs | 12 ++++++++++++ tools/jnimarshalmethod-gen/App.cs | 4 +++- tools/jnimarshalmethod-gen/TypeMover.cs | 5 ++--- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 8b47b4507..1caa2f69a 100644 --- a/Makefile +++ b/Makefile @@ -159,9 +159,7 @@ run-android: $(ATESTS) run-test-jnimarshal: bin/Test$(CONFIGURATION)/Java.Interop.Export-Tests.dll bin/Test$(CONFIGURATION)/$(JAVA_INTEROP_LIB) MONO_TRACE_LISTENER=Console.Out \ $(RUNTIME) bin/$(CONFIGURATION)/jnimarshalmethod-gen.exe -v -L $(JI_MONO_LIB_PATH)mono/4.5 -L $(JI_MONO_LIB_PATH)mono/4.5/Facades "$<" - $(RM) "$<" "$(basename $<)-JniMarshalMethods.dll" - mv "$(basename $<)-new.dll" "$<" - mv "$(basename $<)-new.pdb" "$(basename $<).pdb" + $(RM) "$(basename $<)-JniMarshalMethods.dll" $(call RUN_TEST,$<) diff --git a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs index c596fed86..921e9bef9 100644 --- a/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs +++ b/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs @@ -110,6 +110,18 @@ public Dictionary ToResolverCache () return new Dictionary(cache); } + public bool AddToCache (AssemblyDefinition assembly) + { + var name = Path.GetFileNameWithoutExtension (assembly.MainModule.FileName); + + if (cache.ContainsKey (name)) + return false; + + cache [name] = assembly; + + return true; + } + public virtual AssemblyDefinition Load (string fileName, bool forceLoad = false) { if (!File.Exists (fileName)) diff --git a/tools/jnimarshalmethod-gen/App.cs b/tools/jnimarshalmethod-gen/App.cs index 0c1902ac9..bd2fb3a19 100644 --- a/tools/jnimarshalmethod-gen/App.cs +++ b/tools/jnimarshalmethod-gen/App.cs @@ -62,6 +62,7 @@ public static int Main (string [] args) return 0; } + var readWriteParameters = new ReaderParameters { ReadWrite = true, AssemblyResolver = resolver, ReadSymbols = true }; foreach (var assembly in assemblies) { if (!File.Exists (assembly)) { Error ($"Path '{assembly}' does not exist."); @@ -69,7 +70,8 @@ public static int Main (string [] args) } resolver.SearchDirectories.Add (Path.GetDirectoryName (assembly)); - resolver.Load (assembly); + var ad = AssemblyDefinition.ReadAssembly (assembly, readWriteParameters); + resolver.AddToCache (ad); try { CreateMarshalMethodAssembly (assembly); diff --git a/tools/jnimarshalmethod-gen/TypeMover.cs b/tools/jnimarshalmethod-gen/TypeMover.cs index 0ba1baacf..e2e425666 100644 --- a/tools/jnimarshalmethod-gen/TypeMover.cs +++ b/tools/jnimarshalmethod-gen/TypeMover.cs @@ -46,11 +46,10 @@ public void Move () return; } - var newName = $"{Path.Combine (Path.GetDirectoryName (Destination.MainModule.FileName), Path.GetFileNameWithoutExtension (Destination.MainModule.FileName))}-new{Path.GetExtension (Destination.MainModule.FileName)}"; - Destination.Write (newName, new WriterParameters () { WriteSymbols = true }); + Destination.Write (new WriterParameters () { WriteSymbols = true }); if (App.Verbose) - App.ColorWriteLine ($"Wrote {newName} assembly", ConsoleColor.Cyan); + App.ColorWriteLine ($"Wrote updated {Destination.MainModule.FileName} assembly", ConsoleColor.Cyan); } static readonly string nestedName = "__<$>_jni_marshal_methods"; From 6d1886bf064cc9ecfe7214330f2ca1924a8f72b8 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Wed, 18 Apr 2018 15:15:49 -0400 Subject: [PATCH 2/2] Fix indentation. --- tools/jnimarshalmethod-gen/App.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/jnimarshalmethod-gen/App.cs b/tools/jnimarshalmethod-gen/App.cs index bd2fb3a19..b098dac5e 100644 --- a/tools/jnimarshalmethod-gen/App.cs +++ b/tools/jnimarshalmethod-gen/App.cs @@ -62,7 +62,11 @@ public static int Main (string [] args) return 0; } - var readWriteParameters = new ReaderParameters { ReadWrite = true, AssemblyResolver = resolver, ReadSymbols = true }; + var readWriteParameters = new ReaderParameters { + AssemblyResolver = resolver, + ReadSymbols = true, + ReadWrite = true, + }; foreach (var assembly in assemblies) { if (!File.Exists (assembly)) { Error ($"Path '{assembly}' does not exist.");