Skip to content

Commit 8aa373f

Browse files
committed
EOFs
1 parent 3707eed commit 8aa373f

File tree

15 files changed

+255
-0
lines changed

15 files changed

+255
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
An example library containing native code that should be bundled with Dart and
2+
Flutter applications.
3+
4+
## Usage
5+
6+
Run tests with `dart --enable-experiment=native-assets test`.
7+
8+
## Code organization
9+
10+
A typical layout of a package with native code is:
11+
12+
* `lib/` contains Dart code which uses [`dart:ffi`] and [`package:ffigen`]
13+
to call into native code.
14+
* `src/` contains C code which is built and then invoked through `dart:ffi`.
15+
* `build.dart` implements the CLI that communicates which native assets
16+
to build/bundle with the Dart/Flutter SDK.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2023, 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+
import 'package:logging/logging.dart';
6+
import 'package:native_assets_cli/native_assets_cli.dart';
7+
import 'package:native_toolchain_c/native_toolchain_c.dart';
8+
9+
const packageName = 'native_split_library';
10+
11+
/// Implements the protocol from `package:native_assets_cli` by building
12+
/// the C code in `src/` and reporting what native assets it built.
13+
void main(List<String> args) async {
14+
// Parse the build configuration passed to this CLI from Dart or Flutter.
15+
final buildConfig = await BuildConfig.fromArgs(args);
16+
final buildOutput = BuildOutput();
17+
18+
// Configure `package:native_toolchain_c` to build the C code for us.
19+
final addCBuilder = CBuilder.library(
20+
name: '${packageName}_add',
21+
assetId: 'package:$packageName/native_add.dart',
22+
sources: [
23+
'src/native_add.c',
24+
],
25+
);
26+
await addCBuilder.run(
27+
buildConfig: buildConfig,
28+
// `package:native_toolchain_c` will output the dynamic or static libraries it built,
29+
// what files it accessed (for caching the build), etc.
30+
buildOutput: buildOutput,
31+
logger: Logger('')..onRecord.listen((record) => print(record.message)),
32+
);
33+
34+
final addLibraryAsset = buildOutput.assets.firstWhere(
35+
(asset) => asset.id == 'package:$packageName/native_add.dart',
36+
);
37+
38+
final multiplyCBuilder = CBuilder.library(
39+
name: '${packageName}_multiply',
40+
assetId: 'package:$packageName/native_multiply.dart',
41+
includes: [
42+
'src',
43+
],
44+
sources: [
45+
'src/native_multiply.c',
46+
],
47+
flags: [
48+
(addLibraryAsset.path as AssetAbsolutePath).uri.toFilePath(),
49+
],
50+
);
51+
await multiplyCBuilder.run(
52+
buildConfig: buildConfig,
53+
// `package:native_toolchain_c` will output the dynamic or static libraries it built,
54+
// what files it accessed (for caching the build), etc.
55+
buildOutput: buildOutput,
56+
logger: Logger('')..onRecord.listen((record) => print(record.message)),
57+
);
58+
59+
// Write the output according to the native assets protocol so that Dart or
60+
// Flutter can find the native assets produced by this script.
61+
await buildOutput.writeToFile(outDir: buildConfig.outDir);
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Run with `flutter pub run ffigen --config ffigen_native_add.yaml`.
2+
name: NativeAddBindings
3+
description: |
4+
Bindings for `src/native_add.h`.
5+
6+
Regenerate bindings with `flutter pub run ffigen --config ffigen_native_add.yaml`.
7+
output: 'lib/native_add.dart'
8+
headers:
9+
entry-points:
10+
- 'src/native_add.h'
11+
include-directives:
12+
- 'src/native_add.h'
13+
preamble: |
14+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
15+
// for details. All rights reserved. Use of this source code is governed by a
16+
// BSD-style license that can be found in the LICENSE file.
17+
comments:
18+
style: any
19+
length: full
20+
ffi-native:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Run with `flutter pub run ffigen --config ffigen_native_multiply.yaml`.
2+
name: NativeMultiplyBindings
3+
description: |
4+
Bindings for `src/native_multiply.h`.
5+
6+
Regenerate bindings with `flutter pub run ffigen --config ffigen_native_multiply.yaml`.
7+
output: 'lib/native_multiply.dart'
8+
headers:
9+
entry-points:
10+
- 'src/native_multiply.h'
11+
include-directives:
12+
- 'src/native_multiply.h'
13+
preamble: |
14+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
15+
// for details. All rights reserved. Use of this source code is governed by a
16+
// BSD-style license that can be found in the LICENSE file.
17+
comments:
18+
style: any
19+
length: full
20+
ffi-native:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023, 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+
// AUTO GENERATED FILE, DO NOT EDIT.
6+
//
7+
// Generated by `package:ffigen`.
8+
// ignore_for_file: type=lint
9+
import 'dart:ffi' as ffi;
10+
11+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
12+
external int add(
13+
int a,
14+
int b,
15+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2023, 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+
// AUTO GENERATED FILE, DO NOT EDIT.
6+
//
7+
// Generated by `package:ffigen`.
8+
// ignore_for_file: type=lint
9+
import 'dart:ffi' as ffi;
10+
11+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'multiply')
12+
external int multiply(
13+
int a,
14+
int b,
15+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
publish_to: none
2+
3+
name: native_split_library
4+
description:
5+
Builds two native libraries that depend on each other and makes them available
6+
to Dart.
7+
version: 0.1.0
8+
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/native_add_library
9+
10+
environment:
11+
sdk: '>=3.0.0 <4.0.0'
12+
13+
dependencies:
14+
cli_config: ^0.1.1
15+
logging: ^1.1.1
16+
native_assets_cli: ^0.2.0
17+
# native_toolchain_c: ^0.2.2
18+
native_toolchain_c:
19+
path: ../../../native_toolchain_c
20+
21+
dev_dependencies:
22+
ffigen: ^8.0.2
23+
lints: ^2.0.0
24+
test: ^1.21.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2023, 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+
#include "native_add.h"
6+
7+
#ifdef DEBUG
8+
#include <stdio.h>
9+
#endif
10+
11+
int32_t add(int32_t a, int32_t b) {
12+
#ifdef DEBUG
13+
printf("Adding %i and %i.\n", a, b);
14+
#endif
15+
return a + b;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2023, 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+
#include <stdint.h>
6+
7+
#if _WIN32
8+
#define MYLIB_EXPORT __declspec(dllexport)
9+
#else
10+
#define MYLIB_EXPORT
11+
#endif
12+
13+
MYLIB_EXPORT int32_t add(int32_t a, int32_t b);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2023, 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+
#include "native_multiply.h"
6+
7+
#ifdef DEBUG
8+
#include <stdio.h>
9+
#endif
10+
11+
int32_t multiply(int32_t a, int32_t b) {
12+
#ifdef DEBUG
13+
printf("Multiplying %i and %i.\n", a, b);
14+
#endif
15+
int32_t result = 0;
16+
for (int32_t i = 0; i < a; i++) {
17+
result = add(result, b);
18+
}
19+
return result;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2023, 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+
#include <stdint.h>
6+
#include "native_add.h"
7+
8+
#if _WIN32
9+
#define MYLIB_EXPORT __declspec(dllexport)
10+
#else
11+
#define MYLIB_EXPORT
12+
#endif
13+
14+
MYLIB_EXPORT int32_t multiply(int32_t a, int32_t b);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2023, 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+
import 'package:native_split_library/native_add.dart';
6+
import 'package:native_split_library/native_multiply.dart';
7+
import 'package:test/test.dart';
8+
9+
void main() {
10+
test('invoke native add function', () {
11+
expect(add(24, 18), 42);
12+
});
13+
14+
test('invoke native multiply function', () {
15+
expect(multiply(24, 18), 432);
16+
});
17+
}

pkgs/native_toolchain_c/test/cbuilder/testfiles/hello_world_cpp/src/hello_world_cpp.cc

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ int main() {
1111
std::cout << "Hello world." << std::endl;
1212
return 0;
1313
}
14+

pkgs/native_toolchain_c/test/cbuilder/testfiles/includes/include/includes.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
#endif
1010

1111
FFI_EXPORT int x = 42;
12+

pkgs/native_toolchain_c/test/cbuilder/testfiles/includes/src/includes.c

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
#include "includes.h"
6+

0 commit comments

Comments
 (0)