Commit a5b229d
authored
[java-interop] Resuscitate MonoVM GC Bridge (#1316)
Context: dotnet/runtime#112109
Context: f5ce0ad
Context: 3824b97
Context: c6c487b
Commit f5ce0ad copied portions of the .NET for Android Mono GC Bridge
code into `src/java-interop`, but to *build* that code "on Desktop" --
for possible CI and unit test use -- it needed to build against Mono
*somehow*.
The *actual* "how" was unspecified:
* The macOS build implicitly required that
`/Library/Frameworks/Mono.framework` exist, and would pull in
header files and libraries from there.
This directory would only exist if you installed Mono system-wide
on your Mac.
* Linux and Windows were not support *at all*.
Eventually `src/java-interop` got Windows build support in 3824b97.
This allowed `Java.Interop-Tests.dll` to run under .NET Core, but
this did *not* contain any GC bridge code; it was *just* the JNI
wrapper functions such as `java_interop_jnienv_get_version()` and
JVM loading functions such as `java_interop_jvm_create()`.
Eventually (c6c487b), a native `java-interop` library was not needed
*at all*: C#9 function pointers and `NativeLibrary` removed the need
for it to be built at all.
The Mono GC bridge code within java-interop effectively bitrotted, as
nothing *built* it anymore. .NET for Android has its own (original!)
copy, and CI is primarily concerned about .NET (Core) and not Mono.
(This means the GC bridge *isn't* tested, but everything else is.)
Three things have happened in the meantime:
1. The Mono team was absorbed into the .NET team, with the new
"MonoVM" as a supported backend for .NET, selectable by setting
`$(UseMonoRuntime)`=true on desktop platforms. MonoVM is used by
.NET for Android and a supported backend with .NET for iOS, Mac…
2. MonoVM is now provided as NuGet packages!
3. There are now efforts to investigate adding something like the GC
Bridge to CoreCLR and NativeAOT; see dotnet/runtime#112109.
Some developers involved with this would like to debug how the
current GC bridge works. *On desktop Windows*.
Embrace the new world order: remove support for using "system" Mono
installations, the `_CreateMonoInfoProps` target, generation of
`MonoInfo.props`, etc., and instead use MonoVM packages to provide
the MonoVM runtime and header files needed for compilation & linking.
This allows for a consistent build environment across Linux, macOS,
and Windows, though Windows adds one wrinkle:
MonoVM doesn't contain a `coreclr.lib` file for linking.
Add `coreclr.def` and a `coreclr.lib` file to use for linking against
MonoVM on Windows. `coreclr.lib` was created by:
lib /def:coreclr.def /out:coreclr.lib /machine:X64
where `coreclr.def` contains the `mono_` symbols that were referenced
by the macOS `libjava-interop.dylib` build, as per
`nm bin/Release-net8.0/libjava-interop.dylib | grep 'U _mono'`.
Update `src/java-interop` so that it builds with the latest MonoVM
headers (`<mono/metadata/appdomain.h>` must now be included), and
allow it to build under Windows (use `Interlocked*` instead of
clang's `__sync_*_and_fetch()`.)
There is one ("tiny") problem with this approach: .NET 9 doesn't
contain MonoVM packages. For example, the
[`Microsoft.NETCore.App.Runtime.Mono.osx-x64` NuGet package][0] was
last updated for .NET 9 Preview 7; the last *stable* version is for
.NET *8*.
Fortunately we only recently migrated to .NET 9 (f30e420).
Update `JniRuntime.cs` so that we can build the repo when overriding
with `-p:DotNetTargetFramework=net8.0`.
Update `Java.Interop.Sdk` so that the `.jar` it creates is
appropriately copied to `$(OutputPath)` of the referencing project.
This required using `%(Content.CopyToOutputDirectory)`=PreserveNewest
alongside using `%(Content.TargetPath)` -- which sets the relative
name within `$(OutputPath)` -- and in turn required making
`_JavaCreateJcws` run after `CoreCompile` instead of `Build`, so that
it would properly participate in `%(CopyToOutputDirectory)` logic.
The code to detect that MonoVM is being used within
`Java.Runtime.Environment.dll` has changed; the `Mono.Runtime` type
no longer exists. Check for `Mono.RuntimeStructs` instead.
Update `TestJVM.GetJvmLibraryPath()` so that it can find
`bin\BuildRelease\JdkInfo.props` when running an app from the
`samples` directory and not from `bin\TestRelease*\…`.
All of this allows for `samples/Hello-Java.Base` to be built against
and run with MonoVM, on Windows!
# need Release config build because `dotnet publish` is implicitly Release
dotnet build -c Release -t:Prepare -p:DotNetTargetFramework=net8.0 Java.Interop.sln
dotnet build -c Release -p:DotNetTargetFramework=net8.0 Java.Interop.sln
# -p:UseMonoRuntime enables use of MonoVM
dotnet publish --self-contained -p:UseMonoRuntime=true -p:DotNetTargetFramework=net8.0 -p:UseAppHost=true -p:ErrorOnDuplicatePublishOutputFiles=false samples/Hello-Java.Base/Hello-Java.Base.csproj -r win-x64
Note: replace `-r win-x64` with the appropriate RID for your host.
With that setup, you can then run the sample:
> samples\Hello-Java.Base\bin\Release\win-x64\publish\Hello-Java.Base.exe
MonoVM support enabled
# jonp: LoadJvmLibrary(C:\Users\jopryo\android-toolchain\jdk-17\bin\server\jvm.dll)=140710450692096
# jonp: JNI_CreateJavaVM=140710454831248; JNI_GetCreatedJavaVMs=140710454831280
# jonp: executing JNI_CreateJavaVM=7ff9b4ad2890
# jonp: r=0 javavm=7ff9b51fd7f0 jnienv=24d781dddc0
WARNING in native method: JNI call made without checking exceptions when required to from CallStaticObjectMethodV
binding? net.dot.jni.sample.MyJLO@a09ee92
[0]: https://www.nuget.org/packages/Microsoft.NETCore.App.Runtime.Mono.osx-x64/1 parent 6096f8b commit a5b229d
File tree
16 files changed
+182
-172
lines changed- build-tools
- Java.Interop.BootstrapTasks
- Java.Interop.Sdk/Sdk
- samples/Hello-Java.Base
- src
- Java.Interop/Java.Interop
- Java.Runtime.Environment/Java.Interop
- java-interop
- tests
- Java.Base-Tests
- NativeTiming
- TestJVM
16 files changed
+182
-172
lines changedLines changed: 0 additions & 88 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | 3 | | |
92 | 4 | | |
93 | 5 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
| |||
169 | 177 | | |
170 | 178 | | |
171 | 179 | | |
172 | | - | |
173 | | - | |
| 180 | + | |
| 181 | + | |
174 | 182 | | |
175 | 183 | | |
176 | 184 | | |
| |||
184 | 192 | | |
185 | 193 | | |
186 | 194 | | |
187 | | - | |
| 195 | + | |
188 | 196 | | |
189 | 197 | | |
190 | 198 | | |
| |||
197 | 205 | | |
198 | 206 | | |
199 | 207 | | |
200 | | - | |
| 208 | + | |
201 | 209 | | |
202 | 210 | | |
203 | 211 | | |
| |||
215 | 223 | | |
216 | 224 | | |
217 | 225 | | |
218 | | - | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
219 | 231 | | |
220 | 232 | | |
221 | 233 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| 25 | + | |
24 | 26 | | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | 30 | | |
29 | | - | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
37 | 48 | | |
38 | 49 | | |
39 | 50 | | |
| |||
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
48 | | - | |
| 59 | + | |
49 | 60 | | |
50 | 61 | | |
51 | 62 | | |
| |||
70 | 81 | | |
71 | 82 | | |
72 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
73 | 91 | | |
74 | 92 | | |
75 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
445 | 445 | | |
446 | 446 | | |
447 | 447 | | |
448 | | - | |
| 448 | + | |
449 | 449 | | |
| 450 | + | |
450 | 451 | | |
451 | 452 | | |
452 | 453 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
| 96 | + | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Binary file not shown.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
533 | 536 | | |
| 537 | + | |
534 | 538 | | |
535 | 539 | | |
536 | 540 | | |
537 | 541 | | |
538 | 542 | | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
539 | 546 | | |
| 547 | + | |
540 | 548 | | |
541 | 549 | | |
542 | 550 | | |
| |||
0 commit comments