
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.
361 lines
13 KiB
Dart
361 lines
13 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.
|
|
|
|
// This test exercises the embedding of the native assets mapping in dill files.
|
|
// An initial dill file is created by `flutter assemble` and used for running
|
|
// the application. This dill must contain the mapping.
|
|
// When doing hot reload, this mapping must stay in place.
|
|
// When doing a hot restart, a new dill file is pushed. This dill file must also
|
|
// contain the native assets mapping.
|
|
// When doing a hot reload, this mapping must stay in place.
|
|
|
|
@Timeout(Duration(minutes: 10))
|
|
library;
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:file/file.dart';
|
|
import 'package:file_testing/file_testing.dart';
|
|
|
|
import '../src/common.dart';
|
|
import 'test_utils.dart' show fileSystem, platform;
|
|
import 'transition_test_utils.dart';
|
|
|
|
final String hostOs = platform.operatingSystem;
|
|
|
|
final List<String> devices = <String>[
|
|
'flutter-tester',
|
|
hostOs,
|
|
];
|
|
|
|
final List<String> buildSubcommands = <String>[
|
|
hostOs,
|
|
if (hostOs == 'macos') 'ios',
|
|
];
|
|
|
|
final List<String> add2appBuildSubcommands = <String>[
|
|
if (hostOs == 'macos') ...<String>[
|
|
'macos-framework',
|
|
'ios-framework',
|
|
],
|
|
];
|
|
|
|
/// The build modes to target for each flutter command that supports passing
|
|
/// a build mode.
|
|
///
|
|
/// The flow of compiling kernel as well as bundling dylibs can differ based on
|
|
/// build mode, so we should cover this.
|
|
const List<String> buildModes = <String>[
|
|
'debug',
|
|
'profile',
|
|
'release',
|
|
];
|
|
|
|
const String packageName = 'package_with_native_assets';
|
|
|
|
const String exampleAppName = '${packageName}_example';
|
|
|
|
const String dylibName = 'lib$packageName.dylib';
|
|
|
|
void main() {
|
|
if (!platform.isMacOS) {
|
|
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
|
|
return;
|
|
}
|
|
|
|
setUpAll(() {
|
|
processManager.runSync(<String>[
|
|
flutterBin,
|
|
'config',
|
|
'--enable-native-assets',
|
|
]);
|
|
});
|
|
|
|
for (final String device in devices) {
|
|
for (final String buildMode in buildModes) {
|
|
if (device == 'flutter-tester' && buildMode != 'debug') {
|
|
continue;
|
|
}
|
|
final String hotReload = buildMode == 'debug' ? ' hot reload and hot restart' : '';
|
|
testWithoutContext('flutter run$hotReload with native assets $device $buildMode', () async {
|
|
await inTempDir((Directory tempDirectory) async {
|
|
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
|
|
final Directory exampleDirectory = packageDirectory.childDirectory('example');
|
|
|
|
final ProcessTestResult result = await runFlutter(
|
|
<String>[
|
|
'run',
|
|
'-d$device',
|
|
'--$buildMode',
|
|
],
|
|
exampleDirectory.path,
|
|
<Transition>[
|
|
Multiple(<Pattern>[
|
|
'Flutter run key commands.',
|
|
], handler: (String line) {
|
|
if (buildMode == 'debug') {
|
|
// Do a hot reload diff on the initial dill file.
|
|
return 'r';
|
|
} else {
|
|
// No hot reload and hot restart in release mode.
|
|
return 'q';
|
|
}
|
|
}),
|
|
if (buildMode == 'debug') ...<Transition>[
|
|
Barrier(
|
|
'Performing hot reload...'.padRight(progressMessageWidth),
|
|
logging: true,
|
|
),
|
|
Multiple(<Pattern>[
|
|
RegExp('Reloaded .*'),
|
|
], handler: (String line) {
|
|
// Do a hot restart, pushing a new complete dill file.
|
|
return 'R';
|
|
}),
|
|
Barrier('Performing hot restart...'.padRight(progressMessageWidth)),
|
|
Multiple(<Pattern>[
|
|
RegExp('Restarted application .*'),
|
|
], handler: (String line) {
|
|
// Do another hot reload, pushing a diff to the second dill file.
|
|
return 'r';
|
|
}),
|
|
Barrier(
|
|
'Performing hot reload...'.padRight(progressMessageWidth),
|
|
logging: true,
|
|
),
|
|
Multiple(<Pattern>[
|
|
RegExp('Reloaded .*'),
|
|
], handler: (String line) {
|
|
return 'q';
|
|
}),
|
|
],
|
|
const Barrier('Application finished.'),
|
|
],
|
|
logging: false,
|
|
);
|
|
if (result.exitCode != 0) {
|
|
throw Exception('flutter run failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
|
|
}
|
|
final String stdout = result.stdout.join('\n');
|
|
// Check that we did not fail to resolve the native function in the
|
|
// dynamic library.
|
|
expect(stdout, isNot(contains("Invalid argument(s): Couldn't resolve native function 'sum'")));
|
|
// And also check that we did not have any other exceptions that might
|
|
// shadow the exception we would have gotten.
|
|
expect(stdout, isNot(contains('EXCEPTION CAUGHT BY WIDGETS LIBRARY')));
|
|
|
|
if (device == 'macos') {
|
|
expectDylibIsBundledMacOS(exampleDirectory, buildMode);
|
|
}
|
|
if (device == hostOs) {
|
|
expectCCompilerIsConfigured(exampleDirectory);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
testWithoutContext('flutter test with native assets', () async {
|
|
await inTempDir((Directory tempDirectory) async {
|
|
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
|
|
|
|
final ProcessTestResult result = await runFlutter(
|
|
<String>[
|
|
'test',
|
|
],
|
|
packageDirectory.path,
|
|
<Transition>[
|
|
Barrier(RegExp('.* All tests passed!')),
|
|
],
|
|
logging: false,
|
|
);
|
|
if (result.exitCode != 0) {
|
|
throw Exception('flutter test failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
|
|
}
|
|
});
|
|
});
|
|
|
|
for (final String buildSubcommand in buildSubcommands) {
|
|
for (final String buildMode in buildModes) {
|
|
testWithoutContext('flutter build $buildSubcommand with native assets $buildMode', () async {
|
|
await inTempDir((Directory tempDirectory) async {
|
|
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
|
|
final Directory exampleDirectory = packageDirectory.childDirectory('example');
|
|
|
|
final ProcessResult result = processManager.runSync(
|
|
<String>[
|
|
flutterBin,
|
|
'build',
|
|
buildSubcommand,
|
|
'--$buildMode',
|
|
if (buildSubcommand == 'ios') '--no-codesign',
|
|
],
|
|
workingDirectory: exampleDirectory.path,
|
|
);
|
|
if (result.exitCode != 0) {
|
|
throw Exception('flutter build failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
|
|
}
|
|
|
|
if (buildSubcommand == 'macos') {
|
|
expectDylibIsBundledMacOS(exampleDirectory, buildMode);
|
|
} else if (buildSubcommand == 'ios') {
|
|
expectDylibIsBundledIos(exampleDirectory, buildMode);
|
|
}
|
|
expectCCompilerIsConfigured(exampleDirectory);
|
|
});
|
|
});
|
|
}
|
|
|
|
// This could be an hermetic unit test if the native_assets_builder
|
|
// could mock process runs and file system.
|
|
// https://github.com/dart-lang/native/issues/90.
|
|
testWithoutContext('flutter build $buildSubcommand error on static libraries', () async {
|
|
await inTempDir((Directory tempDirectory) async {
|
|
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
|
|
final File buildDotDart = packageDirectory.childFile('build.dart');
|
|
final String buildDotDartContents = await buildDotDart.readAsString();
|
|
// Overrides the build to output static libraries.
|
|
final String buildDotDartContentsNew = buildDotDartContents.replaceFirst(
|
|
'final buildConfig = await BuildConfig.fromArgs(args);',
|
|
r'''
|
|
final buildConfig = await BuildConfig.fromArgs([
|
|
'-D${LinkModePreference.configKey}=${LinkModePreference.static}',
|
|
...args,
|
|
]);
|
|
''',
|
|
);
|
|
expect(buildDotDartContentsNew, isNot(buildDotDartContents));
|
|
await buildDotDart.writeAsString(buildDotDartContentsNew);
|
|
final Directory exampleDirectory = packageDirectory.childDirectory('example');
|
|
|
|
final ProcessResult result = processManager.runSync(
|
|
<String>[
|
|
flutterBin,
|
|
'build',
|
|
buildSubcommand,
|
|
if (buildSubcommand == 'ios') '--no-codesign',
|
|
],
|
|
workingDirectory: exampleDirectory.path,
|
|
);
|
|
expect(result.exitCode, isNot(0));
|
|
expect(result.stderr, contains('link mode set to static, but this is not yet supported'));
|
|
});
|
|
});
|
|
}
|
|
|
|
for (final String add2appBuildSubcommand in add2appBuildSubcommands) {
|
|
testWithoutContext('flutter build $add2appBuildSubcommand with native assets', () async {
|
|
await inTempDir((Directory tempDirectory) async {
|
|
final Directory packageDirectory = await createTestProject(packageName, tempDirectory);
|
|
final Directory exampleDirectory = packageDirectory.childDirectory('example');
|
|
|
|
final ProcessResult result = processManager.runSync(
|
|
<String>[
|
|
flutterBin,
|
|
'build',
|
|
add2appBuildSubcommand,
|
|
],
|
|
workingDirectory: exampleDirectory.path,
|
|
);
|
|
if (result.exitCode != 0) {
|
|
throw Exception('flutter build failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
|
|
}
|
|
|
|
for (final String buildMode in buildModes) {
|
|
expectDylibIsBundledWithFrameworks(exampleDirectory, buildMode, add2appBuildSubcommand.replaceAll('-framework', ''));
|
|
}
|
|
expectCCompilerIsConfigured(exampleDirectory);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
/// For `flutter build` we can't easily test whether running the app works.
|
|
/// Check that we have the dylibs in the app.
|
|
void expectDylibIsBundledMacOS(Directory appDirectory, String buildMode) {
|
|
final Directory appBundle = appDirectory.childDirectory('build/$hostOs/Build/Products/${buildMode.upperCaseFirst()}/$exampleAppName.app');
|
|
expect(appBundle, exists);
|
|
final Directory dylibsFolder = appBundle.childDirectory('Contents/Frameworks');
|
|
expect(dylibsFolder, exists);
|
|
final File dylib = dylibsFolder.childFile(dylibName);
|
|
expect(dylib, exists);
|
|
}
|
|
|
|
void expectDylibIsBundledIos(Directory appDirectory, String buildMode) {
|
|
final Directory appBundle = appDirectory.childDirectory('build/ios/${buildMode.upperCaseFirst()}-iphoneos/Runner.app');
|
|
expect(appBundle, exists);
|
|
final Directory dylibsFolder = appBundle.childDirectory('Frameworks');
|
|
expect(dylibsFolder, exists);
|
|
final File dylib = dylibsFolder.childFile(dylibName);
|
|
expect(dylib, exists);
|
|
}
|
|
|
|
/// For `flutter build` we can't easily test whether running the app works.
|
|
/// Check that we have the dylibs in the app.
|
|
void expectDylibIsBundledWithFrameworks(Directory appDirectory, String buildMode, String os) {
|
|
final Directory frameworksFolder = appDirectory.childDirectory('build/$os/framework/${buildMode.upperCaseFirst()}');
|
|
expect(frameworksFolder, exists);
|
|
final File dylib = frameworksFolder.childFile(dylibName);
|
|
expect(dylib, exists);
|
|
}
|
|
|
|
/// Check that the native assets are built with the C Compiler that Flutter uses.
|
|
///
|
|
/// This inspects the build configuration to see if the C compiler was configured.
|
|
void expectCCompilerIsConfigured(Directory appDirectory) {
|
|
final Directory nativeAssetsBuilderDir = appDirectory.childDirectory('.dart_tool/native_assets_builder/');
|
|
for (final Directory subDir in nativeAssetsBuilderDir.listSync().whereType<Directory>()) {
|
|
final File config = subDir.childFile('config.yaml');
|
|
expect(config, exists);
|
|
final String contents = config.readAsStringSync();
|
|
// Dry run does not pass compiler info.
|
|
if (contents.contains('dry_run: true')) {
|
|
continue;
|
|
}
|
|
expect(contents, contains('cc: '));
|
|
}
|
|
}
|
|
|
|
extension on String {
|
|
String upperCaseFirst() {
|
|
return replaceFirst(this[0], this[0].toUpperCase());
|
|
}
|
|
}
|
|
|
|
Future<Directory> createTestProject(String packageName, Directory tempDirectory) async {
|
|
final ProcessResult result = processManager.runSync(
|
|
<String>[
|
|
flutterBin,
|
|
'create',
|
|
'--template=package_ffi',
|
|
packageName,
|
|
],
|
|
workingDirectory: tempDirectory.path,
|
|
);
|
|
|
|
if (result.exitCode != 0) {
|
|
throw Exception('flutter create failed: ${result.exitCode}\n${result.stderr}\n${result.stdout}');
|
|
}
|
|
|
|
final Directory packageDirectory = tempDirectory.childDirectory(packageName);
|
|
|
|
// No platform-specific boilerplate files.
|
|
expect(packageDirectory.childDirectory('android/'), isNot(exists));
|
|
expect(packageDirectory.childDirectory('ios/'), isNot(exists));
|
|
expect(packageDirectory.childDirectory('linux/'), isNot(exists));
|
|
expect(packageDirectory.childDirectory('macos/'), isNot(exists));
|
|
expect(packageDirectory.childDirectory('windows/'), isNot(exists));
|
|
|
|
return packageDirectory;
|
|
}
|
|
|
|
Future<void> inTempDir(Future<void> Function(Directory tempDirectory) fun) async {
|
|
final Directory tempDirectory = fileSystem.directory(fileSystem.systemTempDirectory.createTempSync().resolveSymbolicLinksSync());
|
|
try {
|
|
await fun(tempDirectory);
|
|
} finally {
|
|
tryToDelete(tempDirectory);
|
|
}
|
|
}
|