From 6ea42cb4352c0c354011d17487ab65e6eb1e2cb8 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Thu, 16 Jun 2016 16:54:59 -0700 Subject: Consolidate macro in one single file --- .../main/scala/ch/jodersky/jni/annotations.scala | 79 +++++++++++++--------- .../ch/jodersky/jni/util/PlatformMacros.scala | 31 --------- 2 files changed, 47 insertions(+), 63 deletions(-) delete mode 100644 macros/src/main/scala/ch/jodersky/jni/util/PlatformMacros.scala diff --git a/macros/src/main/scala/ch/jodersky/jni/annotations.scala b/macros/src/main/scala/ch/jodersky/jni/annotations.scala index c1caa48..f2ace96 100644 --- a/macros/src/main/scala/ch/jodersky/jni/annotations.scala +++ b/macros/src/main/scala/ch/jodersky/jni/annotations.scala @@ -1,7 +1,6 @@ package ch.jodersky.jni import macrocompat.bundle -import util.PlatformMacros import scala.language.experimental.macros import scala.reflect.macros.whitebox.Context @@ -38,43 +37,59 @@ class nativeLoaderMacro(val c: Context) { //q"$mods object $name extends ..$parents {$self => ..$body }" :: Nil => case ModuleDef(mods, name, Template(parents, self, body)) :: Nil => - val extra = q"""{ - def loadPackaged(): Unit = { - import java.io.File - import java.nio.file.{Files, Path} - - val lib: String = System.mapLibraryName($nativeLibrary) - - val tmp: Path = Files.createTempDirectory("jni-") - val plat: String = ${PlatformMacros.current(c)} - - val resourcePath: String = "/native/" + plat + "/" + lib - val resourceStream = Option($name.getClass.getResourceAsStream(resourcePath)) match { - case Some(s) => s - case None => throw new UnsatisfiedLinkError( - "Native library " + lib + " (" + resourcePath + ") cannot be found on the classpath.") + val extra = q""" + { + def loadPackaged(): Unit = { + import java.io.File + import java.nio.file.{Files, Path} + + val lib: String = System.mapLibraryName($nativeLibrary) + + val tmp: Path = Files.createTempDirectory("jni-") + val plat: String = { + val line = try { + scala.sys.process.Process("uname -sm").lines.head + } catch { + case ex: Exception => sys.error("Error running `uname` command") + } + val parts = line.split(" ") + if (parts.length != 2) { + sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line) + } else { + val arch = parts(1).toLowerCase.replaceAll("\\s", "") + val kernel = parts(0).toLowerCase.replaceAll("\\s", "") + arch + "-" + kernel + } + } + + val resourcePath: String = "/native/" + plat + "/" + lib + val resourceStream = Option($name.getClass.getResourceAsStream(resourcePath)) match { + case Some(s) => s + case None => throw new UnsatisfiedLinkError( + "Native library " + lib + " (" + resourcePath + ") cannot be found on the classpath.") + } + + val extractedPath = tmp.resolve(lib) + + try { + Files.copy(resourceStream, extractedPath) + } catch { + case ex: Exception => throw new UnsatisfiedLinkError( + "Error while extracting native library: " + ex) + } + + System.load(extractedPath.toAbsolutePath.toString) } - val extractedPath = tmp.resolve(lib) - - try { - Files.copy(resourceStream, extractedPath) + def load(): Unit = try { + System.loadLibrary($nativeLibrary) } catch { - case ex: Exception => throw new UnsatisfiedLinkError( - "Error while extracting native library: " + ex) + case ex: UnsatisfiedLinkError => loadPackaged() } - System.load(extractedPath.toAbsolutePath.toString) - } - - def load(): Unit = try { - System.loadLibrary($nativeLibrary) - } catch { - case ex: UnsatisfiedLinkError => loadPackaged() + load() } - - load() - }""" + """ ModuleDef(mods, name, Template(parents, self, body :+ extra)) :: Nil diff --git a/macros/src/main/scala/ch/jodersky/jni/util/PlatformMacros.scala b/macros/src/main/scala/ch/jodersky/jni/util/PlatformMacros.scala deleted file mode 100644 index 08d7967..0000000 --- a/macros/src/main/scala/ch/jodersky/jni/util/PlatformMacros.scala +++ /dev/null @@ -1,31 +0,0 @@ -package ch.jodersky.jni -package util - -import scala.language.experimental.macros - -import scala.reflect.macros.whitebox.Context - -object PlatformMacros { - - // arch-kernel - def current(c: Context): c.Expr[String] = { - import c.universe._ - val result = q""" - val line = try { - scala.sys.process.Process("uname -sm").lines.head - } catch { - case ex: Exception => sys.error("Error running `uname` command") - } - val parts = line.split(" ") - if (parts.length != 2) { - sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line) - } else { - val arch = parts(1).toLowerCase.replaceAll("\\s", "") - val kernel = parts(0).toLowerCase.replaceAll("\\s", "") - arch + "-" + kernel - } - """ - c.Expr[String](result) - } - -} -- cgit v1.2.3