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

import '../../artifacts.dart';
import '../../base/build.dart';
import '../../base/file_system.dart';
import '../../build_info.dart';
import '../../compile.dart';
10
import '../../convert.dart';
11
import '../../globals.dart' as globals;
12 13
import '../../project.dart';
import '../build_system.dart';
14
import '../depfile.dart';
15
import '../exceptions.dart';
16
import 'assets.dart';
17
import 'icon_tree_shaker.dart';
18 19 20 21 22 23 24 25 26 27

/// The define to pass a [BuildMode].
const String kBuildMode= 'BuildMode';

/// The define to pass whether we compile 64-bit android-arm code.
const String kTargetPlatform = 'TargetPlatform';

/// The define to control what target file is used.
const String kTargetFile = 'TargetFile';

28 29 30
/// The define to control whether the AOT snapshot is built with bitcode.
const String kBitcodeFlag = 'EnableBitcode';

31 32 33
/// Whether to enable or disable track widget creation.
const String kTrackWidgetCreation = 'TrackWidgetCreation';

34 35 36 37 38 39 40 41 42 43
/// Additional configuration passed to the dart front end.
///
/// This is expected to be a comma separated list of strings.
const String kExtraFrontEndOptions = 'ExtraFrontEndOptions';

/// Additional configuration passed to gen_snapshot.
///
/// This is expected to be a comma separated list of strings.
const String kExtraGenSnapshotOptions = 'ExtraGenSnapshotOptions';

44 45 46
/// Whether to strip source code information out of release builds and where to save it.
const String kSplitDebugInfo = 'SplitDebugInfo';

47 48
/// Alternative scheme for file URIs.
///
49
/// May be used along with [kFileSystemRoots] to support a multi-root
50 51 52 53 54 55 56 57
/// filesystem.
const String kFileSystemScheme = 'FileSystemScheme';

/// Additional filesystem roots.
///
/// If provided, must be used along with [kFileSystemScheme].
const String kFileSystemRoots = 'FileSystemRoots';

58 59 60
/// Defines specified via the `--dart-define` command-line option.
const String kDartDefines = 'DartDefines';

61 62 63 64 65 66 67 68
/// The define to control what iOS architectures are built for.
///
/// This is expected to be a comma-separated list of architectures. If not
/// provided, defaults to arm64.
///
/// The other supported value is armv7, the 32-bit iOS architecture.
const String kIosArchs = 'IosArchs';

69 70 71
/// Whether to enable Dart obfuscation and where to save the symbol map.
const String kDartObfuscation = 'DartObfuscation';

72
/// Copies the pre-built flutter bundle.
73 74 75 76 77 78 79 80 81 82 83 84
// This is a one-off rule for implementing build bundle in terms of assemble.
class CopyFlutterBundle extends Target {
  const CopyFlutterBundle();

  @override
  String get name => 'copy_flutter_bundle';

  @override
  List<Source> get inputs => const <Source>[
    Source.artifact(Artifact.vmSnapshotData, mode: BuildMode.debug),
    Source.artifact(Artifact.isolateSnapshotData, mode: BuildMode.debug),
    Source.pattern('{BUILD_DIR}/app.dill'),
85
    ...IconTreeShaker.inputs,
86 87 88 89 90 91 92
  ];

  @override
  List<Source> get outputs => const <Source>[
    Source.pattern('{OUTPUT_DIR}/vm_snapshot_data'),
    Source.pattern('{OUTPUT_DIR}/isolate_snapshot_data'),
    Source.pattern('{OUTPUT_DIR}/kernel_blob.bin'),
93 94 95 96 97
  ];

  @override
  List<String> get depfiles => <String>[
    'flutter_assets.d'
98 99 100 101 102 103 104 105 106 107 108 109
  ];

  @override
  Future<void> build(Environment environment) async {
    if (environment.defines[kBuildMode] == null) {
      throw MissingDefineException(kBuildMode, 'copy_flutter_bundle');
    }
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
    environment.outputDir.createSync(recursive: true);

    // Only copy the prebuilt runtimes and kernel blob in debug mode.
    if (buildMode == BuildMode.debug) {
110 111
      final String vmSnapshotData = globals.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
      final String isolateSnapshotData = globals.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
112 113
      environment.buildDir.childFile('app.dill')
          .copySync(environment.outputDir.childFile('kernel_blob.bin').path);
114
      globals.fs.file(vmSnapshotData)
115
          .copySync(environment.outputDir.childFile('vm_snapshot_data').path);
116
      globals.fs.file(isolateSnapshotData)
117 118
          .copySync(environment.outputDir.childFile('isolate_snapshot_data').path);
    }
119
    final Depfile assetDepfile = await copyAssets(environment, environment.outputDir);
120 121 122 123 124 125 126 127 128
    final DepfileService depfileService = DepfileService(
      fileSystem: globals.fs,
      logger: globals.logger,
      platform: globals.platform,
    );
    depfileService.writeToFile(
      assetDepfile,
      environment.buildDir.childFile('flutter_assets.d'),
    );
129 130 131 132 133 134 135 136
  }

  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
  ];
}

