common.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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:package_config/package_config.dart';

7 8 9 10 11
import '../../artifacts.dart';
import '../../base/build.dart';
import '../../base/file_system.dart';
import '../../build_info.dart';
import '../../compile.dart';
12
import '../../dart/package_map.dart';
13
import '../../globals.dart' as globals show xcode;
14
import '../build_system.dart';
15
import '../depfile.dart';
16
import '../exceptions.dart';
17
import 'assets.dart';
18
import 'dart_plugin_registrant.dart';
19
import 'icon_tree_shaker.dart';
20
import 'localizations.dart';
21
import 'shader_compiler.dart';
22

23
/// Copies the pre-built flutter bundle.
24 25 26 27 28 29 30 31 32 33 34 35
// 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'),
36
    ...IconTreeShaker.inputs,
37
    ...ShaderCompiler.inputs,
38 39 40 41 42 43 44
  ];

  @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'),
45 46 47 48
  ];

  @override
  List<String> get depfiles => <String>[
49
    'flutter_assets.d',
50 51 52 53
  ];

  @override
  Future<void> build(Environment environment) async {
54 55
    final String? buildModeEnvironment = environment.defines[kBuildMode];
    if (buildModeEnvironment == null) {
56 57
      throw MissingDefineException(kBuildMode, 'copy_flutter_bundle');
    }
58
    final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
59 60 61 62
    environment.outputDir.createSync(recursive: true);

    // Only copy the prebuilt runtimes and kernel blob in debug mode.
    if (buildMode == BuildMode.debug) {
63 64
      final String vmSnapshotData = environment.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
      final String isolateSnapshotData = environment.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
65 66
      environment.buildDir.childFile('app.dill')
          .copySync(environment.outputDir.childFile('kernel_blob.bin').path);
67
      environment.fileSystem.file(vmSnapshotData)
68
          .copySync(environment.outputDir.childFile('vm_snapshot_data').path);
69
      environment.fileSystem.file(isolateSnapshotData)
70 71
          .copySync(environment.outputDir.childFile('isolate_snapshot_data').path);
    }
72 73 74 75
    final Depfile assetDepfile = await copyAssets(
      environment,
      environment.outputDir,
      targetPlatform: TargetPlatform.android,
76
      buildMode: buildMode,
77
    );
78
    final DepfileService depfileService = DepfileService(
79 80
      fileSystem: environment.fileSystem,
      logger: environment.logger,
81 82 83 84 85
    );
    depfileService.writeToFile(
      assetDepfile,
      environment.buildDir.childFile('flutter_assets.d'),
    );
86 87 88 89 90 91 92 93
  }

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

94
/// Copies the pre-built flutter bundle for release mode.
95 96 97 98 99 100 101
class ReleaseCopyFlutterBundle extends CopyFlutterBundle {
  const ReleaseCopyFlutterBundle();

  @override
  String get name => 'release_flutter_bundle';

  @override
102
  List<Source> get inputs => const <Source>[];
103 104

  @override
105 106 107 108 109
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => const <String>[
    'flutter_assets.d',
110 111 112 113 114 115
  ];

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

116
/// Generate a snapshot of the dart code used in the program.
117 118 119
///
/// Note that this target depends on the `.dart_tool/package_config.json` file
/// even though it is not listed as an input. Pub inserts a timestamp into
120
/// the file which causes unnecessary rebuilds, so instead a subset of the contents
121
/// are used an input instead.
122 123 124 125 126 127 128 129
class KernelSnapshot extends Target {
  const KernelSnapshot();

  @override
  String get name => 'kernel_snapshot';

  @override
  List<Source> get inputs => const <Source>[
130
    Source.pattern('{PROJECT_DIR}/.dart_tool/package_config_subset'),
131
    Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/common.dart'),
132
    Source.artifact(Artifact.platformKernelDill),
133
    Source.hostArtifact(HostArtifact.engineDartBinary),
134 135 136 137
    Source.artifact(Artifact.frontendServerSnapshotForEngineDartSdk),
  ];

  @override
138 139 140 141 142
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => <String>[
    'kernel_snapshot.d',
143 144 145
  ];

  @override
146 147
  List<Target> get dependencies => const <Target>[
    GenerateLocalizationsTarget(),
148
    DartPluginRegistrantTarget(),
149
  ];
150 151

