-
Notifications
You must be signed in to change notification settings - Fork 16.2k
fix: cc_toolchain should prefer protoc when prebuilt flag is flipped. #25168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # 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*")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Simulate a non-functional CC toolchain | ||
| common --per_file_copt=external/.*protobuf.*@--THIS_CC_TOOLCHAIN_IS_BROKEN | ||
| common --host_per_file_copt=external/.*protobuf.*@--THIS_CC_TOOLCHAIN_IS_BROKEN | ||
| common --per_file_copt=external/.*protobuf.*/src/google/protobuf/compiler/main.cc@--THIS_CC_TOOLCHAIN_IS_BROKEN | ||
| common --host_per_file_copt=external/.*protobuf.*/src/google/protobuf/compiler/main.cc@--THIS_CC_TOOLCHAIN_IS_BROKEN | ||
| # But, users should be able to use pre-built protoc toolchains instead. | ||
| common --incompatible_enable_proto_toolchain_resolution | ||
| common --@com_google_protobuf//bazel/toolchains:prefer_prebuilt_protoc | ||
| common --@com_google_protobuf//bazel/toolchains:prefer_prebuilt_protoc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library") | ||
| load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library") | ||
|
|
||
| proto_library( | ||
| name = "empty_proto", | ||
| srcs = ["empty.proto"], | ||
| ) | ||
|
|
||
| cc_proto_library( | ||
| name = "cc_empty_proto", | ||
| deps = [":empty_proto"], | ||
| # We already have a anaylsis test that asserts that the cc_proto_library | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: analysis |
||
| # uses the correct prebuilt toolchain, this cc_proto_library will never | ||
| # compile since the prebuilt toolchain is always behind the HEAD therefore | ||
| # will end up in version skew compilation errors. | ||
|
mkruskal-google marked this conversation as resolved.
|
||
| tags = ["manual"], | ||
|
mkruskal-google marked this conversation as resolved.
|
||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we actually do a positive check that this uses the prebuilt? Negative regex checks are kindof brittle (a typo trivially passes) and this might start failing if we switch to a prebuilt protoc_minimal