1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // Author: kenton@google.com (Kenton Varda) 32 // Based on original Protocol Buffers design by 33 // Sanjay Ghemawat, Jeff Dean, and others. 34 // 35 // Implements the Protocol Compiler front-end such that it may be reused by 36 // custom compilers written to support other languages. 37 38 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ 39 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ 40 41 #include <map> 42 #include <set> 43 #include <string> 44 #include <unordered_map> 45 #include <utility> 46 #include <vector> 47 48 #include <google/protobuf/stubs/common.h> 49 50 #include <google/protobuf/port_def.inc> 51 52 namespace google { 53 namespace protobuf { 54 55 class Descriptor; // descriptor.h 56 class DescriptorDatabase; // descriptor_database.h 57 class DescriptorPool; // descriptor.h 58 class FileDescriptor; // descriptor.h 59 class FileDescriptorSet; // descriptor.h 60 class FileDescriptorProto; // descriptor.pb.h 61 template <typename T> 62 class RepeatedPtrField; // repeated_field.h 63 class SimpleDescriptorDatabase; // descriptor_database.h 64 65 namespace compiler { 66 67 class CodeGenerator; // code_generator.h 68 class GeneratorContext; // code_generator.h 69 class DiskSourceTree; // importer.h 70 71 // This class implements the command-line interface to the protocol compiler. 72 // It is designed to make it very easy to create a custom protocol compiler 73 // supporting the languages of your choice. For example, if you wanted to 74 // create a custom protocol compiler binary which includes both the regular 75 // C++ support plus support for your own custom output "Foo", you would 76 // write a class "FooGenerator" which implements the CodeGenerator interface, 77 // then write a main() procedure like this: 78 // 79 // int main(int argc, char* argv[]) { 80 // google::protobuf::compiler::CommandLineInterface cli; 81 // 82 // // Support generation of C++ source and headers. 83 // google::protobuf::compiler::cpp::CppGenerator cpp_generator; 84 // cli.RegisterGenerator("--cpp_out", &cpp_generator, 85 // "Generate C++ source and header."); 86 // 87 // // Support generation of Foo code. 88 // FooGenerator foo_generator; 89 // cli.RegisterGenerator("--foo_out", &foo_generator, 90 // "Generate Foo file."); 91 // 92 // return cli.Run(argc, argv); 93 // } 94 // 95 // The compiler is invoked with syntax like: 96 // protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto 97 // 98 // The .proto file to compile can be specified on the command line using either 99 // its physical file path, or a virtual path relative to a diretory specified 100 // in --proto_path. For example, for src/foo.proto, the following two protoc 101 // invocations work the same way: 102 // 1. protoc --proto_path=src src/foo.proto (physical file path) 103 // 2. protoc --proto_path=src foo.proto (virtual path relative to src) 104 // 105 // If a file path can be interpreted both as a physical file path and as a 106 // relative virtual path, the physical file path takes precendence. 107 // 108 // For a full description of the command-line syntax, invoke it with --help. 109 class PROTOC_EXPORT CommandLineInterface { 110 public: 111 static const char* const kPathSeparator; 112 113 CommandLineInterface(); 114 ~CommandLineInterface(); 115 116 // Register a code generator for a language. 117 // 118 // Parameters: 119 // * flag_name: The command-line flag used to specify an output file of 120 // this type. The name must start with a '-'. If the name is longer 121 // than one letter, it must start with two '-'s. 122 // * generator: The CodeGenerator which will be called to generate files 123 // of this type. 124 // * help_text: Text describing this flag in the --help output. 125 // 126 // Some generators accept extra parameters. You can specify this parameter 127 // on the command-line by placing it before the output directory, separated 128 // by a colon: 129 // protoc --foo_out=enable_bar:outdir 130 // The text before the colon is passed to CodeGenerator::Generate() as the 131 // "parameter". 132 void RegisterGenerator(const std::string& flag_name, CodeGenerator* generator, 133 const std::string& help_text); 134 135 // Register a code generator for a language. 136 // Besides flag_name you can specify another option_flag_name that could be 137 // used to pass extra parameters to the registered code generator. 138 // Suppose you have registered a generator by calling: 139 // command_line_interface.RegisterGenerator("--foo_out", "--foo_opt", ...) 140 // Then you could invoke the compiler with a command like: 141 // protoc --foo_out=enable_bar:outdir --foo_opt=enable_baz 142 // This will pass "enable_bar,enable_baz" as the parameter to the generator. 143 void RegisterGenerator(const std::string& flag_name, 144 const std::string& option_flag_name, 145 CodeGenerator* generator, 146 const std::string& help_text); 147 148 // Enables "plugins". In this mode, if a command-line flag ends with "_out" 149 // but does not match any registered generator, the compiler will attempt to 150 // find a "plugin" to implement the generator. Plugins are just executables. 151 // They should live somewhere in the PATH. 152 // 153 // The compiler determines the executable name to search for by concatenating 154 // exe_name_prefix with the unrecognized flag name, removing "_out". So, for 155 // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out, 156 // the compiler will try to run the program "protoc-foo". 157 // 158 // The plugin program should implement the following usage: 159 // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS 160 // --out indicates the output directory (as passed to the --foo_out 161 // parameter); if omitted, the current directory should be used. --parameter 162 // gives the generator parameter, if any was provided (see below). The 163 // PROTO_FILES list the .proto files which were given on the compiler 164 // command-line; these are the files for which the plugin is expected to 165 // generate output code. Finally, DESCRIPTORS is an encoded FileDescriptorSet 166 // (as defined in descriptor.proto). This is piped to the plugin's stdin. 167 // The set will include descriptors for all the files listed in PROTO_FILES as 168 // well as all files that they import. The plugin MUST NOT attempt to read 169 // the PROTO_FILES directly -- it must use the FileDescriptorSet. 170 // 171 // The plugin should generate whatever files are necessary, as code generators 172 // normally do. It should write the names of all files it generates to 173 // stdout. The names should be relative to the output directory, NOT absolute 174 // names or relative to the current directory. If any errors occur, error 175 // messages should be written to stderr. If an error is fatal, the plugin 176 // should exit with a non-zero exit code. 177 // 178 // Plugins can have generator parameters similar to normal built-in 179 // generators. Extra generator parameters can be passed in via a matching 180 // "_opt" parameter. For example: 181 // protoc --plug_out=enable_bar:outdir --plug_opt=enable_baz 182 // This will pass "enable_bar,enable_baz" as the parameter to the plugin. 183 // 184 void AllowPlugins(const std::string& exe_name_prefix); 185 186 // Run the Protocol Compiler with the given command-line parameters. 187 // Returns the error code which should be returned by main(). 188 // 189 // It may not be safe to call Run() in a multi-threaded environment because 190 // it calls strerror(). I'm not sure why you'd want to do this anyway. 191 int Run(int argc, const char* const argv[]); 192 193 // DEPRECATED. Calling this method has no effect. Protocol compiler now 194 // always try to find the .proto file relative to the current directory 195 // first and if the file is not found, it will then treat the input path 196 // as a virutal path. SetInputsAreProtoPathRelative(bool)197 void SetInputsAreProtoPathRelative(bool /* enable */) {} 198 199 // Provides some text which will be printed when the --version flag is 200 // used. The version of libprotoc will also be printed on the next line 201 // after this text. SetVersionInfo(const std::string & text)202 void SetVersionInfo(const std::string& text) { version_info_ = text; } 203 204 205 private: 206 // ----------------------------------------------------------------- 207 208 class ErrorPrinter; 209 class GeneratorContextImpl; 210 class MemoryOutputStream; 211 typedef std::unordered_map<std::string, GeneratorContextImpl*> 212 GeneratorContextMap; 213 214 // Clear state from previous Run(). 215 void Clear(); 216 217 // Remaps the proto file so that it is relative to one of the directories 218 // in proto_path_. Returns false if an error occurred. 219 bool MakeProtoProtoPathRelative(DiskSourceTree* source_tree, 220 std::string* proto, 221 DescriptorDatabase* fallback_database); 222 223 // Remaps each file in input_files_ so that it is relative to one of the 224 // directories in proto_path_. Returns false if an error occurred. 225 bool MakeInputsBeProtoPathRelative(DiskSourceTree* source_tree, 226 DescriptorDatabase* fallback_database); 227 228 229 // Return status for ParseArguments() and InterpretArgument(). 230 enum ParseArgumentStatus { 231 PARSE_ARGUMENT_DONE_AND_CONTINUE, 232 PARSE_ARGUMENT_DONE_AND_EXIT, 233 PARSE_ARGUMENT_FAIL 234 }; 235 236 // Parse all command-line arguments. 237 ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); 238 239 // Read an argument file and append the file's content to the list of 240 // arguments. Return false if the file cannot be read. 241 bool ExpandArgumentFile(const std::string& file, 242 std::vector<std::string>* arguments); 243 244 // Parses a command-line argument into a name/value pair. Returns 245 // true if the next argument in the argv should be used as the value, 246 // false otherwise. 247 // 248 // Examples: 249 // "-Isrc/protos" -> 250 // name = "-I", value = "src/protos" 251 // "--cpp_out=src/foo.pb2.cc" -> 252 // name = "--cpp_out", value = "src/foo.pb2.cc" 253 // "foo.proto" -> 254 // name = "", value = "foo.proto" 255 bool ParseArgument(const char* arg, std::string* name, std::string* value); 256 257 // Interprets arguments parsed with ParseArgument. 258 ParseArgumentStatus InterpretArgument(const std::string& name, 259 const std::string& value); 260 261 // Print the --help text to stderr. 262 void PrintHelpText(); 263 264 // Loads proto_path_ into the provided source_tree. 265 bool InitializeDiskSourceTree(DiskSourceTree* source_tree, 266 DescriptorDatabase* fallback_database); 267 268 // Verify that all the input files exist in the given database. 269 bool VerifyInputFilesInDescriptors(DescriptorDatabase* fallback_database); 270 271 // Loads descriptor_set_in into the provided database 272 bool PopulateSimpleDescriptorDatabase(SimpleDescriptorDatabase* database); 273 274 // Parses input_files_ into parsed_files 275 bool ParseInputFiles(DescriptorPool* descriptor_pool, 276 std::vector<const FileDescriptor*>* parsed_files); 277 278 // Generate the given output file from the given input. 279 struct OutputDirective; // see below 280 bool GenerateOutput(const std::vector<const FileDescriptor*>& parsed_files, 281 const OutputDirective& output_directive, 282 GeneratorContext* generator_context); 283 bool GeneratePluginOutput( 284 const std::vector<const FileDescriptor*>& parsed_files, 285 const std::string& plugin_name, const std::string& parameter, 286 GeneratorContext* generator_context, std::string* error); 287 288 // Implements --encode and --decode. 289 bool EncodeOrDecode(const DescriptorPool* pool); 290 291 // Implements the --descriptor_set_out option. 292 bool WriteDescriptorSet( 293 const std::vector<const FileDescriptor*>& parsed_files); 294 295 // Implements the --dependency_out option 296 bool GenerateDependencyManifestFile( 297 const std::vector<const FileDescriptor*>& parsed_files, 298 const GeneratorContextMap& output_directories, 299 DiskSourceTree* source_tree); 300 301 // Get all transitive dependencies of the given file (including the file 302 // itself), adding them to the given list of FileDescriptorProtos. The 303 // protos will be ordered such that every file is listed before any file that 304 // depends on it, so that you can call DescriptorPool::BuildFile() on them 305 // in order. Any files in *already_seen will not be added, and each file 306 // added will be inserted into *already_seen. If include_source_code_info is 307 // true then include the source code information in the FileDescriptorProtos. 308 // If include_json_name is true, populate the json_name field of 309 // FieldDescriptorProto for all fields. 310 static void GetTransitiveDependencies( 311 const FileDescriptor* file, bool include_json_name, 312 bool include_source_code_info, 313 std::set<const FileDescriptor*>* already_seen, 314 RepeatedPtrField<FileDescriptorProto>* output); 315 316 // Implements the --print_free_field_numbers. This function prints free field 317 // numbers into stdout for the message and it's nested message types in 318 // post-order, i.e. nested types first. Printed range are left-right 319 // inclusive, i.e. [a, b]. 320 // 321 // Groups: 322 // For historical reasons, groups are considered to share the same 323 // field number space with the parent message, thus it will not print free 324 // field numbers for groups. The field numbers used in the groups are 325 // excluded in the free field numbers of the parent message. 326 // 327 // Extension Ranges: 328 // Extension ranges are considered ocuppied field numbers and they will not be 329 // listed as free numbers in the output. 330 void PrintFreeFieldNumbers(const Descriptor* descriptor); 331 332 // ----------------------------------------------------------------- 333 334 // The name of the executable as invoked (i.e. argv[0]). 335 std::string executable_name_; 336 337 // Version info set with SetVersionInfo(). 338 std::string version_info_; 339 340 // Registered generators. 341 struct GeneratorInfo { 342 std::string flag_name; 343 std::string option_flag_name; 344 CodeGenerator* generator; 345 std::string help_text; 346 }; 347 typedef std::map<std::string, GeneratorInfo> GeneratorMap; 348 GeneratorMap generators_by_flag_name_; 349 GeneratorMap generators_by_option_name_; 350 // A map from generator names to the parameters specified using the option 351 // flag. For example, if the user invokes the compiler with: 352 // protoc --foo_out=outputdir --foo_opt=enable_bar ... 353 // Then there will be an entry ("--foo_out", "enable_bar") in this map. 354 std::map<std::string, std::string> generator_parameters_; 355 // Similar to generator_parameters_, but stores the parameters for plugins. 356 std::map<std::string, std::string> plugin_parameters_; 357 358 // See AllowPlugins(). If this is empty, plugins aren't allowed. 359 std::string plugin_prefix_; 360 361 // Maps specific plugin names to files. When executing a plugin, this map 362 // is searched first to find the plugin executable. If not found here, the 363 // PATH (or other OS-specific search strategy) is searched. 364 std::map<std::string, std::string> plugins_; 365 366 // Stuff parsed from command line. 367 enum Mode { 368 MODE_COMPILE, // Normal mode: parse .proto files and compile them. 369 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. 370 MODE_DECODE, // --decode: read binary from stdin, write text to stdout. 371 MODE_PRINT, // Print mode: print info of the given .proto files and exit. 372 }; 373 374 Mode mode_; 375 376 enum PrintMode { 377 PRINT_NONE, // Not in MODE_PRINT 378 PRINT_FREE_FIELDS, // --print_free_fields 379 }; 380 381 PrintMode print_mode_; 382 383 enum ErrorFormat { 384 ERROR_FORMAT_GCC, // GCC error output format (default). 385 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). 386 }; 387 388 ErrorFormat error_format_; 389 390 std::vector<std::pair<std::string, std::string> > 391 proto_path_; // Search path for proto files. 392 std::vector<std::string> input_files_; // Names of the input proto files. 393 394 // Names of proto files which are allowed to be imported. Used by build 395 // systems to enforce depend-on-what-you-import. 396 std::set<std::string> direct_dependencies_; 397 bool direct_dependencies_explicitly_set_; 398 399 // If there's a violation of depend-on-what-you-import, this string will be 400 // presented to the user. "%s" will be replaced with the violating import. 401 std::string direct_dependencies_violation_msg_; 402 403 // output_directives_ lists all the files we are supposed to output and what 404 // generator to use for each. 405 struct OutputDirective { 406 std::string name; // E.g. "--foo_out" 407 CodeGenerator* generator; // NULL for plugins 408 std::string parameter; 409 std::string output_location; 410 }; 411 std::vector<OutputDirective> output_directives_; 412 413 // When using --encode or --decode, this names the type we are encoding or 414 // decoding. (Empty string indicates --decode_raw.) 415 std::string codec_type_; 416 417 // If --descriptor_set_in was given, these are filenames containing 418 // parsed FileDescriptorSets to be used for loading protos. Otherwise, empty. 419 std::vector<std::string> descriptor_set_in_names_; 420 421 // If --descriptor_set_out was given, this is the filename to which the 422 // FileDescriptorSet should be written. Otherwise, empty. 423 std::string descriptor_set_out_name_; 424 425 // If --dependency_out was given, this is the path to the file where the 426 // dependency file will be written. Otherwise, empty. 427 std::string dependency_out_name_; 428 429 // True if --include_imports was given, meaning that we should 430 // write all transitive dependencies to the DescriptorSet. Otherwise, only 431 // the .proto files listed on the command-line are added. 432 bool imports_in_descriptor_set_; 433 434 // True if --include_source_info was given, meaning that we should not strip 435 // SourceCodeInfo from the DescriptorSet. 436 bool source_info_in_descriptor_set_; 437 438 // Was the --disallow_services flag used? 439 bool disallow_services_; 440 441 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); 442 }; 443 444 } // namespace compiler 445 } // namespace protobuf 446 } // namespace google 447 448 #include <google/protobuf/port_undef.inc> 449 450 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ 451