Native assets: package in framework on iOS and MacOS (#140907)

Packages the native assets for iOS and MacOS in frameworks.

Issue:

* https://github.com/flutter/flutter/issues/140544
* https://github.com/flutter/flutter/issues/129757

## Details

* [x] This packages dylibs from the native assets feature in frameworks. It packages every dylib in a separate framework.
* [x] The dylib name is updated to use `@rpath` instead of `@executable_path`.
* [x] The dylibs for flutter-tester are no longer modified to change the install name. (Previously it was wrongly updating the install name to the location the dylib would have once deployed in an app.)
* [x] Use symlinking on MacOS.
This commit is contained in:
Daco Harkes
2024-01-19 21:29:13 +01:00
committed by GitHub
parent 77c3807c80
commit 2e229be2ff
9 changed files with 479 additions and 107 deletions

View File

@@ -135,7 +135,7 @@ void main() {
linkMode: native_assets_cli.LinkMode.dynamic,
target: native_assets_cli.Target.iOSArm64,
path: native_assets_cli.AssetAbsolutePath(
Uri.file('libfoo.dylib'),
Uri.file('foo.framework/foo'),
),
)
], dependencies: <Uri>[
@@ -165,7 +165,7 @@ void main() {
nativeAssetsYaml.readAsStringSync(),
stringContainsInOrder(<String>[
'package:foo/foo.dart',
'libfoo.dylib',
'foo.framework',
]),
);
},

View File

@@ -130,13 +130,13 @@ void main() {
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.macOSArm64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
path: AssetAbsolutePath(Uri.file('libbar.dylib')),
),
Asset(
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.macOSX64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
path: AssetAbsolutePath(Uri.file('libbar.dylib')),
),
],
),
@@ -219,16 +219,16 @@ void main() {
'lipo',
'-create',
'-output',
'/build/native_assets/ios/bar.dylib',
'bar.dylib',
'/build/native_assets/ios/bar.framework/bar',
'libbar.dylib',
],
),
const FakeCommand(
command: <Pattern>[
'install_name_tool',
'-id',
'@executable_path/Frameworks/bar.dylib',
'/build/native_assets/ios/bar.dylib',
'@rpath/bar.framework/bar',
'/build/native_assets/ios/bar.framework/bar'
],
),
const FakeCommand(
@@ -238,7 +238,7 @@ void main() {
'--sign',
'-',
'--timestamp=none',
'/build/native_assets/ios/bar.dylib',
'/build/native_assets/ios/bar.framework',
],
),
],
@@ -267,7 +267,7 @@ void main() {
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.iOSArm64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
path: AssetAbsolutePath(Uri.file('libbar.dylib')),
),
],
),

View File

@@ -0,0 +1,68 @@
// 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_tools/src/macos/native_assets_host.dart';
import '../../src/common.dart';
void main() {
test('framework name', () {
expect(
frameworkUri('libfoo.dylib', <String>{}),
equals(Uri.file('foo.framework/foo')),
);
expect(
frameworkUri('foo', <String>{}),
equals(Uri.file('foo.framework/foo')),
);
expect(
frameworkUri('foo_foo', <String>{}),
equals(Uri.file('foo_foo.framework/foo_foo')),
);
expect(
frameworkUri('foo-foo', <String>{}),
equals(Uri.file('foo-foo.framework/foo-foo')),
);
expect(
frameworkUri(r'foo$foo', <String>{}),
equals(Uri.file('foofoo.framework/foofoo')),
);
expect(
frameworkUri('foo.foo', <String>{}),
equals(Uri.file('foofoo.framework/foofoo')),
);
expect(
frameworkUri('libatoolongfilenameforaframework.dylib', <String>{}),
equals(Uri.file('atoolongfilenam.framework/atoolongfilenam')),
);
});
test('framework name name confilicts', () {
final Set<String> alreadyTakenNames = <String>{};
expect(
frameworkUri('libfoo.dylib', alreadyTakenNames),
equals(Uri.file('foo.framework/foo')),
);
expect(
frameworkUri('libfoo.dylib', alreadyTakenNames),
equals(Uri.file('foo1.framework/foo1')),
);
expect(
frameworkUri('libfoo.dylib', alreadyTakenNames),
equals(Uri.file('foo2.framework/foo2')),
);
expect(
frameworkUri('libatoolongfilenameforaframework.dylib', alreadyTakenNames),
equals(Uri.file('atoolongfilenam.framework/atoolongfilenam')),
);
expect(
frameworkUri('libatoolongfilenameforaframework.dylib', alreadyTakenNames),
equals(Uri.file('atoolongfile1.framework/atoolongfile1')),
);
expect(
frameworkUri('libatoolongfilenameforaframework.dylib', alreadyTakenNames),
equals(Uri.file('atoolongfile2.framework/atoolongfile2')),
);
});
}

