dart.dart 13.8 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
    // This configuration is all optional.
207
    final List<String> extraFrontEndOptions = environment.defines[kExtraFrontEndOptions]?.split(',');
208 209 210
    final List<String> fileSystemRoots = environment.defines[kFileSystemRoots]?.split(',');
    final String fileSystemScheme = environment.defines[kFileSystemScheme];

211 212 213 214 215
    TargetModel targetModel = TargetModel.flutter;
    if (targetPlatform == TargetPlatform.fuchsia_x64 ||
        targetPlatform == TargetPlatform.fuchsia_arm64) {
      targetModel = TargetModel.flutterRunner;
    }
216 217 218 219 220 221 222 223 224 225 226 227 228
    // 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;
    }
229 230

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

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

260 261 262
  @override
  String get analyticsName => 'android_aot';

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

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

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

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

/// Generate an ELF binary from a dart kernel file in release mode.
335 336 337 338 339 340 341 342 343 344
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'),
345 346 347 348 349 350 351
    Source.pattern('{PROJECT_DIR}/.packages'),
    Source.artifact(Artifact.engineDartBinary),
    Source.artifact(Artifact.skyEnginePath),
    Source.artifact(Artifact.genSnapshot,
      platform: TargetPlatform.android_arm,
      mode: BuildMode.release,
    ),
352
  ];
353

354 355 356 357
  @override
  List<Source> get outputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];
358

359 360 361 362 363
  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
  ];
}
364

365
/// Copies the pre-built flutter aot bundle.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
// 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);
  }
}

390 391
/// Dart defines are encoded inside [Environment] as a JSON array.
List<String> parseDartDefines(Environment environment) {
392
  if (!environment.defines.containsKey(kDartDefines) || environment.defines[kDartDefines].isEmpty) {
393 394 395 396 397
    return const <String>[];
  }

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