Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,93 +1,5 @@
<Project>

<Target Name="_CreateMonoInfoProps"
Condition=" ($([MSBuild]::IsOSPlatform ('linux')) Or $([MSBuild]::IsOSPlatform ('osx'))) And '$(_MonoPath)' != '' "
AfterTargets="AfterBuild"
DependsOnTargets="_GetLinuxMonoPaths;_GetMacOSMonoPaths"
Inputs="$(MSBuildThisFileFullPath);$(MSBuildThisFileDirectory)Java.Interop.BootstrapTasks.csproj"
Outputs="$(_OutputPath)MonoInfo.props">
<ItemGroup>
<_MonoInfoLine Include="&lt;Project&gt;" />
<_MonoInfoLine Include=" &lt;Choose&gt;" />
<_MonoInfoLine Include=" &lt;When Condition=&quot; %27%24(MonoFrameworkPath)%27 == %27%27 &quot;&gt;" />
<_MonoInfoLine Include=" &lt;PropertyGroup&gt;" />
<_MonoInfoLine Include=" &lt;MonoFrameworkPath&gt;$(_MonoFrameworkPath)&lt;/MonoFrameworkPath&gt;" />
<_MonoInfoLine Include=" &lt;MonoLibs &gt;$(_MonoLibs)&lt;/MonoLibs&gt;" />
<_MonoInfoLine Include=" &lt;/PropertyGroup&gt;" />
<_MonoInfoLine Include=" &lt;ItemGroup&gt;" />
<_MonoInfoLine Include=" &lt;MonoIncludePath Include=&quot;$(_MonoIncludePath)&quot; /&gt;" />
<_MonoInfoLine Include=" &lt;/ItemGroup&gt;" />
<_MonoInfoLine Include=" &lt;/When&gt;" />
<_MonoInfoLine Include=" &lt;/Choose&gt;" />
<_MonoInfoLine Include="&lt;/Project&gt;" />
</ItemGroup>
<WriteLinesToFile
File="$(_OutputPath)MonoInfo.props"
Lines="@(_MonoInfoLine)"
Overwrite="True"
/>
<Touch Files="$(_OutputPath)MonoInfo.props" />
</Target>

<Target Name="_CreateMonoMk"
AfterTargets="AfterBuild"
DependsOnTargets="_GetLinuxMonoPaths;_GetMacOSMonoPaths"
Inputs="$(MSBuildThisFileFullPath);$(MSBuildThisFileDirectory)Java.Interop.BootstrapTasks.csproj"
Outputs="$(_OutputPath)mono.mk">
<ItemGroup>
<_MonoMkLine Include="JI_MONO_LIB_PATH=$(_MonoLibPath)" />
</ItemGroup>
<WriteLinesToFile
File="$(_OutputPath)mono.mk"
Lines="@(_MonoMkLine)"
Overwrite="True"
/>
<Touch Files="$(_OutputPath)mono.mk" />
</Target>

<Target Name="_GetLinuxMonoPath"
Condition=" $([MSBuild]::IsOSPlatform ('linux')) ">
<Exec
Command="which mono"
ConsoleToMsBuild="true"
IgnoreExitCode="true">
<Output TaskParameter="ConsoleOutput" PropertyName="_MonoPath" />
</Exec>
</Target>

<Target Name="_GetLinuxMonoPaths"
DependsOnTargets="_GetLinuxMonoPath"
Condition=" $([MSBuild]::IsOSPlatform ('linux')) And '$(_MonoPath)' != '' ">
<Exec
Command="pkg-config --variable=libdir mono-2"
ConsoleToMsBuild="True">
<Output TaskParameter="ConsoleOutput" PropertyName="_MonoPkgConfigLibdir" />
</Exec>
<Exec
Command="pkg-config --variable=includedir mono-2"
ConsoleToMsBuild="True">
<Output TaskParameter="ConsoleOutput" PropertyName="_MonoPkgConfigIncludedir" />
</Exec>
<PropertyGroup>
<_MonoLibPath>$([System.IO.Path]::GetDirectoryName($(_MonoPath)))/../lib/</_MonoLibPath>
<_MonoFrameworkPath>$(_MonoPkgConfigLibdir)/libmonosgen-2.0.so</_MonoFrameworkPath>
<_MonoIncludePath>$(_MonoPkgConfigIncludedir)</_MonoIncludePath>
<_MonoLibs>-L "$(_MonoPkgConfigLibdir)" -lmonosgen-2.0</_MonoLibs>
</PropertyGroup>
</Target>

