fuchsia_kernel_compiler.dart 4.1 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
// @dart = 2.8

7 8 9 10 11 12
import 'package:meta/meta.dart';

import '../artifacts.dart';
import '../base/common.dart';
import '../base/logger.dart';
import '../build_info.dart';
13
import '../globals_null_migrated.dart' as globals;
14 15
import '../project.dart';

16 17
/// This is a simple wrapper around the custom kernel compiler from the Fuchsia
/// SDK.
18
class FuchsiaKernelCompiler {
19
  /// Compiles the [fuchsiaProject] with entry point [target] to a collection of
20 21 22 23 24 25 26 27 28 29 30 31 32 33
  /// .dilp files (consisting of the app split along package: boundaries, but
  /// the Flutter tool should make no use of that fact), and a manifest that
  /// refers to them.
  Future<void> build({
    @required FuchsiaProject fuchsiaProject,
    @required String target, // E.g., lib/main.dart
    BuildInfo buildInfo = BuildInfo.debug,
  }) async {
    // TODO(zra): Use filesystem root and scheme information from buildInfo.
    const String multiRootScheme = 'main-root';
    final String packagesFile = fuchsiaProject.project.packagesFile.path;
    final String outDir = getFuchsiaBuildDirectory();
    final String appName = fuchsiaProject.project.manifest.appName;
    final String fsRoot = fuchsiaProject.project.directory.path;
34 35 36
    final String relativePackagesFile = globals.fs.path.relative(packagesFile, from: fsRoot);
    final String manifestPath = globals.fs.path.join(outDir, '$appName.dilpmanifest');
    final String kernelCompiler = globals.artifacts.getArtifactPath(
37
      Artifact.fuchsiaKernelCompiler,
38
      platform: TargetPlatform.fuchsia_arm64,  // This file is not arch-specific.
39
      mode: buildInfo.mode,
40
    );
41
    if (!globals.fs.isFileSync(kernelCompiler)) {
42
      throwToolExit('Fuchsia kernel compiler not found at "$kernelCompiler"');
43
    }
44
    final String platformDill = globals.artifacts.getArtifactPath(
45
      Artifact.platformKernelDill,
46
      platform: TargetPlatform.fuchsia_arm64,  // This file is not arch-specific.
47 48
      mode: buildInfo.mode,
    );
49
    if (!globals.fs.isFileSync(platformDill)) {
50
      throwToolExit('Fuchsia platform file not found at "$platformDill"');
51
    }
52 53
    List<String> flags = <String>[
      '--target', 'flutter_runner',
54
      '--platform', platformDill,
55 56 57
      '--filesystem-scheme', 'main-root',
      '--filesystem-root', fsRoot,
      '--packages', '$multiRootScheme:///$relativePackagesFile',
58
      '--output', globals.fs.path.join(outDir, '$appName.dil'),
59
      '--component-name', appName,
60
      ...getBuildInfoFlags(buildInfo: buildInfo, manifestPath: manifestPath)
61
    ];
62 63 64 65 66 67

    flags += <String>[
      '$multiRootScheme:///$target',
    ];

    final List<String> command = <String>[
68
      globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
69
      '--disable-dart-dev',
70
      kernelCompiler,
71 72
      ...flags,
    ];
73
    final Status status = globals.logger.startProgress(
74 75 76 77
      'Building Fuchsia application...',
    );
    int result;
    try {
78
      result = await globals.processUtils.stream(command, trace: true);
79 80 81 82 83 84 85
    } finally {
      status.cancel();
    }
    if (result != 0) {
      throwToolExit('Build process failed');
    }
  }
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

  /// Provide flags that are affected by [BuildInfo]
  @visibleForTesting
  static List<String> getBuildInfoFlags({
    @required BuildInfo buildInfo,
    @required String manifestPath,
  }) {
    return <String>[
      // AOT/JIT:
      if (buildInfo.usesAot) ...<String>[
        '--aot',
        '--tfa'
      ] else ...<String>[
        '--no-link-platform',
        '--split-output-by-packages',
        '--manifest',
        manifestPath
      ],

      // debug, profile, jit release, release:
      if (buildInfo.isDebug)
        '--embed-sources'
      else
        '--no-embed-sources',

      if (buildInfo.isProfile) ...<String>[
        '-Ddart.vm.profile=true',
        '-Ddart.vm.product=false',
      ],

      if (buildInfo.mode.isRelease) ...<String>[
        '-Ddart.vm.profile=false',
        '-Ddart.vm.product=true',
      ],

      for (final String dartDefine in buildInfo.dartDefines)
        '-D$dartDefine',
    ];
  }
125
}