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