blob: 61542c70d16bef833cc8db9288960b63e638bd12 [file] [log] [blame]
// Copyright (c) 2015, 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.
/// This tools verifies that all elements that are included in the output are
/// reachable from the program entrypoint. If there are elements that are not
/// reachable from the entrypoint, then this indicates that we are missing
/// dependencies. If all functions are reachable from the entrypoint, this
/// script will return with exitcode 0. Otherwise it will list the unreachable
/// functions and return with exitcode 1.
library dart2js_info.bin.verify_deps;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dart2js_info/info.dart';
import 'package:dart2js_info/src/graph.dart';
import 'package:dart2js_info/src/util.dart';
Future main(List<String> args) async {
if (args.length > 1) {
printUsage();
exit(1);
}
var json = JSON.decode(await new File(args[0]).readAsString());
var info = new AllInfoJsonCodec().decode(json);
var graph = graphFromInfo(info);
var entrypoint = info.program.entrypoint;
var reachables = findReachable(graph, entrypoint);
var unreachables = info.functions.where((func) => !reachables.contains(func));
if (unreachables.isNotEmpty) {
unreachables.forEach(print);
exit(1);
} else {
print('all elements are reachable from the entrypoint');
}
}
/// Finds the set of nodes reachable from [start] in [graph].
Set<Info> findReachable(Graph<Info> graph, Info start) {
var visited = new Set<Info>();
var stack = <Info>[start];
while (stack.isNotEmpty) {
var next = stack.removeLast();
visited.add(next);
stack.addAll(
graph.targetsOf(next).where((target) => !visited.contains(target)));
}
return visited;
}
void printUsage() {
print('usage: dart2js_info_verify_deps <info file>');
}