Skip to content

Commit bf76046

Browse files
committed
Add another example
1 parent 3f5bd87 commit bf76046

File tree

16 files changed

+79
-133
lines changed

16 files changed

+79
-133
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# https://dart.dev/guides/libraries/private-files
22
# Created by `dart pub`
33
.dart_tool/
4-
bin/treeshaking_native_assets/
4+
bin/drop_dylib_link/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This sample builds a native library for adding and multiplying, but then only keeps the one for adding in the linking step.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:treeshaking_native_assets/treeshaking_native_assets.dart';
5+
import 'package:drop_dylib_link/drop_dylib_link.dart';
66

77
void main(List<String> arguments) {
8-
print('Hello world: ${MyMath.multiply(3, 4)}!');
8+
print('Hello world: ${MyMath.add(3, 4)}!');
99
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2024, 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 = 'drop_dylib_link';
10+
11+
void main(List<String> arguments) async {
12+
await build(arguments, (config, output) async {
13+
final logger = Logger('')
14+
..level = Level.ALL
15+
..onRecord.listen((record) {
16+
print('${record.level.name}: ${record.time}: ${record.message}');
17+
});
18+
await CBuilder.library(
19+
name: 'add',
20+
assetName: 'dylib_add',
21+
sources: [
22+
'src/native_add.c',
23+
],
24+
dartBuildFiles: ['hook/build.dart'],
25+
linkModePreference: LinkModePreference.dynamic,
26+
).run(
27+
buildConfig: config,
28+
buildOutput: output,
29+
logger: logger,
30+
linkInPackage: packageName,
31+
);
32+
33+
await CBuilder.library(
34+
name: 'multiply',
35+
assetName: 'dylib_multiply',
36+
sources: [
37+
'src/native_multiply.c',
38+
],
39+
dartBuildFiles: ['hook/build.dart'],
40+
linkModePreference: LinkModePreference.dynamic,
41+
).run(
42+
buildConfig: config,
43+
buildOutput: output,
44+
logger: logger,
45+
linkInPackage: packageName,
46+
);
47+
});
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2024, 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_assets_cli/native_assets_cli.dart';
6+
7+
void main(List<String> arguments) async {
8+
await link(arguments, (config, output) async {
9+
print('''
10+
Received ${config.assets.length} assets: ${config.assets.map((e) => e.id)}.
11+
''');
12+
output.addAssets(config.assets.where((asset) => asset.id.endsWith('add')));
13+
print('''
14+
Keeping only ${output.assets.map((e) => e.id)}.
15+
''');
16+
output.addDependency(config.packageRoot.resolve('hook/link.dart'));
17+
});
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
export 'src/treeshaking_native_assets.dart';
5+
export 'src/drop_dylib_link.dart';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:meta/meta.dart';
66

7-
import 'treeshaking_native_assets_bindings.dart' as bindings;
7+
import 'drop_dylib_link_bindings.dart' as bindings;
88

99
class MyMath {
1010
@ResourceIdentifier('add')
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
import 'dart:ffi' as ffi;
66

7-
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>()
7+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(
8+
assetId: 'package:drop_dylib_link/dylib_add')
89
external int add(
910
int a,
1011
int b,
1112
);
1213

13-
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>()
14+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(
15+
assetId: 'package:drop_dylib_link/dylib_multiply')
1416
external int multiply(
1517
int a,
1618
int b,

pkgs/native_assets_builder/test_data/treeshaking_native_assets/pubspec.yaml renamed to pkgs/native_assets_builder/test_data/drop_dylib_link/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
name: treeshaking_native_assets
2-
description: A sample on how to treeshake symbols using a build/link combo.
1+
name: drop_dylib_link
2+
description: Generate two dylibs, remove one in linking.
33
version: 1.0.0
44

55
publish_to: none

pkgs/native_assets_builder/test_data/treeshaking_native_assets/src/native_add.c renamed to pkgs/native_assets_builder/test_data/drop_dylib_link/src/native_add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
#include "native_add.h"
66

7-
int32_t add(int32_t a, int32_t b) {
7+
MYLIB_EXPORT int32_t add(int32_t a, int32_t b) {
88
return a + b;
99
}

pkgs/native_assets_builder/test_data/treeshaking_native_assets/README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

pkgs/native_assets_builder/test_data/treeshaking_native_assets/hook/build.dart

Lines changed: 0 additions & 55 deletions
This file was deleted.

pkgs/native_assets_builder/test_data/treeshaking_native_assets/hook/link.dart

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)