[native_assets] Add support for link hooks (#148474)
This PR adds support invoking `link.dart` hooks. Link hooks can add new assets. Link hooks can transform assets sent to link hook from build hooks. This PR does not yet add support for getting tree-shake information in the link hooks. This is pending on defining the `resources.json` format (https://github.com/dart-lang/sdk/issues/55494). Issue: * https://github.com/flutter/flutter/issues/146263 ## Implementation considerations The build hooks could be run before Dart compilation and the link hooks after Dart compilation. (This is how it's done in Dart standalone.) However, due to the way the `Target`s are set up, this would require two targets and serializing and deserializing the `BuildResult` in between these. This would lead to more code but no benefits. Currently there is nothing that mandates running build hooks before Dart compilation. ## Testing * The unit tests verify that the native_assets_builder `link` and `linkDryRun` would be invoked with help of the existing fake. * The native assets integration test now also invokes an FFI call of a package that adds the asset during the link hook instead of the build hook. * In order to keep coverage of the `flutter create --template=package_ffi`, `flutter create` is still run and the extra dependency is added and an extra ffi call is added. (Open to alternative suggestions.)
This commit is contained in:
30
dev/integration_tests/link_hook/.gitignore
vendored
Normal file
30
dev/integration_tests/link_hook/.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
# Miscellaneous
|
||||
*.class
|
||||
*.log
|
||||
*.pyc
|
||||
*.swp
|
||||
.DS_Store
|
||||
.atom/
|
||||
.buildlog/
|
||||
.history
|
||||
.svn/
|
||||
migrate_working_dir/
|
||||
|
||||
# IntelliJ related
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.idea/
|
||||
|
||||
# The .vscode folder contains launch configuration and tasks you configure in
|
||||
# VS Code which you may wish to be included in version control, so this line
|
||||
# is commented out by default.
|
||||
#.vscode/
|
||||
|
||||
# Flutter/Dart/Pub related
|
||||
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
|
||||
/pubspec.lock
|
||||
**/doc/api/
|
||||
.dart_tool/
|
||||
.packages
|
||||
build/
|
||||
3
dev/integration_tests/link_hook/README.md
Normal file
3
dev/integration_tests/link_hook/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# link_hook
|
||||
|
||||
Test project for the native assets test to exercise adding assets during a link hook.
|
||||
20
dev/integration_tests/link_hook/ffigen.yaml
Normal file
20
dev/integration_tests/link_hook/ffigen.yaml
Normal file
@@ -0,0 +1,20 @@
|
||||
# Run with `dart run ffigen --config ffigen.yaml`.
|
||||
name: LinkHookBindings
|
||||
description: |
|
||||
Bindings for `src/link_hook.h`.
|
||||
|
||||
Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
|
||||
output: 'lib/link_hook_bindings_generated.dart'
|
||||
headers:
|
||||
entry-points:
|
||||
- 'src/link_hook.h'
|
||||
include-directives:
|
||||
- 'src/link_hook.h'
|
||||
ffi-native:
|
||||
preamble: |
|
||||
// ignore_for_file: always_specify_types
|
||||
// ignore_for_file: camel_case_types
|
||||
// ignore_for_file: non_constant_identifier_names
|
||||
comments:
|
||||
style: any
|
||||
length: full
|
||||
36
dev/integration_tests/link_hook/hook/build.dart
Normal file
36
dev/integration_tests/link_hook/hook/build.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2014 The Flutter Authors. 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, (BuildConfig config, BuildOutput output) async {
|
||||
final String packageName = config.packageName;
|
||||
final CBuilder cbuilder = CBuilder.library(
|
||||
name: packageName,
|
||||
assetName: 'some_asset_name_that_is_not_used',
|
||||
sources: <String>[
|
||||
'src/$packageName.c',
|
||||
],
|
||||
dartBuildFiles: <String>['hook/build.dart'],
|
||||
);
|
||||
final BuildOutput outputCatcher = BuildOutput();
|
||||
await cbuilder.run(
|
||||
buildConfig: config,
|
||||
buildOutput: outputCatcher,
|
||||
logger: Logger('')
|
||||
..level = Level.ALL
|
||||
..onRecord.listen((LogRecord record) => print(record.message)),
|
||||
);
|
||||
output.addDependencies(outputCatcher.dependencies);
|
||||
// Send the asset to hook/link.dart.
|
||||
output.addAsset(
|
||||
outputCatcher.assets.single,
|
||||
linkInPackage: 'link_hook',
|
||||
);
|
||||
});
|
||||
}
|
||||
22
dev/integration_tests/link_hook/hook/link.dart
Normal file
22
dev/integration_tests/link_hook/hook/link.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2014 The Flutter Authors. 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:native_assets_cli/native_assets_cli.dart';
|
||||
|
||||
void main(List<String> args) async {
|
||||
await link(args, (LinkConfig config, LinkOutput output) async {
|
||||
final NativeCodeAsset asset = config.assets.single as NativeCodeAsset;
|
||||
final String packageName = config.packageName;
|
||||
output.addAsset(NativeCodeAsset(
|
||||
package: packageName,
|
||||
// Change the asset id to something that is used.
|
||||
name: '${packageName}_bindings_generated.dart',
|
||||
linkMode: asset.linkMode,
|
||||
os: asset.os,
|
||||
architecture: asset.architecture,
|
||||
file: asset.file,
|
||||
));
|
||||
output.addDependency(config.packageRoot.resolve('hook/link.dart'));
|
||||
});
|
||||
}
|
||||
8
dev/integration_tests/link_hook/lib/link_hook.dart
Normal file
8
dev/integration_tests/link_hook/lib/link_hook.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'link_hook_bindings_generated.dart' as bindings;
|
||||
|
||||
/// A very short-lived native function.
|
||||
int difference(int a, int b) => bindings.difference(a, b);
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// ignore_for_file: always_specify_types
|
||||
// ignore_for_file: camel_case_types
|
||||
// ignore_for_file: non_constant_identifier_names
|
||||
|
||||
// AUTO GENERATED FILE, DO NOT EDIT.
|
||||
//
|
||||
// Generated by `package:ffigen`.
|
||||
import 'dart:ffi' as ffi;
|
||||
|
||||
/// A very short-lived native function.
|
||||
@ffi.Native<ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr)>()
|
||||
external int difference(
|
||||
int a,
|
||||
int b,
|
||||
);
|
||||
70
dev/integration_tests/link_hook/pubspec.yaml
Normal file
70
dev/integration_tests/link_hook/pubspec.yaml
Normal file
@@ -0,0 +1,70 @@
|
||||
name: link_hook
|
||||
description: "A new Dart FFI package project."
|
||||
version: 0.0.1
|
||||
|
||||
environment:
|
||||
sdk: '>=3.5.0-154.0.dev <4.0.0'
|
||||
|
||||
dependencies:
|
||||
cli_config: 0.2.0
|
||||
logging: 1.2.0
|
||||
native_assets_cli: 0.6.0
|
||||
native_toolchain_c: 0.4.2
|
||||
|
||||
args: 2.5.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
async: 2.11.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
collection: 1.18.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
crypto: 3.0.3 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
file: 7.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
glob: 2.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
meta: 1.14.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
path: 1.9.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
pub_semver: 2.1.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
source_span: 1.10.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
string_scanner: 1.2.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
term_glyph: 1.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
typed_data: 1.3.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
yaml: 3.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
|
||||
dev_dependencies:
|
||||
ffi: 2.1.2
|
||||
ffigen: 12.0.0
|
||||
flutter_lints: 4.0.0
|
||||
test: 1.25.5
|
||||
|
||||
_fe_analyzer_shared: 68.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
analyzer: 6.5.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
boolean_selector: 2.1.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
cli_util: 0.4.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
convert: 3.1.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
coverage: 1.8.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
frontend_server_client: 4.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
http_multi_server: 3.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
http_parser: 4.0.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
io: 1.0.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
js: 0.7.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
lints: 4.0.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
matcher: 0.12.16+1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
mime: 1.0.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
node_preamble: 2.0.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
package_config: 2.1.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
pool: 1.5.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
quiver: 3.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
shelf: 1.4.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
shelf_packages_handler: 3.0.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
shelf_static: 1.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
shelf_web_socket: 1.0.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
source_map_stack_trace: 2.1.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
source_maps: 0.10.12 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
stack_trace: 1.11.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
stream_channel: 2.1.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
test_api: 0.7.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
test_core: 0.6.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
vm_service: 14.2.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
watcher: 1.1.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
web: 0.5.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
web_socket_channel: 2.4.5 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
webkit_inspection_protocol: 1.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
yaml_edit: 2.2.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
|
||||
|
||||
# PUBSPEC CHECKSUM: e03f
|
||||
12
dev/integration_tests/link_hook/src/link_hook.c
Normal file
12
dev/integration_tests/link_hook/src/link_hook.c
Normal file
@@ -0,0 +1,12 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "link_hook.h"
|
||||
|
||||
// A very short-lived native function.
|
||||
//
|
||||
// For very short-lived functions, it is fine to call them on the main isolate.
|
||||
// They will block the Dart execution while running the native function, so
|
||||
// only do this for native functions which are guaranteed to be short-lived.
|
||||
FFI_PLUGIN_EXPORT intptr_t difference(intptr_t a, intptr_t b) { return a - b; }
|
||||
22
dev/integration_tests/link_hook/src/link_hook.h
Normal file
22
dev/integration_tests/link_hook/src/link_hook.h
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if _WIN32
|
||||
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define FFI_PLUGIN_EXPORT
|
||||
#endif
|
||||
|
||||
FFI_PLUGIN_EXPORT intptr_t difference(intptr_t a, intptr_t b);
|
||||
13
dev/integration_tests/link_hook/test/link_hook_test.dart
Normal file
13
dev/integration_tests/link_hook/test/link_hook_test.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2014 The Flutter Authors. 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:link_hook/link_hook.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('invoke native function', () {
|
||||
// Tests are run in debug mode.
|
||||
expect(difference(24, 18), 24 - 18);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user