<Target Name="_GetMacOSMonoPaths"
Condition=" $([MSBuild]::IsOSPlatform ('osx')) ">
<PropertyGroup>
<_MonoBase>/Library/Frameworks/Mono.framework/</_MonoBase>
<_MonoLibPath>$(_MonoBase)Libraries/</_MonoLibPath>
<_MonoFrameworkPath>$(_MonoLibPath)libmonosgen-2.0.1.dylib</_MonoFrameworkPath>
<_MonoIncludePath>$(_MonoBase)Headers/mono-2.0</_MonoIncludePath>
<_MonoLibs>-L "$(_MonoLibPath)" -lmonosgen-2.0</_MonoLibs>
</PropertyGroup>
</Target>


<Target Name="_CreatePackagePathsProps"
AfterTargets="AfterBuild"
Inputs="$(MSBuildThisFileFullPath);$(MSBuildThisFileDirectory)Java.Interop.BootstrapTasks.csproj"
Expand Down
26 changes: 19 additions & 7 deletions build-tools/Java.Interop.Sdk/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemDefinitionGroup>

<PropertyGroup>
<JavaOutputJar Condition=" '$(JavaOutputJar)' == '' ">$(OutputPath)$(AssemblyName).jar</JavaOutputJar>
<JavaOutputJarName Condition=" '$(JavaOutputJarName)' == '' ">$(AssemblyName).jar</JavaOutputJarName>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -32,7 +32,7 @@
</Target>

<Target Name="JavaCreateOutputJar"
AfterTargets="Build"
AfterTargets="CoreCompile"
DependsOnTargets="$(JavaCreateOutputJarDependsOn)">
</Target>

Expand All @@ -43,8 +43,16 @@
<_JavaManagedBindingDir>$(_JavaIntermediateDir)mcw\</_JavaManagedBindingDir>
<_JavaJcwClassesDir>$(_JavaIntermediateDir)classes\</_JavaJcwClassesDir>
<_JavaJcwSourcesDir>$(_JavaIntermediateDir)java\</_JavaJcwSourcesDir>
<_JavaOutputJarPath>$(_JavaIntermediateDir)$(JavaOutputJarName)</_JavaOutputJarPath>
</PropertyGroup>

<ItemGroup>
<Content Include="$(_JavaOutputJarPath)"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(JavaOutputJarName)"
/>
</ItemGroup>

<Target Name="_CollectJavaCompileForManagedBindingInputs">
<ItemGroup>
<_JavaCompileForBindingInputs
Expand Down Expand Up @@ -169,8 +177,8 @@
<Target Name="_CleanupManagedBinding" />

<Target Name="_JavaCreateJcws"
Condition=" '$(TargetPath)' != '' And Exists($(TargetPath))"
Inputs="$(TargetPath)"
Condition=" '@(IntermediateAssembly)' != '' And Exists(@(IntermediateAssembly))"
Inputs="@(IntermediateAssembly)"
Outputs="$(_JavaJcwSourcesDir).stamp">
<RemoveDir Directories="$(_JavaJcwSourcesDir)" />
<MakeDir Directories="$(_JavaJcwSourcesDir)" />
Expand All @@ -184,7 +192,7 @@
<_Output>-o "$(_JavaJcwSourcesDir)."</_Output>
<_Libpath>@(_RefAsmDirs->'-L "%(Identity)"', ' ')</_Libpath>
</PropertyGroup>
<Exec Command="$(DotnetToolPath) $(_JcwGen) -v &quot;$(TargetPath)&quot; $(_Target) $(_Output) $(_Libpath)" />
<Exec Command="$(DotnetToolPath) $(_JcwGen) -v @(IntermediateAssembly->'&quot;%(Identity)&quot;', ' ') $(_Target) $(_Output) $(_Libpath)" />
<Touch Files="$(_JavaJcwSourcesDir).stamp" AlwaysCreate="True" />
</Target>

