Make JNI Bindings in Rust

This commit is contained in:
GuilhermeWerner 2021-05-09 17:03:08 -03:00
parent 151bdebf43
commit 7583e38f38
11 changed files with 67 additions and 134 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[build]
target-dir = "Binaries"

View file

@ -4,9 +4,6 @@
if ($IsWindows)
{
echo "Generating Visual Studio project..."
& "./Vendor/Premake/Windows/premake5.exe" "vs2019"
}
# Mac
@ -15,11 +12,6 @@ elseif ($IsMacOS)
{
sudo chmod +x ./gradlew
sudo chmod +x ./Scripts/Package.ps1
echo "Generating XCode project..."
sudo chmod +x ./Vendor/Premake/Mac/premake5
& "./Vendor/Premake/Mac/premake5" "xcode4"
}
# Linux
@ -28,9 +20,4 @@ elseif ($IsLinux)
{
sudo chmod +x ./gradlew
sudo chmod +x ./Scripts/Package.ps1
echo "Generating GMake project..."
sudo chmod +x ./Vendor/Premake/Linux/premake5
& "./Vendor/Premake/Linux/premake5" "gmake2"
}

View file

@ -7,17 +7,10 @@ sudo chmod +x ./Scripts/Package.ps1
if [ "$(expr substr $(uname -s) 1 5)" = "Linux" ]
then
echo "Generating GMake project..."
sudo chmod +x ./Vendor/Premake/Linux/premake5
./Vendor/Premake/Linux/premake5 gmake2
# Mac
elif [ "$(uname)" = "Darwin" ]
then
echo "Generating XCode project..."
sudo chmod +x ./Vendor/Premake/Mac/premake5
./Vendor/Premake/Mac/premake5 xcode4
fi

18
Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "TribuFu-jvm"
version = "0.0.1"
description = "Jvm bindings to TribuFu SDK"
repository = "https://github.com/TribuFu/SDK"
authors = ["TribuFu <contact@tribufu.com>"]
license = "Apache-2.0"
edition = "2018"
publish = false
[lib]
name="TribuFu_jvm"
crate-type = ["cdylib", "rlib"]
path = "Source/lib.rs"
[dependencies]
TribuFu = { path = "../DevKit.Rs", package = "TribuFu" }
jni = "0.19.0"

View file

@ -1,12 +0,0 @@
// Copyright (c) TribuFu. All Rights Reserved
#include <jni.h>
#include "TribuFu.h"
#include "Library.h"
JNIEXPORT jstring JNICALL Java_tribufu_TribuFu_Hello(JNIEnv *env, jobject obj, jstring str)
{
char *result = Hello("C");
return result;
}

View file

@ -1,13 +0,0 @@
// Copyright (c) TribuFu. All Rights Reserved
#pragma once
#include <jni.h>
#include "TribuFu.h"
/*
* Class: tribufu_TribuFu
* Method: Hello
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_tribufu_TribuFu_Hello(JNIEnv *, jobject, jstring);

View file

@ -1,15 +0,0 @@
// Copyright (c) TribuFu. All Rights Reserved
package tribufu;
public class TribuFu {
/**
* A native method that is implemented by the 'TribuFu' native library.
*/
public native String Hello(String input);
// Used to load the 'TribuFu' library on application startup.
static {
System.loadLibrary("TribuFu_jvm");
}
}

View file

@ -1,64 +0,0 @@
project "TribuFu_jvm"
location "."
kind "SharedLib"
language "C"
targetdir ("./Binaries/%{cfg.buildcfg}")
objdir ("./Intermediate/")
files
{
"./Source/**.h",
"./Source/**.c",
}
includedirs
{
"../Vendor/TribuFu",
"D:/Program Files/AdoptOpenJDK/jdk-11.0.10.9-hotspot/include",
"D:/Program Files/AdoptOpenJDK/jdk-11.0.10.9-hotspot/include/win32"
}
filter "system:windows"
staticruntime "Off"
systemversion "latest"
links
{
"TribuFu.lib"
}
libdirs
{
"../Vendor/TribuFu/Windows"
}
filter { "system:windows", "configurations:debug" }
runtime "Debug"
symbols "on"
filter { "system:windows", "configurations:release" }
runtime "Release"
optimize "on"
filter "system:linux"
links
{
"TribuFu"
}
libdirs
{
"../Vendor/TribuFu/Linux"
}
filter "system:darwin"
links
{
"TribuFu"
}
libdirs
{
"../Vendor/TribuFu/Mac"
}

View file

@ -4,5 +4,11 @@
./gradlew jar
New-Item -Path "./Release" -ItemType Directory -Force
New-Item -Path "./Release/Windows" -ItemType Directory -Force
New-Item -Path "./Release/Mac" -ItemType Directory -Force
New-Item -Path "./Release/Linux" -ItemType Directory -Force
New-Item -Path "./Release/Android" -ItemType Directory -Force
Copy-Item -Path "./lib/build/libs/lib-0.0.1.jar" -Destination "./Release/TribuFu.jar" -Recurse -Force
Copy-Item -Path "./Binaries/debug/TribuFu_jvm.dll" -Destination "./Release/Windows/TribuFu_jvm.dll" -Recurse -Force

41
Source/lib.rs Normal file
View file

@ -0,0 +1,41 @@
// Copyright (c) TribuFu. All Rights Reserved
//! Jvm bindings to TribuFu SDK.
#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(unused_imports)]
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "android",
)))]
compile_error!("INVALID_TARGET_PLATFORM");
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;
use std::ffi::{CStr, CString};
#[no_mangle]
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
pub extern "system" fn Java_tribufu_TribuFu_Hello(
env: JNIEnv,
_: JClass,
j_input: JString,
) -> jstring {
// First, we have to get the string out of Java. Check out the `strings`
// module for more info on how this works.
let input = CString::from(unsafe { CStr::from_ptr(env.get_string(j_input).unwrap().as_ptr()) });
// Then we have to create a new Java string to return. Again, more info
// in the `strings` module.
let res = env.new_string(input.to_str().unwrap()).unwrap();
// Finally, extract the raw pointer to return.
return res.into_inner();
}

View file

@ -1,10 +0,0 @@
workspace "DevKit.Jvm"
architecture "x64"
configurations
{
"debug",
"release"
}
include "Native"