• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "gn/switches.h"
6 
7 namespace switches {
8 
9 const char kArgs[] = "args";
10 const char kArgs_HelpShort[] = "--args: Specifies build arguments overrides.";
11 const char kArgs_Help[] =
12     R"(--args: Specifies build arguments overrides.
13 
14   See "gn help buildargs" for an overview of how build arguments work.
15 
16   Most operations take a build directory. The build arguments are taken from
17   the previous build done in that directory. If a command specifies --args, it
18   will override the previous arguments stored in the build directory, and use
19   the specified ones.
20 
21   The args specified will be saved to the build directory for subsequent
22   commands. Specifying --args="" will clear all build arguments.
23 
24 Formatting
25 
26   The value of the switch is interpreted in GN syntax. For typical usage of
27   string arguments, you will need to be careful about escaping of quotes.
28 
29 Examples
30 
31   gn gen out/Default --args="foo=\"bar\""
32 
33   gn gen out/Default --args='foo="bar" enable=true blah=7'
34 
35   gn check out/Default --args=""
36     Clears existing build args from the directory.
37 
38   gn desc out/Default --args="some_list=[1, false, \"foo\"]"
39 )";
40 
41 #define COLOR_HELP_LONG                                                       \
42   "--[no]color: Forces colored output on or off.\n"                           \
43   "\n"                                                                        \
44   "  Normally GN will try to detect whether it is outputting to a terminal\n" \
45   "  and will enable or disable color accordingly. Use of these switches\n"   \
46   "  will override the default.\n"                                            \
47   "\n"                                                                        \
48   "Examples\n"                                                                \
49   "\n"                                                                        \
50   "  gn gen out/Default --color\n"                                            \
51   "\n"                                                                        \
52   "  gn gen out/Default --nocolor\n"
53 const char kColor[] = "color";
54 const char kColor_HelpShort[] = "--color: Force colored output.";
55 const char kColor_Help[] = COLOR_HELP_LONG;
56 
57 const char kDotfile[] = "dotfile";
58 const char kDotfile_HelpShort[] =
59     "--dotfile: Override the name of the \".gn\" file.";
60 const char kDotfile_Help[] =
61     R"(--dotfile: Override the name of the ".gn" file.
62 
63   Normally GN loads the ".gn" file from the source root for some basic
64   configuration (see "gn help dotfile"). This flag allows you to
65   use a different file.
66 )";
67 
68 const char kFailOnUnusedArgs[] = "fail-on-unused-args";
69 const char kFailOnUnusedArgs_HelpShort[] =
70     "--fail-on-unused-args: Treat unused build args as fatal errors.";
71 const char kFailOnUnusedArgs_Help[] =
72     R"(--fail-on-unused-args: Treat unused build args as fatal errors.
73 
74   If you set a value in a build's "gn args" and never use it in the build (in
75   a declare_args() block), GN will normally print an error but not fail the
76   build.
77 
78   In many cases engineers would use build args to enable or disable features
79   that would sometimes get removed. It would by annoying to block work for
80   typically benign problems. In Chrome in particular, flags might be configured
81   for build bots in a separate infrastructure repository, or a declare_args
82   block might be changed in a third party repository. Treating these errors as
83   blocking forced complex multi- way patches to land what would otherwise be
84   simple changes.
85 
86   In some cases, such concerns are not as important, and a mismatch in build
87   flags between the invoker of the build and the build files represents a
88   critical mismatch that should be immediately fixed. Such users can set this
89   flag to force GN to fail in that case.
90 )";
91 
92 const char kMarkdown[] = "markdown";
93 const char kMarkdown_HelpShort[] =
94     "--markdown: Write help output in the Markdown format.";
95 const char kMarkdown_Help[] =
96     "--markdown: Write help output in the Markdown format.\n";
97 
98 const char kNoColor[] = "nocolor";
99 const char kNoColor_HelpShort[] = "--nocolor: Force non-colored output.";
100 const char kNoColor_Help[] = COLOR_HELP_LONG;
101 
102 const char kNinjaExecutable[] = "ninja-executable";
103 const char kNinjaExecutable_HelpShort[] =
104     "--ninja-executable: Set the Ninja executable.";
105 const char kNinjaExecutable_Help[] =
106     R"(--ninja-executable: Set the Ninja executable.
107 
108   When set specifies the Ninja executable that will be used to perform some
109   post-processing on the generated files for more consistent builds.
110 )";
111 
112 const char kScriptExecutable[] = "script-executable";
113 const char kScriptExecutable_HelpShort[] =
114     "--script-executable: Set the executable used to execute scripts.";
115 const char kScriptExecutable_Help[] =
116     R"(--script-executable: Set the executable used to execute scripts.
117 
118   Path to specific Python executable or other interpreter to use in
119   action targets and exec_script calls. By default GN searches the
120   PATH for Python to execute these scripts.
121 
122   If set to the empty string, the path of scripts specified in action
123   targets and exec_script calls will be executed directly.
124 )";
125 
126 const char kQuiet[] = "q";
127 const char kQuiet_HelpShort[] =
128     "-q: Quiet mode. Don't print output on success.";
129 const char kQuiet_Help[] =
130     R"(-q: Quiet mode. Don't print output on success.
131 
132   This is useful when running as a part of another script.
133 )";
134 
135 const char kRoot[] = "root";
136 const char kRoot_HelpShort[] = "--root: Explicitly specify source root.";
137 const char kRoot_Help[] =
138     R"(--root: Explicitly specify source root.
139 
140   Normally GN will look up in the directory tree from the current directory to
141   find a ".gn" file. The source root directory specifies the meaning of "//"
142   beginning with paths, and the BUILD.gn file in that directory will be the
143   first thing loaded.
144 
145   Specifying --root allows GN to do builds in a specific directory regardless
146   of the current directory.
147 
148 Examples
149 
150   gn gen //out/Default --root=/home/baracko/src
151 
152   gn desc //out/Default --root="C:\Users\BObama\My Documents\foo"
153 )";
154 
155 const char kRootTarget[] = "root-target";
156 const char kRootTarget_HelpShort[] = "--root-target: Override the root target.";
157 const char kRootTarget_Help[] =
158     R"(--root-target: Override the root target.
159 
160   The root target is the target initially loaded to begin population of the
161   build graph. It defaults to "//:" which normally causes the "//BUILD.gn" file
162   to be loaded. It can be specified in the .gn file via the "root" variable (see
163   "gn help dotfile").
164 
165   If specified, the value of this switch will be take precedence over the value
166   in ".gn". The target name (after the colon) is ignored, only the directory
167   name is required. Relative paths will be resolved relative to the current "//"
168   directory.
169 
170   Specifying a different initial BUILD.gn file does not change the meaning of
171   the source root (the "//" directory) which can be independently set via the
172   --root switch. It also does not prevent the build file located at "//BUILD.gn"
173   from being loaded if a target in the build references that directory.
174 
175   One use-case of this feature is to load a different set of initial targets
176   from project that uses GN without modifying any files.
177 
178 Examples
179 
180   gn gen //out/Default --root-target="//third_party/icu"
181 
182   gn gen //out/Default --root-target="//third_party/grpc"
183 )";
184 
185 const char kRootPattern[] = "root-pattern";
186 const char kRootPattern_HelpShort[] =
187     "--root-pattern: Add root pattern override.";
188 const char kRootPattern_Help[] =
189     R"(--root-pattern: Add root pattern override.
190 
191   The root patterns is a list of label patterns used to control which
192   targets are defined when evaluating BUILD.gn files in the default toolchain.
193 
194   The list is empty by default, meaning that all targets defined in all
195   BUILD.gn files evaluated in the default toolchain will be added to the
196   final GN build graph (GN's default behavior for historical reasons).
197 
198   When this list is not empty, only targets matching any of the root patterns,
199   as well as their transitive dependencies, will be defined in the default
200   toolchain instead. This is a way to restrict the size of the final build graph
201   for projects with a very large number of target definitions per BUILD.gn file.
202 
203   Using --root-pattern overrides the root_patterns value specified in the .gn file.
204 
205   The argument must be a GN label pattern, and each --root-pattern=<pattern>
206   on the command-line will append a pattern to the list.
207 
208 Example
209 
210   gn gen //out/Default --root-pattern="//:*"
211 )";
212 
213 const char kRuntimeDepsListFile[] = "runtime-deps-list-file";
214 const char kRuntimeDepsListFile_HelpShort[] =
215     "--runtime-deps-list-file: Save runtime dependencies for targets in file.";
216 const char kRuntimeDepsListFile_Help[] =
217     R"(--runtime-deps-list-file: Save runtime dependencies for targets in file.
218 
219   --runtime-deps-list-file=<filename>
220 
221   Where <filename> is a text file consisting of the labels, one per line, of
222   the targets for which runtime dependencies are desired.
223 
224   See "gn help runtime_deps" for a description of how runtime dependencies are
225   computed.
226 
227 Runtime deps output file
228 
229   For each target requested, GN will write a separate runtime dependency file.
230   The runtime dependency file will be in the output directory alongside the
231   output file of the target, with a ".runtime_deps" extension. For example, if
232   the target "//foo:bar" is listed in the input file, and that target produces
233   an output file "bar.so", GN will create a file "bar.so.runtime_deps" in the
234   build directory.
235 
236   If a source set, action, copy, or group is listed, the runtime deps file will
237   correspond to the .stamp file corresponding to that target. This is probably
238   not useful; the use-case for this feature is generally executable targets.
239 
240   The runtime dependency file will list one file per line, with no escaping.
241   The files will be relative to the root_build_dir. The first line of the file
242   will be the main output file of the target itself (in the above example,
243   "bar.so").
244 )";
245 
246 const char kThreads[] = "threads";
247 const char kThreads_HelpShort[] =
248     "--threads: Specify number of worker threads.";
249 const char kThreads_Help[] =
250     R"(--threads: Specify number of worker threads.
251 
252   GN runs many threads to load and run build files. This can make debugging
253   challenging. Or you may want to experiment with different values to see how
254   it affects performance.
255 
256   The parameter is the number of worker threads. This does not count the main
257   thread (so there are always at least two).
258 
259 Examples
260 
261   gen gen out/Default --threads=1
262 )";
263 
264 const char kTime[] = "time";
265 const char kTime_HelpShort[] =
266     "--time: Outputs a summary of how long everything took.";
267 const char kTime_Help[] =
268     R"(--time: Outputs a summary of how long everything took.
269 
270   Hopefully self-explanatory.
271 
272 Examples
273 
274   gn gen out/Default --time
275 )";
276 
277 const char kTracelog[] = "tracelog";
278 const char kTracelog_HelpShort[] =
279     "--tracelog: Writes a Chrome-compatible trace log to the given file.";
280 const char kTracelog_Help[] =
281     R"(--tracelog: Writes a Chrome-compatible trace log to the given file.
282 
283   The trace log will show file loads, executions, scripts, and writes. This
284   allows performance analysis of the generation step.
285 
286   To view the trace, open Chrome and navigate to "chrome://tracing/", then
287   press "Load" and specify the file you passed to this parameter.
288 
289 Examples
290 
291   gn gen out/Default --tracelog=mytrace.trace
292 )";
293 
294 const char kVerbose[] = "v";
295 const char kVerbose_HelpShort[] = "-v: Verbose logging.";
296 const char kVerbose_Help[] =
297     R"(-v: Verbose logging.
298 
299   This will spew logging events to the console for debugging issues.
300 
301   Good luck!
302 )";
303 
304 const char kVersion[] = "version";
305 const char kVersion_HelpShort[] =
306     "--version: Prints the GN version number and exits.";
307 // It's impossible to see this since gn_main prints the version and exits
308 // immediately if this switch is used.
309 const char kVersion_Help[] = "";
310 
311 const char kDefaultToolchain[] = "default-toolchain";
312 
313 const char kRegeneration[] = "regeneration";
314 
315 const char kAddExportCompileCommands[] = "add-export-compile-commands";
316 
317 // -----------------------------------------------------------------------------
318 
SwitchInfo()319 SwitchInfo::SwitchInfo() : short_help(""), long_help("") {}
320 
SwitchInfo(const char * short_help,const char * long_help)321 SwitchInfo::SwitchInfo(const char* short_help, const char* long_help)
322     : short_help(short_help), long_help(long_help) {}
323 
324 #define INSERT_VARIABLE(var) \
325   info_map[k##var] = SwitchInfo(k##var##_HelpShort, k##var##_Help);
326 
GetSwitches()327 const SwitchInfoMap& GetSwitches() {
328   static SwitchInfoMap info_map;
329   if (info_map.empty()) {
330     INSERT_VARIABLE(Args)
331     INSERT_VARIABLE(Color)
332     INSERT_VARIABLE(Dotfile)
333     INSERT_VARIABLE(FailOnUnusedArgs)
334     INSERT_VARIABLE(Markdown)
335     INSERT_VARIABLE(NinjaExecutable)
336     INSERT_VARIABLE(NoColor)
337     INSERT_VARIABLE(Root)
338     INSERT_VARIABLE(RootTarget)
339     INSERT_VARIABLE(Quiet)
340     INSERT_VARIABLE(RuntimeDepsListFile)
341     INSERT_VARIABLE(ScriptExecutable)
342     INSERT_VARIABLE(Threads)
343     INSERT_VARIABLE(Time)
344     INSERT_VARIABLE(Tracelog)
345     INSERT_VARIABLE(Verbose)
346     INSERT_VARIABLE(Version)
347   }
348   return info_map;
349 }
350 
351 #undef INSERT_VARIABLE
352 
353 }  // namespace switches
354