Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_assets_cli] Add example using dart_api_dl.h #858

Merged
merged 9 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
7 changes: 7 additions & 0 deletions .github/workflows/native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ jobs:
- run: dart pub get -C example/native_add_library/
if: ${{ matrix.package == 'native_assets_cli' }}

- run: dart pub get -C example/use_dart_api/
if: ${{ matrix.package == 'native_assets_cli' }}

- run: dart analyze --fatal-infos
# Run on dev to ensure we're not depending on deprecated SDK things.

Expand Down Expand Up @@ -121,6 +124,10 @@ jobs:
working-directory: pkgs/${{ matrix.package }}/example/native_add_app/bin/native_add_app/
if: ${{ matrix.package == 'native_assets_cli' && matrix.sdk == 'dev' && !matrix.breaking-change }}

- run: dart --enable-experiment=native-assets test
working-directory: pkgs/${{ matrix.package }}/example/use_dart_api/
if: ${{ matrix.package == 'native_assets_cli' && matrix.sdk == 'dev' && !matrix.breaking-change }}

- name: Install coverage
run: dart pub global activate coverage
if: ${{ matrix.sdk == 'stable' && matrix.dependencies == 'published' }}
Expand Down
8 changes: 8 additions & 0 deletions pkgs/native_assets_cli/example/use_dart_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
An example that uses `dart_api_dl.h`.
dcharkes marked this conversation as resolved.
Show resolved Hide resolved

Open TODO is to pass the path to the include folder rather than copying it in.
https://github.com/dart-lang/native/issues/839

## Usage

Run tests with `dart --enable-experiment=native-assets test`.
32 changes: 32 additions & 0 deletions pkgs/native_assets_cli/example/use_dart_api/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023, 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.

import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

const packageName = 'use_dart_api';

void main(List<String> args) async {
final buildConfig = await BuildConfig.fromArgs(args);
final buildOutput = BuildOutput();
final cbuilder = CBuilder.library(
name: packageName,
assetId: 'package:$packageName/src/${packageName}_bindings_generated.dart',
sources: [
'src/$packageName.c',
'src/dart_api_dl.c',
],
);
await cbuilder.run(
buildConfig: buildConfig,
buildOutput: buildOutput,
logger: Logger('')
..level = Level.ALL
..onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
}),
);
await buildOutput.writeToFile(outDir: buildConfig.outDir);
}
20 changes: 20 additions & 0 deletions pkgs/native_assets_cli/example/use_dart_api/ffigen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Run with `flutter pub run ffigen --config ffigen.yaml`.
name: NativeAddBindings
description: |
Bindings for `src/use_dart_api.h`.

Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
output: "lib/src/use_dart_api_bindings_generated.dart"
headers:
entry-points:
- "src/use_dart_api.h"
include-directives:
- "src/use_dart_api.h"
preamble: |
// Copyright (c) 2023, 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.
comments:
style: any
length: full
ffi-native:
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023, 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.

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

@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
external int add(
int a,
int b,
);

@ffi.Native<ffi.IntPtr Function(ffi.Pointer<ffi.Void>)>(symbol: 'InitDartApiDL')
external int InitDartApiDL(
ffi.Pointer<ffi.Void> data,
);

@ffi.Native<ffi.Pointer<ffi.Void> Function(ffi.Handle)>(
symbol: 'NewPersistentHandle')
external ffi.Pointer<ffi.Void> NewPersistentHandle(
Object non_persistent_handle,
);

@ffi.Native<ffi.Handle Function(ffi.Pointer<ffi.Void>)>(
symbol: 'HandleFromPersistent')
external Object HandleFromPersistent(
ffi.Pointer<ffi.Void> persistent_handle,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2023, 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.

export 'src/use_dart_api_bindings_generated.dart';
10 changes: 10 additions & 0 deletions pkgs/native_assets_cli/example/use_dart_api/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- build.dart
- ffigen.yaml
- lib/native_add.dart
- lib/src/native_add_bindings_generated.dart
- lib/src/native_add.dart
- pubspec.yaml
- pubspec_overrides.yaml
- src/native_add.c
- src/native_add.h
- test/native_add_test.dart
19 changes: 19 additions & 0 deletions pkgs/native_assets_cli/example/use_dart_api/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: use_dart_api
description: Uses some functions from `dart_api_dl.h`.
version: 0.1.0

publish_to: none

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
cli_config: ^0.1.1
logging: ^1.1.1
native_assets_cli: ^0.3.2
native_toolchain_c: ^0.3.2

dev_dependencies:
ffigen: ^10.0.0
lints: ^3.0.0
test: ^1.23.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependency_overrides:
native_assets_cli:
path: ../../../native_assets_cli/
native_toolchain_c:
path: ../../../native_toolchain_c/
Loading
Loading