diff options
Diffstat (limited to 'jni-library/src/main/scala/ch/jodersky/jni/Platform.scala')
-rw-r--r-- | jni-library/src/main/scala/ch/jodersky/jni/Platform.scala | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala b/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala new file mode 100644 index 0000000..db1662d --- /dev/null +++ b/jni-library/src/main/scala/ch/jodersky/jni/Platform.scala @@ -0,0 +1,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 + +} |