Skip to content

Commit 724f1f3

Browse files
committed
[java-interop] Resuscitate Mono GC Bridge
Insert description here. `src/java-interop` support for building against MonoVM was broken at some point, and is arguably still *wrong* because even now it's using the legacy Mono header files, *not* the current MonoVM headers. TODO: figure out how to fix that. *For now*, fix `_CreateMonoInfoProps` so that `bin/Build$(Configuration)/MonoInfo.props` is created, which in turn allows `src/java-interop` ***on macOS and Linux*** to use the mono headers and link against libmonosgen-2.0.dylib/etc.: % nm src/java-interop/obj/Debug-net8.0/libjava-interop.dylib | grep _mono … U _mono_class_from_mono_type Currently unknown: how do we *use* `libjava-interop.dylib`? The 'obvious' way is to build a project with `-p:UseMonoRuntime=true`. That fails under .NET 9: error NU1102: Unable to find package Microsoft.NETCore.App.Runtime.Mono.osx-x64 with version (= 9.0.2) Try to re-add support for building with .NET 8, by overriding `$(DotNetTargetFramework)`, which *does* build with `-p:UseMonoRuntime=true`: ``` dotnet build -c Release -t:Prepare -p:DotNetTargetFramework=net8.0 *.sln dotnet build -c Release -p:DotNetTargetFramework=net8.0 *.sln dotnet publish --self-contained -p:UseMonoRuntime=true -p:DotNetTargetFramework=net8.0 -p:UseAppHost=true -p:ErrorOnDuplicatePublishOutputFiles=false -r osx-x64 samples/Hello-Java.Base/Hello-Java.Base.csproj cp samples/Hello-Java.Base/bin/Release/Hello-Java.Base.jar samples/Hello-Java.Base/bin/Release/osx-x64/publish samples/Hello-Java.Base/bin/Release/osx-x64/publish/Hello-Java.Base ``` Update `JreRuntime.cs` to check for the existence of `Mono.RuntimeStructs`, as `Mono.Runtime` no longer exists. I don't yet know if this uses the GC bridge, though.
1 parent 719e615 commit 724f1f3

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

build-tools/Java.Interop.BootstrapTasks/Java.Interop.BootstrapTasks.targets

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project>
22

33
<Target Name="_CreateMonoInfoProps"
4-
Condition=" ($([MSBuild]::IsOSPlatform ('linux')) Or $([MSBuild]::IsOSPlatform ('osx'))) And '$(_MonoPath)' != '' "
4+
Condition=" ($([MSBuild]::IsOSPlatform ('linux')) Or $([MSBuild]::IsOSPlatform ('osx'))) "
55
AfterTargets="AfterBuild"
66
DependsOnTargets="_GetLinuxMonoPaths;_GetMacOSMonoPaths"
77
Inputs="$(MSBuildThisFileFullPath);$(MSBuildThisFileDirectory)Java.Interop.BootstrapTasks.csproj"
88
Outputs="$(_OutputPath)MonoInfo.props">
9-
<ItemGroup>
9+
<ItemGroup Condition=" Exists($(_MonoPath)) ">
1010
<_MonoInfoLine Include="&lt;Project&gt;" />
1111
<_MonoInfoLine Include=" &lt;Choose&gt;" />
1212
<_MonoInfoLine Include=" &lt;When Condition=&quot; %27%24(MonoFrameworkPath)%27 == %27%27 &quot;&gt;" />
@@ -22,11 +22,15 @@
2222
<_MonoInfoLine Include="&lt;/Project&gt;" />
2323
</ItemGroup>
2424
<WriteLinesToFile
25+
Condition=" Exists($(_MonoPath)) "
2526
File="$(_OutputPath)MonoInfo.props"
2627
Lines="@(_MonoInfoLine)"
2728
Overwrite="True"
2829
/>
29-
<Touch Files="$(_OutputPath)MonoInfo.props" />
30+
<Touch
31+
Condition=" Exists($(_MonoPath)) "
32+
Files="$(_OutputPath)MonoInfo.props"
33+
/>
3034
</Target>
3135

3236
<Target Name="_CreateMonoMk"
@@ -84,6 +88,7 @@
8488
<_MonoFrameworkPath>$(_MonoLibPath)libmonosgen-2.0.1.dylib</_MonoFrameworkPath>
8589
<_MonoIncludePath>$(_MonoBase)Headers/mono-2.0</_MonoIncludePath>
8690
<_MonoLibs>-L "$(_MonoLibPath)" -lmonosgen-2.0</_MonoLibs>
91+
<_MonoPath>$(_MonoBase)Commands/mono</_MonoPath>
8792
</PropertyGroup>
8893
</Target>
8994

src/Java.Interop/Java.Interop/JniRuntime.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,9 @@ public virtual void OnEnterMarshalMethod ()
445445
public virtual void OnUserUnhandledException (ref JniTransition transition, Exception e)
446446
{
447447
transition.SetPendingException (e);
448-
448+
#if NET9_0_OR_GREATER
449449
Debugger.BreakForUserUnhandledException (e);
450+
#endif // NET9_0_OR_GREATER
450451
}
451452

452453
public virtual void RaisePendingException (Exception pendingException)

src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ static unsafe JreRuntimeOptions CreateJreVM (JreRuntimeOptions builder)
9191
builder.TypeManager ??= new JreTypeManager (builder.typeMappings);
9292
#endif // NET
9393

94-
bool onMono = Type.GetType ("Mono.Runtime", throwOnError: false) != null;
94+
bool onMono = Type.GetType ("Mono.RuntimeStructs", throwOnError: false) != null;
9595
if (onMono) {
96+
Console.WriteLine ($"MonoVM support enabled");
9697
builder.ValueManager = builder.ValueManager ?? new MonoRuntimeValueManager ();
9798
builder.ObjectReferenceManager = builder.ObjectReferenceManager ?? new MonoRuntimeObjectReferenceManager ();
9899
}

src/java-interop/java-interop.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</Target>
4848

4949
<Target Name="_GetCmakeOptions">
50-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net472' And '@(MonoIncludePath->Count())' != '0' ">
50+
<PropertyGroup Condition=" '@(MonoIncludePath->Count())' != '0' ">
5151
<_MonoDirs>"-DMONO_INCLUDE_LIST=@(MonoIncludePath, ';')"</_MonoDirs>
5252
<_MonoLib>"-DMONO_LINK_FLAGS=$(MonoLibs)"</_MonoLib>
5353
<_EnableMono>-DENABLE_MONO_INTEGRATION=ON</_EnableMono>

0 commit comments

Comments
 (0)