fuchsia_kernel_compiler.dart 4.38 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
  /// .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({
22 23
    required FuchsiaProject fuchsiaProject,
    required String target, // E.g., lib/main.dart
24 25
    BuildInfo buildInfo = BuildInfo.debug,
  }) async {
26
    // TODO(zanderso): Use filesystem root and scheme information from buildInfo.
27 28 29 30 31
    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
    final String relativePackagesFile = globals.fs.path.relative(packagesFile, from: fsRoot);
    final String manifestPath = globals.fs.path.join(outDir, '$appName.dilpmanifest');
34
    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 (kernelCompiler == null || !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 (platformDill == null || !globals.fs.isFileSync(platformDill)) {
48
      throwToolExit('Fuchsia platform file not found at "$platformDill"');
49
    }
50
    List<String> flags = <String>[
51
      '--no-sound-null-safety',
52 53 54 55 56 57 58 59 60 61 62 63 64 65
      '--target',
      'flutter_runner',
      '--platform',
      platformDill,
      '--filesystem-scheme',
      'main-root',
      '--filesystem-root',
      fsRoot,
      '--packages',
      '$multiRootScheme:///$relativePackagesFile',
      '--output',
      globals.fs.path.join(outDir, '$appName.dil'),
      '--component-name',
      appName,
66
      ...getBuildInfoFlags(buildInfo: buildInfo, manifestPath: manifestPath),
67
    ];
68 69 70 71 72

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

73
    final String? engineDartBinaryPath = globals.artifacts?.getArtifactPath(Artifact.engineDartBinary);
74 75 76
    if (engineDartBinaryPath == null) {
      throwToolExit('Engine dart binary not found at "$engineDartBinaryPath"');
    }
77
    final List<String> command = <String>[
78
      engineDartBinaryPath,
79
      '--disable-dart-dev',
80
      kernelCompiler,
81 82
      ...flags,
    ];
83
    final Status status = globals.logger.startProgress(
84 85 86 87
      'Building Fuchsia application...',
    );
    int result;
    try {
88
      result = await globals.processUtils.stream(command, trace: true);
89 90 91 92 93 94 95
    } finally {
      status.cancel();
    }
    if (result != 0) {
      throwToolExit('Build process failed');
    }
  }
96 97 98 99

  /// Provide flags that are affected by [BuildInfo]
  @visibleForTesting
  static List<String> getBuildInfoFlags({
100 101
    required BuildInfo buildInfo,
    required String manifestPath,
102 103 104 105 106
  }) {
    return <String>[
      // AOT/JIT:
      if (buildInfo.usesAot) ...<String>[
        '--aot',
107
        '--tfa',
108 109 110 111
      ] else ...<String>[
        '--no-link-platform',
        '--split-output-by-packages',
        '--manifest',
112
        manifestPath,
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      ],

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