  @override
152
  Future<void> build(Environment environment) async {
153 154 155 156 157
    final KernelCompiler compiler = KernelCompiler(
      fileSystem: environment.fileSystem,
      logger: environment.logger,
      processManager: environment.processManager,
      artifacts: environment.artifacts,
158
      fileSystemRoots: <String>[],
159
    );
160 161
    final String? buildModeEnvironment = environment.defines[kBuildMode];
    if (buildModeEnvironment == null) {
162 163
      throw MissingDefineException(kBuildMode, 'kernel_snapshot');
    }
164 165
    final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
    if (targetPlatformEnvironment == null) {
166 167
      throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
    }
168
    final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
169
    final String targetFile = environment.defines[kTargetFile] ?? environment.fileSystem.path.join('lib', 'main.dart');
170 171 172
    final File packagesFile = environment.projectDir
      .childDirectory('.dart_tool')
      .childFile('package_config.json');
173
    final String targetFileAbsolute = environment.fileSystem.file(targetFile).absolute.path;
174 175
    // everything besides 'false' is considered to be enabled.
    final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false';
176
    final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
177

178
    // This configuration is all optional.
179
    final List<String> extraFrontEndOptions = decodeCommaSeparated(environment.defines, kExtraFrontEndOptions);
180 181
    final List<String>? fileSystemRoots = environment.defines[kFileSystemRoots]?.split(',');
    final String? fileSystemScheme = environment.defines[kFileSystemScheme];
182

183 184 185 186 187
    TargetModel targetModel = TargetModel.flutter;
    if (targetPlatform == TargetPlatform.fuchsia_x64 ||
        targetPlatform == TargetPlatform.fuchsia_arm64) {
      targetModel = TargetModel.flutterRunner;
    }
188 189 190 191 192
    // 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) {
193
      case TargetPlatform.darwin:
194 195
      case TargetPlatform.windows_x64:
      case TargetPlatform.linux_x64:
196 197
        forceLinkPlatform = true;
        break;
198 199 200 201 202 203 204 205 206 207 208
      case TargetPlatform.android:
      case TargetPlatform.android_arm:
      case TargetPlatform.android_arm64:
      case TargetPlatform.android_x64:
      case TargetPlatform.android_x86:
      case TargetPlatform.fuchsia_arm64:
      case TargetPlatform.fuchsia_x64:
      case TargetPlatform.ios:
      case TargetPlatform.linux_arm64:
      case TargetPlatform.tester:
      case TargetPlatform.web_javascript:
209
        forceLinkPlatform = false;
210
        break;
211
    }
212

213
    final PackageConfig packageConfig = await loadPackageConfigWithLogging(
214
      packagesFile,
215
      logger: environment.logger,
216 217
    );

218
    final CompilerOutput? output = await compiler.compile(
219
      sdkRoot: environment.artifacts.getArtifactPath(
220 221 222 223
        Artifact.flutterPatchedSdkPath,
        platform: targetPlatform,
        mode: buildMode,
      ),
224
      aot: buildMode.isPrecompiled,
225
      buildMode: buildMode,
226
      trackWidgetCreation: trackWidgetCreation && buildMode != BuildMode.release,
227
      targetModel: targetModel,
228
      outputFilePath: environment.buildDir.childFile('app.dill').path,
229 230
      initializeFromDill: buildMode.isPrecompiled ? null :
          environment.buildDir.childFile('app.dill').path,
231
      packagesPath: packagesFile.path,
232
      linkPlatformKernelIn: forceLinkPlatform || buildMode.isPrecompiled,
233
      mainPath: targetFileAbsolute,
234
      depFilePath: environment.buildDir.childFile('kernel_snapshot.d').path,
235 236 237
      extraFrontEndOptions: extraFrontEndOptions,
      fileSystemRoots: fileSystemRoots,
      fileSystemScheme: fileSystemScheme,
238
      dartDefines: decodeDartDefines(environment.defines, kDartDefines),
239
      packageConfig: packageConfig,
240 241
      buildDir: environment.buildDir,
      checkDartPluginRegistry: environment.generateDartPluginRegistry,
242
    );
243
    if (output == null || output.errorCount != 0) {
244
      throw Exception();
245
    }
246
  }
247
}
248

