-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add sbt plugin (#60) #111
Merged
Merged
Add sbt plugin (#60) #111
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the problem with Scala/sbt versions? How does this
project
function solve it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem was that Scala Native requires
scalaVersion
2.11 and sbt either 2.10 or 2.12. Except for the samples project which tests the generated bindings, all other sbt projects use thescalaVersion
associated with sbt.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