aboutsummaryrefslogtreecommitdiff
path: root/jni-plugin/src/main/scala/ch
diff options
context:
space:
mode:
Diffstat (limited to 'jni-plugin/src/main/scala/ch')
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/Autotools.scala30
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala14
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildToolApi.scala27
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/CMake.scala31
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/ConfigureMakeInstall.scala49
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniJavah.scala69
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniLoading.scala28
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala100
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniPackaging.scala119
-rw-r--r--jni-plugin/src/main/scala/ch/jodersky/sbt/jni/util/ByteCode.scala57
10 files changed, 0 insertions, 524 deletions
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/Autotools.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/Autotools.scala
deleted file mode 100644
index 960e66e..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/Autotools.scala
+++ /dev/null
@@ -1,30 +0,0 @@
-package ch.jodersky.sbt.jni
-package build
-
-import java.io.File
-import sbt._
-
-object Autotools extends BuildTool {
-
- val name = "Autotools"
-
- def detect(baseDirectory: File) = baseDirectory.list().contains("configure")
-
- object api extends ConfigureMakeInstall {
-
- override def configure(base: File, build: File, target: File) = {
- val targetPath = target.getAbsolutePath
-
- Process(
- s"${base.getAbsolutePath}/configure " +
- s"--prefix=$targetPath " +
- s"--libdir=$targetPath " +
- "--disable-versioned-lib",
- build
- )
- }
-
- }
-
-}
-
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala
deleted file mode 100644
index 23df2b2..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildTool.scala
+++ /dev/null
@@ -1,14 +0,0 @@
-package ch.jodersky.sbt.jni
-package build
-
-import java.io.File
-
-trait BuildTool {
-
- def name: String
-
- def detect(baseDirectory: File): Boolean
-
- def api: BuildToolApi
-
-}
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildToolApi.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildToolApi.scala
deleted file mode 100644
index 43dd17a..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/BuildToolApi.scala
+++ /dev/null
@@ -1,27 +0,0 @@
-package ch.jodersky.sbt.jni
-package build
-
-import java.io.File
-import sbt.Logger
-
-trait BuildToolApi {
-
- /** Invokes the native build tool's clean task */
- def clean(baseDirectory: File, log: Logger): Unit
-
- /**
- * Invokes the native build tool's main task, resulting in a single shared
- * library file.
- * @param baseDirectory the directory where the native project is located
- * @param buildDirectory a directory from where the build is called, it may be used to store temporary files
- * @param targetDirectory the directory into which the native library is copied
- * @return the native library file
- */
- def library(
- baseDirectory: File,
- targetDirectory: File,
- buildDirectory: File,
- log: Logger
- ): File
-
-}
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/CMake.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/CMake.scala
deleted file mode 100644
index 3f75585..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/CMake.scala
+++ /dev/null
@@ -1,31 +0,0 @@
-package ch.jodersky.sbt.jni
-package build
-
-import java.io.File
-import sbt._
-
-object CMake extends BuildTool {
-
- val name = "CMake"
-
- def detect(baseDirectory: File) = baseDirectory.list().contains("CMakeLists.txt")
-
- object api extends ConfigureMakeInstall {
-
- override def configure(base: File, build: File, target: File) = {
- val targetPath = target.getAbsolutePath
- Process(
- //Disable producing versioned library files, not needed for fat jars.
- "cmake " +
- s"-DCMAKE_INSTALL_PREFIX:PATH=$targetPath " +
- s"-DLIB_INSTALL_DIR:PATH=$targetPath " +
- "-DENABLE_VERSIONED_LIB:BOOLEAN=OFF " +
- base.getAbsolutePath,
- build
- )
- }
-
- }
-
-}
-
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/ConfigureMakeInstall.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/ConfigureMakeInstall.scala
deleted file mode 100644
index 97011ed..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/build/ConfigureMakeInstall.scala
+++ /dev/null
@@ -1,49 +0,0 @@
-package ch.jodersky.sbt.jni
-package build
-
-import java.io.File
-import sbt._
-
-/** Native build tools relying on a standard 'configure && make && make install' process */
-trait ConfigureMakeInstall extends BuildToolApi {
-
- override def clean(baseDirectory: File, log: Logger) = Process("make clean", baseDirectory) ! log
-
- def configure(baseDirectory: File, buildDirectory: File, targetDirectory: File): ProcessBuilder
-
- def make(baseDirectory: File, buildDirectory: File, targetDirectory: File): ProcessBuilder = Process("make", buildDirectory)
-
- def install(baseDirectory: File, buildDirectory: File, targetDirectory: File): ProcessBuilder = Process("make install", buildDirectory)
-
- override def library(
- baseDirectory: File,
- buildDirectory: File,
- targetDirectory: File,
- log: Logger
- ): File = {
-
- val ev = (
- configure(baseDirectory, buildDirectory, targetDirectory) #&&
- make(baseDirectory, buildDirectory, targetDirectory) #&&
- install(baseDirectory, buildDirectory, targetDirectory)
- ) ! log
-
- if (ev != 0) sys.error(s"Building native library failed. Exit code: ${ev}")
-
- val products: List[File] = (targetDirectory ** ("*" -- "*.la")).get.filter(_.isFile).toList
-
- //only one produced library is expected
- products match {
- case Nil =>
- sys.error("No files were created during compilation, " +
- "something went wrong with the autotools configuration.")
- case head :: Nil =>
- head
- case head :: tail =>
- log.warn("More than one file was created during compilation, " +
- s"only the first one (${head.getAbsolutePath}) will be used.")
- head
- }
- }
-}
-
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniJavah.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniJavah.scala
deleted file mode 100644
index 63e5b23..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniJavah.scala
+++ /dev/null
@@ -1,69 +0,0 @@
-package ch.jodersky.sbt.jni
-package plugins
-
-import sbt._
-import sbt.Keys._
-import util.ByteCode
-
-/** Adds `javah` header-generation functionality to projects. */
-object JniJavah extends AutoPlugin {
-
- override def requires = plugins.JvmPlugin
- override def trigger = allRequirements
-
- object autoImport {
-
- val javahClasses = taskKey[Set[String]](
- "Fully qualified names of classes containing native declarations."
- )
-
- val javah = taskKey[File](
- "Generate JNI headers. Returns the directory containing generated headers."
- )
-
- }
- import autoImport._
-
- lazy val mainSettings: Seq[Setting[_]] = Seq(
-
- javahClasses in javah := {
- val compiled: inc.Analysis = (compile in Compile).value
- val classFiles: Set[File] = compiled.relations.allProducts.toSet
- val nativeClasses = classFiles flatMap { file =>
- ByteCode.natives(file)
- }
- nativeClasses
- },
-
- target in javah := target.value / "include",
-
- fullClasspath in javah := fullClasspath.value,
-
- javah := {
- val out = (target in javah).value
- val jcp: Seq[File] = (fullClasspath in javah).value.map(_.data)
- val cp = jcp.mkString(sys.props("path.separator"))
- val log = streams.value.log
-
- val classes = (javahClasses in javah).value
-
- log.info("Headers will be generated to " + out.getAbsolutePath)
- for (clazz <- classes) {
- log.info("Generating header for " + clazz + "...")
- val parts = Seq(
- "javah",
- "-d", out.getAbsolutePath,
- "-classpath", cp,
- clazz
- )
- val cmd = parts.mkString(" ")
- val ev = Process(cmd) ! streams.value.log
- if (ev != 0) sys.error(s"Error occured running javah. Exit code: ${ev}")
- }
- out
- }
- )
-
- override lazy val projectSettings = inConfig(Compile)(mainSettings)
-
-}
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniLoading.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniLoading.scala
deleted file mode 100644
index 599bfa1..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniLoading.scala
+++ /dev/null
@@ -1,28 +0,0 @@
-package ch.jodersky.sbt.jni
-package plugins
-
-import sbt._
-import sbt.Keys._
-import util.ByteCode
-
-/**
- * Enables loading native libraries from the classpath, typically created
- * from a project using JniPackaging.
- */
-object JniLoading extends AutoPlugin {
-
- override def requires = plugins.JvmPlugin
-
- lazy val settings = Seq(
-
- //enable enhanced native library extraction
- libraryDependencies += "ch.jodersky" %% "jni-library" % Version.PluginVersion,
-
- //fork new JVM, since native libraries can only be loaded once
- fork in run := true
-
- )
-
- override lazy val projectSettings = settings
-
-}
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala
deleted file mode 100644
index 2ac179d..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala
+++ /dev/null
@@ -1,100 +0,0 @@
-package ch.jodersky.sbt.jni
-package plugins
-
-import build._
-import ch.jodersky.jni.{NativeLoader, Platform}
-import sbt._
-import sbt.Keys._
-
-/** Wraps a native build system in sbt tasks. */
-object JniNative extends AutoPlugin {
-
- object autoImport {
-
- //Main task, inspect this first
- val nativeCompile = taskKey[File]("Builds a native library (by calling the native build tool).")
-
- val nativePlatform = settingKey[Platform]("Platform of the system this build is running on.")
-
- val nativeBuildTools = taskKey[Seq[BuildTool]](
- "A collection of build tools that are tested to determine the current build environment"
- )
- val nativeBuildTool = taskKey[BuildTool](
- "The build tool to be used when building a native library."
- )
-
- }
- import autoImport._
-
- lazy val settings: Seq[Setting[_]] = Seq(
-
- nativePlatform := Platform.current.getOrElse {
- sLog.value.warn("Warning: cannot determine platform! It will be set to 'unknown'.")
- Platform.Unknown
- },
-
- sourceDirectory in nativeCompile := sourceDirectory.value / "native",
-
- target in nativeCompile := target.value / "native" / (nativePlatform).value.id,
-
- nativeBuildTools := Seq(CMake, Autotools),
-
- nativeBuildTool := {
- val tools = nativeBuildTools.value
-
- val src = (sourceDirectory in nativeCompile).value
-
- val tool = if (src.exists && src.isDirectory) {
- tools.find(t => t detect src)
- } else {
- None
- }
- tool.getOrElse(
- sys.error("No supported native build tool detected. " +
- s"Check that the setting 'sourceDirectory in nativeCompile' (currently $src) " +
- "points to a valid directory. Supported build tools are: " +
- tools.map(_.name).mkString(","))
- )
-
- },
-
- clean in nativeCompile := {
- val log = streams.value.log
-
- log.debug("Cleaning native build")
- try {
- val tool = nativeBuildTool.value
- tool.api.clean(
- (sourceDirectory in nativeCompile).value,
- log
- )
- } catch {
- case ex: Exception =>
- log.debug(s"Native cleaning failed: $ex")
- }
-
- },
-
- nativeCompile := {
- val tool = nativeBuildTool.value
- val src = (sourceDirectory in nativeCompile).value
- val buildDir = (target in nativeCompile).value / "build"
- val targetDir = (target in nativeCompile).value / "lib"
- val log = streams.value.log
-
- IO.createDirectory(buildDir)
- IO.createDirectory(targetDir)
-
- tool.api.library(src, buildDir, targetDir, log)
- },
-
- // change clean task to also clean native sources
- clean := {
- (clean in nativeCompile).value
- clean.value
- }
-
- )
-
- override lazy val projectSettings = inConfig(Compile)(settings)
-}
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniPackaging.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniPackaging.scala
deleted file mode 100644
index cc3a851..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniPackaging.scala
+++ /dev/null
@@ -1,119 +0,0 @@
-package ch.jodersky.sbt.jni
-package plugins
-
-import ch.jodersky.jni._
-import sbt._
-import sbt.Keys._
-
-/** Packages libraries built with JniNative. */
-object JniPackaging extends AutoPlugin {
-
- //JvmPlugin is required or else resource generators will be overriden
- override def requires = JniNative && plugins.JvmPlugin
- override def trigger = allRequirements
-
- object autoImport {
-
- val nativeLibraryPath = settingKey[String](
- "String that is prepended to the path of a native library when packaged. This value should be " +
- "passed to `NativeLoader.load()`"
- )
-
- val enableNativeCompilation = settingKey[Boolean](
- "Determines if native compilation is enabled. If disabled, only pre-compiled libraries in " +
- "`unmanagedNativeDirectories` will be packaged."
- )
-
- val unmanagedNativeDirectories = settingKey[Seq[File]](
- "Unmanaged directories containing native libraries. The libraries must be regular files " +
- "contained in a subdirectory corresponding to a platform. For example " +
- "`<unamagedNativeDirectory>/x86_64-linux/libfoo.so` is an unmanaged library for machines having " +
- "the x86_64 architecture and running the Linux kernel."
- )
-
- val unmanagedNativeLibraries = taskKey[Map[Platform, File]](
- "Reads `unmanagedNativeDirectories` and maps platforms to library files specified theirin."
- )
-
- val managedNativeLibraries = taskKey[Map[Platform, File]](
- "Maps locally built, platform-dependant libraries."
- )
-
- val nativeLibraries = taskKey[Map[Platform, File]](
- "All native libraries, managed and unmanaged."
- )
-
- }
- import autoImport._
- import JniNative.autoImport._
-
- lazy val settings: Seq[Setting[_]] = Seq(
-
- nativeLibraryPath := {
- val orgPath = organization.value.replaceAll("\\.", "/")
- s"/${orgPath}/${name.value}/native"
- },
-
- enableNativeCompilation := true,
-
- unmanagedNativeDirectories := Seq(baseDirectory.value / "lib_native"),
-
- unmanagedNativeLibraries := {
- val dirs: Seq[File] = unmanagedNativeDirectories.value
- val seq: Seq[(Platform, File)] = for (
- dir <- dirs;
- if (dir.exists && dir.isDirectory);
- platformDir <- dir.listFiles();
- if platformDir.isDirectory;
- platform = Platform.fromId(platformDir.name);
- library <- platformDir.listFiles();
- if library.isFile
- ) yield {
- platform -> library.getCanonicalFile()
- }
-
- seq.toMap
- },
-
- managedNativeLibraries := Def.taskDyn[Map[Platform, File]] {
- val enableManaged = (enableNativeCompilation).value
- if (enableManaged) Def.task {
- val library: File = nativeCompile.value
- val platform = nativePlatform.value
- Map(platform -> library)
- }
- else Def.task {
- Map()
- }
- }.value,
-
- // managed native libraries take precedence
- nativeLibraries := unmanagedNativeLibraries.value ++ managedNativeLibraries.value,
-
- resourceGenerators += Def.task {
-
- val libraries: Seq[(Platform, File)] = nativeLibraries.value.toSeq
-
- val resources: Seq[File] = for ((plat, file) <- libraries) yield {
-
- //native library as a managed resource file
- val resource = resourceManaged.value /
- NativeLoader.fullLibraryPath(
- (nativeLibraryPath).value,
- plat
- )
-
- //copy native library to a managed resource (so it can also be loaded when not packaged as a jar)
- IO.copyFile(file, resource)
- resource
- }
- resources
- }.taskValue
-
- )
-
- override lazy val projectSettings = inConfig(Compile)(settings) ++
- Seq(crossPaths := false) //don't add scala version to native jars
-
-
-}
diff --git a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/util/ByteCode.scala b/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/util/ByteCode.scala
deleted file mode 100644
index b3c6fa3..0000000
--- a/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/util/ByteCode.scala
+++ /dev/null
@@ -1,57 +0,0 @@
-package ch.jodersky.sbt.jni
-package util
-
-import java.io.{File, FileInputStream, Closeable}
-import scala.collection.mutable.{HashSet}
-
-import org.objectweb.asm.{ClassReader, ClassVisitor, MethodVisitor, Opcodes}
-
-object ByteCode {
-
- private class NativeFinder extends ClassVisitor(Opcodes.ASM5) {
-
- //contains any classes found to contain at least one @native def
- val _nativeClasses = new HashSet[String]
- def nativeClasses = _nativeClasses.toSet
-
- private var fullyQualifiedName: String = ""
-
- override def visit(version: Int, access: Int, name: String, signature: String,
- superName: String, interfaces: Array[String]): Unit = {
- fullyQualifiedName = name.replaceAll("/", ".")
- }
-
- override def visitMethod(access: Int, name: String, desc: String,
- signature: String, exceptions: Array[String]): MethodVisitor = {
-
- val isNative = (access & Opcodes.ACC_NATIVE) != 0
-
- if (isNative) {
- _nativeClasses += fullyQualifiedName
- }
-
- null //return null, do not visit method further
- }
-
- }
-
- private def using[A >: Null <: Closeable, R](mkStream: => A)(action: A => R): R = {
- var stream: A = null
- try {
- stream = mkStream
- action(stream)
- } finally {
- if (stream != null) {
- stream.close()
- }
- }
- }
-
- def natives(classFile: File): Set[String] = using(new FileInputStream(classFile)) { in =>
- val reader = new ClassReader(in)
- val finder = new NativeFinder
- reader.accept(finder, 0)
- finder.nativeClasses
- }
-
-}