aboutsummaryrefslogtreecommitdiff
path: root/flow-main/src/main/scala/com/github/jodersky/flow/internal/NativeLoader.scala
blob: 4254c5c0b0b0732a2b5d7a9ee2c0e2be74d06582 (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
package com.github.jodersky.flow
package internal

import java.io.{File, FileOutputStream, InputStream, OutputStream}
import scala.io.Source
import scala.sys.process.Process
import scala.util.Try

/** Handles loading of the current platform's native library for flow. */
object NativeLoader {

  /** A platform is the representation of an os-architecture combination */
  case class Platform(kernel: String, arch: String) {
    val id = kernel + "-" + arch
  }

  object Platform {

    /** Create a platform with spaces stripped and case normalized. */
    def normalize(kernel: String, arch: String) = Platform(
      kernel.toLowerCase.filter(!_.isWhitespace),
      arch
    )

    /** Run 'uname' to determine current platform. Returns None if uname does not exist. */
    lazy val uname: Option[Platform] = {
      val lines = Try { Process("uname -sm").lineStream.head }.toOption
      lines.map { line =>
        val parts = line.split(" ")
        if (parts.length != 2) {
          sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line)
        } else {
          Platform.normalize(parts(0), parts(1))
        }
      }
    }

  }

  private final val BufferSize = 4096

  private final val LibraryManifest = "library"

  /** Extract a resource from this class loader to a temporary file. */
  private def extract(path: String): Option[File] = {
    var in: Option[InputStream] = None
    var out: Option[OutputStream] = None

    try {
      in = Option(NativeLoader.getClass.getResourceAsStream(path))
      if (in.isEmpty) return None

      val file = File.createTempFile(path, "")
      out = Some(new FileOutputStream(file))

      val buffer = new Array[Byte](BufferSize)
      var length = -1;
      do {
        length = in.get.read(buffer)
        if (length != -1) out.get.write(buffer, 0, length)
      } while (length != -1)

      Some(file)
    } finally {
      in.foreach(_.close)
      out.foreach(_.close)
    }
  }

  private def loadError(msg: String): Nothing = throw new UnsatisfiedLinkError(
    "Error during native library extraction " +
      "(this can happen if your platform is not supported by flow): " +
      msg
  )

  private def loadFromJar(libraryPrefix: String): Unit = {
    val platformDir: String = libraryPrefix + "/" + Platform.uname.map(_.id).getOrElse {
      loadError("Cannot determine current platform.")
    }

    val manifest: File = {
      val path = platformDir + "/" + LibraryManifest
      extract(path) getOrElse {
        loadError(s"Manifest file $path does not exist.")
      }
    }

    Source.fromFile(manifest, "utf-8").getLines foreach { libname =>
      val path = platformDir + "/" + libname
      val lib = extract(path) getOrElse loadError(
        s"Library $path not found."
      )
      System.load(lib.getAbsolutePath())
    }
  }

  /**
   * Load a native library from the available library path or fall back
   * to extracting and loading a native library from available resources.
   */
  def load(library: String, libraryPrefix: String): Unit = try {
    System.loadLibrary(library)
  } catch {
    case ex: UnsatisfiedLinkError => loadFromJar(libraryPrefix)
  }

}