build_macos.dart 9.46 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 6
import 'package:unified_analytics/unified_analytics.dart';

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

26
/// When run in -quiet mode, Xcode should only print from the underlying tasks to stdout.
27
/// Passing this regexp to trace moves the stdout output to stderr.
28 29
///
/// Filter out xcodebuild logging unrelated to macOS builds:
30
/// ```
31
/// 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
32
///
33
/// note: Using new build system
34 35 36 37 38 39 40
///
/// xcodebuild[61115:1017566] [MT] DVTAssertions: Warning in /System/Volumes/Data/SWE/Apps/DT/BuildRoots/BuildRoot11/ActiveBuildRoot/Library/Caches/com.apple.xbs/Sources/IDEFrameworks/IDEFrameworks-22267/IDEFoundation/Provisioning/Capabilities Infrastructure/IDECapabilityQuerySelection.swift:103
/// Details:  createItemModels creation requirements should not create capability item model for a capability item model that already exists.
/// Function: createItemModels(for:itemModelSource:)
/// Thread:   <_NSMainThread: 0x6000027c0280>{number = 1, name = main}
/// Please file a bug at https://feedbackassistant.apple.com with this warning message and any useful information you can provide.

41
/// ```
42 43 44 45 46 47 48 49 50 51 52
final RegExp _filteredOutput = RegExp(
  r'^((?!'
  r'Requested but did not find extension point with identifier|'
  r'note\:|'
  r'\[MT\] DVTAssertions: Warning in /System/Volumes/Data/SWE/|'
  r'Details\:  createItemModels|'
  r'Function\: createItemModels|'
  r'Thread\:   <_NSMainThread\:|'
  r'Please file a bug at https\://feedbackassistant\.apple\.'
  r').)*$'
  );
53

54
/// Builds the macOS project through xcodebuild.
55
// TODO(zanderso): refactor to share code with the existing iOS code.
56
Future<void> buildMacOS({
57 58 59 60
  required FlutterProject flutterProject,
  required BuildInfo buildInfo,
  String? targetOverride,
  required bool verboseLogging,
61
  bool configOnly = false,
62
  SizeAnalyzer? sizeAnalyzer,
63
}) async {
64 65
  final Directory? xcodeWorkspace = flutterProject.macos.xcodeWorkspace;
  if (xcodeWorkspace == null) {
66
    throwToolExit('No macOS desktop project configured. '
67
      'See https://docs.flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-app '
68 69 70
      'to learn about adding macOS support to a project.');
  }

71 72 73 74 75
  final List<ProjectMigrator> migrators = <ProjectMigrator>[
    RemoveMacOSFrameworkLinkAndEmbeddingMigration(
      flutterProject.macos,
      globals.logger,
      globals.flutterUsage,
76
      globals.analytics,
77
    ),
78
    MacOSDeploymentTargetMigration(flutterProject.macos, globals.logger),
79 80
    XcodeProjectObjectVersionMigration(flutterProject.macos, globals.logger),
    XcodeScriptBuildPhaseMigration(flutterProject.macos, globals.logger),
81
    XcodeThinBinaryBuildPhaseInputPathsMigration(flutterProject.macos, globals.logger),
82
    FlutterApplicationMigration(flutterProject.macos, globals.logger),
83 84 85
  ];

  final ProjectMigration migration = ProjectMigration(migrators);
86
  migration.run();
87

88
  final Directory flutterBuildDir = globals.fs.directory(getMacOSBuildDirectory());
89 90 91
  if (!flutterBuildDir.existsSync()) {
    flutterBuildDir.createSync(recursive: true);
  }
92 93 94 95
  // Write configuration to an xconfig file in a standard location.
  await updateGeneratedXcodeProperties(
    project: flutterProject,
    buildInfo: buildInfo,
96
    targetOverride: targetOverride,
97 98
    useMacOSConfig: true,
  );
99
  await processPodsIfNeeded(flutterProject.macos, getMacOSBuildDirectory(), buildInfo.mode);
100 101
  // If the xcfilelists do not exist, create empty version.
  if (!flutterProject.macos.inputFileList.existsSync()) {
102
    flutterProject.macos.inputFileList.createSync(recursive: true);
103 104 105 106
  }
  if (!flutterProject.macos.outputFileList.existsSync()) {
    flutterProject.macos.outputFileList.createSync(recursive: true);
  }
107 108 109
  if (configOnly) {
    return;
  }
110

111
  final Directory xcodeProject = flutterProject.macos.xcodeProject;
112

113 114 115
  // 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.
116
  final String? xcodeProjectName = xcodeProject.existsSync() ? xcodeProject.basename : null;
117
  final XcodeProjectInfo? projectInfo = await globals.xcodeProjectInterpreter?.getInfo(
118
    xcodeProject.parent.path,
119
    projectFilename: xcodeProjectName,
120
  );
121
  final String? scheme = projectInfo?.schemeFor(buildInfo);
122
  if (scheme == null) {
123
    projectInfo!.reportFlavorNotFoundAndExit();
124
  }
125
  final String? configuration = projectInfo?.buildConfigurationFor(buildInfo, scheme);
126 127
  if (configuration == null) {
    throwToolExit('Unable to find expected configuration in Xcode project.');
128
  }
129
  // Run the Xcode build.
130
  final Stopwatch sw = Stopwatch()..start();
131
  final Status status = globals.logger.startProgress(
132 133 134 135
    'Building macOS application...',
  );
  int result;
  try {
136
    result = await globals.processUtils.stream(<String>[
137 138 139
      '/usr/bin/env',
      'xcrun',
      'xcodebuild',
140
      '-workspace', xcodeWorkspace.path,
141
      '-configuration', configuration,
142
      '-scheme', scheme,
143
      '-derivedDataPath', flutterBuildDir.absolute.path,
144
      '-destination', 'platform=macOS',
145 146
      'OBJROOT=${globals.fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Intermediates.noindex')}',
      'SYMROOT=${globals.fs.path.join(flutterBuildDir.absolute.path, 'Build', 'Products')}',
147
      if (verboseLogging)
148 149 150
        'VERBOSE_SCRIPT_LOGGING=YES'
      else
        '-quiet',
151
      'COMPILER_INDEX_STORE_ENABLE=NO',
152
      ...environmentVariablesAsXcodeBuildSettings(globals.platform),
153 154
    ],
    trace: true,
155 156
    stdoutErrorMatcher: verboseLogging ? null : _filteredOutput,
    mapFunction: verboseLogging ? null : (String line) => _filteredOutput.hasMatch(line) ? line : null,
157
  );
