Generating Bindings with sbt
To add the sbt plugin to your project add the following lines in project/plugins.sbt
:
addSbtPlugin("org.scala-native.bindgen" % "sbt-scala-native-bindgen" % VERSION)
resolvers += Resolver.bintrayIvyRepo("scala-native-bindgen", "sbt-plugins")
resolvers += Resolver.bintrayRepo("scala-native-bindgen", "maven")
Full source at GitHub
Replacing VERSION
with 0.3.1 to use the latest version.
Next configure the plugin using the nativeBindings
setting scoped to either Compile
or Test
. The NativeBinding
type to configure each binding that should be generated.
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.
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.:
nativeBindgenPath := file("/path/to/scala-native-bindgen")
Example settings:
enablePlugins(ScalaNativeBindgenPlugin)
inConfig(Compile)(
Def.settings(
nativeBindings += {
NativeBinding(resourceDirectory.value / "stdlib.h")
.name("stdlib")
.packageName("org.example.app.stdlib")
.excludePrefix("__")
}
))
Full source at GitHub
Given that the stdlib.h
header file contains:
int access(const char *path, int mode);
int read(int fildes, void *buf, int nbyte);
int printf(const char *restrict format, ...);
int __excluded(void);
Full source at GitHub
Running nativeBindgen
will generate a file named target/scala-2.11/src_managed/main/sbt-scala-native-bindgen/stdlib.scala
containing the following lines:
package org.example.app.stdlib
import scala.scalanative._
import scala.scalanative.native._
@native.extern
object stdlib {
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
}
Full source at GitHub