Skip to content

Commit a016495

Browse files
committed
Document how to use the sbt plugin
1 parent 799396a commit a016495

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/src/paradox/obtaining-bindgen/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
@@@ index
44

5+
* [Use the sbt plugin](sbt-plugin.md)
56
* [Use Docker Container](docker-container.md)
67
* [Build Binary with CMake](cmake.md)
78
* [Build Binary with docker-compose](docker-compose.md)
@@ -10,6 +11,8 @@
1011

1112
There are 3 ways to obtain bindgen:
1213

14+
* @ref:[Use the sbt plugin](sbt-plugin.md)
15+
1316
* @ref:[Use docker container](docker-container.md)
1417

1518
* @ref:[Build binary with CMake](cmake.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)