From 4403f3cfcdba57657918b47d86799a6b10455022 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 21 May 2013 17:16:23 +0200 Subject: move native build to sbt --- Makefile | 19 ------------ build.sbt | 9 ------ project/Build.scala | 43 +++++++++++++++++++++++++++ project/NativeBuild.scala | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 28 deletions(-) delete mode 100644 Makefile delete mode 100644 build.sbt create mode 100644 project/Build.scala create mode 100644 project/NativeBuild.scala diff --git a/Makefile b/Makefile deleted file mode 100644 index 01c1b6a..0000000 --- a/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -TARGET=target -SCALA_VERSION=2.10 -CLASSPATH=$(TARGET)/scala-$(SCALA_VERSION)/classes -TARGETDIR=target - -all: flow.so - -javah: $(CLASSPATH) - javah -d src/main/c/ -classpath $(CLASSPATH) com.github.jodersky.flow.NativeSerial - -flow.o: - gcc -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux -fPIC -c src/main/c/flow.c -o flow.o - -flow.so: flow.o - gcc -shared -Wl,-soname,libflow.so.1 -o libflow.so $< -lc - -clean: - rm -f *.o - rm -f *.so diff --git a/build.sbt b/build.sbt deleted file mode 100644 index 0ae0f6f..0000000 --- a/build.sbt +++ /dev/null @@ -1,9 +0,0 @@ -name := "flow" - -scalaVersion := "2.10.1" - -fork := true - -connectInput in run := true - -javaOptions in run += "-Djava.library.path=." \ No newline at end of file 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 -- cgit v1.2.3