Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,13 @@ proto_lang_toolchain(
command_line = "--cpp_out=$(OUT)",
plugin = "//src/google/protobuf/compiler/cpp:protoc-gen-cpp",
plugin_format_flag = "--plugin=protoc-gen-cpp=%s",
protoc_minimal_do_not_use = "//src/google/protobuf/compiler:protoc_minimal",
protoc_minimal_do_not_use = select({
# Set minimal protoc if prefer_prebuilt_protoc.flag_set is true since a prebuilt for protoc_minimal is not available
# TODO: Add a flag to switch between minimal and full protoc with proto_lang_toolchain once we have a minimal protoc binary.
# Setting this attribute to None will make the toolchain to pick the full protoc binary.
"//bazel/toolchains:prefer_prebuilt_protoc.flag_set": None,
"//conditions:default": "//src/google/protobuf/compiler:protoc_minimal",
}),
runtime = "//src/google/protobuf",
visibility = ["//visibility:public"],
)
Expand Down
5 changes: 5 additions & 0 deletions bazel/tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//bazel:proto_library.bzl", "proto_library")
load(":bazel_proto_library_tests.bzl", "bazel_proto_library_test_suite")
load(":cc_toolchain_tests.bzl", "cc_toolchain_test_suite")
load(":java_proto_library_tests.bzl", "java_proto_library_test_suite")
load(":proto_common_check_collocated_tests.bzl", "proto_common_check_collocated_test_suite")
load(":proto_common_compile_tests.bzl", "proto_common_compile_test_suite")
Expand All @@ -17,4 +20,6 @@ proto_common_check_collocated_test_suite(name = "proto_common_check_collocated_t

bazel_proto_library_test_suite(name = "bazel_proto_library_test_suite")

cc_toolchain_test_suite(name = "cc_toolchain_test_suite")

java_proto_library_test_suite(name = "java_proto_library_test_suite")
81 changes: 81 additions & 0 deletions bazel/tests/cc_toolchain_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Protocol Buffers - Google's data interchange format
# Copyright 2025 Google Inc. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""Tests for cc_toolchain prebuilt protoc configuration."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", "util")
load("//bazel:proto_library.bzl", "proto_library")
load("//bazel/tests/testdata:compile_rule.bzl", "compile_rule")

_PREFER_PREBUILT_PROTOC = str(Label("//bazel/toolchains:prefer_prebuilt_protoc"))

def cc_toolchain_test_suite(name):
test_suite(
name = name,
tests = [
_test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set,
_test_cc_toolchain_uses_protoc_minimal_by_default,
],
)

def _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set(name):
util.helper_target(
proto_library,
name = name + "_proto",
srcs = ["A.proto"],
)
util.helper_target(
compile_rule,
name = name + "_compile",
proto_dep = ":" + name + "_proto",
toolchain = "//:cc_toolchain",
)

analysis_test(
name = name,
target = name + "_compile",
impl = _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set_impl,
config_settings = {_PREFER_PREBUILT_PROTOC: True},
)

def _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set_impl(env, target):
# Find the compile action
action = env.expect.that_target(target).action_named("GenProto")

# When prefer_prebuilt_protoc is True, protoc_minimal_do_not_use is None,
# so the cc_toolchain should use the full protoc (not protoc_minimal).
# The protoc path should end with "/protoc" not contain "protoc_minimal"
action.argv().contains_predicate(matching.str_matches("*/protoc"))
action.argv().not_contains_predicate(matching.str_matches("*protoc_minimal*"))

def _test_cc_toolchain_uses_protoc_minimal_by_default(name):
util.helper_target(
proto_library,
name = name + "_proto",
srcs = ["A.proto"],
)
util.helper_target(
compile_rule,
name = name + "_compile",
proto_dep = ":" + name + "_proto",
toolchain = "//:cc_toolchain",
)

analysis_test(
name = name,
target = name + "_compile",
impl = _test_cc_toolchain_uses_protoc_minimal_by_default_impl,
)

def _test_cc_toolchain_uses_protoc_minimal_by_default_impl(env, target):
# Find the compile action
action = env.expect.that_target(target).action_named("GenProto")

# By default (prefer_prebuilt_protoc is False), protoc_minimal_do_not_use is set,
# so the cc_toolchain should use protoc_minimal.
action.argv().contains_predicate(matching.str_matches("*protoc_minimal*"))
6 changes: 5 additions & 1 deletion bazel/toolchains/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ bool_flag(
config_setting(
name = "prefer_prebuilt_protoc.flag_set",
flag_values = {":prefer_prebuilt_protoc": "true"},
visibility = ["//bazel/private/toolchains/prebuilt:__pkg__"],
visibility = [
"//bazel/private/toolchains/prebuilt:__pkg__",
# Needed by cc_toolchain to switch between minimal and full protoc
"//:__pkg__",
],
)

# The public API users set to disable the validation action failing.
Expand Down
6 changes: 6 additions & 0 deletions cmake/conformance.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ add_custom_command(
${protobuf_BINARY_DIR}/conformance/conformance.pb.cc
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition2023.pb.h
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition2023.pb.cc
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition_unstable.pb.h
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition_unstable.pb.cc
DEPENDS ${protobuf_PROTOC_EXE}
${protobuf_SOURCE_DIR}/conformance/conformance.proto
${protobuf_SOURCE_DIR}/conformance/test_protos/test_messages_edition2023.proto
${protobuf_SOURCE_DIR}/conformance/test_protos/test_messages_edition_unstable.proto
COMMAND ${protobuf_PROTOC_EXE}
${protobuf_SOURCE_DIR}/conformance/conformance.proto
${protobuf_SOURCE_DIR}/conformance/test_protos/test_messages_edition2023.proto
${protobuf_SOURCE_DIR}/conformance/test_protos/test_messages_edition_unstable.proto
--proto_path=${protobuf_SOURCE_DIR}
--cpp_out=${protobuf_BINARY_DIR}
)
Expand Down Expand Up @@ -89,6 +93,8 @@ add_library(libconformance_common STATIC
${protobuf_BINARY_DIR}/conformance/conformance.pb.cc
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition2023.pb.h
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition2023.pb.cc
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition_unstable.pb.h
${protobuf_BINARY_DIR}/conformance/test_protos/test_messages_edition_unstable.pb.cc
${protobuf_BINARY_DIR}/editions/golden/test_messages_proto3_editions.pb.h
${protobuf_BINARY_DIR}/editions/golden/test_messages_proto3_editions.pb.cc
${protobuf_BINARY_DIR}/editions/golden/test_messages_proto2_editions.pb.h
Expand Down
10 changes: 10 additions & 0 deletions conformance/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ cc_library(
"//:test_messages_proto2_cc_proto",
"//:test_messages_proto3_cc_proto",
"//conformance/test_protos:test_messages_edition2023_cc_proto",
"//conformance/test_protos:test_messages_edition_unstable_cc_proto",
"//editions:test_messages_proto2_editions_cc_proto",
"//editions:test_messages_proto3_editions_cc_proto",
"//src/google/protobuf",
Expand Down Expand Up @@ -167,6 +168,7 @@ cc_library(
"//:test_messages_proto2_cc_proto",
"//:test_messages_proto3_cc_proto",
"//conformance/test_protos:test_messages_edition2023_cc_proto",
"//conformance/test_protos:test_messages_edition_unstable_cc_proto",
"//editions:test_messages_proto2_editions_cc_proto",
"//editions:test_messages_proto3_editions_cc_proto",
"//src/google/protobuf",
Expand Down Expand Up @@ -250,6 +252,7 @@ cc_test(
"//:test_messages_proto2_cc_proto",
"//:test_messages_proto3_cc_proto",
"//conformance/test_protos:test_messages_edition2023_cc_proto",
"//conformance/test_protos:test_messages_edition_unstable_cc_proto",
"//editions:test_messages_proto2_editions_cc_proto",
"//editions:test_messages_proto3_editions_cc_proto",
"@googletest//:gtest",
Expand Down Expand Up @@ -449,6 +452,7 @@ cc_binary(
"//:type_cc_proto",
"//:wrappers_cc_proto",
"//conformance/test_protos:test_messages_edition2023_cc_proto",
"//conformance/test_protos:test_messages_edition_unstable_cc_proto",
"//editions:test_messages_proto2_editions_cc_proto",
"//editions:test_messages_proto3_editions_cc_proto",
"//src/google/protobuf",
Expand Down Expand Up @@ -481,6 +485,7 @@ java_binary(
"//:test_messages_proto2_java_proto",
"//:test_messages_proto3_java_proto",
"//conformance/test_protos:test_messages_edition2023_java_proto",
"//conformance/test_protos:test_messages_edition_unstable_java_proto",
"//editions:test_messages_proto2_editions_java_proto",
"//editions:test_messages_proto3_editions_java_proto",
],
Expand All @@ -501,6 +506,7 @@ java_binary(
"//:test_messages_proto2_java_proto_lite",
"//:test_messages_proto3_java_proto_lite",
"//conformance/test_protos:test_messages_edition2023_java_proto_lite",
"//conformance/test_protos:test_messages_edition_unstable_java_proto_lite",
"//editions:test_messages_proto2_editions_java_proto_lite",
"//editions:test_messages_proto3_editions_java_proto_lite",
],
Expand All @@ -520,6 +526,7 @@ py_binary(
":conformance_py_proto",
"//:protobuf_python",
"//conformance/test_protos:test_messages_edition2023_py_pb2",
"//conformance/test_protos:test_messages_edition_unstable_py_pb2",
"//editions:test_messages_proto2_editions_py_pb2",
"//editions:test_messages_proto3_editions_py_pb2",
"//python:_message", # Make upb visible if we need it.
Expand Down Expand Up @@ -581,6 +588,7 @@ inline_sh_binary(
visibility = ["//csharp:__subpackages__"],
deps = [
"//conformance/test_protos:test_messages_edition2023_csharp_proto",
"//conformance/test_protos:test_messages_edition_unstable_csharp_proto",
"//csharp/src/Google.Protobuf.Conformance:conformance_runfiles",
],
)
Expand All @@ -596,6 +604,7 @@ objc_library(
"//:test_messages_proto2_objc_proto",
"//:test_messages_proto3_objc_proto",
"//conformance/test_protos:test_messages_edition2023_objc_proto",
"//conformance/test_protos:test_messages_edition_unstable_objc_proto",
"//editions:test_messages_proto2_editions_objc_proto",
"//editions:test_messages_proto3_editions_objc_proto",
],
Expand All @@ -619,6 +628,7 @@ rb_binary(
deps = [
":conformance_ruby_proto",
"//conformance/test_protos:test_messages_edition2023_ruby_proto",
"//conformance/test_protos:test_messages_edition_unstable_ruby_proto",
"//ruby:conformance_editions_test_ruby_proto",
"//ruby:conformance_test_ruby_proto",
"//ruby:protobuf",
Expand Down
6 changes: 6 additions & 0 deletions conformance/ConformanceJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.protobuf.util.JsonFormat.TypeRegistry;
import com.google.protobuf_test_messages.edition2023.TestAllTypesEdition2023;
import com.google.protobuf_test_messages.edition2023.TestMessagesEdition2023;
import com.google.protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable;
import com.google.protobuf_test_messages.edition_unstable.TestMessagesEditionUnstableProto;
import com.google.protobuf_test_messages.editions.proto2.TestMessagesProto2Editions;
import com.google.protobuf_test_messages.editions.proto3.TestMessagesProto3Editions;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
Expand Down Expand Up @@ -217,6 +219,8 @@ private Class<? extends AbstractMessage> createTestMessage(String messageType) {
return TestAllTypesProto2.class;
case "protobuf_test_messages.editions.TestAllTypesEdition2023":
return TestAllTypesEdition2023.class;
case "protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable":
return TestAllTypesEditionUnstable.class;
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
return TestMessagesProto3Editions.TestAllTypesProto3.class;
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
Expand All @@ -235,6 +239,8 @@ private Class<?> createTestFile(String messageType) {
return TestMessagesProto2.class;
case "protobuf_test_messages.editions.TestAllTypesEdition2023":
return TestMessagesEdition2023.class;
case "protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable":
return TestMessagesEditionUnstableProto.class;
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
return TestMessagesProto3Editions.class;
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
Expand Down
6 changes: 6 additions & 0 deletions conformance/ConformanceJavaLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.google.protobuf.conformance.Conformance;
import com.google.protobuf_test_messages.edition2023.TestAllTypesEdition2023;
import com.google.protobuf_test_messages.edition2023.TestMessagesEdition2023;
import com.google.protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable;
import com.google.protobuf_test_messages.edition_unstable.TestMessagesEditionUnstableProto;
import com.google.protobuf_test_messages.editions.proto2.TestMessagesProto2Editions;
import com.google.protobuf_test_messages.editions.proto3.TestMessagesProto3Editions;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
Expand Down Expand Up @@ -216,6 +218,8 @@ private Class<? extends AbstractMessageLite> createTestMessage(String messageTyp
return TestAllTypesProto2.class;
case "protobuf_test_messages.editions.TestAllTypesEdition2023":
return TestAllTypesEdition2023.class;
case "protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable":
return TestAllTypesEditionUnstable.class;
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
return TestMessagesProto3Editions.TestAllTypesProto3.class;
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
Expand All @@ -234,6 +238,8 @@ private Class<?> createTestFile(String messageType) {
return TestMessagesProto2.class;
case "protobuf_test_messages.editions.TestAllTypesEdition2023":
return TestMessagesEdition2023.class;
case "protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable":
return TestMessagesEditionUnstableProto.class;
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
return TestMessagesProto3Editions.class;
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
Expand Down
19 changes: 19 additions & 0 deletions conformance/binary_json_conformance_suite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "conformance/conformance.pb.h"
#include "conformance_test.h"
#include "conformance/test_protos/test_messages_edition2023.pb.h"
#include "conformance/test_protos/test_messages_edition_unstable.pb.h"
#include "editions/golden/test_messages_proto2_editions.pb.h"
#include "editions/golden/test_messages_proto3_editions.pb.h"
#include "google/protobuf/json/json.h"
Expand All @@ -51,6 +52,7 @@ using google::protobuf::Descriptor;
using google::protobuf::FieldDescriptor;
using google::protobuf::internal::WireFormatLite;
using google::protobuf::util::NewTypeResolverForDescriptorPool;
using protobuf_test_messages::edition_unstable::TestAllTypesEditionUnstable;
using protobuf_test_messages::editions::TestAllTypesEdition2023;
using protobuf_test_messages::proto2::TestAllTypesProto2;
using protobuf_test_messages::proto3::TestAllTypesProto3;
Expand Down Expand Up @@ -318,6 +320,7 @@ void BinaryAndJsonConformanceSuite::RunSuiteImpl() {
BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto2Editions>(
this, /*run_proto3_tests=*/false);
RunDelimitedFieldTests();
RunUnstableTests();
}
}

Expand Down Expand Up @@ -375,6 +378,22 @@ void BinaryAndJsonConformanceSuite::RunDelimitedFieldTests() {
R"pb([protobuf_test_messages.editions.delimited_ext] { c: 99 })pb");
}

void BinaryAndJsonConformanceSuite::RunUnstableTests() {
SetTypeUrl(GetTypeUrl(TestAllTypesEditionUnstable::GetDescriptor()));

RunValidProtobufTest<TestAllTypesEditionUnstable>(
absl::StrCat("ValidInt32"), REQUIRED,
field(1, WireFormatLite::WIRETYPE_VARINT, varint(99)),
R"pb(optional_int32: 99)pb");

RunValidProtobufTest<TestAllTypesEditionUnstable>(
absl::StrCat("ValidMap.Integer"), REQUIRED,
len(8,
absl::StrCat(field(1, WireFormatLite::WIRETYPE_VARINT, varint(99)),
field(2, WireFormatLite::WIRETYPE_VARINT, varint(87)))),
R"pb(map_int32_int32 { key: 99 value: 87 })pb");
}

void BinaryAndJsonConformanceSuite::RunMessageSetTests() {
RunValidBinaryProtobufTest<TestAllTypesProto2>(
absl::StrCat("ValidMessageSetEncoding"), REQUIRED,
Expand Down
2 changes: 2 additions & 0 deletions conformance/binary_json_conformance_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class BinaryAndJsonConformanceSuite : public ConformanceTestSuite {

void RunDelimitedFieldTests();

void RunUnstableTests();

void RunMessageSetTests();

template <typename MessageType>
Expand Down
3 changes: 2 additions & 1 deletion conformance/conformance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ message ConformanceRequest {
// protobuf_test_messages.proto2.TestAllTypesProto2 or
// protobuf_test_messages.editions.proto2.TestAllTypesProto2 or
// protobuf_test_messages.editions.proto3.TestAllTypesProto3 or
// protobuf_test_messages.editions.TestAllTypesEdition2023.
// protobuf_test_messages.editions.TestAllTypesEdition2023 or
// protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable.
string message_type = 4;

// Each test is given a specific test category. Some category may need
Expand Down
3 changes: 3 additions & 0 deletions conformance/conformance_cpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "absl/strings/str_cat.h"
#include "conformance/conformance.pb.h"
#include "conformance/test_protos/test_messages_edition2023.pb.h"
#include "conformance/test_protos/test_messages_edition_unstable.pb.h"
#include "editions/golden/test_messages_proto2_editions.pb.h"
#include "editions/golden/test_messages_proto3_editions.pb.h"
#include "google/protobuf/endian.h"
Expand All @@ -58,6 +59,7 @@ using ::google::protobuf::util::JsonStringToMessage;
using ::google::protobuf::util::MessageToJsonString;
using ::google::protobuf::util::NewTypeResolverForDescriptorPool;
using ::google::protobuf::util::TypeResolver;
using ::protobuf_test_messages::edition_unstable::TestAllTypesEditionUnstable;
using ::protobuf_test_messages::editions::TestAllTypesEdition2023;
using ::protobuf_test_messages::proto2::TestAllTypesProto2;
using ::protobuf_test_messages::proto3::TestAllTypesProto3;
Expand Down Expand Up @@ -97,6 +99,7 @@ class Harness {
google::protobuf::LinkMessageReflection<TestAllTypesProto2>();
google::protobuf::LinkMessageReflection<TestAllTypesProto3>();
google::protobuf::LinkMessageReflection<TestAllTypesEdition2023>();
google::protobuf::LinkMessageReflection<TestAllTypesEditionUnstable>();
google::protobuf::LinkMessageReflection<TestAllTypesProto2Editions>();
google::protobuf::LinkMessageReflection<TestAllTypesProto3Editions>();

Expand Down
6 changes: 6 additions & 0 deletions conformance/conformance_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "google/protobuf/TestMessagesProto2.pbobjc.h"
#import "google/protobuf/TestMessagesProto3.pbobjc.h"
#import "test_protos/TestMessagesEdition2023.pbobjc.h"
#import "test_protos/TestMessagesEditionUnstable.pbobjc.h"

static void Die(NSString *format, ...) __dead2;

Expand Down Expand Up @@ -65,6 +66,11 @@ static void Die(NSString *format, ...) {
isEqual:@"protobuf_test_messages.editions.TestAllTypesEdition2023"]) {
msgClass = [EditionsTestAllTypesEdition2023 class];
registry = [EditionsTestMessagesEdition2023Root extensionRegistry];
} else if ([request.messageType
isEqual:
@"protobuf_test_messages.edition_unstable.TestAllTypesEditionUnstable"]) {
msgClass = [EditionUnstableTestAllTypesEditionUnstable class];
registry = [EditionUnstableTestMessagesEditionUnstableRoot extensionRegistry];
} else if ([request.messageType
isEqual:@"protobuf_test_messages.editions.proto2.TestAllTypesProto2"]) {
msgClass = [EditionsProto2TestAllTypesProto2 class];
Expand Down
Loading
Loading