249 250 251 252
/// Supports compiling a dart kernel file to an ELF binary.
abstract class AotElfBase extends Target {
  const AotElfBase();

253 254 255
  @override
  String get analyticsName => 'android_aot';

256
  @override
257
  Future<void> build(Environment environment) async {
258
    final AOTSnapshotter snapshotter = AOTSnapshotter(
259 260
      fileSystem: environment.fileSystem,
      logger: environment.logger,
261
      xcode: globals.xcode!,
262 263
      processManager: environment.processManager,
      artifacts: environment.artifacts,
264
    );
265
    final String outputPath = environment.buildDir.path;
266 267
    final String? buildModeEnvironment = environment.defines[kBuildMode];
    if (buildModeEnvironment == null) {
268 269
      throw MissingDefineException(kBuildMode, 'aot_elf');
    }
270 271
    final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
    if (targetPlatformEnvironment == null) {
272 273
      throw MissingDefineException(kTargetPlatform, 'aot_elf');
    }
274
    final List<String> extraGenSnapshotOptions = decodeCommaSeparated(environment.defines, kExtraGenSnapshotOptions);
275 276 277
    final BuildMode buildMode = getBuildModeForName(buildModeEnvironment);
    final TargetPlatform targetPlatform = getTargetPlatformForName(targetPlatformEnvironment);
    final String? splitDebugInfo = environment.defines[kSplitDebugInfo];
278
    final bool dartObfuscation = environment.defines[kDartObfuscation] == 'true';
279
    final String? codeSizeDirectory = environment.defines[kCodeSizeDirectory];
280 281 282 283 284 285 286 287 288 289 290 291

    if (codeSizeDirectory != null) {
      final File codeSizeFile = environment.fileSystem
        .directory(codeSizeDirectory)
        .childFile('snapshot.${environment.defines[kTargetPlatform]}.json');
      final File precompilerTraceFile = environment.fileSystem
        .directory(codeSizeDirectory)
        .childFile('trace.${environment.defines[kTargetPlatform]}.json');
      extraGenSnapshotOptions.add('--write-v8-snapshot-profile-to=${codeSizeFile.path}');
      extraGenSnapshotOptions.add('--trace-precompiler-to=${precompilerTraceFile.path}');
    }

292 293 294
    final int snapshotExitCode = await snapshotter.build(
      platform: targetPlatform,
      buildMode: buildMode,
295
      mainPath: environment.buildDir.childFile('app.dill').path,
296
      outputPath: outputPath,
297
      bitcode: false,
298
      extraGenSnapshotOptions: extraGenSnapshotOptions,
299 300
      splitDebugInfo: splitDebugInfo,
      dartObfuscation: dartObfuscation,
301 302 303 304 305 306 307 308
    );
    if (snapshotExitCode != 0) {
      throw Exception('AOT snapshotter exited with code $snapshotExitCode');
    }
  }
}

/// Generate an ELF binary from a dart kernel file in profile mode.
309
class AotElfProfile extends AotElfBase {
310
  const AotElfProfile(this.targetPlatform);
311 312 313 314 315

  @override
  String get name => 'aot_elf_profile';

  @override
316
  List<Source> get inputs => <Source>[
317
    const Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/common.dart'),
318
    const Source.pattern('{BUILD_DIR}/app.dill'),
319
    const Source.hostArtifact(HostArtifact.engineDartBinary),
320
    const Source.artifact(Artifact.skyEnginePath),
321
    Source.artifact(Artifact.genSnapshot,
322
      platform: targetPlatform,
323 324
      mode: BuildMode.profile,
    ),
325 326 327 328
  ];

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

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

337
  final TargetPlatform targetPlatform;
338
}
339 340

/// Generate an ELF binary from a dart kernel file in release mode.
341
class AotElfRelease extends AotElfBase {
342
  const AotElfRelease(this.targetPlatform);
343 344 345 346 347

  @override
  String get name => 'aot_elf_release';

  @override
348
  List<Source> get inputs => <Source>[
349
    const Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/common.dart'),
350
    const Source.pattern('{BUILD_DIR}/app.dill'),
351
    const Source.hostArtifact(HostArtifact.engineDartBinary),
352
    const Source.artifact(Artifact.skyEnginePath),
353
    Source.artifact(Artifact.genSnapshot,
354
      platform: targetPlatform,
355 356
      mode: BuildMode.release,
    ),
357
  ];
358

359 360 361 362
  @override
  List<Source> get outputs => const <Source>[
    Source.pattern('{BUILD_DIR}/app.so'),
  ];
363

364 365 366 367
  @override
  List<Target> get dependencies => const <Target>[
    KernelSnapshot(),
  ];
368

369
  final TargetPlatform targetPlatform;
370
}
371

372
/// Copies the pre-built flutter aot bundle.
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
// 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);
  }
}