137
/// Copies the pre-built flutter bundle for release mode.
138 139 140 141 142 143 144
class ReleaseCopyFlutterBundle extends CopyFlutterBundle {
  const ReleaseCopyFlutterBundle();

  @override
  String get name => 'release_flutter_bundle';

  @override
145
  List<Source> get inputs => const <Source>[];
146 147

  @override
148 149 150 151 152
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => const <String>[
    'flutter_assets.d',
153 154 155 156 157 158 159
  ];

  @override
  List<Target> get dependencies => const <Target>[];
}


160 161 162 163 164 165 166 167 168
/// Generate a snapshot of the dart code used in the program.
class KernelSnapshot extends Target {
  const KernelSnapshot();

  @override
  String get name => 'kernel_snapshot';

  @override
  List<Source> get inputs => const <Source>[
169
    Source.pattern('{PROJECT_DIR}/.packages'),
170 171 172 173 174 175 176
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/dart.dart'),
    Source.artifact(Artifact.platformKernelDill),
    Source.artifact(Artifact.engineDartBinary),
    Source.artifact(Artifact.frontendServerSnapshotForEngineDartSdk),
  ];

  @override
177 178 179 180 181
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => <String>[
    'kernel_snapshot.d',
182 183 184 185 186 187
  ];

  @override
  List<Target> get dependencies => <Target>[];

  @override
188
  Future<void> build(Environment environment) async {
189 190 191 192 193 194
    final KernelCompiler compiler = await kernelCompilerFactory.create(
      FlutterProject.fromDirectory(environment.projectDir),
    );
    if (environment.defines[kBuildMode] == null) {
      throw MissingDefineException(kBuildMode, 'kernel_snapshot');
    }
195 196 197
    if (environment.defines[kTargetPlatform] == null) {
      throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
    }
198
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
199
    final String targetFile = environment.defines[kTargetFile] ?? globals.fs.path.join('lib', 'main.dart');
200
    final String packagesPath = environment.projectDir.childFile('.packages').path;
201
    final String targetFileAbsolute = globals.fs.file(targetFile).absolute.path;
202 203
    // everything besides 'false' is considered to be enabled.
    final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false';
204 205
    final TargetPlatform targetPlatform = getTargetPlatformForName(environment.defines[kTargetPlatform]);

206 207 208 209 210 211 212
    // This configuration is all optional.
    final List<String> extraFrontEndOptions = <String>[
      ...?environment.defines[kExtraFrontEndOptions]?.split(',')
    ];
    final List<String> fileSystemRoots = environment.defines[kFileSystemRoots]?.split(',');
    final String fileSystemScheme = environment.defines[kFileSystemScheme];

213 214 215 216 217
    TargetModel targetModel = TargetModel.flutter;
    if (targetPlatform == TargetPlatform.fuchsia_x64 ||
        targetPlatform == TargetPlatform.fuchsia_arm64) {
      targetModel = TargetModel.flutterRunner;
    }
218 219 220 221 222 223 224 225 226 227 228 229 230
    // Force linking of the platform for desktop embedder targets since these
    // do not correctly load the core snapshots in debug mode.
    // See https://github.com/flutter/flutter/issues/44724
    bool forceLinkPlatform;
    switch (targetPlatform) {
      case TargetPlatform.darwin_x64:
      case TargetPlatform.windows_x64:
      case TargetPlatform.linux_x64:
        forceLinkPlatform = true;
        break;
      default:
        forceLinkPlatform = false;
    }
231 232

    final CompilerOutput output = await compiler.compile(
233
      sdkRoot: globals.artifacts.getArtifactPath(
234 235 236 237
        Artifact.flutterPatchedSdkPath,
        platform: targetPlatform,
        mode: buildMode,
      ),
238
      aot: buildMode.isPrecompiled,
239
      buildMode: buildMode,
240
      trackWidgetCreation: trackWidgetCreation && buildMode == BuildMode.debug,
241
      targetModel: targetModel,
242
      outputFilePath: environment.buildDir.childFile('app.dill').path,
243
      packagesPath: packagesPath,
244
      linkPlatformKernelIn: forceLinkPlatform || buildMode.isPrecompiled,
245
      mainPath: targetFileAbsolute,
246
      depFilePath: environment.buildDir.childFile('kernel_snapshot.d').path,
247 248 249
      extraFrontEndOptions: extraFrontEndOptions,
      fileSystemRoots: fileSystemRoots,
      fileSystemScheme: fileSystemScheme,
250
      dartDefines: parseDartDefines(environment),
251
    );
252
    if (output == null || output.errorCount != 0) {
253 254
      throw Exception('Errors during snapshot creation: $output');
    }
255
  }
256
}
257

