Skip to content
This repository was archived by the owner on Jan 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
33db279
Export symbol.yaml
mannprerak2 Oct 4, 2022
21bafda
Use symbol-map after type-map usage.
mannprerak2 Oct 4, 2022
3039894
Exclude all symbols from symbol-file-map to be generated (Unless used…
mannprerak2 Oct 4, 2022
831ddea
Fix relative path from config handling in symbol-file-map
mannprerak2 Oct 4, 2022
5604ad9
Add a small example for shared bindings
mannprerak2 Oct 4, 2022
364cd71
Ignore lint in shared_bindings
mannprerak2 Oct 4, 2022
2d0e278
Merge symbol-file config into library-imports.
mannprerak2 Oct 5, 2022
64e3075
format
mannprerak2 Oct 5, 2022
2f665ae
Update symbol-file config.
mannprerak2 Oct 5, 2022
8593b6d
Completed symbol file output
mannprerak2 Oct 16, 2022
e06ecd1
Complete symbol file import.
mannprerak2 Oct 16, 2022
09e0a1e
nit
mannprerak2 Oct 16, 2022
e13ff7c
Merge branch 'master' of github.com:dart-lang/ffigen into symbol_file…
mannprerak2 Oct 23, 2022
db3c6f2
Replace custom YamlWritor with package:yaml_edit
mannprerak2 Oct 23, 2022
e0c34e5
Add warning for not using package uri in symbol-file -> output, symbl…
mannprerak2 Oct 23, 2022
890dd26
Update version, changelog
mannprerak2 Oct 23, 2022
c9471cc
Minor changes
mannprerak2 Oct 23, 2022
951a9e9
nit
mannprerak2 Oct 23, 2022
b02954c
regen shared_bindings
mannprerak2 Oct 23, 2022
b1bf423
Check - Run analyze on ubuntu-22.04 (since macos-latest is failing un…
mannprerak2 Oct 23, 2022
58b5567
Revert "Check - Run analyze on ubuntu-22.04 (since macos-latest is fa…
mannprerak2 Oct 23, 2022
f5059e8
remove completed todo
mannprerak2 Oct 23, 2022
c4572f9
Check - remove Build test dylib and bindings step
mannprerak2 Oct 23, 2022
38c27a4
Revert "Check - remove Build test dylib and bindings step"
mannprerak2 Oct 23, 2022
aab4561
Check - non package uri
mannprerak2 Oct 23, 2022
bb203f8
Revert "Check - non package uri"
mannprerak2 Oct 23, 2022
b5562d0
Run dart pub get in example/shared_bindings as well in analyze job
mannprerak2 Oct 23, 2022
18bb701
Add example test for shared_bindings
mannprerak2 Oct 23, 2022
406869b
Update readme
mannprerak2 Oct 23, 2022
b3ce1ad
Fix missing codeNormalizer parameters
mannprerak2 Oct 23, 2022
5bce3fc
Remove macros from example/shared_bindings
mannprerak2 Oct 23, 2022
c8d8a53
Revert "Remove macros from example/shared_bindings"
mannprerak2 Oct 24, 2022
5e056cb
Add warning for removing macros, update shared_bindings example
mannprerak2 Oct 24, 2022
3c119d1
nit
mannprerak2 Oct 24, 2022
8fc5cb0
Add newline if not present to yaml_edit output
mannprerak2 Oct 24, 2022
28bc89a
Replace run.sh with generate.dart with Process.runSync
mannprerak2 Oct 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
run: dart pub get && dart pub get --directory="example/shared_bindings"
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 7.2.0
- Added support for sharing bindings using `symbol-file` config. (See `README.md`
and examples/shared_bindings).

# 7.1.0
- Handle declarations with definition accessible from a different entry-point.

Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ The following configuration options are available-

```yaml
output: 'generated_bindings.dart'
```
or
```yaml
output:
bindings: 'generated_bindings.dart'
...
```
</td>
</tr>
Expand Down Expand Up @@ -521,6 +527,36 @@ language: 'objc'
```
</td>
</tr>
<tr>
<td>output -> symbol-file</td>
<td>Generates a symbol file yaml containing all types defined in the generated output.</td>
<td>

```yaml
output:
...
symbol-file:
# Although file paths are supported here, prefer Package Uri's here
# so that other pacakges can use them.
output: 'package:some_pkg/symbols.yaml'
import-path: 'package:some_pkg/base.dart'
```
</td>
</tr>
<tr>
<td>import -> symbol-files</td>
<td>Import symbols from a symbol file. Used for sharing type definitions from other pacakges.</td>
<td>

```yaml
import:
symbol-files:
# Both package Uri and file paths are supported here.
- 'package:some_pkg/symbols.yaml'
- 'path/to/some/symbol_file.yaml'
```
</td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -757,3 +793,16 @@ Ffigen can sometimes generate a lot of logs, especially when it's parsing a lot
Level options are - `[all, fine, info (default), warning, severe]`.
The `all` and `fine` will print a ton of logs are meant for debugging
purposes only.

### How can type definitions be shared?

Ffigen can share type definitions using symbol files.
- A package can generate a symbol file using the `output -> symbol-file` config.
- And another package can then import this, using `import -> symbol-files` config.
- Doing so will reuse all the types such as Struct/Unions, and will automatically
exclude generating other types (E.g functions, enums, macros).

Checkout `examples/shared_bindings` for details.

For manually reusing definitions from another package, the `library-imports`
and `type-map` config can be used.
11 changes: 11 additions & 0 deletions example/shared_bindings/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Files and directories created by pub.
.dart_tool/
.packages
# Remove the following pattern if you wish to check in your lock file.
pubspec.lock

# Conventional directory for build outputs.
build/

# Directory created by dartdoc.
doc/api/
10 changes: 10 additions & 0 deletions example/shared_bindings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Shared bindings

An example to showcase how bindings can share types
with other bindings.

## Generating bindings
At the root of this example (`example/shared_bindings`), run -
```
dart run generate.dart
```
12 changes: 12 additions & 0 deletions example/shared_bindings/ffigen_configs/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

name: NativeLibraryA
description: Bindings to `headers/a.h`.
output: '../lib/generated/a_gen.dart'
headers:
entry-points:
- '../headers/a.h'
preamble: |
// ignore_for_file: non_constant_identifier_names, camel_case_types
16 changes: 16 additions & 0 deletions example/shared_bindings/ffigen_configs/a_shared_base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

name: NativeLibraryASharedB
description: Bindings to `headers/a.h` with shared definitions from `headers/base.h`.
output: '../lib/generated/a_shared_b_gen.dart'
headers:
entry-points:
- '../headers/a.h'
import:
symbol-files:
# Both package Uri and file paths are supported here.
- 'package:shared_bindings/generated/base_symbols.yaml'
preamble: |
// ignore_for_file: non_constant_identifier_names, camel_case_types
17 changes: 17 additions & 0 deletions example/shared_bindings/ffigen_configs/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

name: NativeLibraryBase
description: Bindings to `headers/base.h`.
output:
bindings: '../lib/generated/base_gen.dart'
symbol-file:
# Although file paths are supported here, prefer Package Uri's here.
output: 'package:shared_bindings/generated/base_symbols.yaml'
import-path: 'package:shared_bindings/generated/base_gen.dart'
headers:
entry-points:
- '../headers/base.h'
preamble: |
// ignore_for_file: non_constant_identifier_names, camel_case_types
32 changes: 32 additions & 0 deletions example/shared_bindings/generate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:io';

import 'package:cli_util/cli_util.dart';
import 'package:path/path.dart' as p;

ProcessResult runFfigenForConfig(String sdkPath, String configPath) {
return Process.runSync(
p.join(sdkPath, 'bin', 'dart'),
[
'run',
'ffigen',
'--config=$configPath',
],
runInShell: Platform.isWindows,
);
}

void main() {
final sdkPath = getSdkPath();
final configPaths = [
'ffigen_configs/base.yaml',
'ffigen_configs/a.yaml',
'ffigen_configs/a_shared_base.yaml'
];
for (final configPath in configPaths) {
final res = runFfigenForConfig(sdkPath, configPath);
print(res.stdout.toString());
if (res.exitCode != 0) {
throw Exception("Some error occurred: ${res.stderr.toString()}");
}
}
}
21 changes: 21 additions & 0 deletions example/shared_bindings/headers/a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#include "base.h"

struct A_Struct1{
int a;
};

union A_Union1{
int a;
};

enum A_Enum{
A_ENUM_1,
A_ENUM_2,
};

#define A_MACRO_1 1;

void a_func1();

void a_func2(struct BaseStruct2 s, union BaseUnion2 u, BaseTypedef2 t);
31 changes: 31 additions & 0 deletions example/shared_bindings/headers/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

struct BaseStruct1{
int a;
};

union BaseUnion1{
int a;
};

struct BaseStruct2{
int a;
};

union BaseUnion2{
int a;
};

typedef struct BaseStruct1 BaseTypedef1;
typedef struct BaseStruct2 BaseTypedef2;

enum BaseEnum{
BASE_ENUM_1,
BASE_ENUM_2,
};

#define BASE_MACRO_1 1;

void base_func1(BaseTypedef1 t1, BaseTypedef2 t2);
112 changes: 112 additions & 0 deletions example/shared_bindings/lib/generated/a_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// ignore_for_file: non_constant_identifier_names, camel_case_types

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
import 'dart:ffi' as ffi;

/// Bindings to `headers/a.h`.
class NativeLibraryA {
/// Holds the symbol lookup function.
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
_lookup;

/// The symbols are looked up in [dynamicLibrary].
NativeLibraryA(ffi.DynamicLibrary dynamicLibrary)
: _lookup = dynamicLibrary.lookup;

/// The symbols are looked up with [lookup].
NativeLibraryA.fromLookup(
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
lookup)
: _lookup = lookup;

void base_func1(
BaseTypedef1 t1,
BaseTypedef2 t2,
) {
return _base_func1(
t1,
t2,
);
}

late final _base_func1Ptr = _lookup<
ffi.NativeFunction<ffi.Void Function(BaseTypedef1, BaseTypedef2)>>(
'base_func1');
late final _base_func1 =
_base_func1Ptr.asFunction<void Function(BaseTypedef1, BaseTypedef2)>();

void a_func1() {
return _a_func1();
}

late final _a_func1Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function()>>('a_func1');
late final _a_func1 = _a_func1Ptr.asFunction<void Function()>();

void a_func2(
BaseStruct2 s,
BaseUnion2 u,
BaseTypedef2 t,
) {
return _a_func2(
s,
u,
t,
);
}

late final _a_func2Ptr = _lookup<
ffi.NativeFunction<
ffi.Void Function(BaseStruct2, BaseUnion2, BaseTypedef2)>>('a_func2');
late final _a_func2 = _a_func2Ptr
.asFunction<void Function(BaseStruct2, BaseUnion2, BaseTypedef2)>();
}

class BaseStruct1 extends ffi.Struct {
@ffi.Int()
external int a;
}

class BaseUnion1 extends ffi.Union {
@ffi.Int()
external int a;
}

class BaseStruct2 extends ffi.Struct {
@ffi.Int()
external int a;
}

class BaseUnion2 extends ffi.Union {
@ffi.Int()
external int a;
}

abstract class BaseEnum {
static const int BASE_ENUM_1 = 0;
static const int BASE_ENUM_2 = 1;
}

typedef BaseTypedef1 = BaseStruct1;
typedef BaseTypedef2 = BaseStruct2;

class A_Struct1 extends ffi.Struct {
@ffi.Int()
external int a;
}

class A_Union1 extends ffi.Union {
@ffi.Int()
external int a;
}

abstract class A_Enum {
static const int A_ENUM_1 = 0;
static const int A_ENUM_2 = 1;
}

const int BASE_MACRO_1 = 1;

const int A_MACRO_1 = 1;
Loading