Commit 607b9214 authored by Feng Xiao's avatar Feng Xiao Committed by GitHub

Merge pull request #2437 from xfxyjwf/plugin

Add version number to plugin protocol.
parents 05090726 ced8f73f
......@@ -34,6 +34,7 @@
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.h>
......@@ -89,6 +90,13 @@ void GeneratorContext::ListParsedFiles(
GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles";
}
void GeneratorContext::GetCompilerVersion(Version* version) const {
version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX);
}
// Parses a set of comma-delimited name/value pairs.
void ParseGeneratorParameter(const string& text,
std::vector<std::pair<string, string> >* output) {
......
......@@ -50,6 +50,7 @@ namespace io { class ZeroCopyOutputStream; }
class FileDescriptor;
namespace compiler {
class Version;
// Defined in this file.
class CodeGenerator;
......@@ -143,6 +144,10 @@ class LIBPROTOC_EXPORT GeneratorContext {
// differently when compiled as a set rather than individually.
virtual void ListParsedFiles(std::vector<const FileDescriptor*>* output);
// Retrieves the version number of the protocol compiler associated with
// this GeneratorContext.
virtual void GetCompilerVersion(Version* version) const;
private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext);
};
......
......@@ -1619,6 +1619,13 @@ bool CommandLineInterface::GeneratePluginOutput(
&already_seen, request.mutable_proto_file());
}
google::protobuf::compiler::Version* version =
request.mutable_compiler_version();
version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX);
// Invoke the plugin.
Subprocess subprocess;
......
......@@ -57,6 +57,7 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/unittest.pb.h>
#include <google/protobuf/testing/file.h>
#include <google/protobuf/stubs/stringprintf.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
......@@ -1582,6 +1583,21 @@ TEST_F(CommandLineInterfaceTest, PluginReceivesJsonName) {
ExpectErrorSubstring("Saw json_name: 1");
}
TEST_F(CommandLineInterfaceTest, PluginReceivesCompilerVersion) {
CreateTempFile("foo.proto",
"syntax = \"proto2\";\n"
"message MockCodeGenerator_ShowVersionNumber {\n"
" optional int32 value = 1;\n"
"}\n");
Run("protocol_compiler --plug_out=$tmpdir --proto_path=$tmpdir foo.proto");
ExpectErrorSubstring(
StringPrintf("Saw compiler_version: %d %s",
GOOGLE_PROTOBUF_VERSION,
GOOGLE_PROTOBUF_VERSION_SUFFIX));
}
TEST_F(CommandLineInterfaceTest, GeneratorPluginNotFound) {
// Test what happens if the plugin isn't found.
......
......@@ -40,6 +40,7 @@
#endif
#include <vector>
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/testing/file.h>
......@@ -160,6 +161,15 @@ bool MockCodeGenerator::Generate(
std::cerr << "Saw json_name: "
<< field_descriptor_proto.has_json_name() << std::endl;
abort();
} else if (command == "ShowVersionNumber") {
Version compiler_version;
context->GetCompilerVersion(&compiler_version);
std::cerr << "Saw compiler_version: "
<< compiler_version.major() * 1000000 +
compiler_version.minor() * 1000 +
compiler_version.patch()
<< " " << compiler_version.suffix() << std::endl;
abort();
} else {
GOOGLE_LOG(FATAL) << "Unknown MockCodeGenerator command: " << command;
}
......
......@@ -63,9 +63,12 @@ namespace compiler {
class GeneratorResponseContext : public GeneratorContext {
public:
GeneratorResponseContext(
const Version& compiler_version,
CodeGeneratorResponse* response,
const std::vector<const FileDescriptor*>& parsed_files)
: response_(response), parsed_files_(parsed_files) {}
: compiler_version_(compiler_version),
response_(response),
parsed_files_(parsed_files) {}
virtual ~GeneratorResponseContext() {}
// implements GeneratorContext --------------------------------------
......@@ -88,7 +91,12 @@ class GeneratorResponseContext : public GeneratorContext {
*output = parsed_files_;
}
void GetCompilerVersion(Version* version) const {
*version = compiler_version_;
}
private:
Version compiler_version_;
CodeGeneratorResponse* response_;
const std::vector<const FileDescriptor*>& parsed_files_;
};
......@@ -116,7 +124,8 @@ bool GenerateCode(const CodeGeneratorRequest& request,
}
}
GeneratorResponseContext context(response, parsed_files);
GeneratorResponseContext context(
request.compiler_version(), response, parsed_files);
string error;
bool succeeded = generator.GenerateAll(
......
This diff is collapsed.
This diff is collapsed.
......@@ -53,6 +53,16 @@ option go_package = "plugin_go";
import "google/protobuf/descriptor.proto";
// The version number of protocol compiler.
message Version {
optional int32 major = 1;
optional int32 minor = 2;
optional int32 patch = 3;
// A suffix for alpha, beta or rc release, e.g., "alpha-1", "rc2". It should
// be empty for mainline stable releases.
optional string suffix = 4;
}
// An encoded CodeGeneratorRequest is written to the plugin's stdin.
message CodeGeneratorRequest {
// The .proto files that were explicitly listed on the command-line. The
......@@ -75,6 +85,9 @@ message CodeGeneratorRequest {
// is not similarly optimized on protoc's end -- it will store all fields in
// memory at once before sending them to the plugin.
repeated FileDescriptorProto proto_file = 15;
// The version number of protocol compiler.
optional Version compiler_version = 3;
}
// The plugin writes an encoded CodeGeneratorResponse to stdout.
......
......@@ -98,6 +98,9 @@ namespace internal {
// easier: major * 10^6 + minor * 10^3 + micro
#define GOOGLE_PROTOBUF_VERSION 3001000
// A suffix string for alpha, beta or rc releases. Empty for stable releases.
#define GOOGLE_PROTOBUF_VERSION_SUFFIX ""
// The minimum library version which works with the current version of the
// headers.
#define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 3001000
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment