dart.dart 13.5 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 18 19 20 21 22 23 24 25 26

/// 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';

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

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

33 34 35 36 37 38 39 40 41 42 43 44
/// 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';

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

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

54 55 56
/// Defines specified via the `--dart-define` command-line option.
const String kDartDefines = 'DartDefines';

57 58 59 60 61 62 63 64
/// 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';

65
/// Copies the pre-built flutter bundle.
66 67 68 69 70 71 72 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'),
  ];

  @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'),
85 86 87 88 89
  ];

  @override
  List<String> get depfiles => <String>[
    'flutter_assets.d'
90 91 92 93 94 95 96 97 98 99 100 101
  ];

  @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) {
102 103
      final String vmSnapshotData = globals.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
      final String isolateSnapshotData = globals.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
104 105
      environment.buildDir.childFile('app.dill')
          .copySync(environment.outputDir.childFile('kernel_blob.bin').path);
106
      globals.fs.file(vmSnapshotData)
107
          .copySync(environment.outputDir.childFile('vm_snapshot_data').path);
108
      globals.fs.file(isolateSnapshotData)
109 110
          .copySync(environment.outputDir.childFile('isolate_snapshot_data').path);
    }
111 112
    final Depfile assetDepfile = await copyAssets(environment, environment.outputDir);
    assetDepfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
113 114 115 116 117 118 119 120
  }

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

121
/// Copies the pre-built flutter bundle for release mode.
122 123 124 125 126 127 128
class ReleaseCopyFlutterBundle extends CopyFlutterBundle {
  const ReleaseCopyFlutterBundle();

  @override
  String get name => 'release_flutter_bundle';

  @override
129
  List<Source> get inputs => const <Source>[];
130 131

  @override
132 133 134 135 136
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => const <String>[
    'flutter_assets.d',
137 138 139 140 141 142 143
  ];

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


144 145 146 147 148 149 150 151 152
/// 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>[
153
    Source.pattern('{PROJECT_DIR}/.packages'),
154 155 156 157 158 159 160
    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
161 162 163 164 165
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => <String>[
    'kernel_snapshot.d',
166 167 168 169 170 171
  ];

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

  @override
172
  Future<void> build(Environment environment) async {
173 174 175 176 177 178
    final KernelCompiler compiler = await kernelCompilerFactory.create(
      FlutterProject.fromDirectory(environment.projectDir),
    );
    if (environment.defines[kBuildMode] == null) {
      throw MissingDefineException(kBuildMode, 'kernel_snapshot');
    }
179 180 181
    if (environment.defines[kTargetPlatform] == null) {
      throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
    }
182
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
183
    final String targetFile = environment.defines[kTargetFile] ?? globals.fs.path.join('lib', 'main.dart');
184
    final String packagesPath = environment.projectDir.childFile('.packages').path;
185
    final String targetFileAbsolute = globals.fs.file(targetFile).absolute.path;
186 187
    // everything besides 'false' is considered to be enabled.
    final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false';
188 189
    final TargetPlatform targetPlatform = getTargetPlatformForName(environment.defines[kTargetPlatform]);

190 191 192 193 194 195 196
    // 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];

197 198 199 200 201
    TargetModel targetModel = TargetModel.flutter;
    if (targetPlatform == TargetPlatform.fuchsia_x64 ||
        targetPlatform == TargetPlatform.fuchsia_arm64) {
      targetModel = TargetModel.flutterRunner;
    }
202 203 204 205 206 207 208 209 210 211 212 213 214
    // 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;
    }
215 216

    final CompilerOutput output = await compiler.compile(
217
      sdkRoot: globals.artifacts.getArtifactPath(
218 219 220 221
        Artifact.flutterPatchedSdkPath,
        platform: targetPlatform,
        mode: buildMode,
      ),
222
      aot: buildMode.isPrecompiled,
223
      buildMode: buildMode,
224
      trackWidgetCreation: trackWidgetCreation && buildMode == BuildMode.debug,
225
      targetModel: targetModel,
226
      outputFilePath: environment.buildDir.childFile('app.dill').path,
227
      packagesPath: packagesPath,
228
      linkPlatformKernelIn: forceLinkPlatform || buildMode.isPrecompiled,
229
      mainPath: targetFileAbsolute,
230
      depFilePath: environment.buildDir.childFile('kernel_snapshot.d').path,
231 232 233
      extraFrontEndOptions: extraFrontEndOptions,
      fileSystemRoots: fileSystemRoots,
      fileSystemScheme: fileSystemScheme,
234
      dartDefines: parseDartDefines(environment),
235
    );
236
    if (output == null || output.errorCount != 0) {
237 238
      throw Exception('Errors during snapshot creation: $output');
    }
239
  }
