From 15b42451d124564b9ccf4a6365d2f50f7f3896c9 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 21 May 2013 19:30:04 +0200 Subject: extract native libraries --- .gitignore | 1 + project/Build.scala | 18 ++++++----- project/JNIBuild.scala | 5 ++-- .../com/github/jodersky/flow/NativeSerial.java | 2 +- .../com/github/jodersky/flow/NativeLoader.scala | 35 ++++++++++++++++++++++ 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/main/scala/com/github/jodersky/flow/NativeLoader.scala diff --git a/.gitignore b/.gitignore index 20e1c2d..2112789 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ project/boot/ project/plugins/project/ # Scala-IDE specific +.settings .scala_dependencies .project .classpath diff --git a/project/Build.scala b/project/Build.scala index 714c9d0..9efb049 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -11,7 +11,7 @@ object FlowBuild extends Build { lazy val root = Project( id = "flow", base = file("."), - settings = buildSettings ++ jniSettings ++ runSettings) + settings = buildSettings ++ jniSettings ++ runSettings ++ Seq(libraryDependencies ++= Dependencies.all)) lazy val buildSettings = Defaults.defaultSettings ++ Seq( organization := Organization, @@ -19,8 +19,8 @@ object FlowBuild extends Build { scalaVersion := ScalaVersion, resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/", scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature"), - compileOrder in Compile := CompileOrder.JavaThenScala) - + compileOrder in Compile := CompileOrder.Mixed) + lazy val jniSettings = JNIBuild.defaults ++ Seq( jdkHome := file(System.getProperty("java.home")) / "..", javaClass := "com.github.jodersky.flow.NativeSerial", @@ -29,9 +29,13 @@ object FlowBuild extends Build { NativeBuild.includeDirectories <<= jdkHome apply (jdk => Seq(jdk / "include", jdk / "include" / "linux")), linker := "gcc", linkerOptions := Seq("-shared", "-Wl,-soname,libflow.so.1"), - linkerOutput <<= NativeBuild.outputDirectory(_ / "libflow.so") + linkerOutput <<= NativeBuild.outputDirectory(_ / "libflow.so"), + mappings in (Compile, packageBin) <+= linkerOutput map { out => + out -> ("native/" + System.getProperty("os.name").toLowerCase + "/" + System.getProperty("os.arch").toLowerCase + "/libflow.so") + }, + Keys.`package` in (Compile, packageBin) <<= (Keys.`package` in (Compile, packageBin)).dependsOn(NativeBuild.link) ) - + lazy val runSettings = Seq( fork := true, connectInput in run := true, @@ -41,7 +45,7 @@ object FlowBuild extends Build { object Dependencies { lazy val all = Seq() - //lazy val io = "com.github.scala-incubator.io" %% "scala-io-core" % "0.4.2" - //lazy val file = "com.github.scala-incubator.io" %% "scala-io-file" % "0.4.2" + lazy val io = "com.github.scala-incubator.io" %% "scala-io-core" % "0.4.2" + lazy val file = "com.github.scala-incubator.io" %% "scala-io-file" % "0.4.2" } diff --git a/project/JNIBuild.scala b/project/JNIBuild.scala index 95509fb..ed0d05e 100644 --- a/project/JNIBuild.scala +++ b/project/JNIBuild.scala @@ -13,9 +13,10 @@ object JNIBuild { {} } dependsOn (Keys.compile in Compile) - val defaults = NativeBuild.defaults ++ Seq( + val defaults: Seq[Setting[_]] = NativeBuild.defaults ++ Seq( javahTask, - NativeBuild.compile <<= NativeBuild.compile.dependsOn(javah)) + NativeBuild.compile <<= NativeBuild.compile.dependsOn(javah) + ) } diff --git a/src/main/java/com/github/jodersky/flow/NativeSerial.java b/src/main/java/com/github/jodersky/flow/NativeSerial.java index 10a82c9..f5d708e 100644 --- a/src/main/java/com/github/jodersky/flow/NativeSerial.java +++ b/src/main/java/com/github/jodersky/flow/NativeSerial.java @@ -3,7 +3,7 @@ package com.github.jodersky.flow; public class NativeSerial { static { - System.loadLibrary("flow"); + NativeLoader.load(); } final static int E_PERMISSION = -1; diff --git a/src/main/scala/com/github/jodersky/flow/NativeLoader.scala b/src/main/scala/com/github/jodersky/flow/NativeLoader.scala new file mode 100644 index 0000000..91b3fd8 --- /dev/null +++ b/src/main/scala/com/github/jodersky/flow/NativeLoader.scala @@ -0,0 +1,35 @@ +package com.github.jodersky.flow + +import java.io.File +import java.io.FileOutputStream + +object NativeLoader { + + def load = { + val os = System.getProperty("os.name").toLowerCase + val arch = System.getProperty("os.arch").toLowerCase + + val in = NativeLoader.getClass().getResourceAsStream("/native/" + os + "/" + arch + "/" + "libflow.so") + val temp = File.createTempFile("flow" + os + arch, ".so"); + temp.deleteOnExit() + val out = new FileOutputStream(temp); + + try { + var read: Int = 0; ; + val buffer = new Array[Byte](4096); + do { + read = in.read(buffer) + if (read != -1) { + out.write(buffer, 0, read); + } + } while (read != -1) + } finally { + in.close() + out.close + } + + System.load(temp.getAbsolutePath()) + + } + +} \ No newline at end of file -- cgit v1.2.3