aboutsummaryrefslogtreecommitdiff
path: root/project/nativeFat.scala
blob: 55df06df0e83a0f82dda70267cdb6a89f9f572cf (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
import sbt._
import Keys._
import java.io.File

object NativeKeys {

    val nativeBuildDirectory = settingKey[File]("Directory containing native build scripts.")
    val nativeTarget = settingKey[File]("Target directory to store native artifacts.")

    val nativeBuild = taskKey[File]("Invoke native build.")

    val nativePackUnmanaged = settingKey[File]("")
}

object NativeDefaults {
    import NativeKeys._

    val nativeBuildImpl = Def.task {
        val log = streams.value.log
        val build = nativeBuildDirectory.value
        val target = nativeTarget.value

        val configure = Process(
            "./configure " +
            "--prefix=" + target.getAbsolutePath + " " +
            "--libdir=" + target.getAbsolutePath,
            Some(build))

        val make = Process("make", build)

        val makeInstall = Process("make install", build)

        val ev = configure #&& make #&& makeInstall ! log
        if (ev != 0)
            throw new RuntimeException(s"Building native library failed.")

        (target ** ("*.la")).get.foreach(_.delete())

        target
    }


    val mappingsImpl = Def.task {
        val files = (nativeBuild.value ** "*").get
        val unamanagedDir = nativePackUnmanaged.value

        val managed: Seq[(File, String)] = for (file <- files; if file.isFile) yield {
            file -> ("native/" + (file relativeTo nativeTarget.value).get.getPath)
        }

        val unmanaged: Seq[(File, String)] = for (file <- (unamanagedDir ** "*").get; if file.isFile) yield {
            file -> ("native/" + (file relativeTo unamanagedDir).get.getPath)
        }
    
        managed ++ unmanaged
    }

    def os = System.getProperty("os.name").toLowerCase.filter(_ != ' ')
    def arch = System.getProperty("os.arch").toLowerCase

    val settings: Seq[Setting[_]] = Seq(
        nativeTarget := target.value / "native" / (os + "-" + arch),
        nativeBuild := nativeBuildImpl.value,
        nativePackUnmanaged := baseDirectory.value / "lib_native",
        mappings in (Compile, packageBin) ++= mappingsImpl.value
    )

}