Expand All @@ -197,7 +205,7 @@
<Target Name="_JavaCreateOutputJar"
DependsOnTargets="_JavaCollectGeneratdJcwSource;_JavaCollectJavacRefs"
Inputs="@(_JavaGeneratedJcwSource)"
Outputs="$(JavaOutputJar)">
Outputs="$(_JavaOutputJarPath)">
<RemoveDir Directories="$(_JavaJcwClassesDir)" />
<MakeDir Directories="$(_JavaJcwClassesDir)" />
<PropertyGroup>
Expand All @@ -215,7 +223,11 @@
/>
<Exec Command="&quot;$(JavaCPath)&quot; $(_JavacSourceOptions) -d &quot;$(_JavaJcwClassesDir).&quot; -classpath &quot;$(_Classpath)&quot; &quot;@$(_JavaIntermediateDir)_java_sources.txt&quot;" />
<Delete Files="$(_JavaIntermediateDir)_java_sources.txt" />
<Exec Command="&quot;$(JarPath)&quot; cf &quot;$(JavaOutputJar)&quot; -C &quot;$(_JavaJcwClassesDir).&quot; ." />
<Exec Command="&quot;$(JarPath)&quot; cf &quot;$(_JavaOutputJarPath)&quot; -C &quot;$(_JavaJcwClassesDir).&quot; ." />

<ItemGroup>
<FileWrites Include="$(_JavaOutputJarPath)" />
</ItemGroup>
</Target>

</Project>
24 changes: 21 additions & 3 deletions samples/Hello-Java.Base/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,34 @@ class App

