mac.dart 7.36 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9 10 11 12
import 'dart:async';
import 'dart:convert' show JSON;
import 'dart:io';

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

import '../application_package.dart';
import '../artifacts.dart';
13 14
import '../base/context.dart';
import '../base/process.dart';
15 16 17 18 19 20
import '../globals.dart';
import '../services.dart';
import 'setup_xcodeproj.dart';

String get homeDirectory => path.absolute(Platform.environment['HOME']);

21
const int kXcodeRequiredVersionMajor = 7;
22
const int kXcodeRequiredVersionMinor = 0;
23

24
class XCode {
25 26
  /// Returns [XCode] active in the current app context.
  static XCode get instance => context[XCode] ?? (context[XCode] = new XCode());
27

28 29 30 31 32 33 34 35 36 37 38 39
  bool get isInstalledAndMeetsVersionCheck => isInstalled && xcodeVersionSatisfactory;

  bool _isInstalled;
  bool get isInstalled {
    if (_isInstalled != null) {
      return _isInstalled;
    }

    _isInstalled = exitsHappy(<String>['xcode-select', '--print-path']);
    return _isInstalled;
  }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  /// Has the EULA been signed?
  bool get eulaSigned {
    if (!isInstalled)
      return false;

    try {
      ProcessResult result = Process.runSync('/usr/bin/xcrun', <String>['clang']);
      if (result.stdout != null && result.stdout.contains('license'))
        return false;
      if (result.stderr != null && result.stderr.contains('license'))
        return false;
      return true;
    } catch (error) {
      return false;
    }
  }

57 58
  bool _xcodeVersionSatisfactory;
  bool get xcodeVersionSatisfactory {
59
    if (_xcodeVersionSatisfactory != null)
60 61 62 63 64 65 66 67 68 69 70 71
      return _xcodeVersionSatisfactory;

    try {
      String output = runSync(<String>['xcodebuild', '-version']);
      RegExp regex = new RegExp(r'Xcode ([0-9.]+)');

      String version = regex.firstMatch(output).group(1);
      List<String> components = version.split('.');

      int major = int.parse(components[0]);
      int minor = components.length == 1 ? 0 : int.parse(components[1]);

72
      _xcodeVersionSatisfactory = _xcodeVersionCheckValid(major, minor);
73 74 75 76
    } catch (error) {
      _xcodeVersionSatisfactory = false;
    }

77
    return _xcodeVersionSatisfactory;
78
  }
79
}
80

