Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 | |
| 33 | #include <google/protobuf/compiler/plugin.h> |
| 34 | |
| 35 | #include <iostream> |
| 36 | #include <set> |
| 37 | |
| 38 | #ifdef _WIN32 |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 39 | #include <fcntl.h> |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 40 | #else |
| 41 | #include <unistd.h> |
| 42 | #endif |
| 43 | |
| 44 | #include <google/protobuf/stubs/logging.h> |
| 45 | #include <google/protobuf/stubs/common.h> |
| 46 | #include <google/protobuf/compiler/plugin.pb.h> |
| 47 | #include <google/protobuf/compiler/code_generator.h> |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 48 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 49 | #include <google/protobuf/descriptor.h> |
| 50 | #include <google/protobuf/stubs/io_win32.h> |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 51 | |
| 52 | |
| 53 | namespace google { |
| 54 | namespace protobuf { |
| 55 | namespace compiler { |
| 56 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 57 | #if defined(_WIN32) |
| 58 | // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import |
| 59 | // them like we do below. |
| 60 | using google::protobuf::internal::win32::setmode; |
| 61 | #endif |
| 62 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 63 | class GeneratorResponseContext : public GeneratorContext { |
| 64 | public: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 65 | GeneratorResponseContext( |
| 66 | const Version& compiler_version, |
| 67 | CodeGeneratorResponse* response, |
| 68 | const std::vector<const FileDescriptor*>& parsed_files) |
| 69 | : compiler_version_(compiler_version), |
| 70 | response_(response), |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 71 | parsed_files_(parsed_files) {} |
| 72 | virtual ~GeneratorResponseContext() {} |
| 73 | |
| 74 | // implements GeneratorContext -------------------------------------- |
| 75 | |
| 76 | virtual io::ZeroCopyOutputStream* Open(const string& filename) { |
| 77 | CodeGeneratorResponse::File* file = response_->add_file(); |
| 78 | file->set_name(filename); |
| 79 | return new io::StringOutputStream(file->mutable_content()); |
| 80 | } |
| 81 | |
| 82 | virtual io::ZeroCopyOutputStream* OpenForInsert( |
| 83 | const string& filename, const string& insertion_point) { |
| 84 | CodeGeneratorResponse::File* file = response_->add_file(); |
| 85 | file->set_name(filename); |
| 86 | file->set_insertion_point(insertion_point); |
| 87 | return new io::StringOutputStream(file->mutable_content()); |
| 88 | } |
| 89 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 90 | void ListParsedFiles(std::vector<const FileDescriptor*>* output) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 91 | *output = parsed_files_; |
| 92 | } |
| 93 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 94 | void GetCompilerVersion(Version* version) const { |
| 95 | *version = compiler_version_; |
| 96 | } |
| 97 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 98 | private: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 99 | Version compiler_version_; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 100 | CodeGeneratorResponse* response_; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 101 | const std::vector<const FileDescriptor*>& parsed_files_; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 104 | bool GenerateCode(const CodeGeneratorRequest& request, |
| 105 | const CodeGenerator& generator, CodeGeneratorResponse* response, |
| 106 | string* error_msg) { |
| 107 | DescriptorPool pool; |
| 108 | for (int i = 0; i < request.proto_file_size(); i++) { |
| 109 | const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); |
| 110 | if (file == NULL) { |
| 111 | // BuildFile() already wrote an error message. |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | std::vector<const FileDescriptor*> parsed_files; |
| 117 | for (int i = 0; i < request.file_to_generate_size(); i++) { |
| 118 | parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); |
| 119 | if (parsed_files.back() == NULL) { |
| 120 | *error_msg = "protoc asked plugin to generate a file but " |
| 121 | "did not provide a descriptor for the file: " + |
| 122 | request.file_to_generate(i); |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | GeneratorResponseContext context( |
| 128 | request.compiler_version(), response, parsed_files); |
| 129 | |
| 130 | |
| 131 | string error; |
| 132 | bool succeeded = generator.GenerateAll( |
| 133 | parsed_files, request.parameter(), &context, &error); |
| 134 | |
| 135 | if (!succeeded && error.empty()) { |
| 136 | error = "Code generator returned false but provided no error " |
| 137 | "description."; |
| 138 | } |
| 139 | if (!error.empty()) { |
| 140 | response->set_error(error); |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 146 | int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { |
| 147 | |
| 148 | if (argc > 1) { |
| 149 | std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl; |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | #ifdef _WIN32 |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 154 | setmode(STDIN_FILENO, _O_BINARY); |
| 155 | setmode(STDOUT_FILENO, _O_BINARY); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 156 | #endif |
| 157 | |
| 158 | CodeGeneratorRequest request; |
| 159 | if (!request.ParseFromFileDescriptor(STDIN_FILENO)) { |
| 160 | std::cerr << argv[0] << ": protoc sent unparseable request to plugin." |
| 161 | << std::endl; |
| 162 | return 1; |
| 163 | } |
| 164 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 165 | string error_msg; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 166 | CodeGeneratorResponse response; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 167 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 168 | if (GenerateCode(request, *generator, &response, &error_msg)) { |
| 169 | if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) { |
| 170 | std::cerr << argv[0] << ": Error writing to stdout." << std::endl; |
| 171 | return 1; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 172 | } |
| 173 | } else { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 174 | if (!error_msg.empty()) { |
| 175 | std::cerr << argv[0] << ": " << error_msg << std::endl; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 176 | } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | } // namespace compiler |
| 184 | } // namespace protobuf |
| 185 | } // namespace google |