Skip to content

Commit

Permalink
[jnimarshalmethod-gen] Update the originating assembly (#300)
Browse files Browse the repository at this point in the history
Update the source assembly instead of creating a *new* assembly named
`<AssemblyBaseName>-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.
  • Loading branch information
radekdoulik authored and jonpryor committed Apr 18, 2018
1 parent c084a83 commit a4a7fda
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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,$<)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public Dictionary<string, AssemblyDefinition> ToResolverCache ()
return new Dictionary<string, AssemblyDefinition>(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))
Expand Down
8 changes: 7 additions & 1 deletion tools/jnimarshalmethod-gen/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ public static int Main (string [] args)
return 0;
}

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.");
return 1;
}

resolver.SearchDirectories.Add (Path.GetDirectoryName (assembly));
resolver.Load (assembly);
var ad = AssemblyDefinition.ReadAssembly (assembly, readWriteParameters);
resolver.AddToCache (ad);

try {
CreateMarshalMethodAssembly (assembly);
Expand Down
5 changes: 2 additions & 3 deletions tools/jnimarshalmethod-gen/TypeMover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit a4a7fda

Please sign in to comment.