forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native assets support for MacOS and iOS (#130494)
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
Showing
74 changed files
with
4,134 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.