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 extern const char kCleanStale[]; 98 extern const char kCleanStale_HelpShort[]; 99 extern const char kCleanStale_Help[]; 100 int RunCleanStale(const std::vector<std::string>& args); 101 102 // ----------------------------------------------------------------------------- 103 104 struct CommandInfo { 105 CommandInfo(); 106 CommandInfo(const char* in_help_short, 107 const char* in_help, 108 CommandRunner in_runner); 109 110 const char* help_short; 111 const char* help; 112 CommandRunner runner; 113 }; 114 115 using CommandInfoMap = std::map<std::string_view, CommandInfo>; 116 117 const CommandInfoMap& GetCommands(); 118 119 // Helper functions for some commands ------------------------------------------ 120 121 // Given a setup that has already been run and some command-line input, 122 // resolves that input as a target label and returns the corresponding target. 123 // On failure, returns null and prints the error to the standard output. 124 const Target* ResolveTargetFromCommandLineString( 125 Setup* setup, 126 const std::string& label_string); 127 128 // Resolves a vector of command line inputs and figures out the full set of 129 // things they resolve to. 130 // 131 // On success, returns true and populates the vectors. On failure, prints the 132 // error and returns false. 133 // 134 // Patterns with wildcards will only match targets. The file_matches aren't 135 // validated that they are real files or referenced by any targets. They're just 136 // the set of things that didn't match anything else. 137 bool ResolveFromCommandLineInput( 138 Setup* setup, 139 const std::vector<std::string>& input, 140 bool default_toolchain_only, 141 UniqueVector<const Target*>* target_matches, 142 UniqueVector<const Config*>* config_matches, 143 UniqueVector<const Toolchain*>* toolchain_matches, 144 UniqueVector<SourceFile>* file_matches); 145 146 // Runs the header checker. All targets in the build should be given in 147 // all_targets, and the specific targets to check should be in to_check. 148 // 149 // force_check, if true, will override targets opting out of header checking 150 // with "check_includes = false" and will check them anyway. 151 // 152 // Generated files are normally not checked since they do not exist 153 // unless a build has been run, but passing true for |check_generated| 154 // will attempt to check them anyway, assuming they exist. 155 // 156 // On success, returns true. If the check fails, the error(s) will be printed 157 // to stdout and false will be returned. 158 bool CheckPublicHeaders(const BuildSettings* build_settings, 159 const std::vector<const Target*>& all_targets, 160 const std::vector<const Target*>& to_check, 161 bool force_check, 162 bool check_generated, 163 bool check_system); 164 165 // Filters the given list of targets by the given pattern list. 166 void FilterTargetsByPatterns(const std::vector<const Target*>& input, 167 const std::vector<LabelPattern>& filter, 168 std::vector<const Target*>* output); 169 void FilterTargetsByPatterns(const std::vector<const Target*>& input, 170 const std::vector<LabelPattern>& filter, 171 UniqueVector<const Target*>* output); 172 173 // Removes targets from the input that match the given pattern list. 174 void FilterOutTargetsByPatterns(const std::vector<const Target*>& input, 175 const std::vector<LabelPattern>& filter, 176 std::vector<const Target*>* output); 177 178 // Builds a list of pattern from a semicolon-separated list of labels. 179 bool FilterPatternsFromString(const BuildSettings* build_settings, 180 const std::string& label_list_string, 181 std::vector<LabelPattern>* filters, 182 Err* err); 183 184 // These are the documentation strings for the command-line flags used by 185 // FilterAndPrintTargets. Commands that call that function should incorporate 186 // these into their help. 187 #define TARGET_PRINTING_MODE_COMMAND_LINE_HELP \ 188 " --as=(buildfile|label|output)\n" \ 189 " How to print targets.\n" \ 190 "\n" \ 191 " buildfile\n" \ 192 " Prints the build files where the given target was declared as\n" \ 193 " file names.\n" \ 194 " label (default)\n" \ 195 " Prints the label of the target.\n" \ 196 " output\n" \ 197 " Prints the first output file for the target relative to the\n" \ 198 " root build directory.\n" 199 #define TARGET_TYPE_FILTER_COMMAND_LINE_HELP \ 200 " --type=(action|copy|executable|group|loadable_module|shared_library|\n" \ 201 " source_set|static_library)\n" \ 202 " Restrict outputs to targets matching the given type. If\n" \ 203 " unspecified, no filtering will be performed.\n" 204 #define TARGET_TESTONLY_FILTER_COMMAND_LINE_HELP \ 205 " --testonly=(true|false)\n" \ 206 " Restrict outputs to targets with the testonly flag set\n" \ 207 " accordingly. When unspecified, the target's testonly flags are\n" \ 208 " ignored.\n" 209 210 // Applies any testonly and type filters specified on the command line, 211 // and prints the targets as specified by the --as command line flag. 212 // 213 // If indent is true, the results will be indented two spaces. 214 // 215 // The vector will be modified so that only the printed targets will remain. 216 void FilterAndPrintTargets(bool indent, std::vector<const Target*>* targets); 217 void FilterAndPrintTargets(std::vector<const Target*>* targets, 218 base::ListValue* out); 219 220 void FilterAndPrintTargetSet(bool indent, const TargetSet& targets); 221 void FilterAndPrintTargetSet(const TargetSet& targets, base::ListValue* out); 222 223 // Computes which targets reference the given file and also stores how the 224 // target references the file. 225 enum class HowTargetContainsFile { 226 kSources, 227 kPublic, 228 kInputs, 229 kData, 230 kScript, 231 kOutput, 232 }; 233 using TargetContainingFile = std::pair<const Target*, HowTargetContainsFile>; 234 void GetTargetsContainingFile(Setup* setup, 235 const std::vector<const Target*>& all_targets, 236 const SourceFile& file, 237 bool default_toolchain_only, 238 std::vector<TargetContainingFile>* matches); 239 240 // Extra help from command_check.cc 241 extern const char kNoGnCheck_Help[]; 242 243 } // namespace commands 244 245 #endif // TOOLS_GN_COMMANDS_H_ 246