Skip to content

Commit 3525c01

Browse files
committed
[native_assets_cli] Unify Metadata with Assets
1 parent 3da9128 commit 3525c01

File tree

51 files changed

+814
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+814
-129
lines changed

.github/workflows/native.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ jobs:
108108
- run: dart pub get -C test_data/native_dynamic_linking/
109109
if: ${{ matrix.package == 'native_assets_builder' }}
110110

111+
- run: dart pub get -C test_data/reusable_dynamic_library/
112+
if: ${{ matrix.package == 'native_assets_builder' }}
113+
114+
- run: dart pub get -C test_data/reuse_dynamic_library/
115+
if: ${{ matrix.package == 'native_assets_builder' }}
116+
111117
- run: dart pub get -C test_data/no_hook/
112118
if: ${{ matrix.package == 'native_assets_builder' }}
113119

pkgs/native_assets_builder/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 0.13.1-wip
1+
## 0.14.0-wip
22

3-
- Nothing yet.
3+
- Bump `package:native_assets_cli` to 0.14.0.
4+
- Route assets from build hook to build hook with `ToBuild` `Routing`.
45

56
## 0.13.0
67

pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,27 @@ class NativeAssetsBuildRunner {
9292

9393
var hookResult = HookResult();
9494
final globalMetadata = <String, Metadata>{};
95+
final globalAssetsForBuild = <String, List<EncodedAsset>>{};
9596
for (final package in buildPlan) {
9697
final metadata = <String, Metadata>{};
9798
_metadataForPackage(
9899
packageGraph: packageGraph!,
99100
packageName: package.name,
100101
targetMetadata: globalMetadata,
101102
)?.forEach((key, value) => metadata[key] = value);
103+
final assetsForBuild = _assetsForBuildForPackage(
104+
packageGraph: packageGraph,
105+
packageName: package.name,
106+
globalAssetsForBuild: globalAssetsForBuild,
107+
);
102108

103109
final inputBuilder = BuildInputBuilder();
104110

105111
for (final e in extensions) {
106112
e.setupBuildInput(inputBuilder);
107113
}
108114
inputBuilder.config.setupBuild(linkingEnabled: linkingEnabled);
109-
inputBuilder.setupBuildInput(metadata: metadata);
115+
inputBuilder.setupBuildInput(metadata: metadata, assets: assetsForBuild);
110116

111117
final (buildDirUri, outDirUri, outDirSharedUri) = await _setupDirectories(
112118
Hook.build,
@@ -152,6 +158,8 @@ class NativeAssetsBuildRunner {
152158
final (hookOutput, hookDeps) = result;
153159
hookResult = hookResult.copyAdd(hookOutput, hookDeps);
154160
globalMetadata[package.name] = (hookOutput as BuildOutput).metadata;
161+
globalAssetsForBuild[package.name] =
162+
hookOutput.assets.encodedAssetsForBuild;
155163
}
156164

157165
// We only perform application wide validation in the final result of
@@ -729,6 +737,21 @@ ${compileResult.stdout}
729737
};
730738
}
731739

740+
Map<String, List<EncodedAsset>>? _assetsForBuildForPackage({
741+
required PackageGraph packageGraph,
742+
required String packageName,
743+
Map<String, List<EncodedAsset>>? globalAssetsForBuild,
744+
}) {
745+
if (globalAssetsForBuild == null) {
746+
return null;
747+
}
748+
final dependencies = packageGraph.neighborsOf(packageName).toSet();
749+
return {
750+
for (final entry in globalAssetsForBuild.entries)
751+
if (dependencies.contains(entry.key)) entry.key: entry.value,
752+
};
753+
}
754+
732755
Future<ValidationErrors> _validate(
733756
HookInput input,
734757
HookOutput output,

pkgs/native_assets_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: native_assets_builder
22
description: >-
33
This package is the backend that invokes build hooks.
4-
version: 0.13.1-wip
4+
version: 0.14.0-wip
55
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder
66

77
publish_to: none
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
@OnPlatform({'mac-os': Timeout.factor(2), 'windows': Timeout.factor(10)})
6+
library;
7+
8+
import 'package:test/test.dart';
9+
10+
import '../build_runner/helpers.dart';
11+
import '../helpers.dart';
12+
13+
void main() async {
14+
const name = 'reuse_dynamic_library';
15+
16+
test(
17+
'$name build',
18+
() => inTempDir((tempUri) async {
19+
await copyTestProjects(targetUri: tempUri);
20+
final packageUri = tempUri.resolve('$name/');
21+
22+
await runPubGet(workingDirectory: packageUri, logger: logger);
23+
24+
final logMessages = <String>[];
25+
final result =
26+
(await build(
27+
packageUri,
28+
logger,
29+
dartExecutable,
30+
capturedLogs: logMessages,
31+
buildAssetTypes: [BuildAssetType.code],
32+
))!;
33+
34+
expect(result.encodedAssets.length, 2);
35+
}),
36+
);
37+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void main(List<String> arguments) async {
2727
input: input,
2828
output: output,
2929
logger: logger,
30-
linkInPackage: 'add_asset_link',
30+
routing: const [ToLinker('add_asset_link')],
3131
);
3232
});
3333
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ void main(List<String> args) async {
2727

2828
output.assets.data.add(
2929
DataAsset(package: packageName, name: name, file: dataAsset.uri),
30-
linkInPackage: input.config.linkingEnabled ? packageName : null,
30+
routing:
31+
input.config.linkingEnabled
32+
? ToLinker(packageName)
33+
: const BundleInApp(),
3134
);
3235
// TODO(https://github.com/dart-lang/native/issues/1208): Report
3336
// dependency on asset.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ void main(List<String> args) async {
2828
final forLinking = name.contains('2') || name.contains('3');
2929
output.assets.data.add(
3030
DataAsset(package: packageName, name: name, file: dataAsset.uri),
31-
linkInPackage:
32-
forLinking && input.config.linkingEnabled ? 'complex_link' : null,
31+
routing:
32+
forLinking && input.config.linkingEnabled
33+
? const ToLinker('complex_link')
34+
: const BundleInApp(),
3335
);
3436
// TODO(https://github.com/dart-lang/native/issues/1208): Report
3537
// dependency on asset.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ void main(List<String> arguments) async {
1313
..onRecord.listen((record) {
1414
print('${record.level.name}: ${record.time}: ${record.message}');
1515
});
16-
final linkInPackage =
17-
input.config.linkingEnabled ? input.packageName : null;
16+
final routing = input.config.linkingEnabled
17+
? [ToLinker(input.packageName)]
18+
: [const BundleInApp()];
1819
await CBuilder.library(
1920
name: 'add',
2021
assetName: 'dylib_add',
@@ -26,7 +27,7 @@ void main(List<String> arguments) async {
2627
input: input,
2728
output: output,
2829
logger: logger,
29-
linkInPackage: linkInPackage,
30+
routing: routing,
3031
);
3132

3233
await CBuilder.library(
@@ -40,7 +41,7 @@ void main(List<String> arguments) async {
4041
input: input,
4142
output: output,
4243
logger: logger,
43-
linkInPackage: linkInPackage,
44+
routing: routing,
4445
);
4546
});
4647
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ void main(List<String> arguments) async {
1212
file: input.packageRoot.resolve('assets/data.json'),
1313
package: input.packageName,
1414
),
15-
linkInPackage:
16-
input.config.linkingEnabled ? 'fail_on_os_sdk_version_linker' : null,
15+
routing:
16+
input.config.linkingEnabled
17+
? const ToLinker('fail_on_os_sdk_version_linker')
18+
: const BundleInApp(),
1719
);
1820
});
1921
}

