|
| 1 | +# Protocol Buffers - Google's data interchange format |
| 2 | +# Copyright 2025 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 | +"""Tests for cc_toolchain prebuilt protoc configuration.""" |
| 8 | + |
| 9 | +load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") |
| 10 | +load("@rules_testing//lib:truth.bzl", "matching") |
| 11 | +load("@rules_testing//lib:util.bzl", "util") |
| 12 | +load("//bazel:proto_library.bzl", "proto_library") |
| 13 | +load("//bazel/tests/testdata:compile_rule.bzl", "compile_rule") |
| 14 | + |
| 15 | +_PREFER_PREBUILT_PROTOC = str(Label("//bazel/toolchains:prefer_prebuilt_protoc")) |
| 16 | + |
| 17 | +def cc_toolchain_test_suite(name): |
| 18 | + test_suite( |
| 19 | + name = name, |
| 20 | + tests = [ |
| 21 | + _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set, |
| 22 | + _test_cc_toolchain_uses_protoc_minimal_by_default, |
| 23 | + ], |
| 24 | + ) |
| 25 | + |
| 26 | +def _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set(name): |
| 27 | + util.helper_target( |
| 28 | + proto_library, |
| 29 | + name = name + "_proto", |
| 30 | + srcs = ["A.proto"], |
| 31 | + ) |
| 32 | + util.helper_target( |
| 33 | + compile_rule, |
| 34 | + name = name + "_compile", |
| 35 | + proto_dep = ":" + name + "_proto", |
| 36 | + toolchain = "//:cc_toolchain", |
| 37 | + ) |
| 38 | + |
| 39 | + analysis_test( |
| 40 | + name = name, |
| 41 | + target = name + "_compile", |
| 42 | + impl = _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set_impl, |
| 43 | + config_settings = {_PREFER_PREBUILT_PROTOC: True}, |
| 44 | + ) |
| 45 | + |
| 46 | +def _test_cc_toolchain_uses_full_protoc_when_prefer_prebuilt_flag_set_impl(env, target): |
| 47 | + # Find the compile action |
| 48 | + action = env.expect.that_target(target).action_named("GenProto") |
| 49 | + |
| 50 | + # When prefer_prebuilt_protoc is True, protoc_minimal_do_not_use is None, |
| 51 | + # so the cc_toolchain should use the full protoc (not protoc_minimal). |
| 52 | + # The protoc path should end with "/protoc" not contain "protoc_minimal" |
| 53 | + action.argv().contains_predicate(matching.str_matches("*/protoc")) |
| 54 | + action.argv().not_contains_predicate(matching.str_matches("*protoc_minimal*")) |
| 55 | + |
| 56 | +def _test_cc_toolchain_uses_protoc_minimal_by_default(name): |
| 57 | + util.helper_target( |
| 58 | + proto_library, |
| 59 | + name = name + "_proto", |
| 60 | + srcs = ["A.proto"], |
| 61 | + ) |
| 62 | + util.helper_target( |
| 63 | + compile_rule, |
| 64 | + name = name + "_compile", |
| 65 | + proto_dep = ":" + name + "_proto", |
| 66 | + toolchain = "//:cc_toolchain", |
| 67 | + ) |
| 68 | + |
| 69 | + analysis_test( |
| 70 | + name = name, |
| 71 | + target = name + "_compile", |
| 72 | + impl = _test_cc_toolchain_uses_protoc_minimal_by_default_impl, |
| 73 | + ) |
| 74 | + |
| 75 | +def _test_cc_toolchain_uses_protoc_minimal_by_default_impl(env, target): |
| 76 | + # Find the compile action |
| 77 | + action = env.expect.that_target(target).action_named("GenProto") |
| 78 | + |
| 79 | + # By default (prefer_prebuilt_protoc is False), protoc_minimal_do_not_use is set, |
| 80 | + # so the cc_toolchain should use protoc_minimal. |
| 81 | + action.argv().contains_predicate(matching.str_matches("*protoc_minimal*")) |
0 commit comments