|
| 1 | +# Configuration |
| 2 | + |
| 3 | +Bindgen provides several options for configuring what to include and how |
| 4 | +to output the generated bindings. The options can be provided to the CLI |
| 5 | +or as part of the sbt bindings declaration. |
| 6 | + |
| 7 | +@@ toc |
| 8 | + |
| 9 | +## Excluding Definitions By Prefix |
| 10 | + |
| 11 | +Definitions may be excluded by their prefix. This is useful when private definitions should not be part of the generated binding. This is often the case for definitions starting with `__`. |
| 12 | + |
| 13 | +sbt |
| 14 | +: @@snip [sbt] (../../../build.sbt) { #sbt-exclude-prefix } |
| 15 | + |
| 16 | +CLI |
| 17 | +: ```sh |
| 18 | + scala-native-bindgen --binding-config=/path/to/config |
| 19 | + ``` |
| 20 | + |
| 21 | +To exemplify consider the following header file: |
| 22 | + |
| 23 | +@@snip [sbt] (../../../sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/src/main/resources/stdlib.h) |
| 24 | + |
| 25 | +When the exclude prefix is set to `__`, then the resulting bindings will be: |
| 26 | + |
| 27 | +@@snip [sbt] (../../../sbt-scala-native-bindgen/src/sbt-test/bindgen/generate/expected/stdlib.scala) |
| 28 | + |
| 29 | +## Binding Configuration File |
| 30 | + |
| 31 | +The binding configuration is a JSON file which allows to map the path of |
| 32 | +a header file to the associated object as well as the names of the C |
| 33 | +types and symbols to their respective Scala types and definitions. The |
| 34 | +configuration file can be used when integrating with third party |
| 35 | +bindings. |
| 36 | + |
| 37 | +sbt |
| 38 | +: @@snip [sbt-binding-config](../../../build.sbt) { #sbt-binding-config } |
| 39 | + |
| 40 | +CLI |
| 41 | +: ```sh |
| 42 | + scala-native-bindgen --binding-config=/path/to/config |
| 43 | + ``` |
| 44 | + |
| 45 | +### Using Types From Third Party Bindings |
| 46 | + |
| 47 | +If you need to generate bindings that uses types from bindings that have not been generated with `scala-native-bindgen` you have to provide the mapping between the header path and the Scala object's fully qualified name. And in some cases also the Scala name of the C types. Using the vector library example from @ref:[Using Bindings](using-bindings.md), let's assume that the vector library was created so that `struct point` was named `Point`: |
| 48 | + |
| 49 | +@@snip [Vector] (../test/scala/com/example/custom/binding/Vector.scala) { #example } |
| 50 | + |
| 51 | +To use this binding, create a configuration file with the folllowing content, where `path` is the name of the header file (usually the part of the path inside the `/usr/include` or `/usr/local/include` directory), `object` is the fully qualified name of the Scala object (i.e. package name as well as the Scala object name) and finally `names` for each of the types: |
| 52 | + |
| 53 | +@@snip [vector.h] (../test/resources/3rd-party-bindings/config.json) |
| 54 | + |
| 55 | +Now in the library you are creating a binding for, any usage of `struct point`: |
| 56 | + |
| 57 | +@@snip [vector.h] (../test/resources/3rd-party-bindings/geometry.h) { #using-struct-point } |
| 58 | + |
| 59 | +will reference the `Point` type alias inside the `Vector` object: |
| 60 | + |
| 61 | +@@snip [Geometry] (../test/scala/org/example/Geometry.scala) { #using-struct-point } |
| 62 | + |
| 63 | +### Using Types From the Scala Native Bindings |
| 64 | + |
| 65 | +Similar to the above, the following example shows how you can use |
| 66 | +types defined in the [C standard library] and [C POSIX library] bindings |
| 67 | +shipped with Scala Native. Let's assume we have a binding with a method that uses the `FILE` type |
| 68 | +from `<stdio.h>`: |
| 69 | + |
| 70 | +@@snip [vector.h] (../test/resources/scala-native-bindings/wordcount.h) { #using-stdio-file } |
| 71 | + |
| 72 | +We can then write a binding configuration that maps the header name to the object defined in Scala Native. |
| 73 | + |
| 74 | +@@snip [vector.h] (../test/resources/scala-native-bindings/config.json) |
| 75 | + |
| 76 | +@@@ note |
| 77 | + |
| 78 | +In the above binding configuration, we duplicate the mapping to work on both Linux and macOS since on macOS |
| 79 | +the definition of `FILE` is found inside `/usr/include/_stdio.h`. |
| 80 | + |
| 81 | +@@@ |
| 82 | + |
| 83 | +The generated binding will then use the `stdio.h` binding provided by Scala Native: |
| 84 | + |
| 85 | +@@snip [WordCount] (../test/scala/org/example/WordCount.scala) { #using-stdio-file } |
| 86 | + |
| 87 | +And we can use the binding by opening a file using `fopen(...)`: |
| 88 | + |
| 89 | +@@snip [WordCountSpec] (../test/scala/org/scalanative/bindgen/docs/WordCountSpec.scala) { #example } |
| 90 | + |
| 91 | + [Scala Native memory management]: http://www.scala-native.org/en/latest/user/interop.html#memory-management |
| 92 | + [Scala Native memory layout types]: http://www.scala-native.org/en/latest/user/interop.html#memory-layout-types |
| 93 | + [C standard library]: http://www.scala-native.org/en/latest/lib/libc.html |
| 94 | + [C POSIX library]: http://www.scala-native.org/en/latest/lib/posixlib.html |
0 commit comments