View File

@@ -149,13 +149,13 @@ void main() {
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.macOSArm64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
path: AssetAbsolutePath(Uri.file('libbar.dylib')),
),
Asset(
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.macOSX64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
path: AssetAbsolutePath(Uri.file('libbar.dylib')),
),
],
),
@@ -236,35 +236,47 @@ void main() {
if (flutterTester) {
testName += ' flutter tester';
}
final String dylibPath;
final String signPath;
if (flutterTester) {
// Just the dylib.
dylibPath = '/build/native_assets/macos/libbar.dylib';
signPath = '/build/native_assets/macos/libbar.dylib';
} else {
// Packaged in framework.
dylibPath = '/build/native_assets/macos/bar.framework/Versions/A/bar';
signPath = '/build/native_assets/macos/bar.framework';
}
testUsingContext('build with assets$testName', overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true),
ProcessManager: () => FakeProcessManager.list(
<FakeCommand>[
const FakeCommand(
FakeCommand(
command: <Pattern>[
'lipo',
'-create',
'-output',
'/build/native_assets/macos/bar.dylib',
'bar.dylib',
dylibPath,
'libbar.dylib',
],
),
const FakeCommand(
command: <Pattern>[
'install_name_tool',
'-id',
'@executable_path/Frameworks/bar.dylib',
'/build/native_assets/macos/bar.dylib',
],
),
const FakeCommand(
if (!flutterTester)
FakeCommand(
command: <Pattern>[
'install_name_tool',
'-id',
'@rpath/bar.framework/bar',
dylibPath,
],
),
FakeCommand(
command: <Pattern>[
'codesign',
'--force',
'--sign',
'-',
'--timestamp=none',
'/build/native_assets/macos/bar.dylib',
signPath,
],
),
],
@@ -292,7 +304,7 @@ void main() {
id: 'package:bar/bar.dart',
linkMode: LinkMode.dynamic,
target: native_assets_cli.Target.macOSArm64,
path: AssetAbsolutePath(Uri.file('bar.dylib')),
path: AssetAbsolutePath(Uri.file('libbar.dylib')),
),
],
),
@@ -315,10 +327,10 @@ void main() {
'package:bar/bar.dart',
if (flutterTester)
// Tests run on host system, so the have the full path on the system.
'- ${projectUri.resolve('build/native_assets/macos/bar.dylib').toFilePath()}'
'- ${projectUri.resolve('build/native_assets/macos/libbar.dylib').toFilePath()}'
else
// Apps are a bundle with the dylibs on their dlopen path.
'- bar.dylib',
'- bar.framework/bar',
]),
);
});

View File

@@ -292,18 +292,48 @@ void main() {
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(OS.macOS.dylibFileName(packageName));
expect(dylib, exists);
final Directory frameworksFolder =
appBundle.childDirectory('Contents/Frameworks');
expect(frameworksFolder, exists);
// MyFramework.framework/
// MyFramework -> Versions/Current/MyFramework
// Resources -> Versions/Current/Resources
// Versions/
// A/
// MyFramework
// Resources/
// Info.plist
// Current -> A
final String frameworkName = packageName.substring(0, 15);
final Directory frameworkDir =
frameworksFolder.childDirectory('$frameworkName.framework');
final Directory versionsDir = frameworkDir.childDirectory('Versions');
final Directory versionADir = versionsDir.childDirectory('A');
final Directory resourcesDir = versionADir.childDirectory('Resources');
expect(resourcesDir, exists);
final File dylibFile = versionADir.childFile(frameworkName);
expect(dylibFile, exists);
final Link currentLink = versionsDir.childLink('Current');
expect(currentLink, exists);
expect(currentLink.resolveSymbolicLinksSync(), versionADir.path);
final Link resourcesLink = frameworkDir.childLink('Resources');
expect(resourcesLink, exists);
expect(resourcesLink.resolveSymbolicLinksSync(), resourcesDir.path);
final Link dylibLink = frameworkDir.childLink(frameworkName);
expect(dylibLink, exists);
expect(dylibLink.resolveSymbolicLinksSync(), dylibFile.path);
}
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(OS.iOS.dylibFileName(packageName));
final Directory frameworksFolder = appBundle.childDirectory('Frameworks');
expect(frameworksFolder, exists);
final String frameworkName = packageName.substring(0, 15);
final File dylib = frameworksFolder
.childDirectory('$frameworkName.framework')
.childFile(frameworkName);
expect(dylib, exists);
}
@@ -379,7 +409,10 @@ void expectDylibIsBundledAndroid(Directory appDirectory, String buildMode) {
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(OS.macOS.dylibFileName(packageName));
final String frameworkName = packageName.substring(0, 15);
final File dylib = frameworksFolder
.childDirectory('$frameworkName.framework')
.childFile(frameworkName);
expect(dylib, exists);
}