From b1afd603beeed7ac387faa8fc958db622b984043 Mon Sep 17 00:00:00 2001 From: Nicolas Arciniega Date: Mon, 3 Feb 2020 11:40:46 -0800 Subject: Add 'inputs' tool to print out all inputs for a set of targets --- doc/manual.asciidoc | 4 ++++ src/ninja.cc | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/manual.asciidoc b/doc/manual.asciidoc index 9976ce4..cdff9c0 100644 --- a/doc/manual.asciidoc +++ b/doc/manual.asciidoc @@ -257,6 +257,10 @@ than the _depth_ mode. executed in order, may be used to rebuild those targets, assuming that all output files are out of date. +`inputs`:: given a list of targets, print a list of all inputs which are used +to rebuild those targets. +_Available since Ninja 1.10._ + `clean`:: remove built files. By default it removes all built files except for those created by the generator. Adding the `-g` flag also removes built files created by the generator (see <* seen, PrintCommandMode mode) { } int NinjaMain::ToolCommands(const Options* options, int argc, char* argv[]) { - // The clean tool uses getopt, and expects argv[0] to contain the name of + // The commands tool uses getopt, and expects argv[0] to contain the name of // the tool, i.e. "commands". ++argc; --argv; @@ -669,6 +670,35 @@ int NinjaMain::ToolCommands(const Options* options, int argc, char* argv[]) { return 0; } +void PrintInputs(Edge* edge, set* seen) { + if (!edge) + return; + if (!seen->insert(edge).second) + return; + + for (vector::iterator in = edge->inputs_.begin(); + in != edge->inputs_.end(); ++in) + PrintInputs((*in)->in_edge(), seen); + + if (!edge->is_phony()) + puts(edge->GetBinding("in_newline").c_str()); +} + +int NinjaMain::ToolInputs(const Options* options, int argc, char* argv[]) { + vector nodes; + string err; + if (!CollectTargetsFromArgs(argc, argv, &nodes, &err)) { + Error("%s", err.c_str()); + return 1; + } + + set seen; + for (vector::iterator in = nodes.begin(); in != nodes.end(); ++in) + PrintInputs((*in)->in_edge(), &seen); + + return 0; +} + int NinjaMain::ToolClean(const Options* options, int argc, char* argv[]) { // The clean tool uses getopt, and expects argv[0] to contain the name of // the tool, i.e. "clean". @@ -956,6 +986,8 @@ const Tool* ChooseTool(const string& tool_name) { Tool::RUN_AFTER_LOAD, &NinjaMain::ToolClean }, { "commands", "list all commands required to rebuild given targets", Tool::RUN_AFTER_LOAD, &NinjaMain::ToolCommands }, + { "inputs", "list all inputs required to rebuild given targets", + Tool::RUN_AFTER_LOAD, &NinjaMain::ToolInputs}, { "deps", "show dependencies stored in the deps log", Tool::RUN_AFTER_LOGS, &NinjaMain::ToolDeps }, { "graph", "output graphviz dot file for targets", -- cgit v1.2.1