build.dart 10.5 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 'package:process/process.dart';
6

7
import '../artifacts.dart';
8
import '../build_info.dart';
9
import '../macos/xcode.dart';
10

11
import 'file_system.dart';
12
import 'logger.dart';
13 14 15 16
import 'process.dart';

/// A snapshot build configuration.
class SnapshotType {
17 18
  SnapshotType(this.platform, this.mode)
    : assert(mode != null);
19

20
  final TargetPlatform? platform;
21
  final BuildMode mode;
22 23 24

  @override
  String toString() => '$platform $mode';
25 26 27 28
}

/// Interface to the gen_snapshot command-line tool.
class GenSnapshot {
29
  GenSnapshot({
30 31 32
    required Artifacts artifacts,
    required ProcessManager processManager,
    required Logger logger,
33 34 35 36 37 38 39 40
  }) : _artifacts = artifacts,
       _processUtils = ProcessUtils(logger: logger, processManager: processManager);

  final Artifacts _artifacts;
  final ProcessUtils _processUtils;

  String getSnapshotterPath(SnapshotType snapshotType) {
    return _artifacts.getArtifactPath(
41
        Artifact.genSnapshot, platform: snapshotType.platform, mode: snapshotType.mode);
42 43
  }

44 45 46 47 48 49 50 51 52 53
  /// Ignored warning messages from gen_snapshot.
  static const Set<String> kIgnoredWarnings = <String>{
    // --strip on elf snapshot.
    'Warning: Generating ELF library without DWARF debugging information.',
    // --strip on ios-assembly snapshot.
    'Warning: Generating assembly code without DWARF debugging information.',
    // A fun two-part message with spaces for obfuscation.
    'Warning: This VM has been configured to obfuscate symbol information which violates the Dart standard.',
    '         See dartbug.com/30524 for more information.',
  };
54

55
  Future<int> run({
56 57
    required SnapshotType snapshotType,
    DarwinArch? darwinArch,
58
    Iterable<String> additionalArgs = const <String>[],
59
  }) {
60
    assert(snapshotType.platform != TargetPlatform.ios || darwinArch != null);
61
    final List<String> args = <String>[
62 63
      ...additionalArgs,
    ];
64

65
    String snapshotterPath = getSnapshotterPath(snapshotType);
66

67 68
    // iOS has a separate gen_snapshot for armv7 and arm64 in the same,
    // directory. So we need to select the right one.
69
    if (snapshotType.platform == TargetPlatform.ios) {
70
      snapshotterPath += '_${getNameForDarwinArch(darwinArch!)}';
71 72
    }

73
    return _processUtils.stream(
74
      <String>[snapshotterPath, ...args],
75
      mapFunction: (String line) =>  kIgnoredWarnings.contains(line) ? null : line,
76
    );
77 78
  }
}
79

80
class AOTSnapshotter {
81 82
  AOTSnapshotter({
    this.reportTimings = false,
83 84 85 86 87
    required Logger logger,
    required FileSystem fileSystem,
    required Xcode xcode,
    required ProcessManager processManager,
    required Artifacts artifacts,
88 89 90 91 92 93 94 95 96 97 98 99 100
  }) : _logger = logger,
      _fileSystem = fileSystem,
      _xcode = xcode,
      _genSnapshot = GenSnapshot(
        artifacts: artifacts,
        processManager: processManager,
        logger: logger,
      );

  final Logger _logger;
  final FileSystem _fileSystem;
  final Xcode _xcode;
  final GenSnapshot _genSnapshot;
101 102 103 104 105 106

