|
| 1 | +# Using the sbt plugin |
| 2 | + |
| 3 | +To add the sbt plugin to your project add the following lines in `project/plugins.sbt`: |
| 4 | + |
| 5 | +@@@ vars |
| 6 | +```sbt |
| 7 | +addSbtPlugin("org.scala-native.bindgen" % "sbt-scala-native-bindgen" % "$project.version$") |
| 8 | + |
| 9 | +resolvers += Resolver.bintrayIvyRepo("scala-native-bindgen", "sbt-plugin") |
| 10 | +resolvers += Resolver.bintrayRepo("scala-native-bindgen", "maven") |
| 11 | +``` |
| 12 | +@@@ |
| 13 | + |
| 14 | +Next configure the plugin using the settings scoped to either `Compile` or `Test`: |
| 15 | + |
| 16 | +|---------------------------|-------------------| |
| 17 | +|`nativeBindgenHeader` | The C header file to read. |
| 18 | +|`nativeBindgenPackage` | Package of the enclosing object. No package by default. |
| 19 | +|`name in nativeBindgen` | Name of the enclosing object. |
| 20 | +|`nativeBindgenLink` | Name of library to be linked. |
| 21 | + |
| 22 | +@@@ note |
| 23 | + |
| 24 | +By default the `scala-native-bindgen` executable is downloaded automatically for supported platforms. Set `version in nativeBindgen` (unscoped) to configure the version of the `scala-native-bindgen` to use if you want a version different from the version of the sbt plugin. |
| 25 | + |
| 26 | +In case your platform is not supported, you must compile `scala-native-bindgen` yourself and configure the path to the executable using `nativeBindgenPath`, e.g.: |
| 27 | + |
| 28 | +```sbt |
| 29 | +nativeBindgenPath := file("/path/to/scala-native-bindgen") |
| 30 | +``` |
| 31 | + |
| 32 | +@@@ |
| 33 | + |
| 34 | +Example settings: |
| 35 | + |
| 36 | +```sbt |
| 37 | +enablePlugins(ScalaNativeBindgenPlugin) |
| 38 | +inConfig(Compile)( |
| 39 | + Def.settings( |
| 40 | + nativeBindgenHeader := (resourceDirectory in Compile).value / "header.h", |
| 41 | + nativeBindgenPackage := Some("org.example.mylib"), |
| 42 | + nativeBindgenLink := Some("mylib"), // Will pass `-lmylib` to the linker |
| 43 | + nativeBindgenExclude := Some("__"), |
| 44 | + name in nativeBindgen := "MyLib" |
| 45 | + )) |
| 46 | +``` |
| 47 | + |
| 48 | +Running `nativeBindgen` will generate a file named `target/scala-2.x/src_managed/main/sbt-scala-native-bindgen//ScalaNativeBindgen.scala` containing something along the following lines: |
| 49 | + |
| 50 | +```scala |
| 51 | +package org.example.mylib |
| 52 | + |
| 53 | +import scala.scalanative._ |
| 54 | +import scala.scalanative.native._ |
| 55 | + |
| 56 | +@native.link("mylib") |
| 57 | +@native.extern |
| 58 | +object MyLib { |
| 59 | + // ... left out for brevity ... |
| 60 | +} |
| 61 | +``` |
0 commit comments