|
4 | 4 |
|
5 | 5 | This tool generates Scala Native bindings from C headers. It's built upon clang and Libtooling and thus respects the conventions of clang-tools. |
6 | 6 |
|
7 | | -## Usage |
8 | | - |
9 | | -Calling the tool is pretty easy, you need to specify the file(s) and the name of the created bindings. |
10 | | - |
11 | | -```sh |
12 | | -scala-native-bindgen --name uv /usr/include/uv.h -- |
13 | | -``` |
14 | | - |
15 | | -Running the previous command wild also yield warnings along with the translation. To keep only the bindings please redirect the output to a file like this: |
16 | | - |
17 | | -```sh |
18 | | -scala-native-bindgen --name uv /usr/include/uv.h -- > uv.scala |
19 | | -``` |
20 | | - |
21 | | -Run `scala-native-bindgen --help` to see all available options. |
22 | | - |
23 | | -## Obtaining bindgen |
24 | | - |
25 | | -There are 2 ways to obtain bindgen: |
26 | | - * [use docker container](#docker-container) |
27 | | - * [build binary from sources](#building) |
28 | | - |
29 | | -### Docker container |
30 | | - |
31 | | -This option requires [Docker]. |
32 | | - |
33 | | -Download docker image with the binary: |
34 | | - |
35 | | -```sh |
36 | | -docker pull scalabindgen/scala-native-bindgen |
37 | | -``` |
38 | | - |
39 | | -Mount directories with required header files and run bindgen: |
40 | | - |
41 | | -```sh |
42 | | -docker run -v "$(pwd)":/src -v /usr/include:/usr/include \ |
43 | | - --rm scalabindgen/scala-native-bindgen \ |
44 | | - relative/path/to/my_header.h --name my_header -- |
45 | | -``` |
46 | | - |
47 | | -The docker image does not contain standard headers so it is important to |
48 | | -mount all system include directories that are used by the header file |
49 | | -passed to `scala-native-bindgen`. See the [docker-bindgen.sh] script for |
50 | | -how to wrap the dockerized program. The `$CWD` of the container is |
51 | | -`/src` which should be mounted from `$(pwd)` in case relative paths are |
52 | | -used. |
53 | | - |
54 | | -Note, the `scalabindgen/scala-native-bindgen:latest` image is updated on |
55 | | -each merge to the `master` branch. |
56 | | - |
57 | | - [Docker]: https://www.docker.com/ |
58 | | - [docker-bindgen.sh]: scripts/docker-bindgen.sh |
59 | | - |
60 | | -## Building |
61 | | - |
62 | | -Building `scala-native-bindgen` requires the following tools and libraries: |
63 | | - |
64 | | - - [CMake] version 3.9 or higher |
65 | | - - [LLVM] and [Clang] version 5.0 or 6.0. |
66 | | - |
67 | | -```sh |
68 | | -# On apt-based systems |
69 | | -apt install cmake clang-$LLVM_VERSION libclang-$LLVM_VERSION-dev llvm-$LLVM_VERSION-dev |
70 | | - |
71 | | -# With `brew` |
72 | | -brew install cmake llvm@6 |
73 | | -``` |
74 | | - |
75 | | -To run the tests you also need the required Scala Native libraries. |
76 | | -See the [Scala Native setup guide] for instructions on installing the dependencies. |
77 | | - |
78 | | -```sh |
79 | | -mkdir -p bindgen/target |
80 | | -cd bindgen/target |
81 | | -cmake .. |
82 | | -make |
83 | | -./scala-native-bindgen --name ctype /usr/include/ctype.h -- |
84 | | -``` |
85 | | - |
86 | | -To build a statically linked executable pass `-DSTATIC_LINKING=ON` when invoking `cmake`: |
87 | | - |
88 | | -```sh |
89 | | -cmake -DSTATIC_LINKING=ON .. |
90 | | -``` |
91 | | - |
92 | | -Additional compiler and linker flags may be passed as environment variable sor their CMake |
93 | | -equivalent, e.g. to compile with debug symbols the following are the same: |
94 | | - |
95 | | -```sh |
96 | | -cmake -DCMAKE_CXX_FLAGS=-g .. |
97 | | -CXXFLAGS=-g cmake .. |
98 | | -``` |
99 | | - |
100 | | -### Building with `docker-compose` |
101 | | - |
102 | | -Alternatively, you can use [docker-compose] to build and test the program: |
103 | | - |
104 | | -```sh |
105 | | -# Build the docker image with LLVM version 6.0. |
106 | | -docker-compose build ubuntu-18.04-llvm-6.0 |
107 | | -# Build the bindgen tool and run the tests. |
108 | | -docker-compose run --rm ubuntu-18.04-llvm-6.0 ./script/test.sh |
109 | | -# Run the bindgen tool inside the container. |
110 | | -docker-compose run --rm ubuntu-18.04-llvm-6.0 \ |
111 | | - bindgen/target/scala-native-bindgen --name union tests/samples/Union.h -- |
112 | | -``` |
113 | | - |
114 | | - [CMake]: https://cmake.org/ |
115 | | - [LLVM]: https://llvm.org/ |
116 | | - [Clang]: https://clang.llvm.org/ |
117 | | - [Scala Native setup guide]: http://www.scala-native.org/en/latest/user/setup.html |
118 | | - [docker-compose]: https://docs.docker.com/compose/ |
119 | | - |
120 | | -## Testing |
121 | | - |
122 | | -The tests assume that the above instructions for building `scala-native-bindgen` from source |
123 | | -has been followed. |
124 | | - |
125 | | -```sh |
126 | | -cd tests |
127 | | -sbt test |
128 | | -``` |
129 | | - |
130 | | -## Limitations |
131 | | - |
132 | | -There are multiple unsupported cases that should be considered when generating bindings: |
133 | | - |
134 | | - 1. Currently bindgen does not support passing structs by value. |
135 | | - For example, it will not be possible to call these two functions from Scala Native code: |
136 | | - ```c |
137 | | - struct MyStruct { |
138 | | - int a; |
139 | | - }; |
140 | | - |
141 | | - struct MyStruct returnStruct(); |
142 | | - |
143 | | - void handleStruct(struct MyStruct mystr); |
144 | | - ``` |
145 | | - To support such cases one should generate bindings for C wrapper functions that use pointers to structs instead of actual structs. |
146 | | - 2. `#define`s for literals and variables are supported. For other types of `#define`s, |
147 | | - write wrapper functions that return defined values. |
148 | | - ```c |
149 | | - // Supported |
150 | | - #define ESC 0x1b /* Defines for numerical and string literals. */ |
151 | | - extern const int pi_const; |
152 | | - #define PI pi_const /* Defines aliasing extern variables. */ |
153 | | - |
154 | | - // Not supported (non-exhaustive list) |
155 | | - #define COLS (getenv("COLS") ? atoi(getenv("COLS")) : 80) |
156 | | - #define MAX(a, b) (a > b ? a : b) |
157 | | - ``` |
158 | | - |
159 | | - 3. There is no way to reuse already generated bindings. |
160 | | - Bindgen outputs bindings also for headers that were included in a given header. See [#2]. |
161 | | - 4. Type qualifiers `const`, `volatile` and `restrict` are not supported. |
162 | | - 5. Extern variables are read-only. See [scala-native/scala-native#202]. |
163 | | - |
164 | | - [#2]: https://github.com/kornilova-l/scala-native-bindgen/issues/2 |
165 | | - [scala-native/scala-native#202]: https://github.com/scala-native/scala-native/issues/202 |
| 7 | +[Documentation](https://kornilova-l.github.io/scala-native-bindgen/) |
166 | 8 |
|
167 | 9 | ## License |
168 | 10 |
|
|
0 commit comments