Skip to content

Commit

Permalink
Elide gn desc --tree output by default.
Browse files Browse the repository at this point in the history
When gn's desc command is used with --tree, it prints a dependency tree. But recursively printing the dependency tree of a high-level target in Chrome prints too much output to be useful because it's printing, e.g. base and it's deps over and over (//chrome/browser-s --tree is 3210166 lines).

With this patch, a target will be recursed into the first time it's printed, but subsequent times the children will be elided with "..." This brings down //chrome/browser to 2050 lines. The previous behavior is still available (it could be useful in certain cases) via "--tree --all".

This also updates an optimization comment in the the optimization setup (optimize_max isn't just Windows-only).

R=dalecurtis@chromium.org

Review URL: https://codereview.chromium.org/437033002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287424 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Aug 5, 2014
1 parent c8fe838 commit 9019e16
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,9 @@ config("no_optimize") {
}
}

# On Windows, turns up the optimization level. This implies whole program
# Turns up the optimization level. On Windows, this implies whole program
# optimization and link-time code generation which is very expensive and should
# be used sparingly. For non-Windows, this is the same as "optimize".
# be used sparingly.
config("optimize_max") {
cflags = common_optimize_on_cflags
ldflags = common_optimize_on_ldflags
Expand Down
54 changes: 45 additions & 9 deletions tools/gn/command_desc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ void RecursiveCollectChildDeps(const Target* target, std::set<Label>* result) {
RecursiveCollectDeps(datadeps[i].ptr, result);
}

// Prints dependencies of the given target (not the target itself).
// Prints dependencies of the given target (not the target itself). If the
// set is non-null, new targets encountered will be added to the set, and if
// a dependency is in the set already, it will not be recused into. When the
// set is null, all dependencies will be printed.
void RecursivePrintDeps(const Target* target,
const Label& default_toolchain,
std::set<const Target*>* seen_targets,
int indent_level) {
LabelTargetVector sorted_deps = target->deps();
const LabelTargetVector& datadeps = target->datadeps();
Expand All @@ -70,6 +74,8 @@ void RecursivePrintDeps(const Target* target,

std::string indent(indent_level * 2, ' ');
for (size_t i = 0; i < sorted_deps.size(); i++) {
const Target* cur_dep = sorted_deps[i].ptr;

// Don't print groups. Groups are flattened such that the deps of the
// group are added directly to the target that depended on the group.
// Printing and recursing into groups here will cause such targets to be
Expand All @@ -78,12 +84,31 @@ void RecursivePrintDeps(const Target* target,
// It would be much more intuitive to do the opposite and not display the
// deps that were copied from the group to the target and instead display
// the group, but the source of those dependencies is not tracked.
if (sorted_deps[i].ptr->output_type() == Target::GROUP)
if (cur_dep->output_type() == Target::GROUP)
continue;

OutputString(indent +
sorted_deps[i].label.GetUserVisibleName(default_toolchain) + "\n");
RecursivePrintDeps(sorted_deps[i].ptr, default_toolchain, indent_level + 1);
cur_dep->label().GetUserVisibleName(default_toolchain));
bool print_children = true;
if (seen_targets) {
if (seen_targets->find(cur_dep) == seen_targets->end()) {
// New target, mark it visited.
seen_targets->insert(cur_dep);
} else {
// Already seen.
print_children = false;
// Only print "..." if something is actually elided, which means that
// the current target has children.
if (!cur_dep->deps().empty() || !cur_dep->datadeps().empty())
OutputString("...");
}
}

OutputString("\n");
if (print_children) {
RecursivePrintDeps(cur_dep, default_toolchain, seen_targets,
indent_level + 1);
}
}
}

Expand All @@ -95,7 +120,15 @@ void PrintDeps(const Target* target, bool display_header) {
if (cmdline->HasSwitch("tree")) {
if (display_header)
OutputString("\nDependency tree:\n");
RecursivePrintDeps(target, toolchain_label, 1);

if (cmdline->HasSwitch("all")) {
// Show all tree deps with no eliding.
RecursivePrintDeps(target, toolchain_label, NULL, 1);
} else {
// Don't recurse into duplicates.
std::set<const Target*> seen_targets;
RecursivePrintDeps(target, toolchain_label, &seen_targets, 1);
}
return;
}

Expand All @@ -112,8 +145,9 @@ void PrintDeps(const Target* target, bool display_header) {
deps.push_back(*i);
} else {
if (display_header) {
OutputString("\nDirect dependencies "
"(try also \"--all\" and \"--tree\"):\n");
OutputString(
"\nDirect dependencies "
"(try also \"--all\", \"--tree\", or even \"--all --tree\"):\n");
}

const LabelTargetVector& target_deps = target->deps();
Expand Down Expand Up @@ -420,8 +454,10 @@ const char kDesc_Help[] =
" deps [--all | --tree]\n"
" Show immediate (or, when \"--all\" or \"--tree\" is specified,\n"
" recursive) dependencies of the given target. \"--tree\" shows them\n"
" in a tree format. Otherwise, they will be sorted alphabetically.\n"
" Both \"deps\" and \"datadeps\" will be included.\n"
" in a tree format with duplicates elided (noted by \"...\").\n"
" \"--all\" shows them sorted alphabetically. Using both flags will\n"
" print a tree with no omissions. Both \"deps\" and \"datadeps\"\n"
" will be included.\n"
"\n"
" direct_dependent_configs\n"
" all_dependent_configs\n"
Expand Down

0 comments on commit 9019e16

Please sign in to comment.