1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: kenton@google.com (Kenton Varda)
33
#include <google/protobuf/compiler/plugin.h>
42
#define STDIN_FILENO 0
45
#define STDOUT_FILENO 1
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>
62
class GeneratorResponseOutputDirectory : public OutputDirectory {
64
GeneratorResponseOutputDirectory(CodeGeneratorResponse* response)
65
: response_(response) {}
66
virtual ~GeneratorResponseOutputDirectory() {}
68
// implements OutputDirectory --------------------------------------
70
virtual io::ZeroCopyOutputStream* Open(const string& filename) {
71
CodeGeneratorResponse::File* file = response_->add_file();
72
file->set_name(filename);
73
return new io::StringOutputStream(file->mutable_content());
76
virtual io::ZeroCopyOutputStream* OpenForInsert(
77
const string& filename, const string& insertion_point) {
78
CodeGeneratorResponse::File* file = response_->add_file();
79
file->set_name(filename);
80
file->set_insertion_point(insertion_point);
81
return new io::StringOutputStream(file->mutable_content());
85
CodeGeneratorResponse* response_;
88
int PluginMain(int argc, char* argv[], const CodeGenerator* generator) {
91
cerr << argv[0] << ": Unknown option: " << argv[1] << endl;
96
_setmode(STDIN_FILENO, _O_BINARY);
97
_setmode(STDOUT_FILENO, _O_BINARY);
100
CodeGeneratorRequest request;
101
if (!request.ParseFromFileDescriptor(STDIN_FILENO)) {
102
cerr << argv[0] << ": protoc sent unparseable request to plugin." << endl;
107
for (int i = 0; i < request.proto_file_size(); i++) {
108
const FileDescriptor* file = pool.BuildFile(request.proto_file(i));
110
// BuildFile() already wrote an error message.
115
CodeGeneratorResponse response;
116
GeneratorResponseOutputDirectory output_directory(&response);
118
for (int i = 0; i < request.file_to_generate_size(); i++) {
119
const FileDescriptor* file =
120
pool.FindFileByName(request.file_to_generate(i));
122
cerr << argv[0] << ": protoc asked plugin to generate a file but "
123
"did not provide a descriptor for the file: "
124
<< request.file_to_generate(i) << endl;
129
bool succeeded = generator->Generate(
130
file, request.parameter(), &output_directory, &error);
132
if (!succeeded && error.empty()) {
133
error = "Code generator returned false but provided no error "
136
if (!error.empty()) {
137
response.set_error(file->name() + ": " + error);
142
if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) {
143
cerr << argv[0] << ": Error writing to stdout." << endl;
150
} // namespace compiler
151
} // namespace protobuf
152
} // namespace google