@@ -8,13 +8,17 @@ This tool generates Scala Native bindings from C headers. It's built upon clang
8
8
9
9
Calling the tool is pretty easy, you need to specify the file(s) and the name of the created bindings.
10
10
11
- ` ./scala-native-bindgen /usr/include/uv.h -name uv -- `
11
+ ``` sh
12
+ scala-native-bindgen --name uv /usr/include/uv.h --
13
+ ```
12
14
13
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:
14
16
15
- ` ./scala-native-bindgen /usr/include/uv.h -name uv -- > uv.scala `
17
+ ``` sh
18
+ scala-native-bindgen --name uv /usr/include/uv.h -- > uv.scala
19
+ ```
16
20
17
- Run ` ./ scala-native-bindgen -help` to see all available options.
21
+ Run ` scala-native-bindgen - -help ` to see all available options.
18
22
19
23
## Obtaining bindgen
20
24
@@ -53,28 +57,58 @@ each merge to the `master` branch.
53
57
[ Docker ] : https://www.docker.com/
54
58
[ docker-bindgen.sh ] : scripts/docker-bindgen.sh
55
59
56
- ### Building
60
+ ## Building
57
61
58
- Building this tool requires [ CMake] , [ LLVM] and [ Clang] . See the [ Scala
59
- Native setup guide] for instructions on installing the dependencies.
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.
60
77
61
78
``` sh
62
79
mkdir -p bindgen/target
63
80
cd bindgen/target
64
81
cmake ..
65
82
make
66
- ./scala-native-bindgen /usr/include/ctype.h -name ctype --
83
+ ./scala-native-bindgen --name ctype /usr/include/ctype.h --
67
84
```
68
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
+
69
102
Alternatively, you can use [ docker-compose] to build and test the program:
70
103
71
104
``` sh
72
105
# Build the docker image with LLVM version 6.0.
73
106
docker-compose build ubuntu-18.04-llvm-6.0
74
107
# Build the bindgen tool and run the tests.
75
- docker-compose run --rm ubuntu-18.04-llvm-6.0
108
+ docker-compose run --rm ubuntu-18.04-llvm-6.0 ./script/test.sh
76
109
# Run the bindgen tool inside the container.
77
- docker-compose run --rm ubuntu-18.04-llvm-6.0 bindgen/target/scala-native-bindgen -name union tests/samples/Union.h --
110
+ docker-compose run --rm ubuntu-18.04-llvm-6.0 \
111
+ bindgen/target/scala-native-bindgen --name union tests/samples/Union.h --
78
112
```
79
113
80
114
[ CMake ] : https://cmake.org/
@@ -85,17 +119,19 @@ docker-compose run --rm ubuntu-18.04-llvm-6.0 bindgen/target/scala-native-bindge
85
119
86
120
## Testing
87
121
88
- The tests assume that the above instructions for building has been
89
- followed.
122
+ The tests assume that the above instructions for building ` scala-native-bindgen ` from source
123
+ has been followed.
90
124
91
125
``` sh
92
126
cd tests
93
127
sbt test
94
128
```
95
129
96
- ## Current limitations
130
+ ## Limitations
131
+
97
132
There are multiple unsupported cases that should be considered when generating bindings:
98
- 1 . Currently bindgen does not support passing structs by value.
133
+
134
+ 1 . Currently bindgen does not support passing structs by value.
99
135
For example, it will not be possible to call these two functions from Scala Native code:
100
136
``` c
101
137
struct MyStruct {
@@ -107,12 +143,26 @@ There are multiple unsupported cases that should be considered when generating b
107
143
void handleStruct(struct MyStruct mystr);
108
144
```
109
145
To support such cases one should generate bindings for C wrapper functions that use pointers to structs instead of actual structs.
110
- 2 . Bindgen does not generate bindings for defines.
111
- In order to use defines one should write wrapper functions that return defined values.
112
- 3 . There is no way to reuse already generated bindings.
113
- Bindgen outputs bindings also for headers that were included in a given header.
114
- 4 . Type qualifiers ` const ` , ` volatile ` and ` restrict ` are not supported.
115
- 5 . Extern variables are read-only.
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
116
166
117
167
## License
118
168
0 commit comments