build_macos.dart 7.75 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import '../base/analyze_size.dart';
6
import '../base/common.dart';
7
import '../base/file_system.dart';
8
import '../base/logger.dart';
9
import '../base/project_migrator.dart';
10
import '../build_info.dart';
11
import '../convert.dart';
12
import '../globals.dart' as globals;
13
import '../ios/xcode_build_settings.dart';
14
import '../ios/xcodeproj.dart';
15 16
import '../migrations/xcode_project_object_version_migration.dart';
import '../migrations/xcode_script_build_phase_migration.dart';
17
import '../migrations/xcode_thin_binary_build_phase_input_paths_migration.dart';
18
import '../project.dart';
19
import 'cocoapod_utils.dart';
20
import 'migrations/macos_deployment_target_migration.dart';
21
import 'migrations/remove_macos_framework_link_and_embedding_migration.dart';
22

23
/// When run in -quiet mode, Xcode should only print from the underlying tasks to stdout.
24
/// Passing this regexp to trace moves the stdout output to stderr.
25 26 27 28 29
///
/// Filter out xcodebuild logging unrelated to macOS builds:
/// xcodebuild[2096:1927385] Requested but did not find extension point with identifier Xcode.IDEKit.ExtensionPointIdentifierToBundleIdentifier for extension Xcode.DebuggerFoundation.AppExtensionToBundleIdentifierMap.watchOS of plug-in com.apple.dt.IDEWatchSupportCore
/// note: Using new build system
final RegExp _filteredOutput = RegExp(r'^((?!Requested but did not find extension point with identifier|note\:).)*$');
30

31
/// Builds the macOS project through xcodebuild.
32
// TODO(zanderso): refactor to share code with the existing iOS code.
33
Future<void> buildMacOS({
34 35 36 37
  required FlutterProject flutterProject,
  required BuildInfo buildInfo,
  String? targetOverride,
  required bool verboseLogging,
38
  bool configOnly = false,
39
  SizeAnalyzer? sizeAnalyzer,
40
}) async {
41 42
  final Directory? xcodeWorkspace = flutterProject.macos.xcodeWorkspace;
  if (xcodeWorkspace == null) {
43
    throwToolExit('No macOS desktop project configured. '
44
      'See https://docs.flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-app '
45 46 47
      'to learn about adding macOS support to a project.');
  }

48 49 50 51 52 53
  final List<ProjectMigrator> migrators = <ProjectMigrator>[
    RemoveMacOSFrameworkLinkAndEmbeddingMigration(
      flutterProject.macos,
      globals.logger,
      globals.flutterUsage,
    ),
54
    MacOSDeploymentTargetMigration(flutterProject.macos, globals.logger),
55 56
    XcodeProjectObjectVersionMigration(flutterProject.macos, globals.logger),
    XcodeScriptBuildPhaseMigration(flutterProject.macos, globals.logger),
57
    XcodeThinBinaryBuildPhaseInputPathsMigration(flutterProject.macos, globals.logger),
58 59 60
  ];

  final ProjectMigration migration = ProjectMigration(migrators);
61
  migration.run();
62

63
  final Directory flutterBuildDir = globals.fs.directory(getMacOSBuildDirectory());
64 65 66
  if (!flutterBuildDir.existsSync()) {
    flutterBuildDir.createSync(recursive: true);
  }
67 68 69 70
  // Write configuration to an xconfig file in a standard location.
  await updateGeneratedXcodeProperties(
    project: flutterProject,
    buildInfo: buildInfo,
71
    targetOverride: targetOverride,
72 73
    useMacOSConfig: true,
  );
74
  await processPodsIfNeeded(flutterProject.macos, getMacOSBuildDirectory(), buildInfo.mode);
75 76
  // If the xcfilelists do not exist, create empty version.
  if (!flutterProject.macos.inputFileList.existsSync()) {
77
    flutterProject.macos.inputFileList.createSync(recursive: true);
78 79 80 81
  }
  if (!flutterProject.macos.outputFileList.existsSync()) {
    flutterProject.macos.outputFileList.createSync(recursive: true);
  }
82 83 84
  if (configOnly) {
    return;
  }
85

86
  final Directory xcodeProject = flutterProject.macos.xcodeProject;
87

88 89 90
  // If the standard project exists, specify it to getInfo to handle the case where there are
  // other Xcode projects in the macos/ directory. Otherwise pass no name, which will work
  // regardless of the project name so long as there is exactly one project.
91
  final String? xcodeProjectName = xcodeProject.existsSync() ? xcodeProject.basename : null;
92
  final XcodeProjectInfo? projectInfo = await globals.xcodeProjectInterpreter?.getInfo(
93
    xcodeProject.parent.path,
94
    projectFilename: xcodeProjectName,
95
  );
96
  final String? scheme = projectInfo?.schemeFor(buildInfo);
97
  if (scheme == null) {
98
    projectInfo!.reportFlavorNotFoundAndExit();
99
  }
100
  final String? configuration = projectInfo?.buildConfigurationFor(buildInfo, scheme);
101 102
  if (configuration == null) {
    throwToolExit('Unable to find expected configuration in Xcode project.');
103
  }
104
  // Run the Xcode build.
105
  final Stopwatch sw = Stopwatch()..start();
106
  final Status status = globals.logger.startProgress(
107 108 109 110
    'Building macOS application...',
  );
  int result;
  try {
111
    result = await globals.processUtils.stream(<String>[
112 113 114
      '/usr/bin/env',
      'xcrun',
      'xcodebuild',
115
      '-workspace', xcodeWorkspace.path,
116
      '-configuration', configuration,
117
      '-scheme', scheme,
118
      '-derivedDataPath', flutterBuildDir.absolute.path,
119
      '-destination', 'platform=macOS',
120 121
      'OBJROOT=${globals.fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Intermediates.noindex')}',
      'SYMROOT=${globals.fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Products')}',