258 259 260 261 262
/// Supports compiling a dart kernel file to an ELF binary.
abstract class AotElfBase extends Target {
  const AotElfBase();

  @override
263
  Future<void> build(Environment environment) async {
264 265 266 267 268 269 270 271
    final AOTSnapshotter snapshotter = AOTSnapshotter(reportTimings: false);
    final String outputPath = environment.buildDir.path;
    if (environment.defines[kBuildMode] == null) {
      throw MissingDefineException(kBuildMode, 'aot_elf');
    }
    if (environment.defines[kTargetPlatform] == null) {
      throw MissingDefineException(kTargetPlatform, 'aot_elf');
    }
272 273
    final List<String> extraGenSnapshotOptions = environment.defines[kExtraGenSnapshotOptions]?.split(',')
      ?? const <String>[];
274 275
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
    final TargetPlatform targetPlatform = getTargetPlatformForName(environment.defines[kTargetPlatform]);
276 277
    final String splitDebugInfo = environment.defines[kSplitDebugInfo];
    final bool dartObfuscation = environment.defines[kDartObfuscation] == 'true';
278 279 280
    final int snapshotExitCode = await snapshotter.build(
      platform: targetPlatform,
      buildMode: buildMode,
281
      mainPath: environment.buildDir.childFile('app.dill').path,
282 283
      packagesPath: environment.projectDir.childFile('.packages').path,
      outputPath: outputPath,
284
      bitcode: false,
285
      extraGenSnapshotOptions: extraGenSnapshotOptions,
286 287
      splitDebugInfo: splitDebugInfo,
      dartObfuscation: dartObfuscation,
288 289 290 291 292 293 294 295
    );
    if (snapshotExitCode != 0) {
      throw Exception('AOT snapshotter exited with code $snapshotExitCode');
    }
  }
}

/// Generate an ELF binary from a dart kernel file in profile mode.
296 297 298 299 300 301 302 303 304 305
class AotElfProfile extends AotElfBase {
  const AotElfProfile();

  @override
  String get name => 'aot_elf_profile';

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/dart.dart'),
    Source.pattern('{BUILD_DIR}/app.dill'),
306 307 308 309 310 311 312
    Source.pattern('{PROJECT_DIR}/.packages'),
    Source.artifact(Artifact.engineDartBinary),
    Source.artifact(Artifact.skyEnginePath),
    Source.artifact(Artifact.genSnapshot,
      platform: TargetPlatform.android_arm,
      mode: BuildMode.profile,
    ),
313 314 315 316
  ];

  @override
  List<Source> get outputs => const <Source>[
317
    Source.pattern('{BUILD_DIR}/app.so'),
318 319 320 321 322 323 324
  ];

  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
  ];
}
325 326

/// Generate an ELF binary from a dart kernel file in release mode.
327 328 329 330 331 332 333 334 335 336
class AotElfRelease extends AotElfBase {
  const AotElfRelease();

  @override
  String get name => 'aot_elf_release';

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/dart.dart'),
    Source.pattern('{BUILD_DIR}/app.dill'),
337 338 339 340 341 342 343
    Source.pattern('{PROJECT_DIR}/.packages'),
    Source.artifact(Artifact.engineDartBinary),
    Source.artifact(Artifact.skyEnginePath),
    Source.artifact(Artifact.genSnapshot,
      platform: TargetPlatform.android_arm,
      mode: BuildMode.release,
    ),
344
  ];
345

346 347 348 349
  @override
  List<Source> get outputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];
350

351 352 353 354 355
  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
  ];
}
356

357
/// Copies the pre-built flutter aot bundle.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
// This is a one-off rule for implementing build aot in terms of assemble.
abstract class CopyFlutterAotBundle extends Target {
  const CopyFlutterAotBundle();

  @override
  List<Source> get inputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];

  @override
  List<Source> get outputs => const <Source>[
    Source.pattern('{OUTPUT_DIR}/app.so'),
  ];

  @override
  Future<void> build(Environment environment) async {
    final File outputFile = environment.outputDir.childFile('app.so');
    if (!outputFile.parent.existsSync()) {
      outputFile.parent.createSync(recursive: true);
    }
    environment.buildDir.childFile('app.so').copySync(outputFile.path);
  }
}

382 383
/// Dart defines are encoded inside [Environment] as a JSON array.
List<String> parseDartDefines(Environment environment) {
384
  if (!environment.defines.containsKey(kDartDefines) || environment.defines[kDartDefines].isEmpty) {
385 386 387 388 389
    return const <String>[];
  }

  final String dartDefinesJson = environment.defines[kDartDefines];
  try {
390
    final List<Object> parsedDefines = jsonDecode(dartDefinesJson) as List<Object>;
391
    return parsedDefines.cast<String>();
392
  } on FormatException {
393 394 395 396 397 398 399
    throw Exception(
      'The value of -D$kDartDefines is not formatted correctly.\n'
      'The value must be a JSON-encoded list of strings but was:\n'
      '$dartDefinesJson'
    );
  }
}