  /// If true then AOTSnapshotter would report timings for individual building
  /// steps (Dart front-end parsing and snapshot generation) in a stable
  /// machine readable form. See [AOTSnapshotter._timedStep].
  final bool reportTimings;

107
  /// Builds an architecture-specific ahead-of-time compiled snapshot of the specified script.
108
  Future<int> build({
109 110 111 112 113 114
    required TargetPlatform platform,
    required BuildMode buildMode,
    required String mainPath,
    required String outputPath,
    DarwinArch? darwinArch,
    String? sdkRoot,
115
    List<String> extraGenSnapshotOptions = const <String>[],
116 117 118
    required bool bitcode,
    String? splitDebugInfo,
    required bool dartObfuscation,
xster's avatar
xster committed
119
    bool quiet = false,
120
  }) async {
121
    assert(platform != TargetPlatform.ios || darwinArch != null);
122
    if (bitcode && platform != TargetPlatform.ios) {
123
      _logger.printError('Bitcode is only supported for iOS.');
124 125 126
      return 1;
    }

127
    if (!_isValidAotPlatform(platform, buildMode)) {
128
      _logger.printError('${getNameForTargetPlatform(platform)} does not support AOT compilation.');
129
      return 1;
130 131
    }

132
    final Directory outputDir = _fileSystem.directory(outputPath);
133 134
    outputDir.createSync(recursive: true);

135
    final List<String> genSnapshotArgs = <String>[
136
      '--deterministic',
137
    ];
138 139 140 141 142

    // We strip snapshot by default, but allow to suppress this behavior
    // by supplying --no-strip in extraGenSnapshotOptions.
    bool shouldStrip = true;

143
    if (extraGenSnapshotOptions != null && extraGenSnapshotOptions.isNotEmpty) {
144
      _logger.printTrace('Extra gen_snapshot options: $extraGenSnapshotOptions');
145 146 147 148 149 150 151
      for (final String option in extraGenSnapshotOptions) {
        if (option == '--no-strip') {
          shouldStrip = false;
          continue;
        }
        genSnapshotArgs.add(option);
      }
152 153
    }

154
    final String assembly = _fileSystem.path.join(outputDir.path, 'snapshot_assembly.S');
155
    if (platform == TargetPlatform.ios || platform == TargetPlatform.darwin) {
156 157 158 159
      genSnapshotArgs.addAll(<String>[
        '--snapshot_kind=app-aot-assembly',
        '--assembly=$assembly',
      ]);
160
    } else {
161 162 163 164 165
      final String aotSharedLibrary = _fileSystem.path.join(outputDir.path, 'app.so');
      genSnapshotArgs.addAll(<String>[
        '--snapshot_kind=app-aot-elf',
        '--elf=$aotSharedLibrary',
      ]);
166 167
    }

168 169 170 171
    if (shouldStrip) {
      genSnapshotArgs.add('--strip');
    }

172
    if (platform == TargetPlatform.android_arm || darwinArch == DarwinArch.armv7) {
173
      // Use softfp for Android armv7 devices.
Ian Hickson's avatar
Ian Hickson committed
174
      // This is the default for armv7 iOS builds, but harmless to set.
175
      // TODO(cbracken): eliminate this when we fix https://github.com/flutter/flutter/issues/17489
176 177
      genSnapshotArgs.add('--no-sim-use-hardfp');

178 179
      // Not supported by the Pixel in 32-bit mode.
      genSnapshotArgs.add('--no-use-integer-division');
180 181
    }

182
    // The name of the debug file must contain additional information about
183 184 185 186
    // the architecture, since a single build command may produce
    // multiple debug files.
    final String archName = getNameForTargetPlatform(platform, darwinArch: darwinArch);
    final String debugFilename = 'app.$archName.symbols';
187 188
    final bool shouldSplitDebugInfo = splitDebugInfo?.isNotEmpty ?? false;
    if (shouldSplitDebugInfo) {
189
      _fileSystem.directory(splitDebugInfo)
190 191 192
        .createSync(recursive: true);
    }

193 194 195
    // Optimization arguments.
    genSnapshotArgs.addAll(<String>[
      // Faster async/await
196
      if (shouldSplitDebugInfo) ...<String>[
197
        '--dwarf-stack-traces',
198
        '--save-debugging-info=${_fileSystem.path.join(splitDebugInfo!, debugFilename)}'
199 200 201
      ],
      if (dartObfuscation)
        '--obfuscate',
202 203
    ]);

204
    genSnapshotArgs.add(mainPath);
205

206
    final SnapshotType snapshotType = SnapshotType(platform, buildMode);
207
    final int genSnapshotExitCode = await _genSnapshot.run(
208
      snapshotType: snapshotType,
209
      additionalArgs: genSnapshotArgs,
210
      darwinArch: darwinArch,
211
    );
212
    if (genSnapshotExitCode != 0) {
213
      _logger.printError('Dart snapshot generator failed with exit code $genSnapshotExitCode');
214
      return genSnapshotExitCode;
215 216
    }

217
    // On iOS and macOS, we use Xcode to compile the snapshot into a dynamic library that the
218
    // end-developer can link into their app.
219
    if (platform == TargetPlatform.ios || platform == TargetPlatform.darwin) {
220
      final RunResult result = await _buildFramework(
221
        appleArch: darwinArch!,
222
        isIOS: platform == TargetPlatform.ios,
223
        sdkRoot: sdkRoot,
224
        assemblyPath: assembly,
225 226
        outputPath: outputDir.path,
        bitcode: bitcode,
xster's avatar
xster committed
227
        quiet: quiet,
228
      );
229
      if (result.exitCode != 0) {
230
        return result.exitCode;
231
      }
232 233 234 235
    }
    return 0;
  }

236
  /// Builds an iOS or macOS framework at [outputPath]/App.framework from the assembly
237
  /// source at [assemblyPath].
238
  Future<RunResult> _buildFramework({
239 240 241 242 243 244 245
    required DarwinArch appleArch,
    required bool isIOS,
    String? sdkRoot,
    required String assemblyPath,
    required String outputPath,
    required bool bitcode,
    required bool quiet
246
  }) async {
247
    final String targetArch = getNameForDarwinArch(appleArch);
xster's avatar
xster committed
248
    if (!quiet) {
249
      _logger.printStatus('Building App.framework for $targetArch...');
xster's avatar
xster committed
250
    }
251

252
    final List<String> commonBuildOptions = <String>[
253 254
      '-arch',
      targetArch,
255
      if (isIOS)
256
        // When the minimum version is updated, remember to update
257
        // template MinimumOSVersion.
258
        // https://github.com/flutter/flutter/pull/62902
259
        '-miphoneos-version-min=9.0',
260 261 262 263
      if (sdkRoot != null) ...<String>[
        '-isysroot',
        sdkRoot,
      ],
264
    ];
265

266
    const String embedBitcodeArg = '-fembed-bitcode';
267
    final String assemblyO = _fileSystem.path.join(outputPath, 'snapshot_assembly.o');
268

269
    final RunResult compileResult = await _xcode.cc(<String>[
270
      ...commonBuildOptions,
271
      if (bitcode) embedBitcodeArg,
272 273 274 275 276
      '-c',
      assemblyPath,
      '-o',
      assemblyO,
    ]);
277
    if (compileResult.exitCode != 0) {
278
      _logger.printError('Failed to compile AOT snapshot. Compiler terminated with exit code ${compileResult.exitCode}');
279
      return compileResult;
280
    }
281

282 283 284
    final String frameworkDir = _fileSystem.path.join(outputPath, 'App.framework');
    _fileSystem.directory(frameworkDir).createSync(recursive: true);
    final String appLib = _fileSystem.path.join(frameworkDir, 'App');
285 286 287 288 289 290
    final List<String> linkArgs = <String>[
      ...commonBuildOptions,
      '-dynamiclib',
      '-Xlinker', '-rpath', '-Xlinker', '@executable_path/Frameworks',
      '-Xlinker', '-rpath', '-Xlinker', '@loader_path/Frameworks',
      '-install_name', '@rpath/App.framework/App',
291
      if (bitcode) embedBitcodeArg,
292 293 294
      '-o', appLib,
      assemblyO,
    ];
295
    final RunResult linkResult = await _xcode.clang(linkArgs);
296
    if (linkResult.exitCode != 0) {
297
      _logger.printError('Failed to link AOT snapshot. Linker terminated with exit code ${compileResult.exitCode}');
298
    }
299
    return linkResult;
300 301
  }

302
  bool _isValidAotPlatform(TargetPlatform platform, BuildMode buildMode) {
303
    if (buildMode == BuildMode.debug) {
304
      return false;
305
    }
306 307 308
    return const <TargetPlatform>[
      TargetPlatform.android_arm,
      TargetPlatform.android_arm64,
309
      TargetPlatform.android_x64,
310
      TargetPlatform.ios,
311
      TargetPlatform.darwin,
312
      TargetPlatform.linux_x64,
313
      TargetPlatform.linux_arm64,
314
      TargetPlatform.windows_x64,
315
      TargetPlatform.windows_uwp_x64,
316 317
    ].contains(platform);
  }
318
}