mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

Support for FFI calls with `@Native external` functions through Native assets on MacOS and iOS. This enables bundling native code without any build-system boilerplate code. For more info see: * https://github.com/flutter/flutter/issues/129757 ### Implementation details for MacOS and iOS. Dylibs are bundled by (1) making them fat binaries if multiple architectures are targeted, (2) code signing these, and (3) copying them to the frameworks folder. These steps are done manual rather than via CocoaPods. CocoaPods would have done the same steps, but (a) needs the dylibs to be there before the `xcodebuild` invocation (we could trick it, by having a minimal dylib in the place and replace it during the build process, that works), and (b) can't deal with having no dylibs to be bundled (we'd have to bundle a dummy dylib or include some dummy C code in the build file). The dylibs are build as a new target inside flutter assemble, as that is the moment we know what build-mode and architecture to target. The mapping from asset id to dylib-path is passed in to every kernel compilation path. The interesting case is hot-restart where the initial kernel file is compiled by the "inner" flutter assemble, while after hot restart the "outer" flutter run compiled kernel file is pushed to the device. Both kernel files need to contain the mapping. The "inner" flutter assemble gets its mapping from the NativeAssets target which builds the native assets. The "outer" flutter run get its mapping from a dry-run invocation. Since this hot restart can be used for multiple target devices (`flutter run -d all`) it contains the mapping for all known targets. ### Example vs template The PR includes a new template that uses the new native assets in a package and has an app importing that. Separate discussion in: https://github.com/flutter/flutter/issues/131209. ### Tests This PR adds new tests to cover the various use cases. * dev/devicelab/bin/tasks/native_assets_ios.dart * Runs an example app with native assets in all build modes, doing hot reload and hot restart in debug mode. * dev/devicelab/bin/tasks/native_assets_ios_simulator.dart * Runs an example app with native assets, doing hot reload and hot restart. * packages/flutter_tools/test/integration.shard/native_assets_test.dart * Runs (incl hot reload/hot restart), builds, builds frameworks for iOS, MacOS and flutter-tester. * packages/flutter_tools/test/general.shard/build_system/targets/native_assets_test.dart * Unit tests the new Target in the backend. * packages/flutter_tools/test/general.shard/ios/native_assets_test.dart * packages/flutter_tools/test/general.shard/macos/native_assets_test.dart * Unit tests the native assets being packaged on a iOS/MacOS build. It also extends various existing tests: * dev/devicelab/bin/tasks/module_test_ios.dart * Exercises the add2app scenario. * packages/flutter_tools/test/general.shard/features_test.dart * Unit test the new feature flag.
275 lines
9.2 KiB
Dart
275 lines
9.2 KiB
Dart
// 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:file/file.dart';
|
|
import 'package:file/memory.dart';
|
|
import 'package:file_testing/file_testing.dart';
|
|
import 'package:flutter_tools/src/artifacts.dart';
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
import 'package:flutter_tools/src/base/logger.dart';
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
|
import 'package:flutter_tools/src/build_info.dart';
|
|
import 'package:flutter_tools/src/build_system/build_system.dart';
|
|
import 'package:flutter_tools/src/features.dart';
|
|
import 'package:flutter_tools/src/globals.dart' as globals;
|
|
import 'package:flutter_tools/src/ios/native_assets.dart';
|
|
import 'package:native_assets_cli/native_assets_cli.dart' hide BuildMode, Target;
|
|
import 'package:native_assets_cli/native_assets_cli.dart' as native_assets_cli;
|
|
import 'package:package_config/package_config_types.dart';
|
|
|
|
import '../../src/common.dart';
|
|
import '../../src/context.dart';
|
|
import '../../src/fakes.dart';
|
|
import '../fake_native_assets_build_runner.dart';
|
|
|
|
void main() {
|
|
late FakeProcessManager processManager;
|
|
late Environment environment;
|
|
late Artifacts artifacts;
|
|
late FileSystem fileSystem;
|
|
late BufferLogger logger;
|
|
late Uri projectUri;
|
|
|
|
setUp(() {
|
|
processManager = FakeProcessManager.empty();
|
|
logger = BufferLogger.test();
|
|
artifacts = Artifacts.test();
|
|
fileSystem = MemoryFileSystem.test();
|
|
environment = Environment.test(
|
|
fileSystem.currentDirectory,
|
|
inputs: <String, String>{},
|
|
artifacts: artifacts,
|
|
processManager: processManager,
|
|
fileSystem: fileSystem,
|
|
logger: logger,
|
|
);
|
|
environment.buildDir.createSync(recursive: true);
|
|
projectUri = environment.projectDir.uri;
|
|
});
|
|
|
|
testUsingContext('dry run with no package config', overrides: <Type, Generator>{
|
|
ProcessManager: () => FakeProcessManager.empty(),
|
|
}, () async {
|
|
expect(
|
|
await dryRunNativeAssetsIOS(
|
|
projectUri: projectUri,
|
|
fileSystem: fileSystem,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
hasPackageConfigResult: false,
|
|
),
|
|
),
|
|
null,
|
|
);
|
|
expect(
|
|
(globals.logger as BufferLogger).traceText,
|
|
contains('No package config found. Skipping native assets compilation.'),
|
|
);
|
|
});
|
|
|
|
testUsingContext('build with no package config', overrides: <Type, Generator>{
|
|
ProcessManager: () => FakeProcessManager.empty(),
|
|
}, () async {
|
|
await buildNativeAssetsIOS(
|
|
darwinArchs: <DarwinArch>[DarwinArch.arm64],
|
|
environmentType: EnvironmentType.simulator,
|
|
projectUri: projectUri,
|
|
buildMode: BuildMode.debug,
|
|
fileSystem: fileSystem,
|
|
yamlParentDirectory: environment.buildDir.uri,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
hasPackageConfigResult: false,
|
|
),
|
|
);
|
|
expect(
|
|
(globals.logger as BufferLogger).traceText,
|
|
contains('No package config found. Skipping native assets compilation.'),
|
|
);
|
|
});
|
|
|
|
testUsingContext('dry run with assets but not enabled', overrides: <Type, Generator>{
|
|
ProcessManager: () => FakeProcessManager.empty(),
|
|
}, () async {
|
|
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
|
|
await packageConfig.parent.create();
|
|
await packageConfig.create();
|
|
expect(
|
|
() => dryRunNativeAssetsIOS(
|
|
projectUri: projectUri,
|
|
fileSystem: fileSystem,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
packagesWithNativeAssetsResult: <Package>[
|
|
Package('bar', projectUri),
|
|
],
|
|
),
|
|
),
|
|
throwsToolExit(
|
|
message: 'Package(s) bar require the native assets feature to be enabled. '
|
|
'Enable using `flutter config --enable-native-assets`.',
|
|
),
|
|
);
|
|
});
|
|
|
|
testUsingContext('dry run with assets', overrides: <Type, Generator>{
|
|
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true),
|
|
ProcessManager: () => FakeProcessManager.empty(),
|
|
}, () async {
|
|
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
|
|
await packageConfig.parent.create();
|
|
await packageConfig.create();
|
|
final Uri? nativeAssetsYaml = await dryRunNativeAssetsIOS(
|
|
projectUri: projectUri,
|
|
fileSystem: fileSystem,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
packagesWithNativeAssetsResult: <Package>[
|
|
Package('bar', projectUri),
|
|
],
|
|
dryRunResult: FakeNativeAssetsBuilderResult(
|
|
assets: <Asset>[
|
|
Asset(
|
|
id: 'package:bar/bar.dart',
|
|
linkMode: LinkMode.dynamic,
|
|
target: native_assets_cli.Target.macOSArm64,
|
|
path: AssetAbsolutePath(Uri.file('bar.dylib')),
|
|
),
|
|
Asset(
|
|
id: 'package:bar/bar.dart',
|
|
linkMode: LinkMode.dynamic,
|
|
target: native_assets_cli.Target.macOSX64,
|
|
path: AssetAbsolutePath(Uri.file('bar.dylib')),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
expect(
|
|
nativeAssetsYaml,
|
|
projectUri.resolve('build/native_assets/ios/native_assets.yaml'),
|
|
);
|
|
expect(
|
|
await fileSystem.file(nativeAssetsYaml).readAsString(),
|
|
contains('package:bar/bar.dart'),
|
|
);
|
|
});
|
|
|
|
testUsingContext('build with assets but not enabled', () async {
|
|
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
|
|
await packageConfig.parent.create();
|
|
await packageConfig.create();
|
|
expect(
|
|
() => buildNativeAssetsIOS(
|
|
darwinArchs: <DarwinArch>[DarwinArch.arm64],
|
|
environmentType: EnvironmentType.simulator,
|
|
projectUri: projectUri,
|
|
buildMode: BuildMode.debug,
|
|
fileSystem: fileSystem,
|
|
yamlParentDirectory: environment.buildDir.uri,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
packagesWithNativeAssetsResult: <Package>[
|
|
Package('bar', projectUri),
|
|
],
|
|
),
|
|
),
|
|
throwsToolExit(
|
|
message: 'Package(s) bar require the native assets feature to be enabled. '
|
|
'Enable using `flutter config --enable-native-assets`.',
|
|
),
|
|
);
|
|
});
|
|
|
|
testUsingContext('build no assets', overrides: <Type, Generator>{
|
|
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true),
|
|
ProcessManager: () => FakeProcessManager.empty(),
|
|
}, () async {
|
|
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
|
|
await packageConfig.parent.create();
|
|
await packageConfig.create();
|
|
await buildNativeAssetsIOS(
|
|
darwinArchs: <DarwinArch>[DarwinArch.arm64],
|
|
environmentType: EnvironmentType.simulator,
|
|
projectUri: projectUri,
|
|
buildMode: BuildMode.debug,
|
|
fileSystem: fileSystem,
|
|
yamlParentDirectory: environment.buildDir.uri,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
packagesWithNativeAssetsResult: <Package>[
|
|
Package('bar', projectUri),
|
|
],
|
|
),
|
|
);
|
|
expect(
|
|
environment.buildDir.childFile('native_assets.yaml'),
|
|
exists,
|
|
);
|
|
});
|
|
|
|
testUsingContext('build with assets', overrides: <Type, Generator>{
|
|
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true),
|
|
ProcessManager: () => FakeProcessManager.list(
|
|
<FakeCommand>[
|
|
const FakeCommand(
|
|
command: <Pattern>[
|
|
'lipo',
|
|
'-create',
|
|
'-output',
|
|
'/build/native_assets/ios/bar.dylib',
|
|
'bar.dylib',
|
|
],
|
|
),
|
|
const FakeCommand(
|
|
command: <Pattern>[
|
|
'install_name_tool',
|
|
'-id',
|
|
'@executable_path/Frameworks/bar.dylib',
|
|
'/build/native_assets/ios/bar.dylib',
|
|
],
|
|
),
|
|
const FakeCommand(
|
|
command: <Pattern>[
|
|
'codesign',
|
|
'--force',
|
|
'--sign',
|
|
'-',
|
|
'--timestamp=none',
|
|
'/build/native_assets/ios/bar.dylib',
|
|
],
|
|
),
|
|
],
|
|
),
|
|
}, () async {
|
|
if (const LocalPlatform().isWindows) {
|
|
return; // Backslashes in commands, but we will never run these commands on Windows.
|
|
}
|
|
final File packageConfig = environment.projectDir.childFile('.dart_tool/package_config.json');
|
|
await packageConfig.parent.create();
|
|
await packageConfig.create();
|
|
await buildNativeAssetsIOS(
|
|
darwinArchs: <DarwinArch>[DarwinArch.arm64],
|
|
environmentType: EnvironmentType.simulator,
|
|
projectUri: projectUri,
|
|
buildMode: BuildMode.debug,
|
|
fileSystem: fileSystem,
|
|
yamlParentDirectory: environment.buildDir.uri,
|
|
buildRunner: FakeNativeAssetsBuildRunner(
|
|
packagesWithNativeAssetsResult: <Package>[
|
|
Package('bar', projectUri),
|
|
],
|
|
buildResult: FakeNativeAssetsBuilderResult(
|
|
assets: <Asset>[
|
|
Asset(
|
|
id: 'package:bar/bar.dart',
|
|
linkMode: LinkMode.dynamic,
|
|
target: native_assets_cli.Target.macOSArm64,
|
|
path: AssetAbsolutePath(Uri.file('bar.dylib')),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
expect(
|
|
environment.buildDir.childFile('native_assets.yaml'),
|
|
exists,
|
|
);
|
|
});
|
|
}
|