Move embedding and linking Flutter frameworks into the tool (#51453)

* Move embedding and linking Flutter frameworks into the tool

* Unused import

* Migrate

* Rename run, add comments, remove typedef

* Add status log to tell the user what we did

* Remove Podfile migration, create IOSMigration superclass

* word-smiting

Co-Authored-By: Jonah Williams <jonahwilliams@google.com>

* for space

Co-Authored-By: Jonah Williams <jonahwilliams@google.com>

Co-authored-by: Jonah Williams <jonahwilliams@google.com>
This commit is contained in:
Jenn Magder
2020-03-03 12:11:28 -08:00
committed by GitHub
parent d2c734a36f
commit e491544588
38 changed files with 643 additions and 350 deletions

View File

@@ -22,6 +22,8 @@ import '../macos/xcode.dart';
import '../project.dart';
import '../reporting/reporting.dart';
import 'code_signing.dart';
import 'migrations/ios_migrator.dart';
import 'migrations/remove_framework_link_and_embedding_migration.dart';
import 'xcodeproj.dart';
class IMobileDevice {
@@ -87,6 +89,16 @@ Future<XcodeBuildResult> buildXcodeProject({
return XcodeBuildResult(success: false);
}
final List<IOSMigrator> migrators = <IOSMigrator>[
RemoveFrameworkLinkAndEmbeddingMigration(app.project, globals.logger, globals.xcode)
];
for (final IOSMigrator migrator in migrators) {
if (!migrator.migrate()) {
return XcodeBuildResult(success: false);
}
}
if (!_checkXcodeVersion()) {
return XcodeBuildResult(success: false);
}
@@ -439,6 +451,20 @@ Future<void> diagnoseXcodeBuildFailure(XcodeBuildResult result) async {
).send();
}
// Building for iOS Simulator, but the linked and embedded framework 'App.framework' was built for iOS.
// or
// Building for iOS, but the linked and embedded framework 'App.framework' was built for iOS Simulator.
if (result.stdout?.contains('Building for iOS') == true
&& result.stdout?.contains('but the linked and embedded framework') == true
&& result.stdout?.contains('was built for iOS') == true) {
globals.printError('');
globals.printError('Your Xcode project requires migration. See https://github.com/flutter/flutter/issues/50568 for details.');
globals.printError('');
globals.printError('You can temporarily work around this issue by running:');
globals.printError(' rm -rf ios/Flutter/App.framework');
return;
}
if (result.xcodeBuildExecution != null &&
result.xcodeBuildExecution.buildForPhysicalDevice &&
result.stdout?.contains('BCEROR') == true &&
@@ -529,6 +555,7 @@ bool _checkXcodeVersion() {
return true;
}
// TODO(jmagman): Refactor to IOSMigrator.
bool upgradePbxProjWithFlutterAssets(IosProject project) {
final File xcodeProjectFile = project.xcodeProjectInfoFile;
assert(xcodeProjectFile.existsSync());

View File

@@ -0,0 +1,56 @@
// 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:meta/meta.dart';
import '../../base/file_system.dart';
import '../../base/logger.dart';
/// iOS project is generated from a template on Flutter project creation.
/// Sometimes (due to behavior changes in Xcode, CocoaPods, etc) these files need to be altered
/// from the original template.
class IOSMigrator {
IOSMigrator(this.logger);
@protected
final Logger logger;
/// Returns whether migration was successful or was skipped.
bool migrate() {
return false;
}
/// [processLine] should return null if the line should be deleted.
@protected
void processFileLines(File file, String Function(String) processLine) {
final List<String> lines = file.readAsLinesSync();
final StringBuffer newProjectContents = StringBuffer();
final String basename = file.basename;
bool migrationRequired = false;
for (final String line in lines) {
final String newProjectLine = processLine(line);
if (newProjectLine == null) {
logger.printTrace('Migrating $basename, removing:');
logger.printTrace(' $line');
migrationRequired = true;
continue;
}
if (newProjectLine != line) {
logger.printTrace('Migrating $basename, replacing:');
logger.printTrace(' $line');
logger.printTrace('with:');
logger.printTrace(' $newProjectLine');
migrationRequired = true;
}
newProjectContents.writeln(newProjectLine);
}
if (migrationRequired) {
logger.printStatus('Upgrading $basename');
file.writeAsStringSync(newProjectContents.toString());
}
}
}

View File

@@ -0,0 +1,112 @@
// 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 '../../base/file_system.dart';
import '../../base/logger.dart';
import '../../macos/xcode.dart';
import '../../project.dart';
import 'ios_migrator.dart';
/// Xcode 11.4 requires linked and embedded frameworks to contain all targeted architectures before build phases are run.
/// This caused issues switching between a real device and simulator due to architecture mismatch.
/// Remove the linking and embedding logic from the Xcode project to give the tool more control over these.
class RemoveFrameworkLinkAndEmbeddingMigration extends IOSMigrator {
RemoveFrameworkLinkAndEmbeddingMigration(
IosProject project,
Logger logger,
Xcode xcode,
) : _xcodeProjectInfoFile = project.xcodeProjectInfoFile,
_xcode = xcode,
super(logger);
final File _xcodeProjectInfoFile;
final Xcode _xcode;
/// Inspect [project] for necessary migrations and rewrite files as needed.
@override
bool migrate() {
return _migrateXcodeProjectInfoFile();
}
bool _migrateXcodeProjectInfoFile() {
if (!_xcodeProjectInfoFile.existsSync()) {
logger.printTrace('Xcode project not found, skipping migration');
return true;
}
bool migrationFailure = false;
processFileLines(_xcodeProjectInfoFile, (String line) {
// App.framework Frameworks reference.
// isa = PBXFrameworksBuildPhase;
// files = (
// 3B80C3941E831B6300D905FE /* App.framework in Frameworks */,
if (line.contains('3B80C3941E831B6300D905FE')) {
return null;
}
// App.framework Embed Framework reference (build phase to embed framework).
// 3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */,
if (line.contains('3B80C3951E831B6300D905FE')
|| line.contains('741F496821356857001E2961')) { // Ephemeral add-to-app variant.
return null;
}
// App.framework project file reference (seen in Xcode navigator pane).
// isa = PBXGroup;
// children = (
// 3B80C3931E831B6300D905FE /* App.framework */,
if (line.contains('3B80C3931E831B6300D905FE')
|| line.contains('741F496521356807001E2961')) { // Ephemeral add-to-app variant.
return null;
}
// Flutter.framework Frameworks reference.
// isa = PBXFrameworksBuildPhase;
// files = (
// 9705A1C61CF904A100538489 /* Flutter.framework in Frameworks */,
if (line.contains('9705A1C61CF904A100538489')) {
return null;
}
// Flutter.framework Embed Framework reference (build phase to embed framework).
// 9705A1C71CF904A300538489 /* Flutter.framework in Embed Frameworks */,
if (line.contains('9705A1C71CF904A300538489')
|| line.contains('741F496221355F47001E2961')) { // Ephemeral add-to-app variant.
return null;
}
// Flutter.framework project file reference (seen in Xcode navigator pane).
// isa = PBXGroup;
// children = (
// 9740EEBA1CF902C7004384FC /* Flutter.framework */,
if (line.contains('9740EEBA1CF902C7004384FC')
|| line.contains('741F495E21355F27001E2961')) { // Ephemeral add-to-app variant.
return null;
}
// Embed and thin frameworks in a script instead of using Xcode's link / embed build phases.
const String thinBinaryScript = 'xcode_backend.sh\\" thin';
if (line.contains(thinBinaryScript)) {
return line.replaceFirst(thinBinaryScript, 'xcode_backend.sh\\" embed_and_thin');
}
if (line.contains('/* App.framework ') || line.contains('/* Flutter.framework ')) {
migrationFailure = true;
}
return line;
});
if (migrationFailure) {
// Print scary message if the user is on Xcode 11.4 or greater, or if Xcode isn't installed.
final bool xcodeIsInstalled = _xcode.isInstalled;
if(!xcodeIsInstalled || (_xcode.majorVersion > 11 || (_xcode.majorVersion == 11 && _xcode.minorVersion >= 4))) {
logger.printError('Your Xcode project requires migration. See https://github.com/flutter/flutter/issues/50568 for details.');
return false;
}
}
return true;
}
}

View File

@@ -184,6 +184,11 @@ List<String> _xcodeBuildSettingsLines({
xcodeBuildSettings.add('SYMROOT=\${SOURCE_ROOT}/../${getIosBuildDirectory()}');
}
// iOS does not link on Flutter in any build phase. Add the linker flag.
if (!useMacOSConfig) {
xcodeBuildSettings.add('OTHER_LDFLAGS=\$(inherited) -framework Flutter');
}
if (!project.isModule) {
// For module projects we do not want to write the FLUTTER_FRAMEWORK_DIR
// explicitly. Rather we rely on the xcode backend script and the Podfile