1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/command_line.h"
6 #include "gn/args.h"
7 #include "gn/commands.h"
8 #include "gn/err.h"
9 #include "gn/functions.h"
10 #include "gn/input_conversion.h"
11 #include "gn/label.h"
12 #include "gn/label_pattern.h"
13 #include "gn/metadata.h"
14 #include "gn/ninja_build_writer.h"
15 #include "gn/output_conversion.h"
16 #include "gn/parser.h"
17 #include "gn/pattern.h"
18 #include "gn/runtime_deps.h"
19 #include "gn/setup.h"
20 #include "gn/standard_out.h"
21 #include "gn/string_utils.h"
22 #include "gn/substitution_writer.h"
23 #include "gn/switches.h"
24 #include "gn/target.h"
25 #include "gn/variables.h"
26 
27 namespace commands {
28 
29 namespace {
30 
31 // Some names exist in multiple sections, these prefixes are used for the
32 // internal links to disambiguate when writing markdown.
33 const char kCommandLinkPrefix[] = "cmd_";
34 const char kFunctionLinkPrefix[] = "func_";
35 const char kVariableLinkPrefix[] = "var_";
36 
PrintToplevelHelp()37 void PrintToplevelHelp() {
38   PrintSectionHelp("Commands", "<command>", "commands");
39   for (const auto& cmd : commands::GetCommands())
40     PrintShortHelp(cmd.second.help_short, kCommandLinkPrefix + cmd.first);
41 
42   // Target declarations.
43   PrintSectionHelp("Target declarations", "<function>", "targets");
44   for (const auto& func : functions::GetFunctions()) {
45     if (func.second.is_target)
46       PrintShortHelp(func.second.help_short, kFunctionLinkPrefix + func.first);
47   }
48 
49   // Functions.
50   PrintSectionHelp("Buildfile functions", "<function>", "functions");
51   for (const auto& func : functions::GetFunctions()) {
52     if (!func.second.is_target)
53       PrintShortHelp(func.second.help_short, kFunctionLinkPrefix + func.first);
54   }
55 
56   // Built-in variables.
57   PrintSectionHelp("Built-in predefined variables", "<variable>",
58                    "predefined_variables");
59   for (const auto& builtin : variables::GetBuiltinVariables()) {
60     PrintShortHelp(builtin.second.help_short,
61                    kVariableLinkPrefix + builtin.first);
62   }
63 
64   // Target variables.
65   PrintSectionHelp("Variables you set in targets", "<variable>",
66                    "target_variables");
67   for (const auto& target : variables::GetTargetVariables()) {
68     PrintShortHelp(target.second.help_short,
69                    kVariableLinkPrefix + target.first);
70   }
71 
72   PrintSectionHelp("Other help topics", "", "other");
73   PrintShortHelp("all: Print all the help at once");
74   PrintShortHelp("buildargs: How build arguments work.", "buildargs");
75   PrintShortHelp("dotfile: Info about the toplevel .gn file.", "dotfile");
76   PrintShortHelp("execution: Build graph and execution overview.", "execution");
77   PrintShortHelp("grammar: Language and grammar for GN build files.",
78                  "grammar");
79   PrintShortHelp(
80       "input_conversion: Processing input from exec_script and read_file.",
81       "io_conversion");
82   PrintShortHelp("file_pattern: Matching more than one file.", "file_pattern");
83   PrintShortHelp("label_pattern: Matching more than one label.",
84                  "label_pattern");
85   PrintShortHelp("labels: About labels.", "labels");
86   PrintShortHelp("metadata_collection: About metadata and its collection.",
87                  "metadata_collection");
88   PrintShortHelp("ninja_rules: How Ninja build rules are named.",
89                  "ninja_rules");
90   PrintShortHelp("nogncheck: Annotating includes for checking.", "nogncheck");
91   PrintShortHelp(
92       "output_conversion: Specifies how to transform a value to output.",
93       "io_conversion");
94   PrintShortHelp("runtime_deps: How runtime dependency computation works.",
95                  "runtime_deps");
96   PrintShortHelp("source_expansion: Map sources to outputs for scripts.",
97                  "source_expansion");
98   PrintShortHelp("switches: Show available command-line switches.",
99                  "switch_list");
100 }
101 
PrintSwitchHelp()102 void PrintSwitchHelp() {
103   const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
104   bool is_markdown = cmdline->HasSwitch(switches::kMarkdown);
105 
106   // This uses "switch_list" for the tag because Markdown seems to generate
107   // implicit tags for headings that match the strings, and some headings are
108   // labeled "switches".
109   PrintLongHelp(R"(Available global switches
110 
111   Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
112   take command-specific switches not listed here. See the help on your specific
113   command for more.
114 )",
115                 "switch_list");
116 
117   if (is_markdown)
118     OutputString("```\n", DECORATION_NONE);
119 
120   for (const auto& s : switches::GetSwitches())
121     PrintShortHelp(s.second.short_help);
122 
123   if (is_markdown)
124     OutputString("```\n", DECORATION_NONE);
125 
126   OutputString("\n");
127 }
128 
PrintAllHelp()129 void PrintAllHelp() {
130   const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
131   bool is_markdown = cmdline->HasSwitch(switches::kMarkdown);
132 
133   if (is_markdown) {
134     OutputString("# GN Reference\n\n");
135     OutputString(
136         "*This page is automatically generated from* "
137         "`gn help --markdown all`.\n\n");
138 
139     // Generate our own table of contents so that we have more control
140     // over what's in and out.
141     OutputString("## Contents\n\n");
142   }
143 
144   PrintToplevelHelp();
145 
146   OutputString("\n");
147 
148   if (is_markdown) {
149     OutputString("## <a name=\"commands\"></a>Commands\n\n", DECORATION_NONE,
150                  NO_ESCAPING);
151   }
152   for (const auto& c : commands::GetCommands())
153     PrintLongHelp(c.second.help, kCommandLinkPrefix + c.first);
154 
155   if (is_markdown) {
156     OutputString("## <a name=\"targets\"></a>Target declarations\n\n",
157                  DECORATION_NONE, NO_ESCAPING);
158   }
159   for (const auto& f : functions::GetFunctions()) {
160     if (f.second.is_target)
161       PrintLongHelp(f.second.help, kFunctionLinkPrefix + f.first);
162   }
163 
164   if (is_markdown) {
165     OutputString("## <a name=\"functions\"></a>Buildfile functions\n\n",
166                  DECORATION_NONE, NO_ESCAPING);
167   }
168   for (const auto& f : functions::GetFunctions()) {
169     if (!f.second.is_target)
170       PrintLongHelp(f.second.help, kFunctionLinkPrefix + f.first);
171   }
172 
173   if (is_markdown) {
174     OutputString(
175         "## <a name=\"predefined_variables\"></a>"
176         "Built-in predefined variables\n\n",
177         DECORATION_NONE, NO_ESCAPING);
178   }
179   for (const auto& v : variables::GetBuiltinVariables())
180     PrintLongHelp(v.second.help, kVariableLinkPrefix + v.first);
181 
182   if (is_markdown) {
183     OutputString(
184         "## <a name=\"target_variables\"></a>"
185         "Variables you set in targets\n\n",
186         DECORATION_NONE, NO_ESCAPING);
187   }
188   for (const auto& v : variables::GetTargetVariables())
189     PrintLongHelp(v.second.help, kVariableLinkPrefix + v.first);
190 
191   if (is_markdown) {
192     OutputString("## <a name=\"other\"></a>Other help topics\n\n",
193                  DECORATION_NONE, NO_ESCAPING);
194   }
195   PrintLongHelp(kBuildArgs_Help, "buildargs");
196   PrintLongHelp(kDotfile_Help, "dotfile");
197   PrintLongHelp(kExecution_Help, "execution");
198   PrintLongHelp(kGrammar_Help, "grammar");
199   PrintLongHelp(kInputOutputConversion_Help, "io_conversion");
200   PrintLongHelp(kFilePattern_Help, "file_pattern");
201   PrintLongHelp(kLabelPattern_Help, "label_pattern");
202   PrintLongHelp(kLabels_Help, "labels");
203   PrintLongHelp(kMetadata_Help, "metadata_collection");
204   PrintLongHelp(kNinjaRules_Help, "ninja_rules");
205   PrintLongHelp(kNoGnCheck_Help, "nogncheck");
206   PrintLongHelp(kRuntimeDeps_Help, "runtime_deps");
207   PrintLongHelp(kSourceExpansion_Help, "source_expansion");
208 
209   PrintSwitchHelp();
210 }
211 
212 // Prints help on the given switch. There should be no leading hyphens. Returns
213 // true if the switch was found and help was printed. False means the switch is
214 // unknown.
PrintHelpOnSwitch(const std::string & what)215 bool PrintHelpOnSwitch(const std::string& what) {
216   const switches::SwitchInfoMap& all = switches::GetSwitches();
217   switches::SwitchInfoMap::const_iterator found =
218       all.find(std::string_view(what));
219   if (found == all.end())
220     return false;
221   PrintLongHelp(found->second.long_help);
222   return true;
223 }
224 
225 // Special-case help for ambiguous "args" case.
PrintArgsHelp()226 void PrintArgsHelp() {
227   PrintLongHelp(
228       "The string \"args\" is both a command and a variable for action "
229       "targets.\nShowing help for both...\n\n");
230   PrintLongHelp(commands::kArgs_Help);
231   PrintLongHelp(
232       "\n----------------------------------------------------------------------"
233       "---------\n\n");
234   PrintLongHelp(variables::kArgs_Help);
235 }
236 
237 }  // namespace
238 
239 const char kHelp[] = "help";
240 const char kHelp_HelpShort[] = "help: Does what you think.";
241 const char kHelp_Help[] =
242     R"(gn help <anything>
243 
244   Yo dawg, I heard you like help on your help so I put help on the help in the
245   help.
246 
247   You can also use "all" as the parameter to get all help at once.
248 
249 Switches
250 
251   --markdown
252       Format output in markdown syntax.
253 
254 Example
255 
256   gn help --markdown all
257       Dump all help to stdout in markdown format.
258 )";
259 
RunHelp(const std::vector<std::string> & args)260 int RunHelp(const std::vector<std::string>& args) {
261   std::string what;
262   if (args.size() == 0) {
263     // If no argument is specified, check for switches to allow things like
264     // "gn help --args" for help on the args switch.
265     const base::CommandLine::SwitchMap& switches =
266         base::CommandLine::ForCurrentProcess()->GetSwitches();
267     if (switches.empty()) {
268       // Still nothing, show help overview.
269       PrintToplevelHelp();
270       return 0;
271     }
272 
273     // Switch help needs to be done separately. The CommandLine will strip the
274     // switch separators so --args will come out as "args" which is then
275     // ambiguous with the variable named "args".
276     if (!PrintHelpOnSwitch(switches.begin()->first))
277       PrintToplevelHelp();
278     return 0;
279   } else {
280     what = args[0];
281   }
282 
283   std::vector<std::string_view> all_help_topics;
284 
285   // Special-case ambiguous topics.
286   if (what == "args") {
287     PrintArgsHelp();
288     return 0;
289   }
290 
291   // Check commands.
292   const commands::CommandInfoMap& command_map = commands::GetCommands();
293   auto found_command = command_map.find(what);
294   if (found_command != command_map.end()) {
295     PrintLongHelp(found_command->second.help);
296     return 0;
297   }
298   for (const auto& entry : command_map)
299     all_help_topics.push_back(entry.first);
300 
301   // Check functions.
302   const functions::FunctionInfoMap& function_map = functions::GetFunctions();
303   auto found_function = function_map.find(what);
304   if (found_function != function_map.end())
305     PrintLongHelp(found_function->second.help);
306   for (const auto& entry : function_map)
307     all_help_topics.push_back(entry.first);
308 
309   // Builtin variables.
310   const variables::VariableInfoMap& builtin_vars =
311       variables::GetBuiltinVariables();
312   auto found_builtin_var = builtin_vars.find(what);
313   if (found_builtin_var != builtin_vars.end())
314     PrintLongHelp(found_builtin_var->second.help);
315   for (const auto& entry : builtin_vars)
316     all_help_topics.push_back(entry.first);
317 
318   // Target variables.
319   const variables::VariableInfoMap& target_vars =
320       variables::GetTargetVariables();
321   auto found_target_var = target_vars.find(what);
322   if (found_target_var != target_vars.end())
323     PrintLongHelp(found_target_var->second.help);
324   for (const auto& entry : target_vars)
325     all_help_topics.push_back(entry.first);
326 
327   if (found_function != function_map.end() ||
328       found_builtin_var != builtin_vars.end() ||
329       found_target_var != target_vars.end())
330     return 0;
331 
332   // Random other topics.
333   std::map<std::string, void (*)()> random_topics;
334   random_topics["all"] = PrintAllHelp;
335   random_topics["execution"] = []() { PrintLongHelp(kExecution_Help); };
336   random_topics["buildargs"] = []() { PrintLongHelp(kBuildArgs_Help); };
337   random_topics["dotfile"] = []() { PrintLongHelp(kDotfile_Help); };
338   random_topics["grammar"] = []() { PrintLongHelp(kGrammar_Help); };
339   random_topics["io_conversion"] = []() {
340     PrintLongHelp(kInputOutputConversion_Help);
341   };
342   random_topics["file_pattern"] = []() { PrintLongHelp(kFilePattern_Help); };
343   random_topics["label_pattern"] = []() { PrintLongHelp(kLabelPattern_Help); };
344   random_topics["labels"] = []() { PrintLongHelp(kLabels_Help); };
345   random_topics["metadata_collection"] = []() {
346     PrintLongHelp(kMetadata_Help);
347   };
348   random_topics["ninja_rules"] = []() { PrintLongHelp(kNinjaRules_Help); };
349   random_topics["nogncheck"] = []() { PrintLongHelp(kNoGnCheck_Help); };
350   random_topics["runtime_deps"] = []() { PrintLongHelp(kRuntimeDeps_Help); };
351   random_topics["source_expansion"] = []() {
352     PrintLongHelp(kSourceExpansion_Help);
353   };
354   random_topics["switches"] = PrintSwitchHelp;
355   auto found_random_topic = random_topics.find(what);
356   if (found_random_topic != random_topics.end()) {
357     found_random_topic->second();
358     return 0;
359   }
360   for (const auto& entry : random_topics)
361     all_help_topics.push_back(entry.first);
362 
363   // No help on this.
364   Err(Location(), "No help on \"" + what + "\".").PrintToStdout();
365   std::string_view suggestion = SpellcheckString(what, all_help_topics);
366   if (suggestion.empty()) {
367     OutputString("Run `gn help` for a list of available topics.\n",
368                  DECORATION_NONE);
369   } else {
370     OutputString("Did you mean `gn help " + std::string(suggestion) + "`?\n",
371                  DECORATION_NONE);
372   }
373   return 1;
374 }
375 
376 }  // namespace commands
377