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

import 'package:meta/meta.dart';

import '../artifacts.dart';
import '../base/common.dart';
import '../base/logger.dart';
import '../build_info.dart';
11
import '../globals.dart' as globals;
12 13
import '../project.dart';

14 15
/// This is a simple wrapper around the custom kernel compiler from the Fuchsia
/// SDK.
16
class FuchsiaKernelCompiler {
17
  /// Compiles the [fuchsiaProject] with entry point [target] to a collection of
18 19 20 21 22 23 24 25 26 27 28 29 30 31
  /// .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;
32 33 34
    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(
35
      Artifact.fuchsiaKernelCompiler,
36
      platform: TargetPlatform.fuchsia_arm64,  // This file is not arch-specific.
37
      mode: buildInfo.mode,
38
    );
39
    if (!globals.fs.isFileSync(kernelCompiler)) {
40
      throwToolExit('Fuchsia kernel compiler not found at "$kernelCompiler"');
41
    }
42
    final String platformDill = globals.artifacts.getArtifactPath(
43
      Artifact.platformKernelDill,
44
      platform: TargetPlatform.fuchsia_arm64,  // This file is not arch-specific.
45 46
      mode: buildInfo.mode,
    );
47
    if (!globals.fs.isFileSync(platformDill)) {
48
      throwToolExit('Fuchsia platform file not found at "$platformDill"');
49
    }
50 51
    List<String> flags = <String>[
      '--target', 'flutter_runner',
52
      '--platform', platformDill,
53 54 55
      '--filesystem-scheme', 'main-root',
      '--filesystem-root', fsRoot,
      '--packages', '$multiRootScheme:///$relativePackagesFile',
56
      '--output', globals.fs.path.join(outDir, '$appName.dil'),
57
      '--component-name', appName,
58
      ...getBuildInfoFlags(buildInfo: buildInfo, manifestPath: manifestPath)
59
    ];
60 61 62 63 64 65

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

    final List<String> command = <String>[
66
      globals.artifacts.getArtifactPath(Artifact.engineDartBinary),
67
      '--disable-dart-dev',
68
      kernelCompiler,
69 70
      ...flags,
    ];
71
    final Status status = globals.logger.startProgress(
72 73 74 75
      'Building Fuchsia application...',
    );
    int result;
    try {
76
      result = await globals.processUtils.stream(command, trace: true);
77 78 79 80 81 82 83
    } finally {
      status.cancel();
    }
    if (result != 0) {
      throwToolExit('Build process failed');
    }
  }
84 85 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

  /// 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',
    ];
  }
123
}