Skip to content

Commit

Permalink
Setting up version updater to prepare for poison pills and embedding …
Browse files Browse the repository at this point in the history
…version info into C++, Python and Java gencode.

PiperOrigin-RevId: 565159224
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 13, 2023
1 parent 09d2511 commit 4322e00
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/google/protobuf/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ cc_library(
],
)

cc_library(
name = "versions",
hdrs = ["versions.h"],
copts = COPTS,
include_prefix = "google/protobuf/compiler",
visibility = [
"//src/google/protobuf/compiler:__subpackages__",
],
deps = ["@com_google_absl//absl/strings"],
)

cc_library(
name = "command_line_interface",
srcs = [
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ cc_library(
":names_internal",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",
"//src/google/protobuf/compiler:versions",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down
12 changes: 9 additions & 3 deletions src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "google/protobuf/compiler/cpp/service.h"
#include "google/protobuf/compiler/retention.h"
#include "google/protobuf/compiler/scc.h"
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/printer.h"
Expand Down Expand Up @@ -308,10 +309,15 @@ void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type,
std::function<void()> cb) {
auto v = p->WithVars(FileVars(file_, options_));
auto guard = IncludeGuard(file_, file_type, options_);
p->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"// source: $filename$\n");
if (options_.opensource_runtime) {
p->Print("// Protobuf C++ Version: $protobuf_cpp_version$\n",
"protobuf_cpp_version", internal::kProtoCppVersionString);
}
p->Print("\n");
p->Emit({{"cb", cb}, {"guard", guard}}, R"(
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: $filename$
#ifndef $guard$
#define $guard$
Expand Down
12 changes: 8 additions & 4 deletions src/google/protobuf/compiler/java/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ cc_library(
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",
"//src/google/protobuf/compiler:versions",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -167,9 +168,12 @@ pkg_files(

filegroup(
name = "test_srcs",
srcs = glob([
"*_test.cc",
"*unittest.cc",
], allow_empty = True),
srcs = glob(
[
"*_test.cc",
"*unittest.cc",
],
allow_empty = True,
),
visibility = ["//src/google/protobuf/compiler:__pkg__"],
)
6 changes: 6 additions & 0 deletions src/google/protobuf/compiler/java/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "absl/container/btree_set.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/java/context.h"
#include "google/protobuf/compiler/java/enum.h"
Expand All @@ -29,6 +30,7 @@
#include "google/protobuf/compiler/java/service.h"
#include "google/protobuf/compiler/java/shared_code_generator.h"
#include "google/protobuf/compiler/retention.h"
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/io/printer.h"
Expand Down Expand Up @@ -244,6 +246,10 @@ void FileGenerator::Generate(io::Printer* printer) {
"// source: $filename$\n"
"\n",
"filename", file_->name());
if (options_.opensource_runtime) {
printer->Print("// Protobuf Java Version: $protobuf_java_version$\n",
"protobuf_java_version", internal::kProtoJavaVersionString);
}
if (!java_package_.empty()) {
printer->Print(
"package $package$;\n"
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/google/protobuf/compiler/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ cc_library(
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"//src/google/protobuf/compiler:retention",
"//src/google/protobuf/compiler:versions",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
],
Expand Down
10 changes: 8 additions & 2 deletions src/google/protobuf/compiler/python/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "google/protobuf/compiler/python/helpers.h"
#include "google/protobuf/compiler/python/pyi_generator.h"
#include "google/protobuf/compiler/retention.h"
#include "google/protobuf/compiler/versions.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_legacy.h"
Expand Down Expand Up @@ -336,9 +337,14 @@ void Generator::PrintTopBoilerplate() const {
printer_->Print(
"# -*- coding: utf-8 -*-\n"
"# Generated by the protocol buffer compiler. DO NOT EDIT!\n"
"# source: $filename$\n"
"\"\"\"Generated protocol buffer code.\"\"\"\n",
"# source: $filename$\n",
"filename", file_->name());
if (opensource_runtime_) {
printer_->Print("# Protobuf Python Version: $protobuf_python_version$\n",
"protobuf_python_version",
internal::kProtoPythonVersionString);
}
printer_->Print("\"\"\"Generated protocol buffer code.\"\"\"\n");
if (!opensource_runtime_) {
// This import is needed so that compatibility proto1 compiler output
// inserted at protoc_insertion_point can refer to other protos like
Expand Down
68 changes: 68 additions & 0 deletions src/google/protobuf/compiler/versions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef GOOGLE_PROTOBUF_VERSIONS_H__
#define GOOGLE_PROTOBUF_VERSIONS_H__

#include "absl/strings/string_view.h"

namespace google {
namespace protobuf {
namespace compiler {
namespace internal {
// Defines compiler version strings for Protobuf code generators.
//
// Note that the version suffix "-main" implies the main branch. For example,
// 4.25-main reflects a main branch version under development towards 25.x
// release, and thus should not be used for production.
//
// Unlike other repo versions, this file should be updated together with
// version.json whenever possible and reflect the correct version numbers
// correspondingly.
//
// Currently, they are embedded into comments at each gencode for public
// Protobuf C++, Java and Python. Further, we will add version strings for rest
// of languages in version.json, and they will be used to validate version
// compatibility between gencode and runtime.
//
// Versions of other plugins should not put versions here since they will not
// caught by Protobuf releases. Plugin owners should define their versions
// separately.
//
// Please avoid changing them manually, as they should be updated automatically
// by Protobuf release process.
constexpr absl::string_view kProtoCppVersionString = "4.25-main";
constexpr absl::string_view kProtoJavaVersionString = "3.25-main";
constexpr absl::string_view kProtoPythonVersionString = "4.25-main";
} // namespace internal
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_VERSIONS_H__
1 change: 1 addition & 0 deletions src/google/protobuf/cpp_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/google/protobuf/descriptor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4322e00

Please sign in to comment.