aboutsummaryrefslogtreecommitdiff
path: root/project
diff options
context:
space:
mode:
Diffstat (limited to 'project')
-rw-r--r--project/Build.scala43
-rw-r--r--project/NativeBuild.scala75
2 files changed, 118 insertions, 0 deletions
diff --git a/project/Build.scala b/project/Build.scala
new file mode 100644
index 0000000..be1febc
--- /dev/null
+++ b/project/Build.scala
@@ -0,0 +1,43 @@
+import sbt._
+import Keys._
+import NativeBuild._
+
+object FlowBuild extends Build {
+ val Organization = "com.github.jodersky"
+ val Version = "1.0-SNAPSHOT"
+ val ScalaVersion = "2.10.1"
+
+ lazy val root = Project(
+ id = "flow",
+ base = file("."),
+ settings = buildSettings ++ nativeSettings ++ runSettings)
+
+ lazy val buildSettings = Defaults.defaultSettings ++ Seq(
+ organization := Organization,
+ version := Version,
+ scalaVersion := ScalaVersion,
+ resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/",
+ scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature"),
+ compileOrder in Compile := CompileOrder.JavaThenScala)
+
+ lazy val nativeSettings = NativeBuild.defaults ++ Seq(
+ NativeBuild.compiler := "gcc",
+ options := Seq("-fPIC"),
+ linker := "gcc",
+ linkerOptions := Seq("-shared", "-Wl,-soname,libflow.so.1"),
+ linkerOutput <<= NativeBuild.outputDirectory(_ / "libflow.so")
+ )
+
+ lazy val runSettings = Seq(
+ fork := true,
+ connectInput in run := true,
+ javaOptions in run += "-Djava.library.path=.")
+}
+
+object Dependencies {
+ lazy val all = Seq()
+
+ //lazy val io = "com.github.scala-incubator.io" %% "scala-io-core" % "0.4.2"
+ //lazy val file = "com.github.scala-incubator.io" %% "scala-io-file" % "0.4.2"
+
+}
diff --git a/project/NativeBuild.scala b/project/NativeBuild.scala
new file mode 100644
index 0000000..a77e8f8
--- /dev/null
+++ b/project/NativeBuild.scala
@@ -0,0 +1,75 @@
+import sbt._
+import Keys._
+
+object NativeBuild {
+
+ //settings
+ val sourceDirectory = SettingKey[File]("native-source-directory", "Native source directory containing files to compile.")
+ val compiler = SettingKey[String]("native-compiler", "Native compiler.")
+ val options = SettingKey[Seq[String]]("native-options", "Flags for native compiler.")
+ val includeDirectories = SettingKey[Seq[File]]("native-include-directories", "Include directories for native compiler.")
+ val outputDirectory = SettingKey[File]("native-output-directory", "Directory for native output.")
+ val linker = SettingKey[String]("native-linker", "Native linker.")
+ val linkerOutput = SettingKey[File]("native-linker-output", "Name of linker output.")
+ val linkerOptions = SettingKey[Seq[String]]("native-linker-options", "Native linker options.")
+ val linkerLibraries = SettingKey[Seq[String]]("native-linker-libraries", "Libraries against which to link.")
+
+ //tasks
+ val outputFromSource = TaskKey[File => File]("native-output-from-source", "Get name of native binary from source file.")
+ val sources = TaskKey[Seq[File]]("native-source", "Native source files to compile.")
+ val makeOutputDirectory = TaskKey[Unit]("native-make-output-directory", "Make native output directory.")
+ val compile = TaskKey[Unit]("native-compile", "Compiles native sources.")
+ val link = TaskKey[Unit]("native-link", "Link native sources.")
+
+ //task implementations
+ val outputFromSourceTask = outputFromSource <<= (outputDirectory) map {
+ outputDir =>
+ ((src: File) => {file((outputDir / src.base).absolutePath + ".o")})
+ }
+
+ val makeOutputDirectoryTask = makeOutputDirectory <<= (outputDirectory) map {o => o.mkdirs(); {}}
+
+ def compileSingleFile(compiler: String, options: Seq[String], includeDirectories: Seq[File], source: File, s2o: File => File): Unit = {
+ val cmdParts =
+ List(compiler) ++
+ options ++
+ includeDirectories.map(i => "-I" + i.absolutePath) ++
+ List("-c", source.absolutePath) ++
+ List("-o", s2o(source))
+
+ val cmd = cmdParts.mkString(" ")
+ cmd !
+ }
+
+ val compileTask = compile <<= (compiler, options, includeDirectories, sources, outputFromSource) map {
+ (c, f, i, srcs, out) => for (s <- srcs) compileSingleFile(c,f,i,s,out)
+ } dependsOn(makeOutputDirectory)
+
+ val linkTask = link <<= (linker, linkerOptions, linkerLibraries, linkerOutput, sources, outputFromSource) map { (l, opts, libs, out, srcs, s2o) =>
+ val outs = srcs.map(s2o(_))
+ val cmd: Seq[String] = Seq(l) ++ opts ++ Seq("-o", out.absolutePath) ++ outs.map(_.absolutePath) ++ libs.map(lib => "-l" + lib)
+ cmd !;
+ {}
+ } dependsOn(compile)
+
+
+
+ lazy val defaults = Seq(
+ compiler := "gcc",
+ options := Seq(),
+ sourceDirectory <<= Keys.sourceDirectory(_ / "main" / "c"),
+ sources <<= sourceDirectory map (dir => (dir ** "*.c").get),
+ includeDirectories <<= sourceDirectory(dir => Seq(dir)),
+ outputDirectory <<= target(_ / "c"),
+ linker := "gcc",
+ linkerOutput <<= outputDirectory(_ / "a.out"),
+ linkerOptions := Seq(),
+ linkerLibraries := Seq(),
+
+ outputFromSourceTask,
+ makeOutputDirectoryTask,
+ compileTask,
+ linkTask
+ )
+
+} \ No newline at end of file