aboutsummaryrefslogtreecommitdiff
path: root/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala
blob: db1662d3a9f419cc8cb23ed0c7e43e71dd649cc7 (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
package ch.jodersky.jni

import java.io.IOException
import scala.sys.process.Process

/**
 * A platform is the representation of an architecture-kernel combination.
 * It is a somewhat ambigous concept defined as the set of all system configurations
 * capable of running a native binary.
 */
case class Platform private (arch: String, kernel: String) {

  /**
   * String representation of this platform. It is inspired by Autotools' platform
   * triplet, without the vendor field.
   */
  def id = arch + "-" + kernel

}

object Platform {

  final val Unknown = Platform("unknown", "unknown")

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

  /** Run 'uname' to determine current platform. Returns None if uname does not exist. */
  def uname: Option[Platform] = {
    val lineOpt = try {
      Some(Process("uname -sm").lines.head)
    } catch {
      case _: IOException => None
    }
    lineOpt.map { line =>
      val parts = line.split(" ")
      if (parts.length != 2) {
        sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line)
      } else {
        Platform.normalized(parts(1), parts(0))
      }
    }
  }

  def current = uname

}