-
Notifications
You must be signed in to change notification settings - Fork 74
[native_assets_cli] Unify Metadata
with Asset
s
#2164
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2024, 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. | ||
|
||
@OnPlatform({'mac-os': Timeout.factor(2), 'windows': Timeout.factor(10)}) | ||
library; | ||
|
||
import 'package:test/test.dart'; | ||
|
||
import '../build_runner/helpers.dart'; | ||
import '../helpers.dart'; | ||
|
||
void main() async { | ||
const name = 'reuse_dynamic_library'; | ||
|
||
test( | ||
'$name build', | ||
() => inTempDir((tempUri) async { | ||
await copyTestProjects(targetUri: tempUri); | ||
final packageUri = tempUri.resolve('$name/'); | ||
|
||
await runPubGet(workingDirectory: packageUri, logger: logger); | ||
|
||
final logMessages = <String>[]; | ||
final result = | ||
(await build( | ||
packageUri, | ||
logger, | ||
dartExecutable, | ||
capturedLogs: logMessages, | ||
buildAssetTypes: [BuildAssetType.code], | ||
))!; | ||
|
||
expect(result.encodedAssets.length, 2); | ||
}), | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,10 @@ void main(List<String> arguments) async { | |
output.assets.code.add( | ||
tempBuildOutput.assets.code.single, | ||
// Send dylib to linking if linking is enabled. | ||
linkInPackage: input.config.linkingEnabled ? packageName : null, | ||
routing: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just an idea: We could avoid the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like having a I'm not sure that having to take the value from the input would make a very nice API. Especially for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I gave this a spin, but make the call sites even more verbose. routing: switch (input.config.routing) {
final LinkingEnabledRouter r => r.toLinkHook(packageName),
LinkingDisabledRouter() => const ToAppBundle(),
}, Then the following is more readable: routing: switch (input.config.routing) {
LinkingEnabledRouter() => ToLinkHook(packageName),
LinkingDisabledRouter() => const ToAppBundle(),
}, Current solution (this doesn't show the routing: switch (input.config.linkingEnabled) {
true => ToLinkHook(packageName),
false => const ToAppBundle(),
}, With routing: switch (input.config.routing.linkHooksEnabled) {
true => ToLinkHook(packageName),
false => const ToAppBundle(),
}, True false is not very pretty. Let's try a routing: switch (input.config.routing) {
final routing when routing.linkHooksEnabled => ToLinkHook(packageName),
_ => const ToAppBundle(),
}, Also not that great. Then the following is more readable: routing: switch (input.config.routing) {
LinkHooksEnabled() => ToLinkHook(packageName),
LinkHooksDisabled() => const ToAppBundle(),
}, But it only works if that's the only option we ever want to add to the routing. It's only the Dart API, so we could do it, it's easy to refactor. (P.S. If we want to go the route with the method, we should probably do the same with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's try this in a more holistic way for everywhere where the input-output constraint could be made more explicit. |
||
input.config.linkingEnabled | ||
? ToLinkHook(packageName) | ||
: const ToAppBundle(), | ||
); | ||
output.addDependencies(tempBuildOutput.dependencies); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
An example of a package with a dynamic library that can be linked against in | ||
a dependent package. | ||
|
||
## Usage | ||
|
||
Run tests with `dart --enable-experiment=native-assets test`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Run with `flutter pub run ffigen --config ffigen.yaml`. | ||
name: AddBindings | ||
description: | | ||
Bindings for `src/add.h`. | ||
|
||
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`. | ||
output: 'lib/add.dart' | ||
headers: | ||
entry-points: | ||
- 'src/add.h' | ||
include-directives: | ||
- 'src/add.h' | ||
preamble: | | ||
// Copyright (c) 2025, 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,38 @@ | ||
// Copyright (c) 2024, 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'; | ||
|
||
void main(List<String> args) async { | ||
await build(args, (input, output) async { | ||
final logger = | ||
Logger('') | ||
..level = Level.ALL | ||
..onRecord.listen((record) => print(record.message)); | ||
|
||
final builder = CBuilder.library( | ||
name: 'add', | ||
assetName: 'add.dart', | ||
sources: ['src/add.c'], | ||
buildMode: BuildMode.debug, | ||
); | ||
|
||
await builder.run( | ||
input: input, | ||
output: output, | ||
logger: logger, | ||
routing: const [ | ||
// Bundle the dylib in the app, someone might use it. | ||
ToAppBundle(), | ||
// Enable other packages to link to the dylib. | ||
ToBuildHooks(), | ||
], | ||
); | ||
|
||
// Enable other packages to find the headers. | ||
output.metadata['include'] = input.packageRoot.resolve('src/').toFilePath(); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2025, 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); |
Uh oh!
There was an error while loading. Please reload this page.