-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from kornilova-l/sbt-plugin
Add sbt plugin (#60)
- Loading branch information
Showing
10 changed files
with
199 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...-native-bindgen/src/main/scala/org/scalanative/bindgen/sbt/ScalaNativeBindgenPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package org.scalanative.bindgen.sbt | ||
|
||
import sbt._ | ||
import sbt.Keys._ | ||
|
||
import org.scalanative.bindgen.Bindgen | ||
|
||
/** | ||
* Generate Scala bindings from C headers. | ||
* | ||
* == Usage == | ||
* | ||
* This plugin must be explicitly enabled. To enable it add the following line | ||
* to your `.sbt` file: | ||
* {{{ | ||
* enablePlugins(ScalaNativeBindgenPlugin) | ||
* }}} | ||
* | ||
* By default, the plugin reads the configured header file and generates | ||
* a single Scala source file in the managed source directory. | ||
* | ||
* See the [[https://github.com/kornilova-l/scala-native-bindgen/tree/master/sbt-scala-native-bindgen/src/sbt-test/bindgen/generate example project]]. | ||
* | ||
* == Configuration == | ||
* | ||
* Keys are defined in [[ScalaNativeBindgenPlugin.autoImport]]. | ||
* | ||
* - `nativeBindgenHeader`: The C header file to read. | ||
* | ||
* - `nativeBindgenPackage`: Package of the enclosing object. | ||
* No package by default. | ||
* | ||
* - `name in nativeBindgen`: Name of the enclosing object. | ||
* | ||
* @example | ||
* {{{ | ||
* nativeBindgenHeader in Compile := file("/usr/include/ctype.h") | ||
* nativeBindgenPackage in Compile := Some("org.example.app") | ||
* name in (Compile, nativeBindgen) := "ctype" | ||
* }}} | ||
*/ | ||
object ScalaNativeBindgenPlugin extends AutoPlugin { | ||
|
||
object autoImport { | ||
val nativeBindgenPath = | ||
taskKey[Option[File]]("Path to the scala-native-bindgen executable") | ||
val nativeBindgenHeader = taskKey[File]("C header file") | ||
val nativeBindgenPackage = | ||
settingKey[Option[String]]("Package for the generated code") | ||
val nativeBindgenLink = | ||
settingKey[Option[String]]("Name of library to be linked") | ||
val nativeBindgenExclude = settingKey[Option[String]]("Exclude prefix") | ||
val nativeBindgen = taskKey[File]("Generate Scala Native bindings") | ||
} | ||
import autoImport._ | ||
|
||
override def requires = plugins.JvmPlugin | ||
|
||
override def projectSettings: Seq[Setting[_]] = | ||
nativeBindgenScopedSettings(Compile) | ||
|
||
private implicit class BindgenOps(val bindgen: Bindgen) extends AnyVal { | ||
def maybe[T](opt: Option[T], f: Bindgen => T => Bindgen): Bindgen = | ||
opt match { | ||
case None => bindgen | ||
case Some(value) => f(bindgen)(value) | ||
} | ||
} | ||
|
||
def nativeBindgenScopedSettings(conf: Configuration): Seq[Setting[_]] = | ||
inConfig(conf)( | ||
Seq( | ||
nativeBindgenHeader := { | ||
sys.error("nativeBindgenHeader not configured") | ||
}, | ||
nativeBindgenPath := None, | ||
nativeBindgenPackage := None, | ||
nativeBindgenExclude := None, | ||
resourceDirectories in nativeBindgen := resourceDirectories.value, | ||
sourceGenerators += Def.task { Seq(nativeBindgen.value) }, | ||
name in nativeBindgen := "ScalaNativeBindgen", | ||
nativeBindgen := { | ||
val output = sourceManaged.value / "sbt-scala-native-bindgen" / "nativeBindgen.scala" | ||
|
||
Bindgen() | ||
.bindgenExecutable(nativeBindgenPath.value.get) | ||
.header(nativeBindgenHeader.value) | ||
.name((name in nativeBindgen).value) | ||
.maybe(nativeBindgenLink.value, _.link) | ||
.maybe(nativeBindgenPackage.value, _.packageName) | ||
.maybe(nativeBindgenExclude.value, _.excludePrefix) | ||
.generate() | ||
.writeToFile(output) | ||
|
||
output | ||
} | ||
)) | ||
} |
35 changes: 35 additions & 0 deletions
35
sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/build.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name := "test" | ||
organization := "org.scalanative.bindgen.sbt.test" | ||
enablePlugins(ScalaNativeBindgenPlugin) | ||
scalaVersion := "2.11.12" | ||
|
||
inConfig(Compile)( | ||
Def.settings( | ||
nativeBindgenPath := Option(System.getProperty("bindgen.path")).map(file), | ||
nativeBindgenHeader := (resourceDirectory in Compile).value / "header.h", | ||
nativeBindgenPackage := Some("org.example.app"), | ||
nativeBindgenLink := Some("app"), | ||
nativeBindgenExclude := Some("__"), | ||
name in nativeBindgen := "AppAPI" | ||
)) | ||
|
||
TaskKey[Unit]("check") := { | ||
val file = (nativeBindgen in Compile).value | ||
val expected = | ||
"""package org.example.app | ||
| | ||
|import scala.scalanative._ | ||
|import scala.scalanative.native._ | ||
| | ||
|@native.link("app") | ||
|@native.extern | ||
|object AppAPI { | ||
| def access(path: native.CString, mode: native.CInt): native.CInt = native.extern | ||
| def read(fildes: native.CInt, buf: native.Ptr[Byte], nbyte: native.CInt): native.CInt = native.extern | ||
| def printf(format: native.CString, varArgs: native.CVararg*): native.CInt = native.extern | ||
|} | ||
""".stripMargin | ||
|
||
assert(file.exists) | ||
assert(IO.read(file).trim == expected.trim) | ||
} |
3 changes: 3 additions & 0 deletions
3
sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
addSbtPlugin( | ||
"org.scalanative.bindgen" % "sbt-scala-native-bindgen" % sys.props( | ||
"plugin.version")) |
3 changes: 3 additions & 0 deletions
3
sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/src/main/resources/header.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int access(const char *path, int mode); | ||
int read(int fildes, void *buf, int nbyte); | ||
int printf(const char *restrict format, ...); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
> check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters