diff --git a/src/google/protobuf/compiler/BUILD.bazel b/src/google/protobuf/compiler/BUILD.bazel index f5d04b0ac8a0..87e075347ce1 100644 --- a/src/google/protobuf/compiler/BUILD.bazel +++ b/src/google/protobuf/compiler/BUILD.bazel @@ -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 = [ diff --git a/src/google/protobuf/compiler/cpp/BUILD.bazel b/src/google/protobuf/compiler/cpp/BUILD.bazel index d49d41128a5e..62cc02a83a17 100644 --- a/src/google/protobuf/compiler/cpp/BUILD.bazel +++ b/src/google/protobuf/compiler/cpp/BUILD.bazel @@ -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", diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index 2fe6c3fa4631..4320aa847f19 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc @@ -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" @@ -308,10 +309,15 @@ void FileGenerator::GenerateFile(io::Printer* p, GeneratedFileType file_type, std::function 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$ diff --git a/src/google/protobuf/compiler/java/BUILD.bazel b/src/google/protobuf/compiler/java/BUILD.bazel index 94573892c491..33c9e8db189c 100644 --- a/src/google/protobuf/compiler/java/BUILD.bazel +++ b/src/google/protobuf/compiler/java/BUILD.bazel @@ -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", @@ -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__"], ) diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index bcf1158b6192..e3aa0cf8a56e 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -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" @@ -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" @@ -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" diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index eeb64ac57ff9..b43349df63a2 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -1,5 +1,6 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/compiler/plugin.proto +// Protobuf C++ Version: 4.25-main #ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto_2epb_2eh #define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcompiler_2fplugin_2eproto_2epb_2eh diff --git a/src/google/protobuf/compiler/python/BUILD.bazel b/src/google/protobuf/compiler/python/BUILD.bazel index 5d26e0ce9014..64b5808cb603 100644 --- a/src/google/protobuf/compiler/python/BUILD.bazel +++ b/src/google/protobuf/compiler/python/BUILD.bazel @@ -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", ], diff --git a/src/google/protobuf/compiler/python/generator.cc b/src/google/protobuf/compiler/python/generator.cc index 9b6e445516a8..cb36ab1a1548 100644 --- a/src/google/protobuf/compiler/python/generator.cc +++ b/src/google/protobuf/compiler/python/generator.cc @@ -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" @@ -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 diff --git a/src/google/protobuf/compiler/versions.h b/src/google/protobuf/compiler/versions.h new file mode 100644 index 000000000000..31f1d45e73e1 --- /dev/null +++ b/src/google/protobuf/compiler/versions.h @@ -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__ diff --git a/src/google/protobuf/cpp_features.pb.h b/src/google/protobuf/cpp_features.pb.h index 4e501679ab0a..09ce59dea5ad 100644 --- a/src/google/protobuf/cpp_features.pb.h +++ b/src/google/protobuf/cpp_features.pb.h @@ -1,5 +1,6 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/cpp_features.proto +// Protobuf C++ Version: 4.25-main #ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcpp_5ffeatures_2eproto_2epb_2eh #define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fcpp_5ffeatures_2eproto_2epb_2eh diff --git a/src/google/protobuf/descriptor.pb.h b/src/google/protobuf/descriptor.pb.h index cf5d01590022..5b4e2e677658 100644 --- a/src/google/protobuf/descriptor.pb.h +++ b/src/google/protobuf/descriptor.pb.h @@ -1,5 +1,6 @@ // Generated by the protocol buffer compiler. DO NOT EDIT! // source: google/protobuf/descriptor.proto +// Protobuf C++ Version: 4.25-main #ifndef GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto_2epb_2eh #define GOOGLE_PROTOBUF_INCLUDED_google_2fprotobuf_2fdescriptor_2eproto_2epb_2eh