flx.dart 5.36 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import 'artifacts.dart';
8
import 'asset.dart';
9
import 'base/build.dart';
10
import 'base/common.dart';
11
import 'base/file_system.dart';
12
import 'build_info.dart';
13
import 'compile.dart';
14
import 'dart/package_map.dart';
15
import 'devfs.dart';
16
import 'globals.dart';
Devon Carew's avatar
Devon Carew committed
17
import 'zip.dart';
18 19

const String defaultMainPath = 'lib/main.dart';
20
const String defaultAssetBasePath = '.';
21
const String defaultManifestPath = 'pubspec.yaml';
22 23 24
String get defaultFlxOutputPath => fs.path.join(getBuildDirectory(), 'app.flx');
String get defaultSnapshotPath => fs.path.join(getBuildDirectory(), 'snapshot_blob.bin');
String get defaultDepfilePath => fs.path.join(getBuildDirectory(), 'snapshot_blob.bin.d');
25 26
const String defaultPrivateKeyPath = 'privatekey.der';

27
const String _kKernelKey = 'kernel_blob.bin';
28
const String _kSnapshotKey = 'snapshot_blob.bin';
29
const String _kDylibKey = 'libapp.so';
30
const String _kPlatformKernelKey = 'platform.dill';
31

32
Future<Null> build({
33 34
  String mainPath: defaultMainPath,
  String manifestPath: defaultManifestPath,
35 36 37
  String outputPath,
  String snapshotPath,
  String depfilePath,
38
  String privateKeyPath: defaultPrivateKeyPath,
39
  String workingDirPath,
40
  String packagesPath,
41
  bool previewDart2 : false,
42
  bool strongMode : false,
43
  bool precompiledSnapshot: false,
44
  bool reportLicensedPackages: false
45
}) async {
46 47 48 49
  outputPath ??= defaultFlxOutputPath;
  snapshotPath ??= defaultSnapshotPath;
  depfilePath ??= defaultDepfilePath;
  workingDirPath ??= getAssetBuildDirectory();
50
  packagesPath ??= fs.path.absolute(PackageMap.globalPackagesPath);
51
  File snapshotFile;
Devon Carew's avatar
Devon Carew committed
52

53
  if (!precompiledSnapshot && !previewDart2) {
54 55 56 57
    ensureDirectoryExists(snapshotPath);

    // In a precompiled snapshot, the instruction buffer contains script
    // content equivalents
58 59
    final Snapshotter snapshotter = new Snapshotter();
    final int result = await snapshotter.buildScriptSnapshot(
60 61
      mainPath: mainPath,
      snapshotPath: snapshotPath,
62
      depfilePath: depfilePath,
63
      packagesPath: packagesPath,
64
    );
65 66
    if (result != 0)
      throwToolExit('Failed to run the Flutter compiler. Exit code: $result', exitCode: result);
67

68
    snapshotFile = fs.file(snapshotPath);
69
  }
70

71
  DevFSContent kernelContent;
72
  if (!precompiledSnapshot && previewDart2) {
73 74
    final String kernelBinaryFilename = await compile(
      sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
75
      incrementalCompilerByteStorePath: fs.path.absolute(getIncrementalCompilerByteStoreDirectory()),
76 77
      mainPath: fs.file(mainPath).absolute.path,
      strongMode: strongMode
78
    );
79 80 81
    if (kernelBinaryFilename == null) {
      throwToolExit('Compiler terminated unexpectedly on $mainPath');
    }
82 83
    kernelContent = new DevFSFileContent(fs.file(kernelBinaryFilename));
  }
84

85
  return assemble(
86
    manifestPath: manifestPath,
87
    kernelContent: kernelContent,
Ian Hickson's avatar
Ian Hickson committed
88 89 90 91
    snapshotFile: snapshotFile,
    outputPath: outputPath,
    privateKeyPath: privateKeyPath,
    workingDirPath: workingDirPath,
92
    packagesPath: packagesPath,
93
    strongMode: strongMode,
94
    reportLicensedPackages: reportLicensedPackages
95
  ).then((_) => null);
96 97
}

98
Future<List<String>> assemble({
99
  String manifestPath,
100
  DevFSContent kernelContent,
Devon Carew's avatar
Devon Carew committed
101
  File snapshotFile,
102
  File dylibFile,
103
  String outputPath,
104
  String privateKeyPath: defaultPrivateKeyPath,
105
  String workingDirPath,
106
  String packagesPath,
107
  bool strongMode : false,
108
  bool includeDefaultFonts: true,
109
  bool reportLicensedPackages: false
110
}) async {
111 112
  outputPath ??= defaultFlxOutputPath;
  workingDirPath ??= getAssetBuildDirectory();
113
  packagesPath ??= fs.path.absolute(PackageMap.globalPackagesPath);
114 115
  printTrace('Building $outputPath');

116
  // Build the asset bundle.
117 118
  final AssetBundle assetBundle = new AssetBundle();
  final int result = await assetBundle.build(
119 120
    manifestPath: manifestPath,
    workingDirPath: workingDirPath,
121
    packagesPath: packagesPath,
122
    includeDefaultFonts: includeDefaultFonts,
123 124
    reportLicensedPackages: reportLicensedPackages
  );
125 126
  if (result != 0)
    throwToolExit('Error building $outputPath: $result', exitCode: result);
127

128
  final ZipBuilder zipBuilder = new ZipBuilder();
129

130 131
  // Add all entries from the asset bundle.
  zipBuilder.entries.addAll(assetBundle.entries);
132

133 134 135 136
  final List<String> fileDependencies = assetBundle.entries.values
      .expand((DevFSContent content) => content.fileDependencies)
      .toList();

137
  if (kernelContent != null) {
138 139 140
    final String platformKernelDill = strongMode ?
        artifacts.getArtifactPath(Artifact.platformKernelStrongDill) :
        artifacts.getArtifactPath(Artifact.platformKernelDill);
141
    zipBuilder.entries[_kKernelKey] = kernelContent;
142 143
    zipBuilder.entries[_kPlatformKernelKey] = new DevFSFileContent(fs.file(platformKernelDill));
  }
144
  if (snapshotFile != null)
145
    zipBuilder.entries[_kSnapshotKey] = new DevFSFileContent(snapshotFile);
146 147
  if (dylibFile != null)
    zipBuilder.entries[_kDylibKey] = new DevFSFileContent(dylibFile);
Ian Hickson's avatar
Ian Hickson committed
148

149
  ensureDirectoryExists(outputPath);
150

151
  printTrace('Encoding zip file to $outputPath');
152 153 154 155

  // TODO(zarah): Remove the zipBuilder and write the files directly once FLX
  // is deprecated.

156
  await zipBuilder.createZip(fs.file(outputPath), fs.directory(workingDirPath));
157

Devon Carew's avatar
Devon Carew committed
158
  printTrace('Built $outputPath.');
159 160

  return fileDependencies;
161
}