122
      if (verboseLogging)
123 124 125
        'VERBOSE_SCRIPT_LOGGING=YES'
      else
        '-quiet',
126
      'COMPILER_INDEX_STORE_ENABLE=NO',
127
      ...environmentVariablesAsXcodeBuildSettings(globals.platform),
128 129
    ],
    trace: true,
130 131
    stdoutErrorMatcher: verboseLogging ? null : _filteredOutput,
    mapFunction: verboseLogging ? null : (String line) => _filteredOutput.hasMatch(line) ? line : null,
132
  );
133 134 135 136 137 138
  } finally {
    status.cancel();
  }
  if (result != 0) {
    throwToolExit('Build process failed');
  }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
    final String arch = getNameForDarwinArch(DarwinArch.x86_64);
    final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('snapshot.$arch.json');
    final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('trace.$arch.json');

    // This analysis is only supported for release builds.
    // Attempt to guess the correct .app by picking the first one.
    final Directory candidateDirectory = globals.fs.directory(
      globals.fs.path.join(getMacOSBuildDirectory(), 'Build', 'Products', 'Release'),
    );
    final Directory appDirectory = candidateDirectory.listSync()
      .whereType<Directory>()
      .firstWhere((Directory directory) {
      return globals.fs.path.extension(directory.path) == '.app';
    });
156
    final Map<String, Object?> output = await sizeAnalyzer.analyzeAotSnapshot(
157 158 159 160 161 162 163
      aotSnapshot: aotSnapshot,
      precompilerTrace: precompilerTrace,
      outputDirectory: appDirectory,
      type: 'macos',
      excludePath: 'Versions', // Avoid double counting caused by symlinks
    );
    final File outputFile = globals.fsUtils.getUniqueFile(
164 165 166
      globals.fs
        .directory(globals.fsUtils.homeDirPath)
        .childDirectory('.flutter-devtools'), 'macos-code-size-analysis', 'json',
167 168 169 170 171
    )..writeAsStringSync(jsonEncode(output));
    // This message is used as a sentinel in analyze_apk_size_test.dart
    globals.printStatus(
      'A summary of your macOS bundle analysis can be found at: ${outputFile.path}',
    );
172 173 174 175 176 177 178 179

    // DevTools expects a file path relative to the .flutter-devtools/ dir.
    final String relativeAppSizePath = outputFile.path.split('.flutter-devtools/').last.trim();
    globals.printStatus(
      '\nTo analyze your app size in Dart DevTools, run the following command:\n'
      'flutter pub global activate devtools; flutter pub global run devtools '
      '--appSizeBase=$relativeAppSizePath'
    );
180
  }
181
  globals.flutterUsage.sendTiming('build', 'xcode-macos', Duration(milliseconds: sw.elapsedMilliseconds));
182
}