Skip to content

Commit

Permalink
Use Xcode legacy build system for iOS builds (#21901) (#21994)
Browse files Browse the repository at this point in the history
Xcode 10 introduces a new build system which includes stricter checks on
duplicate build outputs.

When plugins are in use, there are two competing build actions that copy
Flutter.framework into the build application Frameworks directory:

  1. The Embed Frameworks build phase for the Runner project
  2. The [CP] Embed Pods Frameworks build phase that pod install creates
     in the project.

Item (1) is there to ensure the framework is copied into the built app
in the case where there are no plugins (and therefore no CocoaPods
integration in the Xcode project). Item (2) is there because Flutter's
podspec declares Flutter.framework as a vended_framework, and CocoaPods
automatically adds a copy step for each such vended_framework in the
transitive closure of CocoaPods dependencies.

As an immediate fix, we opt back into the build system used by Xcode 9
and earlier. Longer term, we need to update our templates and
flutter_tools to correctly handle this situation.

See: #20685
  • Loading branch information
cbracken committed Sep 18, 2018
1 parent ff9dc22 commit f8c50ea
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/flutter_tools/lib/src/context_runner.dart
Expand Up @@ -71,6 +71,7 @@ Future<T> runInContext<T>(
KernelCompiler: () => const KernelCompiler(),
Logger: () => platform.isWindows ? WindowsStdoutLogger() : StdoutLogger(),
OperatingSystemUtils: () => OperatingSystemUtils(),
PlistBuddy: () => const PlistBuddy(),
SimControl: () => SimControl(),
Stdio: () => const Stdio(),
Usage: () => Usage(),
Expand Down
98 changes: 97 additions & 1 deletion packages/flutter_tools/lib/src/ios/mac.dart
Expand Up @@ -32,9 +32,57 @@ const int kXcodeRequiredVersionMajor = 9;
const int kXcodeRequiredVersionMinor = 0;

IMobileDevice get iMobileDevice => context[IMobileDevice];

PlistBuddy get plistBuddy => context[PlistBuddy];
Xcode get xcode => context[Xcode];

class PlistBuddy {
const PlistBuddy();

static const String path = '/usr/libexec/PlistBuddy';

Future<ProcessResult> run(List<String> args) => processManager.run(<String>[path]..addAll(args));
}

/// A property list is a key-value representation commonly used for
/// configuration on macOS/iOS systems.
class PropertyList {
const PropertyList(this.plistPath);

final String plistPath;

/// Prints the specified key, or returns null if not present.
Future<String> read(String key) async {
final ProcessResult result = await _runCommand('Print $key');
if (result.exitCode == 0)
return result.stdout.trim();
return null;
}

/// Adds [key]. Has no effect if the key already exists.
Future<void> addString(String key, String value) async {
await _runCommand('Add $key string $value');
}

/// Updates [key] with the new [value]. Has no effect if the key does not exist.
Future<void> update(String key, String value) async {
await _runCommand('Set $key $value');
}

/// Deletes [key].
Future<void> delete(String key) async {
await _runCommand('Delete $key');
}

/// Deletes the content of the property list and creates a new root of the specified type.
Future<void> clearToDict() async {
await _runCommand('Clear dict');
}

Future<ProcessResult> _runCommand(String command) async {
return await plistBuddy.run(<String>['-c', command, plistPath]);
}
}

class IMobileDevice {
const IMobileDevice();

Expand Down Expand Up @@ -181,6 +229,47 @@ class Xcode {
}
}

/// Sets the Xcode system.
///
/// Xcode 10 added a new (default) build system with better performance and
/// stricter checks. Flutter apps without plugins build fine under the new
/// system, but it causes build breakages in projects with CocoaPods enabled.
/// This affects Flutter apps with plugins.
///
/// Once Flutter has been updated to be fully compliant with the new build
/// system, this can be removed.
//
// TODO(cbracken): remove when https://github.com/flutter/flutter/issues/20685 is fixed.
Future<void> setXcodeWorkspaceBuildSystem({
@required Directory workspaceDirectory,
@required File workspaceSettings,
@required bool modern,
}) async {
// If this isn't a workspace, we're not using CocoaPods and can use the new
// build system.
if (!workspaceDirectory.existsSync())
return;

final PropertyList plist = PropertyList(workspaceSettings.path);
if (!workspaceSettings.existsSync()) {
workspaceSettings.parent.createSync(recursive: true);
await plist.clearToDict();
}

const String kBuildSystemType = 'BuildSystemType';
if (modern) {
printTrace('Using new Xcode build system.');
await plist.delete(kBuildSystemType);
} else {
printTrace('Using legacy Xcode build system.');
if (await plist.read(kBuildSystemType) == null) {
await plist.addString(kBuildSystemType, 'Original');
} else {
await plist.update(kBuildSystemType, 'Original');
}
}
}

Future<XcodeBuildResult> buildXcodeProject({
BuildableIOSApp app,
BuildInfo buildInfo,
Expand All @@ -195,6 +284,13 @@ Future<XcodeBuildResult> buildXcodeProject({
if (!_checkXcodeVersion())
return XcodeBuildResult(success: false);

// TODO(cbracken) remove when https://github.com/flutter/flutter/issues/20685 is fixed.
await setXcodeWorkspaceBuildSystem(
workspaceDirectory: app.project.xcodeWorkspace,
workspaceSettings: app.project.xcodeWorkspaceSharedSettings,
modern: false,
);

final XcodeProjectInfo projectInfo = xcodeProjectInterpreter.getInfo(app.project.directory.path);
if (!projectInfo.targets.contains('Runner')) {
printError('The Xcode project does not define target "Runner" which is needed by Flutter tooling.');
Expand Down
9 changes: 9 additions & 0 deletions packages/flutter_tools/lib/src/project.dart
Expand Up @@ -186,6 +186,15 @@ class IosProject {
/// The '.pbxproj' file of the host app.
File get xcodeProjectInfoFile => xcodeProject.childFile('project.pbxproj');

/// Xcode workspace directory of the host app.
Directory get xcodeWorkspace => directory.childDirectory('$_hostAppBundleName.xcworkspace');

/// Xcode workspace shared data directory for the host app.
Directory get xcodeWorkspaceSharedData => xcodeWorkspace.childDirectory('xcshareddata');

/// Xcode workspace shared workspace settings file for the host app.
File get xcodeWorkspaceSharedSettings => xcodeWorkspaceSharedData.childFile('WorkspaceSettings.xcsettings');

/// The product bundle identifier of the host app, or null if not set or if
/// iOS tooling needed to read it is not installed.
String get productBundleIdentifier {
Expand Down
68 changes: 68 additions & 0 deletions packages/flutter_tools/test/ios/mac_test.dart
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart' show ProcessException, ProcessResult;
import 'package:flutter_tools/src/ios/mac.dart';
Expand All @@ -21,6 +22,73 @@ class MockFile extends Mock implements File {}
class MockXcodeProjectInterpreter extends Mock implements XcodeProjectInterpreter {}

void main() {
group('PropertyList', () {
MockProcessManager mockProcessManager;
MemoryFileSystem fs;
Directory workspaceDirectory;
File workspaceSettingsFile;

setUp(() {
mockProcessManager = MockProcessManager();
fs = MemoryFileSystem();
workspaceDirectory = fs.directory('Runner.xcworkspace');
workspaceSettingsFile = workspaceDirectory.childDirectory('xcshareddata').childFile('WorkspaceSettings.xcsettings');
});

testUsingContext('does nothing if workspace directory does not exist', () async {
await setXcodeWorkspaceBuildSystem(workspaceDirectory: workspaceDirectory, workspaceSettings: workspaceSettingsFile, modern: false);
verifyNever(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Print BuildSystemType', workspaceSettingsFile.path]));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
});

testUsingContext('creates dict-based plist if settings file does not exist', () async {
workspaceSettingsFile.parent.createSync(recursive: true);
when(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Print BuildSystemType', workspaceSettingsFile.path]))
.thenAnswer((_) => Future<ProcessResult>.value(ProcessResult(1, 1, '', '')));
await setXcodeWorkspaceBuildSystem(workspaceDirectory: workspaceDirectory, workspaceSettings: workspaceSettingsFile, modern: false);
verify(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Clear dict', workspaceSettingsFile.path]));
verify(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Add BuildSystemType string Original', workspaceSettingsFile.path]));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
});

testUsingContext('writes legacy build mode settings if requested and not present', () async {
workspaceSettingsFile.createSync(recursive: true);
when(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Print BuildSystemType', workspaceSettingsFile.path]))
.thenAnswer((_) => Future<ProcessResult>.value(ProcessResult(1, 1, '', '')));
await setXcodeWorkspaceBuildSystem(workspaceDirectory: workspaceDirectory, workspaceSettings: workspaceSettingsFile, modern: false);
verify(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Add BuildSystemType string Original', workspaceSettingsFile.path]));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
});

testUsingContext('updates legacy build mode setting if requested and existing setting is present', () async {
workspaceSettingsFile.createSync(recursive: true);
when(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Print BuildSystemType', workspaceSettingsFile.path]))
.thenAnswer((_) => Future<ProcessResult>.value(ProcessResult(1, 0, 'FancyNewOne', '')));
await setXcodeWorkspaceBuildSystem(workspaceDirectory: workspaceDirectory, workspaceSettings: workspaceSettingsFile, modern: false);
verify(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Set BuildSystemType Original', workspaceSettingsFile.path]));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
});

testUsingContext('deletes legacy build mode setting if modern build mode requested', () async {
workspaceSettingsFile.createSync(recursive: true);
when(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Print BuildSystemType', workspaceSettingsFile.path]))
.thenAnswer((_) => Future<ProcessResult>.value(ProcessResult(1, 0, 'Original', '')));
await setXcodeWorkspaceBuildSystem(workspaceDirectory: workspaceDirectory, workspaceSettings: workspaceSettingsFile, modern: true);
verify(mockProcessManager.run(<String>[PlistBuddy.path, '-c', 'Delete BuildSystemType', workspaceSettingsFile.path]));
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
});
});

group('IMobileDevice', () {
final FakePlatform osx = FakePlatform.fromPlatform(const LocalPlatform())
..operatingSystem = 'macos';
Expand Down

0 comments on commit f8c50ea

Please sign in to comment.