diff options
Diffstat (limited to 'src/google/protobuf/compiler/command_line_interface.h')
-rw-r--r-- | src/google/protobuf/compiler/command_line_interface.h | 128 |
1 files changed, 82 insertions, 46 deletions
diff --git a/src/google/protobuf/compiler/command_line_interface.h b/src/google/protobuf/compiler/command_line_interface.h index 32084567..7d3037a9 100644 --- a/src/google/protobuf/compiler/command_line_interface.h +++ b/src/google/protobuf/compiler/command_line_interface.h @@ -52,19 +52,16 @@ namespace protobuf { class Descriptor; // descriptor.h class DescriptorPool; // descriptor.h class FileDescriptor; // descriptor.h +class FileDescriptorSet; // descriptor.h class FileDescriptorProto; // descriptor.pb.h template<typename T> class RepeatedPtrField; // repeated_field.h +class SimpleDescriptorDatabase; // descriptor_database.h -} // namespace protobuf -} // namespace google - -namespace google { -namespace protobuf { namespace compiler { -class CodeGenerator; // code_generator.h -class GeneratorContext; // code_generator.h -class DiskSourceTree; // importer.h +class CodeGenerator; // code_generator.h +class GeneratorContext; // code_generator.h +class DiskSourceTree; // importer.h // This class implements the command-line interface to the protocol compiler. // It is designed to make it very easy to create a custom protocol compiler @@ -93,9 +90,21 @@ class DiskSourceTree; // importer.h // The compiler is invoked with syntax like: // protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto // +// The .proto file to compile can be specified on the command line using either +// its physical file path, or a virtual path relative to a diretory specified +// in --proto_path. For example, for src/foo.proto, the following two protoc +// invocations work the same way: +// 1. protoc --proto_path=src src/foo.proto (physical file path) +// 2. protoc --proto_path=src foo.proto (virtual path relative to src) +// +// If a file path can be interpreted both as a physical file path and as a +// relative virtual path, the physical file path takes precendence. +// // For a full description of the command-line syntax, invoke it with --help. class LIBPROTOC_EXPORT CommandLineInterface { public: + static const char* const kPathSeparator; + CommandLineInterface(); ~CommandLineInterface(); @@ -146,14 +155,14 @@ class LIBPROTOC_EXPORT CommandLineInterface { // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS // --out indicates the output directory (as passed to the --foo_out // parameter); if omitted, the current directory should be used. --parameter - // gives the generator parameter, if any was provided. The PROTO_FILES list - // the .proto files which were given on the compiler command-line; these are - // the files for which the plugin is expected to generate output code. - // Finally, DESCRIPTORS is an encoded FileDescriptorSet (as defined in - // descriptor.proto). This is piped to the plugin's stdin. The set will - // include descriptors for all the files listed in PROTO_FILES as well as - // all files that they import. The plugin MUST NOT attempt to read the - // PROTO_FILES directly -- it must use the FileDescriptorSet. + // gives the generator parameter, if any was provided (see below). The + // PROTO_FILES list the .proto files which were given on the compiler + // command-line; these are the files for which the plugin is expected to + // generate output code. Finally, DESCRIPTORS is an encoded FileDescriptorSet + // (as defined in descriptor.proto). This is piped to the plugin's stdin. + // The set will include descriptors for all the files listed in PROTO_FILES as + // well as all files that they import. The plugin MUST NOT attempt to read + // the PROTO_FILES directly -- it must use the FileDescriptorSet. // // The plugin should generate whatever files are necessary, as code generators // normally do. It should write the names of all files it generates to @@ -161,6 +170,13 @@ class LIBPROTOC_EXPORT CommandLineInterface { // names or relative to the current directory. If any errors occur, error // messages should be written to stderr. If an error is fatal, the plugin // should exit with a non-zero exit code. + // + // Plugins can have generator parameters similar to normal built-in + // generators. Extra generator parameters can be passed in via a matching + // "_opt" parameter. For example: + // protoc --plug_out=enable_bar:outdir --plug_opt=enable_baz + // This will pass "enable_bar,enable_baz" as the parameter to the plugin. + // void AllowPlugins(const string& exe_name_prefix); // Run the Protocol Compiler with the given command-line parameters. @@ -170,17 +186,11 @@ class LIBPROTOC_EXPORT CommandLineInterface { // it calls strerror(). I'm not sure why you'd want to do this anyway. int Run(int argc, const char* const argv[]); - // Call SetInputsAreCwdRelative(true) if the input files given on the command - // line should be interpreted relative to the proto import path specified - // using --proto_path or -I flags. Otherwise, input file names will be - // interpreted relative to the current working directory (or as absolute - // paths if they start with '/'), though they must still reside inside - // a directory given by --proto_path or the compiler will fail. The latter - // mode is generally more intuitive and easier to use, especially e.g. when - // defining implicit rules in Makefiles. - void SetInputsAreProtoPathRelative(bool enable) { - inputs_are_proto_path_relative_ = enable; - } + // DEPRECATED. Calling this method has no effect. Protocol compiler now + // always try to find the .proto file relative to the current directory + // first and if the file is not found, it will then treat the input path + // as a virutal path. + void SetInputsAreProtoPathRelative(bool /* enable */) {} // Provides some text which will be printed when the --version flag is // used. The version of libprotoc will also be printed on the next line @@ -217,6 +227,9 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Parse all command-line arguments. ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); + // Read an argument file and append the file's content to the list of + // arguments. Return false if the file cannot be read. + bool ExpandArgumentFile(const string& file, std::vector<string>* arguments); // Parses a command-line argument into a name/value pair. Returns // true if the next argument in the argv should be used as the value, @@ -238,26 +251,36 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Print the --help text to stderr. void PrintHelpText(); + // Loads proto_path_ into the provided source_tree. + bool InitializeDiskSourceTree(DiskSourceTree* source_tree); + + // Loads descriptor_set_in into the provided database + bool PopulateSimpleDescriptorDatabase(SimpleDescriptorDatabase* database); + + // Parses input_files_ into parsed_files + bool ParseInputFiles(DescriptorPool* descriptor_pool, + std::vector<const FileDescriptor*>* parsed_files); + // Generate the given output file from the given input. struct OutputDirective; // see below - bool GenerateOutput(const vector<const FileDescriptor*>& parsed_files, + bool GenerateOutput(const std::vector<const FileDescriptor*>& parsed_files, const OutputDirective& output_directive, GeneratorContext* generator_context); - bool GeneratePluginOutput(const vector<const FileDescriptor*>& parsed_files, - const string& plugin_name, - const string& parameter, - GeneratorContext* generator_context, - string* error); + bool GeneratePluginOutput( + const std::vector<const FileDescriptor*>& parsed_files, + const string& plugin_name, const string& parameter, + GeneratorContext* generator_context, string* error); // Implements --encode and --decode. bool EncodeOrDecode(const DescriptorPool* pool); // Implements the --descriptor_set_out option. - bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files); + bool WriteDescriptorSet( + const std::vector<const FileDescriptor*>& parsed_files); // Implements the --dependency_out option bool GenerateDependencyManifestFile( - const vector<const FileDescriptor*>& parsed_files, + const std::vector<const FileDescriptor*>& parsed_files, const GeneratorContextMap& output_directories, DiskSourceTree* source_tree); @@ -274,7 +297,7 @@ class LIBPROTOC_EXPORT CommandLineInterface { const FileDescriptor* file, bool include_json_name, bool include_source_code_info, - set<const FileDescriptor*>* already_seen, + std::set<const FileDescriptor*>* already_seen, RepeatedPtrField<FileDescriptorProto>* output); // Implements the --print_free_field_numbers. This function prints free field @@ -308,14 +331,16 @@ class LIBPROTOC_EXPORT CommandLineInterface { CodeGenerator* generator; string help_text; }; - typedef map<string, GeneratorInfo> GeneratorMap; + typedef std::map<string, GeneratorInfo> GeneratorMap; GeneratorMap generators_by_flag_name_; GeneratorMap generators_by_option_name_; // A map from generator names to the parameters specified using the option // flag. For example, if the user invokes the compiler with: // protoc --foo_out=outputdir --foo_opt=enable_bar ... // Then there will be an entry ("--foo_out", "enable_bar") in this map. - map<string, string> generator_parameters_; + std::map<string, string> generator_parameters_; + // Similar to generator_parameters_, but stores the parameters for plugins. + std::map<string, string> plugin_parameters_; // See AllowPlugins(). If this is empty, plugins aren't allowed. string plugin_prefix_; @@ -323,7 +348,7 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Maps specific plugin names to files. When executing a plugin, this map // is searched first to find the plugin executable. If not found here, the // PATH (or other OS-specific search strategy) is searched. - map<string, string> plugins_; + std::map<string, string> plugins_; // Stuff parsed from command line. enum Mode { @@ -349,8 +374,18 @@ class LIBPROTOC_EXPORT CommandLineInterface { ErrorFormat error_format_; - vector<pair<string, string> > proto_path_; // Search path for proto files. - vector<string> input_files_; // Names of the input proto files. + std::vector<std::pair<string, string> > + proto_path_; // Search path for proto files. + std::vector<string> input_files_; // Names of the input proto files. + + // Names of proto files which are allowed to be imported. Used by build + // systems to enforce depend-on-what-you-import. + std::set<string> direct_dependencies_; + bool direct_dependencies_explicitly_set_; + + // If there's a violation of depend-on-what-you-import, this string will be + // presented to the user. "%s" will be replaced with the violating import. + string direct_dependencies_violation_msg_; // output_directives_ lists all the files we are supposed to output and what // generator to use for each. @@ -360,15 +395,19 @@ class LIBPROTOC_EXPORT CommandLineInterface { string parameter; string output_location; }; - vector<OutputDirective> output_directives_; + std::vector<OutputDirective> output_directives_; // When using --encode or --decode, this names the type we are encoding or // decoding. (Empty string indicates --decode_raw.) string codec_type_; + // If --descriptor_set_in was given, these are filenames containing + // parsed FileDescriptorSets to be used for loading protos. Otherwise, empty. + std::vector<string> descriptor_set_in_names_; + // If --descriptor_set_out was given, this is the filename to which the // FileDescriptorSet should be written. Otherwise, empty. - string descriptor_set_name_; + string descriptor_set_out_name_; // If --dependency_out was given, this is the path to the file where the // dependency file will be written. Otherwise, empty. @@ -386,9 +425,6 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Was the --disallow_services flag used? bool disallow_services_; - // See SetInputsAreProtoPathRelative(). - bool inputs_are_proto_path_relative_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); }; |