public static void Main (string[] args)
{
string? jvmPath = global::Java.InteropTests.TestJVM.GetJvmLibraryPath ();
var logger = (Action<TraceLevel, string>?) null;
string? jvmPath = null;
bool createMultipleVMs = false;
bool reportTiming = false;
bool showHelp = false;
int verbosity = 0;
var options = new OptionSet () {
"Using the JVM from C#!",
"",
"Options:",
{ "jvm=",
$"{{PATH}} to JVM to use. Default is:\n {jvmPath}",
$"{{PATH}} to JVM to use. Default is:\n{Java.InteropTests.TestJVM.GetJvmLibraryPath (logger)}",
v => jvmPath = v },
{ "m",
"Create multiple Java VMs. This will likely crash.",
v => createMultipleVMs = v != null },
{ "t",
$"Timing; invoke Object.hashCode() {N} times, print average.",
v => reportTiming = v != null },
{ "v|verbosity:",
$"Set console log verbosity to {{LEVEL}}. Default is 0.",
(int? v) => {
verbosity = v.HasValue ? v.Value : verbosity + 1;
if (verbosity > 0) {
logger = CreateConsoleLogger ();
}
}
},
{ "h|help",
"Show this message and exit.",
v => showHelp = v != null },
Expand All @@ -45,7 +56,7 @@ public static void Main (string[] args)
}
var builder = new JreRuntimeOptions () {
JniAddNativeMethodRegistrationAttributePresent = true,
JvmLibraryPath = jvmPath,
JvmLibraryPath = jvmPath ?? global::Java.InteropTests.TestJVM.GetJvmLibraryPath (logger),
ClassPath = {
Path.Combine (Path.GetDirectoryName (typeof (App).Assembly.Location)!, "Hello-Java.Base.jar"),
},
Expand All @@ -70,6 +81,13 @@ public static void Main (string[] args)
CreateJLO ();
}

static Action<TraceLevel, string> CreateConsoleLogger ()
{
return (level, message) => {
Console.WriteLine ($"# {level}: {message}");
};
}

static void CreateJLO ()
{
var jlo = new MyJLO ();
Expand Down
3 changes: 2 additions & 1 deletion src/Java.Interop/Java.Interop/JniRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,9 @@ public virtual void OnEnterMarshalMethod ()
public virtual void OnUserUnhandledException (ref JniTransition transition, Exception e)
{
transition.SetPendingException (e);

#if NET9_0_OR_GREATER
Debugger.BreakForUserUnhandledException (e);
#endif // NET9_0_OR_GREATER
}

public virtual void RaisePendingException (Exception pendingException)
Expand Down
3 changes: 2 additions & 1 deletion src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ static unsafe JreRuntimeOptions CreateJreVM (JreRuntimeOptions builder)
builder.TypeManager ??= new JreTypeManager (builder.typeMappings);
#endif // NET

bool onMono = Type.GetType ("Mono.Runtime", throwOnError: false) != null;
bool onMono = Type.GetType ("Mono.RuntimeStructs", throwOnError: false) != null;
if (onMono) {
Console.WriteLine ($"MonoVM support enabled");
builder.ValueManager = builder.ValueManager ?? new MonoRuntimeValueManager ();
builder.ObjectReferenceManager = builder.ObjectReferenceManager ?? new MonoRuntimeObjectReferenceManager ();
}
Expand Down
1 change: 1 addition & 0 deletions src/java-interop/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coreclr.exp
jni.c
jni.g.cs
6 changes: 3 additions & 3 deletions src/java-interop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(CMAKE_OSX_ARCHITECTURES x86_64 arm64)
cmake_minimum_required(VERSION 3.10.2)

project(
java-interop
Expand All @@ -14,8 +14,9 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

option(ENABLE_MONO_INTEGRATION "Require Mono runtime" OFF)
option(ENABLE_OSX_ARCHITECTURES "macOS architectures" "")

cmake_minimum_required(VERSION 3.10.2)
set(CMAKE_OSX_ARCHITECTURES ${ENABLE_OSX_ARCHITECTURES})

set(JAVA_INTEROP_CORE_SOURCES
java-interop-dlfcn.cc
Expand Down Expand Up @@ -44,7 +45,6 @@ if(ENABLE_MONO_INTEGRATION)
include_directories(${dir})
endforeach()
list(APPEND LINK_FLAGS ${MONO_LINK_FLAGS})
list(APPEND LINK_FLAGS "-Wl,-undefined -Wl,suppress -Wl,-flat_namespace")
set(JAVA_INTEROP_SOURCES ${JAVA_INTEROP_CORE_SOURCES} ${JAVA_INTEROP_MONO_SOURCES})
else()
set(JAVA_INTEROP_SOURCES ${JAVA_INTEROP_CORE_SOURCES})
Expand Down
20 changes: 20 additions & 0 deletions src/java-interop/coreclr.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
LIBRARY CORECLR

EXPORTS
mono_class_from_mono_type
mono_class_get_field_from_name
mono_class_get_name
mono_class_get_namespace
mono_class_is_subclass_of
mono_class_vtable
mono_domain_get
mono_field_get_value
mono_field_set_value
mono_field_static_set_value
mono_gc_register_bridge_callbacks
mono_gc_wait_for_bridge_processing
mono_object_get_class
mono_thread_attach
mono_thread_current
mono_thread_get_managed_id
mono_thread_get_name_utf8
Binary file added src/java-interop/coreclr.lib
Binary file not shown.
10 changes: 9 additions & 1 deletion src/java-interop/java-interop-gc-bridge-mono.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <inttypes.h>

#include "java-interop.h"
Expand All @@ -12,6 +11,7 @@
#include "java-interop-util.h"

#ifdef _WINDOWS
#include <windows.h>
#include <fileapi.h>
#endif

Expand Down Expand Up @@ -530,13 +530,21 @@ get_object_ref_type (JNIEnv *env, jobject handle)
static int
gref_inc (JavaInteropGCBridge *bridge)
{
#if _WINDOWS
return InterlockedIncrement ((LONG volatile*) &bridge->gc_gref_count);
#else // !_WINDOWS
return __sync_add_and_fetch (&bridge->gc_gref_count, 1);
#endif // _!WINDOWS
}

static int
gref_dec (JavaInteropGCBridge *bridge)
{
#if _WINDOWS
return InterlockedDecrement ((LONG volatile*) &bridge->gc_gref_count);
#else // !_WINDOWS
return __sync_sub_and_fetch (&bridge->gc_gref_count, 1);
#endif // _!WINDOWS
}

#if defined (ANDROID)
Expand Down
1 change: 1 addition & 0 deletions src/java-interop/java-interop-mono.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "java-interop.h"

#include <mono/metadata/assembly.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/class.h>
#include <mono/metadata/object.h>
#include <mono/metadata/sgen-bridge.h>
Expand Down
13 changes: 12 additions & 1 deletion src/java-interop/java-interop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@

<ItemGroup>
<ProjectReference Include="..\..\build-tools\jnienv-gen\jnienv-gen.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>
</ItemGroup>

<PropertyGroup>
<DotNetRuntimePacksVersion>8.0.13</DotNetRuntimePacksVersion>
</PropertyGroup>

<ItemGroup Condition=" '$(UseMonoRuntime)' != 'true' ">
<PackageDownload
Include="Microsoft.NETCore.App.Runtime.Mono.$(NETCoreSdkRuntimeIdentifier)"
Version="[$(DotNetRuntimePacksVersion)]"
/>
</ItemGroup>

<Import Project="java-interop.targets" />

Expand Down
Loading