kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 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> |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 37 | |
kenton@google.com | 684d45b | 2009-12-19 04:50:00 +0000 | [diff] [blame] | 38 | #ifdef _WIN32 |
| 39 | #include <io.h> |
| 40 | #include <fcntl.h> |
kenton@google.com | d1ce7a2 | 2009-12-21 21:19:05 +0000 | [diff] [blame] | 41 | #ifndef STDIN_FILENO |
| 42 | #define STDIN_FILENO 0 |
| 43 | #endif |
| 44 | #ifndef STDOUT_FILENO |
| 45 | #define STDOUT_FILENO 1 |
| 46 | #endif |
| 47 | #else |
| 48 | #include <unistd.h> |
kenton@google.com | 684d45b | 2009-12-19 04:50:00 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 51 | #include <google/protobuf/stubs/common.h> |
| 52 | #include <google/protobuf/compiler/plugin.pb.h> |
| 53 | #include <google/protobuf/compiler/code_generator.h> |
| 54 | #include <google/protobuf/descriptor.h> |
| 55 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 56 | |
| 57 | |
| 58 | namespace google { |
| 59 | namespace protobuf { |
| 60 | namespace compiler { |
| 61 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 62 | class GeneratorResponseContext : public GeneratorContext { |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 63 | public: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 64 | GeneratorResponseContext(CodeGeneratorResponse* response, |
| 65 | const vector<const FileDescriptor*>& parsed_files) |
| 66 | : response_(response), |
| 67 | parsed_files_(parsed_files) {} |
| 68 | virtual ~GeneratorResponseContext() {} |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 69 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 70 | // implements GeneratorContext -------------------------------------- |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 71 | |
| 72 | virtual io::ZeroCopyOutputStream* Open(const string& filename) { |
| 73 | CodeGeneratorResponse::File* file = response_->add_file(); |
| 74 | file->set_name(filename); |
| 75 | return new io::StringOutputStream(file->mutable_content()); |
| 76 | } |
| 77 | |
| 78 | virtual io::ZeroCopyOutputStream* OpenForInsert( |
| 79 | const string& filename, const string& insertion_point) { |
| 80 | CodeGeneratorResponse::File* file = response_->add_file(); |
| 81 | file->set_name(filename); |
| 82 | file->set_insertion_point(insertion_point); |
| 83 | return new io::StringOutputStream(file->mutable_content()); |
| 84 | } |
| 85 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 86 | void ListParsedFiles(vector<const FileDescriptor*>* output) { |
| 87 | *output = parsed_files_; |
| 88 | } |
| 89 | |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 90 | private: |
| 91 | CodeGeneratorResponse* response_; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 92 | const vector<const FileDescriptor*>& parsed_files_; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | int PluginMain(int argc, char* argv[], const CodeGenerator* generator) { |
| 96 | |
| 97 | if (argc > 1) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 98 | std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
kenton@google.com | 684d45b | 2009-12-19 04:50:00 +0000 | [diff] [blame] | 102 | #ifdef _WIN32 |
| 103 | _setmode(STDIN_FILENO, _O_BINARY); |
| 104 | _setmode(STDOUT_FILENO, _O_BINARY); |
| 105 | #endif |
| 106 | |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 107 | CodeGeneratorRequest request; |
| 108 | if (!request.ParseFromFileDescriptor(STDIN_FILENO)) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 109 | std::cerr << argv[0] << ": protoc sent unparseable request to plugin." |
| 110 | << std::endl; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | DescriptorPool pool; |
| 115 | for (int i = 0; i < request.proto_file_size(); i++) { |
| 116 | const FileDescriptor* file = pool.BuildFile(request.proto_file(i)); |
| 117 | if (file == NULL) { |
| 118 | // BuildFile() already wrote an error message. |
| 119 | return 1; |
| 120 | } |
| 121 | } |
| 122 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 123 | vector<const FileDescriptor*> parsed_files; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 124 | for (int i = 0; i < request.file_to_generate_size(); i++) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 125 | parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i))); |
| 126 | if (parsed_files.back() == NULL) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 127 | std::cerr << argv[0] << ": protoc asked plugin to generate a file but " |
| 128 | "did not provide a descriptor for the file: " |
| 129 | << request.file_to_generate(i) << std::endl; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 130 | return 1; |
| 131 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | CodeGeneratorResponse response; |
| 135 | GeneratorResponseContext context(&response, parsed_files); |
| 136 | |
| 137 | for (int i = 0; i < parsed_files.size(); i++) { |
| 138 | const FileDescriptor* file = parsed_files[i]; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 139 | |
| 140 | string error; |
| 141 | bool succeeded = generator->Generate( |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 142 | file, request.parameter(), &context, &error); |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 143 | |
| 144 | if (!succeeded && error.empty()) { |
| 145 | error = "Code generator returned false but provided no error " |
| 146 | "description."; |
| 147 | } |
| 148 | if (!error.empty()) { |
| 149 | response.set_error(file->name() + ": " + error); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 155 | std::cerr << argv[0] << ": Error writing to stdout." << std::endl; |
kenton@google.com | 5e744ff | 2009-12-18 04:51:42 +0000 | [diff] [blame] | 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | } // namespace compiler |
| 163 | } // namespace protobuf |
| 164 | } // namespace google |