Skip to content

Commit

Permalink
Native assets support for MacOS and iOS (#130494)
Browse files Browse the repository at this point in the history
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:

* flutter/flutter#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: flutter/flutter#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.
  • Loading branch information
dcharkes authored Sep 10, 2023
1 parent 690800b commit aa36db1
Show file tree
Hide file tree
Showing 74 changed files with 4,134 additions and 414 deletions.
20 changes: 20 additions & 0 deletions .ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4069,6 +4069,26 @@ targets:
["devicelab", "ios", "mac"]
task_name: microbenchmarks_ios

- name: Mac_ios native_assets_ios_simulator
recipe: devicelab/devicelab_drone
presubmit: false
bringup: true # TODO(dacoharkes): Set to false in follow up PR and check that test works on CI.
timeout: 60
properties:
tags: >
["devicelab", "ios", "mac"]
task_name: native_assets_ios_simulator

- name: Mac_ios native_assets_ios
recipe: devicelab/devicelab_drone
presubmit: false
bringup: true # TODO(dacoharkes): Set to false in follow up PR and check that test works on CI.
timeout: 60
properties:
tags: >
["devicelab", "ios", "mac"]
task_name: native_assets_ios

- name: Mac_ios native_platform_view_ui_tests_ios
recipe: devicelab/devicelab_drone
presubmit: false
Expand Down
2 changes: 2 additions & 0 deletions TESTOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@
/dev/devicelab/bin/tasks/large_image_changer_perf_ios.dart @zanderso @flutter/engine
/dev/devicelab/bin/tasks/macos_chrome_dev_mode.dart @zanderso @flutter/tool
/dev/devicelab/bin/tasks/microbenchmarks_ios.dart @cyanglaz @flutter/engine
/dev/devicelab/bin/tasks/native_assets_ios_simulator.dart @dacoharkes @flutter/ios
/dev/devicelab/bin/tasks/native_assets_ios.dart @dacoharkes @flutter/ios
/dev/devicelab/bin/tasks/native_platform_view_ui_tests_ios.dart @hellohuanlin @flutter/ios
/dev/devicelab/bin/tasks/new_gallery_ios__transition_perf.dart @zanderso @flutter/engine
/dev/devicelab/bin/tasks/new_gallery_skia_ios__transition_perf.dart @zanderso @flutter/engine
Expand Down
65 changes: 62 additions & 3 deletions dev/devicelab/bin/tasks/module_test_ios.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ Future<void> main() async {
final File marquee = File(path.join(flutterModuleLibSource.path, 'marquee'));
marquee.copySync(path.join(flutterModuleLibDestination.path, 'marquee.dart'));

section('Create package with native assets');

await flutter(
'config',
options: <String>['--enable-native-assets'],
);

const String ffiPackageName = 'ffi_package';
await _createFfiPackage(ffiPackageName, tempDir);

section('Add FFI package');

final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
String content = await pubspec.readAsString();
content = content.replaceFirst(
'dependencies:\n',
'''
dependencies:
$ffiPackageName:
path: ../$ffiPackageName
''',
);
await pubspec.writeAsString(content, flush: true);
await inDirectory(projectDir, () async {
await flutter(
'packages',
options: <String>['get'],
);
});

section('Build ephemeral host app in release mode without CocoaPods');

await inDirectory(projectDir, () async {
Expand Down Expand Up @@ -162,10 +192,8 @@ Future<void> main() async {

section('Add plugins');

final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
String content = await pubspec.readAsString();
content = content.replaceFirst(
'\ndependencies:\n',
'dependencies:\n',
// One framework, one Dart-only, one that does not support iOS, and one with a resource bundle.
'''
dependencies:
Expand Down Expand Up @@ -221,6 +249,11 @@ dependencies:
// Dart-only, no embedded framework.
checkDirectoryNotExists(path.join(ephemeralIOSHostApp.path, 'Frameworks', '$dartPluginName.framework'));

// Native assets embedded, no embedded framework.
const String libFfiPackageDylib = 'lib$ffiPackageName.dylib';
checkFileExists(path.join(ephemeralIOSHostApp.path, 'Frameworks', libFfiPackageDylib));
checkDirectoryNotExists(path.join(ephemeralIOSHostApp.path, 'Frameworks', '$ffiPackageName.framework'));

section('Clean and pub get module');

await inDirectory(projectDir, () async {
Expand Down Expand Up @@ -350,6 +383,11 @@ end
'isolate_snapshot_data',
));

checkFileExists(path.join(
hostFrameworksDirectory,
libFfiPackageDylib,
));

section('Check the NOTICE file is correct');

final String licenseFilePath = path.join(
Expand Down Expand Up @@ -449,6 +487,13 @@ end
throw TaskResult.failure('Unexpected armv7 architecture slice in $builtAppBinary');
}

// Check native assets are bundled.
checkFileExists(path.join(
archivedAppPath,
'Frameworks',
libFfiPackageDylib,
));

// The host app example builds plugins statically, url_launcher_ios.framework
// should not exist.
checkDirectoryNotExists(path.join(
Expand Down Expand Up @@ -685,3 +730,17 @@ class $dartPluginClass {
// Remove the native plugin code.
await Directory(path.join(pluginDir, 'ios')).delete(recursive: true);
}

Future<void> _createFfiPackage(String name, Directory parent) async {
await inDirectory(parent, () async {
await flutter(
'create',
options: <String>[
'--org',
'io.flutter.devicelab',
'--template=package_ffi',
name,
],
);
});
}
14 changes: 14 additions & 0 deletions dev/devicelab/bin/tasks/native_assets_ios.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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:flutter_devicelab/framework/devices.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/tasks/native_assets_test.dart';

Future<void> main() async {
await task(() async {
deviceOperatingSystem = DeviceOperatingSystem.ios;
return createNativeAssetsTest()();
});
}
31 changes: 31 additions & 0 deletions dev/devicelab/bin/tasks/native_assets_ios_simulator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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:flutter_devicelab/framework/devices.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/ios.dart';
import 'package:flutter_devicelab/framework/task_result.dart';
import 'package:flutter_devicelab/tasks/native_assets_test.dart';

Future<void> main() async {
await task(() async {
deviceOperatingSystem = DeviceOperatingSystem.ios;
String? simulatorDeviceId;
try {
await testWithNewIOSSimulator(
'TestNativeAssetsSim',
(String deviceId) async {
simulatorDeviceId = deviceId;
await createNativeAssetsTest(
deviceIdOverride: deviceId,
isIosSimulator: true,
)();
},
);
} finally {
await removeIOSimulator(simulatorDeviceId);
}
return TaskResult.success(null);
});
}
191 changes: 191 additions & 0 deletions dev/devicelab/lib/tasks/native_assets_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// 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 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as path;

import '../framework/devices.dart';
import '../framework/framework.dart';
import '../framework/task_result.dart';
import '../framework/utils.dart';

const String _packageName = 'package_with_native_assets';

const List<String> _buildModes = <String>[
'debug',
'profile',
'release',
];

TaskFunction createNativeAssetsTest({
String? deviceIdOverride,
bool checkAppRunningOnLocalDevice = true,
bool isIosSimulator = false,
}) {
return () async {
if (deviceIdOverride == null) {
final Device device = await devices.workingDevice;
await device.unlock();
deviceIdOverride = device.deviceId;
}

await enableNativeAssets();

for (final String buildMode in _buildModes) {
if (buildMode != 'debug' && isIosSimulator) {
continue;
}
final TaskResult buildModeResult = await inTempDir((Directory tempDirectory) async {
final Directory packageDirectory = await createTestProject(_packageName, tempDirectory);
final Directory exampleDirectory = dir(packageDirectory.uri.resolve('example/').toFilePath());

final List<String> options = <String>[
'-d',
deviceIdOverride!,
'--no-android-gradle-daemon',
'--no-publish-port',
'--verbose',
'--uninstall-first',
'--$buildMode',
];
int transitionCount = 0;
bool done = false;

await inDirectory<void>(exampleDirectory, () async {
final int runFlutterResult = await runFlutter(
options: options,
onLine: (String line, Process process) {
if (done) {
return;
}
switch (transitionCount) {
case 0:
if (!line.contains('Flutter run key commands.')) {
return;
}
if (buildMode == 'debug') {
// Do a hot reload diff on the initial dill file.
process.stdin.writeln('r');
} else {
done = true;
process.stdin.writeln('q');
}
case 1:
if (!line.contains('Reloaded')) {
return;
}
process.stdin.writeln('R');
case 2:
// Do a hot restart, pushing a new complete dill file.
if (!line.contains('Restarted application')) {
return;
}
// Do another hot reload, pushing a diff to the second dill file.
process.stdin.writeln('r');
case 3:
if (!line.contains('Reloaded')) {
return;
}
done = true;
process.stdin.writeln('q');
}
transitionCount += 1;
},
);
if (runFlutterResult != 0) {
print('Flutter run returned non-zero exit code: $runFlutterResult.');
}
});

final int expectedNumberOfTransitions = buildMode == 'debug' ? 4 : 1;
if (transitionCount != expectedNumberOfTransitions) {
return TaskResult.failure(
'Did not get expected number of transitions: $transitionCount '
'(expected $expectedNumberOfTransitions)',
);
}
return TaskResult.success(null);
});
if (buildModeResult.failed) {
return buildModeResult;
}
}
return TaskResult.success(null);
};
}

Future<int> runFlutter({
required List<String> options,
required void Function(String, Process) onLine,
}) async {
final Process process = await startFlutter(
'run',
options: options,
);

final Completer<void> stdoutDone = Completer<void>();
final Completer<void> stderrDone = Completer<void>();
process.stdout.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen((String line) {
onLine(line, process);
print('stdout: $line');
}, onDone: stdoutDone.complete);

process.stderr.transform<String>(utf8.decoder).transform<String>(const LineSplitter()).listen(
(String line) => print('stderr: $line'),
onDone: stderrDone.complete,
);

await Future.wait<void>(<Future<void>>[stdoutDone.future, stderrDone.future]);
final int exitCode = await process.exitCode;
return exitCode;
}

final String _flutterBin = path.join(flutterDirectory.path, 'bin', 'flutter');

Future<void> enableNativeAssets() async {
print('Enabling configs for native assets...');
final int configResult = await exec(
_flutterBin,
<String>[
'config',
'-v',
'--enable-native-assets',
],
canFail: true);
if (configResult != 0) {
print('Failed to enable configuration, tasks may not run.');
}
}

Future<Directory> createTestProject(
String packageName,
Directory tempDirectory,
) async {
final int createResult = await exec(
_flutterBin,
<String>[
'create',
'--template=package_ffi',
packageName,
],
workingDirectory: tempDirectory.path,
canFail: true,
);
assert(createResult == 0);

final Directory packageDirectory = Directory.fromUri(tempDirectory.uri.resolve('$packageName/'));
return packageDirectory;
}

Future<T> inTempDir<T>(Future<T> Function(Directory tempDirectory) fun) async {
final Directory tempDirectory = dir(Directory.systemTemp.createTempSync().resolveSymbolicLinksSync());
try {
return await fun(tempDirectory);
} finally {
tempDirectory.deleteSync(recursive: true);
}
}
Loading

0 comments on commit aa36db1

Please sign in to comment.