- add sources.
[platform/framework/web/crosswalk.git] / src / tools / gn / command_help.cc
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 <algorithm>
6 #include <iostream>
7
8 #include "tools/gn/args.h"
9 #include "tools/gn/commands.h"
10 #include "tools/gn/err.h"
11 #include "tools/gn/file_template.h"
12 #include "tools/gn/functions.h"
13 #include "tools/gn/input_conversion.h"
14 #include "tools/gn/pattern.h"
15 #include "tools/gn/setup.h"
16 #include "tools/gn/standard_out.h"
17 #include "tools/gn/variables.h"
18
19 namespace commands {
20
21 namespace {
22
23 void PrintToplevelHelp() {
24   OutputString("Commands (type \"gn help <command>\" for more details):\n");
25
26   const commands::CommandInfoMap& command_map = commands::GetCommands();
27   for (commands::CommandInfoMap::const_iterator i = command_map.begin();
28        i != command_map.end(); ++i)
29     PrintShortHelp(i->second.help_short);
30
31   OutputString(
32       "\n"
33       "  When run with no arguments \"gn gen\" is assumed.\n"
34       "\n"
35       "Common switches:\n");
36   PrintShortHelp(
37       "--args: Specifies build args overrides. See \"gn help buildargs\".");
38   PrintShortHelp(
39       "--no-exec: Skips exec_script calls (for performance testing).");
40   PrintShortHelp(
41       "-q: Quiet mode, don't print anything on success.");
42   PrintShortHelp(
43       "--output: Directory for build output (relative to source root).");
44   PrintShortHelp(
45       "--root: Specifies source root (overrides .gn file).");
46   PrintShortHelp(
47       "--secondary: Specifies secondary source root (overrides .gn file).");
48   PrintShortHelp(
49       "--time: Outputs a summary of how long everything took.");
50   PrintShortHelp(
51       "--tracelog: Writes a Chrome-compatible trace log to the given file.");
52   PrintShortHelp(
53       "-v: Verbose mode, print lots of logging.");
54
55   // Functions.
56   OutputString("\nBuildfile functions (type \"gn help <function>\" for more "
57                "details):\n");
58   const functions::FunctionInfoMap& function_map = functions::GetFunctions();
59   std::vector<std::string> sorted_functions;
60   for (functions::FunctionInfoMap::const_iterator i = function_map.begin();
61        i != function_map.end(); ++i)
62     sorted_functions.push_back(i->first.as_string());
63   std::sort(sorted_functions.begin(), sorted_functions.end());
64   for (size_t i = 0; i < sorted_functions.size(); i++)
65     OutputString("  " + sorted_functions[i] + "\n", DECORATION_YELLOW);
66
67   // Built-in variables.
68   OutputString("\nBuilt-in predefined variables (type \"gn help <variable>\" "
69                "for more details):\n");
70   const variables::VariableInfoMap& builtin_vars =
71       variables::GetBuiltinVariables();
72   for (variables::VariableInfoMap::const_iterator i = builtin_vars.begin();
73        i != builtin_vars.end(); ++i)
74     PrintShortHelp(i->second.help_short);
75
76   // Target variables.
77   OutputString("\nVariables you set in targets (type \"gn help <variable>\" "
78                "for more details):\n");
79   const variables::VariableInfoMap& target_vars =
80       variables::GetTargetVariables();
81   for (variables::VariableInfoMap::const_iterator i = target_vars.begin();
82        i != target_vars.end(); ++i)
83     PrintShortHelp(i->second.help_short);
84
85   OutputString("\nOther help topics:\n");
86   PrintShortHelp("buildargs: How build arguments work.");
87   PrintShortHelp("dotfile: Info about the toplevel .gn file.");
88   PrintShortHelp(
89       "input_conversion: Processing input from exec_script and read_file.");
90   PrintShortHelp("patterns: How to use patterns.");
91   PrintShortHelp("source_expansion: Map sources to outputs for scripts.");
92 }
93
94 }  // namespace
95
96 const char kHelp[] = "help";
97 const char kHelp_HelpShort[] =
98     "help: Does what you think.";
99 const char kHelp_Help[] =
100     "gn help <anything>\n"
101     "  Yo dawg, I heard you like help on your help so I put help on the help\n"
102     "  in the help.\n";
103
104 int RunHelp(const std::vector<std::string>& args) {
105   if (args.size() == 0) {
106     PrintToplevelHelp();
107     return 0;
108   }
109
110   // Check commands.
111   const commands::CommandInfoMap& command_map = commands::GetCommands();
112   commands::CommandInfoMap::const_iterator found_command =
113       command_map.find(args[0]);
114   if (found_command != command_map.end()) {
115     PrintLongHelp(found_command->second.help);
116     return 0;
117   }
118
119   // Check functions.
120   const functions::FunctionInfoMap& function_map = functions::GetFunctions();
121   functions::FunctionInfoMap::const_iterator found_function =
122       function_map.find(args[0]);
123   if (found_function != function_map.end()) {
124     PrintLongHelp(found_function->second.help);
125     return 0;
126   }
127
128   // Builtin variables.
129   const variables::VariableInfoMap& builtin_vars =
130       variables::GetBuiltinVariables();
131   variables::VariableInfoMap::const_iterator found_builtin_var =
132       builtin_vars.find(args[0]);
133   if (found_builtin_var != builtin_vars.end()) {
134     PrintLongHelp(found_builtin_var->second.help);
135     return 0;
136   }
137
138   // Target variables.
139   const variables::VariableInfoMap& target_vars =
140       variables::GetTargetVariables();
141   variables::VariableInfoMap::const_iterator found_target_var =
142       target_vars.find(args[0]);
143   if (found_target_var != target_vars.end()) {
144     PrintLongHelp(found_target_var->second.help);
145     return 0;
146   }
147
148   // Random other topics.
149   if (args[0] == "buildargs") {
150     PrintLongHelp(kBuildArgs_Help);
151     return 0;
152   }
153   if (args[0] == "dotfile") {
154     PrintLongHelp(kDotfile_Help);
155     return 0;
156   }
157   if (args[0] == "input_conversion") {
158     PrintLongHelp(kInputConversion_Help);
159     return 0;
160   }
161   if (args[0] == "patterns") {
162     PrintLongHelp(kPattern_Help);
163     return 0;
164   }
165   if (args[0] == "source_expansion") {
166     PrintLongHelp(kSourceExpansion_Help);
167     return 0;
168   }
169
170   // No help on this.
171   Err(Location(), "No help on \"" + args[0] + "\".").PrintToStdout();
172   RunHelp(std::vector<std::string>());
173   return 1;
174 }
175
176 }  // namespace commands