1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #include "google/protobuf/compiler/versions.h" 9 10 #include <string> 11 #include <vector> 12 13 #include "absl/log/absl_check.h" 14 #include "absl/strings/str_cat.h" 15 #include "absl/strings/str_split.h" 16 #include "absl/strings/string_view.h" 17 #include "google/protobuf/compiler/plugin.pb.h" 18 19 // Must be included last. 20 #include "google/protobuf/port_def.inc" 21 22 namespace google { 23 namespace protobuf { 24 namespace compiler { 25 namespace internal { ParseProtobufVersion(absl::string_view version)26Version ParseProtobufVersion(absl::string_view version) { 27 ABSL_CHECK(!version.empty()) << "version cannot be empty."; 28 Version result; 29 std::vector<std::string> parts = absl::StrSplit(version, '-'); 30 ABSL_CHECK(parts.size() <= 2) 31 << "version cannot have more than one suffix annotated by \"-\"."; 32 if (parts.size() == 2) result.set_suffix(absl::StrCat("-", parts[1])); 33 parts = absl::StrSplit(parts[0], '.'); 34 ABSL_CHECK(parts.size() == 3) 35 << "version string must provide major, minor and micro numbers."; 36 result.set_major(std::stoi(parts[0])); 37 result.set_minor(std::stoi(parts[1])); 38 result.set_patch(std::stoi(parts[2])); 39 return result; 40 } 41 } // namespace internal 42 GetProtobufCPPVersion(bool oss_runtime)43const Version& GetProtobufCPPVersion(bool oss_runtime) { 44 absl::string_view version = PROTOBUF_CPP_VERSION_STRING; 45 // Heap-allocated versions to avoid re-parsing version strings 46 static const Version* cpp_version = 47 new Version(internal::ParseProtobufVersion(version)); 48 return *cpp_version; 49 } 50 GetProtobufJavaVersion(bool oss_runtime)51const Version& GetProtobufJavaVersion(bool oss_runtime) { 52 absl::string_view version = PROTOBUF_JAVA_VERSION_STRING; 53 static const Version* java_version = 54 new Version(internal::ParseProtobufVersion(version)); 55 return *java_version; 56 } 57 GetProtobufPythonVersion(bool oss_runtime)58const Version& GetProtobufPythonVersion(bool oss_runtime) { 59 absl::string_view version = PROTOBUF_PYTHON_VERSION_STRING; 60 static const Version* python_version = 61 new Version(internal::ParseProtobufVersion(version)); 62 return *python_version; 63 } 64 } // namespace compiler 65 } // namespace protobuf 66 } // namespace google 67 68 #include "google/protobuf/port_undef.inc" 69