81 82 83 84 85 86 87 88 89 90
bool _xcodeVersionCheckValid(int major, int minor) {
  if (major > kXcodeRequiredVersionMajor)
    return true;

  if (major == kXcodeRequiredVersionMajor)
    return minor >= kXcodeRequiredVersionMinor;

  return false;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
Future<bool> buildIOSXcodeProject(ApplicationPackage app, { bool buildForDevice }) async {
  String flutterProjectPath = Directory.current.path;

  if (xcodeProjectRequiresUpdate()) {
    printTrace('Initializing the Xcode project.');
    if ((await setupXcodeProjectHarness(flutterProjectPath)) != 0) {
      printError('Could not initialize the Xcode project.');
      return false;
    }
  } else {
   updateXcodeLocalProperties(flutterProjectPath);
  }

  if (!_validateEngineRevision(app))
    return false;

  if (!_checkXcodeVersion())
    return false;

  // Before the build, all service definitions must be updated and the dylibs
  // copied over to a location that is suitable for Xcodebuild to find them.

  await _addServicesToBundle(new Directory(app.localPath));

  List<String> commands = <String>[
    '/usr/bin/env', 'xcrun', 'xcodebuild', '-target', 'Runner', '-configuration', 'Release'
  ];

  if (buildForDevice) {
    commands.addAll(<String>['-sdk', 'iphoneos', '-arch', 'arm64']);
  } else {
    commands.addAll(<String>['-sdk', 'iphonesimulator', '-arch', 'x86_64']);
  }

125 126 127 128 129 130 131 132 133 134 135
  printTrace(commands.join(' '));

  ProcessResult result = Process.runSync(
    commands.first, commands.sublist(1), workingDirectory: app.localPath
  );

  if (result.exitCode != 0) {
    if (result.stderr.isNotEmpty)
      printStatus(result.stderr);
    if (result.stdout.isNotEmpty)
      printStatus(result.stdout);
136
  }
137 138

  return result.exitCode == 0;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

final RegExp _xcodeVersionRegExp = new RegExp(r'Xcode (\d+)\..*');
final String _xcodeRequirement = 'Xcode 7.0 or greater is required to develop for iOS.';

bool _checkXcodeVersion() {
  if (!Platform.isMacOS)
    return false;
  try {
    String version = runCheckedSync(<String>['xcodebuild', '-version']);
    Match match = _xcodeVersionRegExp.firstMatch(version);
    if (int.parse(match[1]) < 7) {
      printError('Found "${match[0]}". $_xcodeRequirement');
      return false;
    }
  } catch (e) {
    printError('Cannot find "xcodebuid". $_xcodeRequirement');
    return false;
  }
  return true;
}

bool _validateEngineRevision(ApplicationPackage app) {
  String skyRevision = ArtifactStore.engineRevision;
  String iosRevision = _getIOSEngineRevision(app);

  if (iosRevision != skyRevision) {
    printError("Error: incompatible sky_engine revision.");
    printStatus('sky_engine revision: $skyRevision, iOS engine revision: $iosRevision');
    return false;
  } else {
    printTrace('sky_engine revision: $skyRevision, iOS engine revision: $iosRevision');
    return true;
  }
}

String _getIOSEngineRevision(ApplicationPackage app) {
  File revisionFile = new File(path.join(app.localPath, 'REVISION'));
  if (revisionFile.existsSync()) {
    return revisionFile.readAsStringSync().trim();
  } else {
    return null;
  }
}

Future _addServicesToBundle(Directory bundle) async {
  List<Map<String, String>> services = [];
  printTrace("Trying to resolve native pub services.");

  // Step 1: Parse the service configuration yaml files present in the service
  //         pub packages.
  await parseServiceConfigs(services);
  printTrace("Found ${services.length} service definition(s).");

  // Step 2: Copy framework dylibs to the correct spot for xcodebuild to pick up.
  Directory frameworksDirectory = new Directory(path.join(bundle.path, "Frameworks"));
  await _copyServiceFrameworks(services, frameworksDirectory);

  // Step 3: Copy the service definitions manifest at the correct spot for
  //         xcodebuild to pick up.
  File manifestFile = new File(path.join(bundle.path, "ServiceDefinitions.json"));
  _copyServiceDefinitionsManifest(services, manifestFile);
}

Future _copyServiceFrameworks(List<Map<String, String>> services, Directory frameworksDirectory) async {
  printTrace("Copying service frameworks to '${path.absolute(frameworksDirectory.path)}'.");
  frameworksDirectory.createSync(recursive: true);
  for (Map<String, String> service in services) {
    String dylibPath = await getServiceFromUrl(service['ios-framework'], service['root'], service['name']);
    File dylib = new File(dylibPath);
    printTrace("Copying ${dylib.path} into bundle.");
    if (!dylib.existsSync()) {
      printError("The service dylib '${dylib.path}' does not exist.");
      continue;
    }
    // Shell out so permissions on the dylib are preserved.
    runCheckedSync(['/bin/cp', dylib.path, frameworksDirectory.path]);
  }
}

void _copyServiceDefinitionsManifest(List<Map<String, String>> services, File manifest) {
  printTrace("Creating service definitions manifest at '${manifest.path}'");
  List<Map<String, String>> jsonServices = services.map((Map<String, String> service) => {
    'name': service['name'],
    // Since we have already moved it to the Frameworks directory. Strip away
    // the directory and basenames.
    'framework': path.basenameWithoutExtension(service['ios-framework'])
  }).toList();
  Map<String, dynamic> json = { 'services' : jsonServices };
  manifest.writeAsStringSync(JSON.encode(json), mode: FileMode.WRITE, flush: true);
}