Skip to content

Commit 07d8c72

Browse files
authored
[ffigen] Symbol file import export (dart-lang#468)
1 parent e50394b commit 07d8c72

31 files changed

Lines changed: 964 additions & 29 deletions

‎pkgs/ffigen/.github/workflows/test-package.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
sdk: ${{ matrix.sdk }}
2828
- id: install
2929
name: Install dependencies
30-
run: dart pub get
30+
run: dart pub get && dart pub get --directory="example/shared_bindings"
3131
- name: Check formatting
3232
run: dart format --output=none --set-exit-if-changed .
3333
if: always() && steps.install.outcome == 'success'

‎pkgs/ffigen/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 7.2.0
2+
- Added support for sharing bindings using `symbol-file` config. (See `README.md`
3+
and examples/shared_bindings).
4+
15
# 7.1.0
26
- Handle declarations with definition accessible from a different entry-point.
37

‎pkgs/ffigen/README.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ The following configuration options are available-
9191

9292
```yaml
9393
output: 'generated_bindings.dart'
94+
```
95+
or
96+
```yaml
97+
output:
98+
bindings: 'generated_bindings.dart'
99+
...
94100
```
95101
</td>
96102
</tr>
@@ -521,6 +527,36 @@ language: 'objc'
521527
```
522528
</td>
523529
</tr>
530+
<tr>
531+
<td>output -> symbol-file</td>
532+
<td>Generates a symbol file yaml containing all types defined in the generated output.</td>
533+
<td>
534+
535+
```yaml
536+
output:
537+
...
538+
symbol-file:
539+
# Although file paths are supported here, prefer Package Uri's here
540+
# so that other pacakges can use them.
541+
output: 'package:some_pkg/symbols.yaml'
542+
import-path: 'package:some_pkg/base.dart'
543+
```
544+
</td>
545+
</tr>
546+
<tr>
547+
<td>import -> symbol-files</td>
548+
<td>Import symbols from a symbol file. Used for sharing type definitions from other pacakges.</td>
549+
<td>
550+
551+
```yaml
552+
import:
553+
symbol-files:
554+
# Both package Uri and file paths are supported here.
555+
- 'package:some_pkg/symbols.yaml'
556+
- 'path/to/some/symbol_file.yaml'
557+
```
558+
</td>
559+
</tr>
524560
</tbody>
525561
</table>
526562

@@ -757,3 +793,16 @@ Ffigen can sometimes generate a lot of logs, especially when it's parsing a lot
757793
Level options are - `[all, fine, info (default), warning, severe]`.
758794
The `all` and `fine` will print a ton of logs are meant for debugging
759795
purposes only.
796+
797+
### How can type definitions be shared?
798+
799+
Ffigen can share type definitions using symbol files.
800+
- A package can generate a symbol file using the `output -> symbol-file` config.
801+
- And another package can then import this, using `import -> symbol-files` config.
802+
- Doing so will reuse all the types such as Struct/Unions, and will automatically
803+
exclude generating other types (E.g functions, enums, macros).
804+
805+
Checkout `examples/shared_bindings` for details.
806+
807+
For manually reusing definitions from another package, the `library-imports`
808+
and `type-map` config can be used.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Files and directories created by pub.
2+
.dart_tool/
3+
.packages
4+
# Remove the following pattern if you wish to check in your lock file.
5+
pubspec.lock
6+
7+
# Conventional directory for build outputs.
8+
build/
9+
10+
# Directory created by dartdoc.
11+
doc/api/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Shared bindings
2+
3+
An example to showcase how bindings can share types
4+
with other bindings.
5+
6+
## Generating bindings
7+
At the root of this example (`example/shared_bindings`), run -
8+
```
9+
dart run generate.dart
10+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
name: NativeLibraryA
6+
description: Bindings to `headers/a.h`.
7+
output: '../lib/generated/a_gen.dart'
8+
headers:
9+
entry-points:
10+
- '../headers/a.h'
11+
preamble: |
12+
// ignore_for_file: non_constant_identifier_names, camel_case_types
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
name: NativeLibraryASharedB
6+
description: Bindings to `headers/a.h` with shared definitions from `headers/base.h`.
7+
output: '../lib/generated/a_shared_b_gen.dart'
8+
headers:
9+
entry-points:
10+
- '../headers/a.h'
11+
import:
12+
symbol-files:
13+
# Both package Uri and file paths are supported here.
14+
- 'package:shared_bindings/generated/base_symbols.yaml'
15+
preamble: |
16+
// ignore_for_file: non_constant_identifier_names, camel_case_types
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
name: NativeLibraryBase
6+
description: Bindings to `headers/base.h`.
7+
output:
8+
bindings: '../lib/generated/base_gen.dart'
9+
symbol-file:
10+
# Although file paths are supported here, prefer Package Uri's here.
11+
output: 'package:shared_bindings/generated/base_symbols.yaml'
12+
import-path: 'package:shared_bindings/generated/base_gen.dart'
13+
headers:
14+
entry-points:
15+
- '../headers/base.h'
16+
preamble: |
17+
// ignore_for_file: non_constant_identifier_names, camel_case_types
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import 'dart:io';
2+
3+
import 'package:cli_util/cli_util.dart';
4+
import 'package:path/path.dart' as p;
5+
6+
ProcessResult runFfigenForConfig(String sdkPath, String configPath) {
7+
return Process.runSync(
8+
p.join(sdkPath, 'bin', 'dart'),
9+
[
10+
'run',
11+
'ffigen',
12+
'--config=$configPath',
13+
],
14+
runInShell: Platform.isWindows,
15+
);
16+
}
17+
18+
void main() {
19+
final sdkPath = getSdkPath();
20+
final configPaths = [
21+
'ffigen_configs/base.yaml',
22+
'ffigen_configs/a.yaml',
23+
'ffigen_configs/a_shared_base.yaml'
24+
];
25+
for (final configPath in configPaths) {
26+
final res = runFfigenForConfig(sdkPath, configPath);
27+
print(res.stdout.toString());
28+
if (res.exitCode != 0) {
29+
throw Exception("Some error occurred: ${res.stderr.toString()}");
30+
}
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#include "base.h"
3+
4+
struct A_Struct1{
5+
int a;
6+
};
7+
8+
union A_Union1{
9+
int a;
10+
};
11+
12+
enum A_Enum{
13+
A_ENUM_1,
14+
A_ENUM_2,
15+
};
16+
17+
#define A_MACRO_1 1;
18+
19+
void a_func1();
20+
21+
void a_func2(struct BaseStruct2 s, union BaseUnion2 u, BaseTypedef2 t);

0 commit comments

Comments
 (0)