240
}
241

242 243 244 245 246
/// Supports compiling a dart kernel file to an ELF binary.
abstract class AotElfBase extends Target {
  const AotElfBase();

  @override
247
  Future<void> build(Environment environment) async {
248 249 250 251 252 253 254 255
    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');
    }
256 257
    final List<String> extraGenSnapshotOptions = environment.defines[kExtraGenSnapshotOptions]?.split(',')
      ?? const <String>[];
258 259
    final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
    final TargetPlatform targetPlatform = getTargetPlatformForName(environment.defines[kTargetPlatform]);
260 261 262
    final int snapshotExitCode = await snapshotter.build(
      platform: targetPlatform,
      buildMode: buildMode,
263
      mainPath: environment.buildDir.childFile('app.dill').path,
264 265
      packagesPath: environment.projectDir.childFile('.packages').path,
      outputPath: outputPath,
266
      bitcode: false,
267
      extraGenSnapshotOptions: extraGenSnapshotOptions,
268 269 270 271 272 273 274 275
    );
    if (snapshotExitCode != 0) {
      throw Exception('AOT snapshotter exited with code $snapshotExitCode');
    }
  }
}

/// Generate an ELF binary from a dart kernel file in profile mode.
276 277 278 279 280 281 282 283 284 285
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'),
286 287 288 289 290 291 292
    Source.pattern('{PROJECT_DIR}/.packages'),
    Source.artifact(Artifact.engineDartBinary),
    Source.artifact(Artifact.skyEnginePath),
    Source.artifact(Artifact.genSnapshot,
      platform: TargetPlatform.android_arm,
      mode: BuildMode.profile,
    ),
293 294 295 296
  ];

  @override
  List<Source> get outputs => const <Source>[
297
    Source.pattern('{BUILD_DIR}/app.so'),
298 299 300 301 302 303 304
  ];

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

/// Generate an ELF binary from a dart kernel file in release mode.
307 308 309 310 311 312 313 314 315 316
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'),
317 318 319 320 321 322 323
    Source.pattern('{PROJECT_DIR}/.packages'),
    Source.artifact(Artifact.engineDartBinary),
    Source.artifact(Artifact.skyEnginePath),
    Source.artifact(Artifact.genSnapshot,
      platform: TargetPlatform.android_arm,
      mode: BuildMode.release,
    ),
324
  ];
325

326 327 328 329
  @override
  List<Source> get outputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];
330

331 332 333 334 335
  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
  ];
}
336

337
/// Copies the pre-built flutter aot bundle.
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
// 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);
  }
}

362
// This is a one-off rule for implementing build aot in terms of assemble.
363 364 365 366
class ProfileCopyFlutterAotBundle extends CopyFlutterAotBundle {
  const ProfileCopyFlutterAotBundle();

  @override
367
  String get name => 'profile_android_flutter_bundle';
368 369 370 371 372 373 374

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

375
// This is a one-off rule for implementing build aot in terms of assemble.
376 377 378 379
class ReleaseCopyFlutterAotBundle extends CopyFlutterAotBundle {
  const ReleaseCopyFlutterAotBundle();

  @override
380
  String get name => 'release_android_flutter_bundle';
381 382 383 384 385 386

  @override
  List<Target> get dependencies => const <Target>[
    AotElfRelease(),
  ];
}
387 388 389 390 391 392 393 394 395

/// Dart defines are encoded inside [Environment] as a JSON array.
List<String> parseDartDefines(Environment environment) {
  if (!environment.defines.containsKey(kDartDefines)) {
    return const <String>[];
  }

  final String dartDefinesJson = environment.defines[kDartDefines];
  try {
396
    final List<Object> parsedDefines = jsonDecode(dartDefinesJson) as List<Object>;
397 398 399 400 401 402 403 404 405
    return parsedDefines.cast<String>();
  } on FormatException catch (_) {
    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'
    );
  }
}