pkgs/native_assets_builder/test_data/manifest.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@
112112
- relative_path/assets/test_asset.txt
113113
- relative_path/hook/build.dart
114114
- relative_path/pubspec.yaml
115+
- reusable_dynamic_library/ffigen.yaml
116+
- reusable_dynamic_library/hook/build.dart
117+
- reusable_dynamic_library/lib/add.dart
118+
- reusable_dynamic_library/pubspec.yaml
119+
- reusable_dynamic_library/src/add.c
120+
- reusable_dynamic_library/src/add.h
121+
- reusable_dynamic_library/test/add_test.dart
122+
- reuse_dynamic_library/ffigen.yaml
123+
- reuse_dynamic_library/hook/build.dart
124+
- reuse_dynamic_library/lib/my_add.dart
125+
- reuse_dynamic_library/pubspec.yaml
126+
- reuse_dynamic_library/src/my_add.c
127+
- reuse_dynamic_library/src/my_add.h
128+
- reuse_dynamic_library/test/add_test.dart
115129
- simple_data_asset/assets/test_asset.txt
116130
- simple_data_asset/bin/deep_modify_data_asset.dart.debug
117131
- simple_data_asset/bin/modify_data_asset.dart.debug

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ void main(List<String> arguments) async {
3232
output.assets.code.add(
3333
tempBuildOutput.assets.code.single,
3434
// Send dylib to linking if linking is enabled.
35-
linkInPackage: input.config.linkingEnabled ? packageName : null,
35+
routing:
36+
input.config.linkingEnabled
37+
? ToLinker(packageName)
38+
: const BundleInApp(),
3639
);
3740
output.addDependencies(tempBuildOutput.dependencies);
3841
});

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
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-
// ignore_for_file: deprecated_member_use
6-
75
import 'package:native_assets_cli/native_assets_cli.dart';
86