158 159 160 161 162 163
  } finally {
    status.cancel();
  }
  if (result != 0) {
    throwToolExit('Build process failed');
  }
164
  await _writeCodeSizeAnalysis(buildInfo, sizeAnalyzer);
165 166 167 168 169 170 171
  final Duration elapsedDuration = sw.elapsed;
  globals.flutterUsage.sendTiming('build', 'xcode-macos', elapsedDuration);
  globals.analytics.send(Event.timing(
    workflow: 'build',
    variableName: 'xcode-macos',
    elapsedMilliseconds: elapsedDuration.inMilliseconds,
  ));
172
}
173

174 175 176 177 178 179 180 181 182
/// Performs a size analysis of the AOT snapshot and writes to an analysis file, if configured.
///
/// Size analysis will be run for release builds where the --analyze-size
/// option has been specified. By default, size analysis JSON output is written
/// to ~/.flutter-devtools/macos-code-size-analysis_NN.json.
Future<void> _writeCodeSizeAnalysis(BuildInfo buildInfo, SizeAnalyzer? sizeAnalyzer) async {
  // Bail out if the size analysis option was not specified.
  if (buildInfo.codeSizeDirectory == null || sizeAnalyzer == null) {
    return;
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
  final String arch = DarwinArch.x86_64.name;
  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';
  });
  final Map<String, Object?> output = await sizeAnalyzer.analyzeAotSnapshot(
    aotSnapshot: aotSnapshot,
    precompilerTrace: precompilerTrace,
    outputDirectory: appDirectory,
    type: 'macos',
    excludePath: 'Versions', // Avoid double counting caused by symlinks
  );
  final File outputFile = globals.fsUtils.getUniqueFile(
    globals.fs
      .directory(globals.fsUtils.homeDirPath)
      .childDirectory('.flutter-devtools'), 'macos-code-size-analysis', 'json',
  )..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}',
  );

  // 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'
    'dart devtools --appSizeBase=$relativeAppSizePath'
  );
223
}