Files
sdk/pkg/kernel/bin/split.dart
T
Jens Johansen 89cec72771 [kernel] Add two new kernel tools; add error handling in existing tools.
This CL adds two new kernel tools which can answer questions about
a dill file:

* count_breakdown.dart
  Enumerates the different node types in the provided dill file and
  counts them.
  This has proved useful in the past to e.g. see how many doubles are
  included in Flutter.

* size_breakdown.dart
  Gives an overview of which parts of the dill file contributes how many
  bytes.
  This has proved useful in the past to see that the string table gets
  huge when we have many doubles (because we currently save doubles in
  kernel as strings).

It also adds error handling and usage information to existing tools.

Change-Id: Ie2ce9e4b8806d5975dba8f57445705a840be3474
Reviewed-on: https://dart-review.googlesource.com/42660
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Kevin Millikin <kmillikin@google.com>
2018-02-21 14:35:16 +00:00

56 lines
1.7 KiB
Dart
Executable File

#!/usr/bin/env dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. 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';
import 'dart:io';
import 'package:kernel/ast.dart';
import 'package:kernel/binary/ast_to_binary.dart';
import 'package:kernel/binary/limited_ast_to_binary.dart';
import 'package:kernel/kernel.dart';
import 'util.dart';
void usage() {
print("Split a dill file into separate dill files (one library per file).");
print("Dart internal libraries are not included in the output.");
print("");
print("Usage: dart <script> dillFile.dill");
print("The given argument should be an existing file");
print("that is valid to load as a dill file.");
exit(1);
}
main(args) async {
CommandLineHelper.requireExactlyOneArgument(true, args, usage);
Program binary = CommandLineHelper.tryLoadDill(args[0], usage);
int part = 1;
binary.libraries.forEach((lib) => lib.isExternal = true);
for (int i = 0; i < binary.libraries.length; ++i) {
Library lib = binary.libraries[i];
if (lib.name?.startsWith("dart.") == true ||
lib.name == "builtin" ||
lib.name == "nativewrappers") continue;
lib.isExternal = false;
String path = args[0] + ".part${part++}.dill";
await writeProgramToFile(binary, path);
print("Wrote $path");
lib.isExternal = true;
}
}
Future<Null> writeProgramToFile(Program program, String path) async {
File output = new File(path);
IOSink sink = output.openWrite();
try {
BinaryPrinter printer =
new LimitedBinaryPrinter(sink, (lib) => !lib.isExternal, false);
printer.writeProgramFile(program);
} finally {
await sink.close();
}
}