aboutsummaryrefslogtreecommitdiff
path: root/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/JniNative.scala
blob: 90f7cc8e80f0c9ad622cf87316fb1cc6e21de397 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ch.jodersky.sbt.jni

import build._
import ch.jodersky.jni.{NativeLoader, Platform}
import sbt._
import sbt.Keys._

object JniNative extends AutoPlugin {

  override def requires = plugins.JvmPlugin

  object autoImport {

    val jni = taskKey[File]("Builds a native library (by calling the native build tool).")

    val jniPlatform = settingKey[Platform]("Platform of the system this build is running on.")
    val jniBuildTool = taskKey[BuildTool]("The build tool to be used when building a native library.")

    val jniLibraryPath = settingKey[String]("String that is prepended to the path of a native library when packaged.")

  }
  import autoImport._

  lazy val settings: Seq[Setting[_]] = Seq(

    sourceDirectory in jni := baseDirectory.value / "src",

    target in jni := target.value / "native" / (jniPlatform in jni).value.id,

    jniPlatform in jni := Platform.current.getOrElse {
      sLog.value.warn("Warning: cannot determine platform! It will be set to 'unknown'.")
      Platform.Unknown
    },

    jniBuildTool in jni := {
      val tools = Seq(CMake, Autotools)

      val base = (sourceDirectory in jni).value

      val tool = if (base.exists && base.isDirectory) {
        tools.find(t => t detect base)
      } else {
        None
      }
      tool.getOrElse(
        sys.error("No supported native build tool detected. " +
          s"Check that the setting 'sourceDirectory in jni' (currently $base) " +
          "points to a valid directory. Supported build tools are: " +
          tools.map(_.name).mkString(","))
      )
    },

    clean in jni := {
      val log = streams.value.log
      val tool = try {
        Some((jniBuildTool in jni).value)
      } catch {
        case _: Exception => None
      }
      tool foreach { t =>
        log.debug("Cleaning native build")
        t.api.clean(
          (sourceDirectory in jni).value,
          log
        )
      }
    },

    jni := {
      val tool = (jniBuildTool in jni).value
      val dir = (sourceDirectory in jni).value
      val buildDir = (target in jni).value / "build"
      val targetDir = (target in jni).value / "lib"
      val log = streams.value.log

      IO.createDirectory(buildDir)
      IO.createDirectory(targetDir)

      tool.api.library(dir, buildDir, targetDir, log)
    },

    clean := {
      (clean in jni).value
      clean.value
    },

    jniLibraryPath in jni := {
      "/" + organization.value.replaceAll("\\.", "/") + "/" + name.value
    },

    unmanagedResourceDirectories += baseDirectory.value / "lib_native",

    resourceGenerators += Def.task {
      //build native library
      val library = jni.value

      //native library as a managed resource file
      val libraryResource = (resourceManaged in Compile).value /
        NativeLoader.fullLibraryPath(
          (jniLibraryPath in jni).value,
          (jniPlatform in jni).value
        )

      //copy native library to a managed resource (so it can also be loaded when not packaged as a jar)
      IO.copyFile(library, libraryResource)

      Seq(libraryResource)
    }.taskValue

  )

  override lazy val projectSettings = inConfig(Compile)(settings) ++  Seq(
    //don't scala version to native jars
    crossPaths := false
  )

}