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 #ifndef TOOLS_GN_COMMANDS_H_
6 #define TOOLS_GN_COMMANDS_H_
7 
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <string_view>
12 #include <vector>
13 
14 #include "base/values.h"
15 #include "gn/target.h"
16 #include "gn/unique_vector.h"
17 
18 class BuildSettings;
19 class Config;
20 class LabelPattern;
21 class Setup;
22 class SourceFile;
23 class Target;
24 class Toolchain;
25 
26 // Each "Run" command returns the value we should return from main().
27 
28 namespace commands {
29 
30 using CommandRunner = int (*)(const std::vector<std::string>&);
31 
32 extern const char kAnalyze[];
33 extern const char kAnalyze_HelpShort[];
34 extern const char kAnalyze_Help[];
35 int RunAnalyze(const std::vector<std::string>& args);
36 
37 extern const char kArgs[];
38 extern const char kArgs_HelpShort[];
39 extern const char kArgs_Help[];
40 int RunArgs(const std::vector<std::string>& args);
41 
42 extern const char kCheck[];
43 extern const char kCheck_HelpShort[];
44 extern const char kCheck_Help[];
45 int RunCheck(const std::vector<std::string>& args);
46 
47 extern const char kClean[];
48 extern const char kClean_HelpShort[];
49 extern const char kClean_Help[];
50 int RunClean(const std::vector<std::string>& args);
51 
52 extern const char kDesc[];
53 extern const char kDesc_HelpShort[];
54 extern const char kDesc_Help[];
55 int RunDesc(const std::vector<std::string>& args);
56 
57 extern const char kGen[];
58 extern const char kGen_HelpShort[];
59 extern const char kGen_Help[];
60 int RunGen(const std::vector<std::string>& args);
61 
62 extern const char kFormat[];
63 extern const char kFormat_HelpShort[];
64 extern const char kFormat_Help[];
65 int RunFormat(const std::vector<std::string>& args);
66 
67 extern const char kHelp[];
68 extern const char kHelp_HelpShort[];
69 extern const char kHelp_Help[];
70 int RunHelp(const std::vector<std::string>& args);
71 
72 extern const char kMeta[];
73 extern const char kMeta_HelpShort[];
74 extern const char kMeta_Help[];
75 int RunMeta(const std::vector<std::string>& args);
76 
77 extern const char kLs[];
78 extern const char kLs_HelpShort[];
79 extern const char kLs_Help[];
80 int RunLs(const std::vector<std::string>& args);
81 
82 extern const char kOutputs[];
83 extern const char kOutputs_HelpShort[];
84 extern const char kOutputs_Help[];
85 int RunOutputs(const std::vector<std::string>& args);
86 
87 extern const char kPath[];
88 extern const char kPath_HelpShort[];
89 extern const char kPath_Help[];
90 int RunPath(const std::vector<std::string>& args);
91 
92 extern const char kRefs[];
93 extern const char kRefs_HelpShort[];
94 extern const char kRefs_Help[];
95 int RunRefs(const std::vector<std::string>& args);
96 
97 // -----------------------------------------------------------------------------
98 
99 struct CommandInfo {
100   CommandInfo();
101   CommandInfo(const char* in_help_short,
102               const char* in_help,
103               CommandRunner in_runner);
104 
105   const char* help_short;
106   const char* help;
107   CommandRunner runner;
108 };
109 
110 using CommandInfoMap = std::map<std::string_view, CommandInfo>;
111 
112 const CommandInfoMap& GetCommands();
113 
114 // Helper functions for some commands ------------------------------------------
115 
116 // Given a setup that has already been run and some command-line input,
117 // resolves that input as a target label and returns the corresponding target.
118 // On failure, returns null and prints the error to the standard output.
119 const Target* ResolveTargetFromCommandLineString(
120     Setup* setup,
121     const std::string& label_string);
122 
123 // Resolves a vector of command line inputs and figures out the full set of
124 // things they resolve to.
125 //
126 // On success, returns true and populates the vectors. On failure, prints the
127 // error and returns false.
128 //
129 // Patterns with wildcards will only match targets. The file_matches aren't
130 // validated that they are real files or referenced by any targets. They're just
131 // the set of things that didn't match anything else.
132 bool ResolveFromCommandLineInput(
133     Setup* setup,
134     const std::vector<std::string>& input,
135     bool default_toolchain_only,
136     UniqueVector<const Target*>* target_matches,
137     UniqueVector<const Config*>* config_matches,
138     UniqueVector<const Toolchain*>* toolchain_matches,
139     UniqueVector<SourceFile>* file_matches);
140 
141 // Runs the header checker. All targets in the build should be given in
142 // all_targets, and the specific targets to check should be in to_check.
143 //
144 // force_check, if true, will override targets opting out of header checking
145 // with "check_includes = false" and will check them anyway.
146 //
147 // Generated files are normally not checked since they do not exist
148 // unless a build has been run, but passing true for |check_generated|
149 // will attempt to check them anyway, assuming they exist.
150 //
151 // On success, returns true. If the check fails, the error(s) will be printed
152 // to stdout and false will be returned.
153 bool CheckPublicHeaders(const BuildSettings* build_settings,
154                         const std::vector<const Target*>& all_targets,
155                         const std::vector<const Target*>& to_check,
156                         bool force_check,
157                         bool check_generated,
158                         bool check_system);
159 
160 // Filters the given list of targets by the given pattern list.
161 void FilterTargetsByPatterns(const std::vector<const Target*>& input,
162                              const std::vector<LabelPattern>& filter,
163                              std::vector<const Target*>* output);
164 void FilterTargetsByPatterns(const std::vector<const Target*>& input,
165                              const std::vector<LabelPattern>& filter,
166                              UniqueVector<const Target*>* output);
167 
168 // Removes targets from the input that match the given pattern list.
169 void FilterOutTargetsByPatterns(const std::vector<const Target*>& input,
170                                 const std::vector<LabelPattern>& filter,
171                                 std::vector<const Target*>* output);
172 
173 // Builds a list of pattern from a semicolon-separated list of labels.
174 bool FilterPatternsFromString(const BuildSettings* build_settings,
175                               const std::string& label_list_string,
176                               std::vector<LabelPattern>* filters,
177                               Err* err);
178 
179 // These are the documentation strings for the command-line flags used by
180 // FilterAndPrintTargets. Commands that call that function should incorporate
181 // these into their help.
182 #define TARGET_PRINTING_MODE_COMMAND_LINE_HELP                                \
183   "  --as=(buildfile|label|output)\n"                                         \
184   "      How to print targets.\n"                                             \
185   "\n"                                                                        \
186   "      buildfile\n"                                                         \
187   "          Prints the build files where the given target was declared as\n" \
188   "          file names.\n"                                                   \
189   "      label  (default)\n"                                                  \
190   "          Prints the label of the target.\n"                               \
191   "      output\n"                                                            \
192   "          Prints the first output file for the target relative to the\n"   \
193   "          root build directory.\n"
194 #define TARGET_TYPE_FILTER_COMMAND_LINE_HELP                                 \
195   "  --type=(action|copy|executable|group|loadable_module|shared_library|\n" \
196   "          source_set|static_library)\n"                                   \
197   "      Restrict outputs to targets matching the given type. If\n"          \
198   "      unspecified, no filtering will be performed.\n"
199 #define TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP                           \
200   "  --testonly=(true|false)\n"                                            \
201   "      Restrict outputs to targets with the testonly flag set\n"         \
202   "      accordingly. When unspecified, the target's testonly flags are\n" \
203   "      ignored.\n"
204 
205 // Applies any testonly and type filters specified on the command line,
206 // and prints the targets as specified by the --as command line flag.
207 //
208 // If indent is true, the results will be indented two spaces.
209 //
210 // The vector will be modified so that only the printed targets will remain.
211 void FilterAndPrintTargets(bool indent, std::vector<const Target*>* targets);
212 void FilterAndPrintTargets(std::vector<const Target*>* targets,
213                            base::ListValue* out);
214 
215 void FilterAndPrintTargetSet(bool indent,
216                              const std::set<const Target*>& targets);
217 void FilterAndPrintTargetSet(const std::set<const Target*>& targets,
218                              base::ListValue* out);
219 
220 // Computes which targets reference the given file and also stores how the
221 // target references the file.
222 enum class HowTargetContainsFile {
223   kSources,
224   kPublic,
225   kInputs,
226   kData,
227   kScript,
228   kOutput,
229 };
230 using TargetContainingFile = std::pair<const Target*, HowTargetContainsFile>;
231 void GetTargetsContainingFile(Setup* setup,
232                               const std::vector<const Target*>& all_targets,
233                               const SourceFile& file,
234                               bool default_toolchain_only,
235                               std::vector<TargetContainingFile>* matches);
236 
237 // Extra help from command_check.cc
238 extern const char kNoGnCheck_Help[];
239 
240 }  // namespace commands
241 
242 #endif  // TOOLS_GN_COMMANDS_H_
243