aboutsummaryrefslogtreecommitdiff
path: root/jni-plugin/src/main/scala/ch/jodersky/sbt/jni/plugins/JniNative.scala
blob: 2ac179d481db9633c5e1924f1069edec4cb53e7c (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
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)
}