97
void main(List<String> args) async {
108
await build(args, (input, _) async {
11-
final someValue = input.metadatum('package_with_metadata', 'some_key');
9+
final someValue = input.metadata['package_with_metadata']['some_key'];
1210
assert(someValue != null);
13-
final someInt = input.metadatum('package_with_metadata', 'some_int');
11+
final someInt = input.metadata['package_with_metadata']['some_int'];
1412
assert(someInt != null);
1513
print({'some_int': someInt, 'some_key': someValue});
14+
15+
// ignore: deprecated_member_use
16+
final someValueOld = input.metadatum('package_with_metadata', 'some_key');
17+
assert(someValueOld != null);
18+
// ignore: deprecated_member_use
19+
final someIntOld = input.metadatum('package_with_metadata', 'some_int');
20+
assert(someIntOld != null);
1621
});
1722
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:native_assets_cli/native_assets_cli.dart';
66

77
void main(List<String> arguments) async {
88
await build(arguments, (input, output) async {
9+
output.metadata.addAll({'some_key': 'some_value', 'some_int': 3});
910
// ignore: deprecated_member_use
1011
output.addMetadata({'some_key': 'some_value', 'some_int': 3});
1112
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
An example library that builds 3 native libraries, 2 of which are dynamically
2+
linked to each other.
3+
4+
## Usage
5+
6+
Run tests with `dart --enable-experiment=native-assets test`.
7+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Run with `flutter pub run ffigen --config ffigen.yaml`.
2+
name: AddBindings
3+
description: |
4+
Bindings for `src/add.h`.
5+
6+
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
7+
output: 'lib/add.dart'
8+
headers:
9+
entry-points:
10+
- 'src/add.h'
11+
include-directives:
12+
- 'src/add.h'
13+
preamble: |
14+
// Copyright (c) 2025, 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:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
void main(List<String> args) async {
10+
await build(args, (input, output) async {
11+
final logger =
12+
Logger('')
13+
..level = Level.ALL
14+
..onRecord.listen((record) => print(record.message));
15+
16+
final builder = CBuilder.library(
17+
name: 'add',
18+
assetName: 'add.dart',
19+
sources: ['src/add.c'],
20+
buildMode: BuildMode.debug,
21+
);
22+
23+
await builder.run(
24+
input: input,
25+
output: output,
26+
logger: logger,
27+
routing: const [
28+
// Bundle the dylib in the app, someone might use it.
29+
BundleInApp(),
30+
// Enable other packages to link to the dylib.
31+
ToBuildHooks(),
32+
],
33+
);
34+
35+
// Enable other packages to find the headers.
36+
output.metadata['include'] = input.packageRoot.resolve('src/').toFilePath();
37+
});
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2025, 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(int a, int b);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: reusable_dynamic_library
2+
3+
description: Dynamically link native libraries to each other.
4+
version: 0.1.0
5+
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli/example/build/native_dynamic_linking
6+
7+
publish_to: none
8+
9+
environment:
10+
sdk: '>=3.7.0 <4.0.0'
11+
12+
dependencies:
13+
logging: ^1.1.1
14+
# native_assets_cli: ^0.13.0
15+
native_assets_cli:
16+
path: ../../../native_assets_cli/
17+
# native_toolchain_c: ^0.10.0
18+
native_toolchain_c:
19+
path: ../../../native_toolchain_c/
20+
21+
dev_dependencies:
22+
ffigen: ^8.0.2
23+
lints: ^5.1.1
24+
test: ^1.21.0
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2025, 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 "add.h"
6+
7+
int32_t add(int32_t a, int32_t b) { return a + b; }

0 commit comments

Comments
 (0)