diff options
Diffstat (limited to 'examples/demo')
-rw-r--r-- | examples/demo/build.sbt | 16 | ||||
-rw-r--r-- | examples/demo/native/CMakeLists.txt | 45 | ||||
-rw-r--r-- | examples/demo/native/main.c | 16 | ||||
-rw-r--r-- | examples/demo/native/org_example_jni_demo_Library__.h | 21 | ||||
-rw-r--r-- | examples/demo/project/plugins.sbt | 1 | ||||
-rw-r--r-- | examples/demo/src/main/scala/org/example/jni/demo/Library.scala | 8 | ||||
-rw-r--r-- | examples/demo/src/main/scala/org/example/jni/demo/Main.scala | 12 |
7 files changed, 119 insertions, 0 deletions
diff --git a/examples/demo/build.sbt b/examples/demo/build.sbt new file mode 100644 index 0000000..8cd1419 --- /dev/null +++ b/examples/demo/build.sbt @@ -0,0 +1,16 @@ +enablePlugins(JniPlugin) + +name := "jni-demo" + +organization := "org.example" + +baseDirectory in jni := (baseDirectory in ThisBuild).value / "native" + +javahObjects in javah += "org.example.jni.demo.Library" + +libraryDependencies += "ch.jodersky" %% "jni-library" % "0.1-SNAPSHOT" + +//exportJars in run := true + +//librearDependencies += "org.example" %% "demo" % "0.1" +//librearDependencies += "org.example" % "demo" % "0.1" % "runtime" classifier "native" diff --git a/examples/demo/native/CMakeLists.txt b/examples/demo/native/CMakeLists.txt new file mode 100644 index 0000000..059a0dd --- /dev/null +++ b/examples/demo/native/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 2.6) + +# Define project and related variables +# +project (demo) + +# Set versions and library name +# Note: major version will be appended to library name +# +set (VERSION_MAJOR 1) +set (VERSION_MINOR 2) +set (VERSION_PATCH 3) +set (LIB_NAME demo${VERSION_MAJOR}) + +# Command-line options +# +# required by sbt-jni to install binaries to correct places +set (LIB_INSTALL_DIR lib CACHE PATH "Path in which to install libraries (Autoconf equivalent to --libdir).") +# required by sbt-jni to disable versioned libraries +set (ENABLE_VERSIONED_LIB ON CACHE BOOLEAN "Generate versioned library files and symlinks.") + +# Setup JNI +find_package(JNI REQUIRED) +if (JNI_FOUND) + message (STATUS "JNI include directories: ${JNI_INCLUDE_DIRS}") +endif() + +# Include directories +include_directories(.) +include_directories(${JNI_INCLUDE_DIRS}) + +# Setup main shared library +# Note: major version is appended to library name +add_library(${LIB_NAME} SHARED main.c) +if (ENABLE_VERSIONED_LIB) + set_target_properties( + ${LIB_NAME} + PROPERTIES + VERSION 0.${VERSION_MINOR}.${VERSION_PATCH} # major version always 0, it is included in name + SOVERSION 0 + ) +endif() + +# Installation targets +install(TARGETS ${LIB_NAME} LIBRARY DESTINATION ${LIB_INSTALL_DIR}) diff --git a/examples/demo/native/main.c b/examples/demo/native/main.c new file mode 100644 index 0000000..5916c48 --- /dev/null +++ b/examples/demo/native/main.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include "org_example_jni_demo_Library__.h" + +/* + * Class: org_example_jni_demo_Library__ + * Method: print + * Signature: (Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_example_jni_demo_Library_00024_print +(JNIEnv *env, jobject clazz, jstring message) { + const char* msg = (*env)->GetStringUTFChars(env, message, 0); + fprintf(stdout, "Printing from native library: %s", msg); + fflush(stdout); + (*env)->ReleaseStringUTFChars(env, message, msg); + return 0; +} diff --git a/examples/demo/native/org_example_jni_demo_Library__.h b/examples/demo/native/org_example_jni_demo_Library__.h new file mode 100644 index 0000000..06a7fa4 --- /dev/null +++ b/examples/demo/native/org_example_jni_demo_Library__.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class org_example_jni_demo_Library__ */ + +#ifndef _Included_org_example_jni_demo_Library__ +#define _Included_org_example_jni_demo_Library__ +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_example_jni_demo_Library__ + * Method: print + * Signature: (Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_example_jni_demo_Library_00024_print + (JNIEnv *, jobject, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/examples/demo/project/plugins.sbt b/examples/demo/project/plugins.sbt new file mode 100644 index 0000000..ab5617e --- /dev/null +++ b/examples/demo/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("ch.jodersky" % "sbt-jni" % "0.1-SNAPSHOT") diff --git a/examples/demo/src/main/scala/org/example/jni/demo/Library.scala b/examples/demo/src/main/scala/org/example/jni/demo/Library.scala new file mode 100644 index 0000000..c157b5d --- /dev/null +++ b/examples/demo/src/main/scala/org/example/jni/demo/Library.scala @@ -0,0 +1,8 @@ +package org.example.jni.demo + +/** A demo object, mapping to a native library. */ +object Library { + + @native def print(message: String): Int + +} diff --git a/examples/demo/src/main/scala/org/example/jni/demo/Main.scala b/examples/demo/src/main/scala/org/example/jni/demo/Main.scala new file mode 100644 index 0000000..7306104 --- /dev/null +++ b/examples/demo/src/main/scala/org/example/jni/demo/Main.scala @@ -0,0 +1,12 @@ +package org.example.jni.demo + +import ch.jodersky.jni.NativeLoader + +object Main { + + def main(args: Array[String]): Unit = { + NativeLoader.load("demo1", "/org/example/jni/demo") + Library.print("Hello world!") + } + +} |