From 3e1e06164fb80a6671b5aad51b649c15f078fdc5 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 3 Oct 2023 03:32:01 -0700 Subject: [PATCH] Use proto toolchains in cc_proto_library Retrieve proto_lang_toolchain using toolchains in cc_proto_library when proto toolchain resolution is enabled. Issue: https://github.com/bazelbuild/rules_proto/issues/179 PiperOrigin-RevId: 570334276 Change-Id: If7b7e25254bf3036fdcad67d36474b8fe64a8ffd --- .../common/cc/cc_proto_library.bzl | 40 ++++++++++--------- .../builtins_bzl/common/cc/semantics.bzl | 1 + .../rules/cpp/proto/CcProtoLibraryTest.java | 28 +++++++++++++ 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl index bd99786c1fb86d..312c9587321b01 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl @@ -14,24 +14,25 @@ """Starlark implementation of cc_proto_library""" -load(":common/proto/proto_info.bzl", "ProtoInfo") +load(":common/cc/cc_common.bzl", "cc_common") load(":common/cc/cc_helper.bzl", "cc_helper") -load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", proto_common = "proto_common_do_not_use") load(":common/cc/cc_info.bzl", "CcInfo") -load(":common/cc/cc_common.bzl", "cc_common") +load(":common/cc/semantics.bzl", "semantics") +load(":common/proto/proto_common.bzl", "toolchains", proto_common = "proto_common_do_not_use") +load(":common/proto/proto_info.bzl", "ProtoInfo") ProtoCcFilesInfo = provider(fields = ["files"], doc = "Provide cc proto files.") ProtoCcHeaderInfo = provider(fields = ["headers"], doc = "Provide cc proto headers.") -def _are_srcs_excluded(ctx, target): - return not proto_common.experimental_should_generate_code(target[ProtoInfo], ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo], "cc_proto_library", target.label) +def _are_srcs_excluded(proto_toolchain, target): + return not proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain, "cc_proto_library", target.label) -def _get_feature_configuration(ctx, target, cc_toolchain, proto_info): +def _get_feature_configuration(ctx, target, cc_toolchain, proto_info, proto_toolchain): requested_features = list(ctx.features) unsupported_features = list(ctx.disabled_features) unsupported_features.append("parse_headers") unsupported_features.append("layering_check") - if not _are_srcs_excluded(ctx, target) and len(proto_info.direct_sources) != 0: + if not _are_srcs_excluded(proto_toolchain, target) and len(proto_info.direct_sources) != 0: requested_features.append("header_modules") else: unsupported_features.append("header_modules") @@ -77,12 +78,13 @@ def _get_strip_include_prefix(ctx, proto_info): def _aspect_impl(target, ctx): cc_toolchain = cc_helper.find_cpp_toolchain(ctx) + proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", semantics.CC_PROTO_TOOLCHAIN) proto_info = target[ProtoInfo] - feature_configuration = _get_feature_configuration(ctx, target, cc_toolchain, proto_info) + feature_configuration = _get_feature_configuration(ctx, target, cc_toolchain, proto_info, proto_toolchain) proto_configuration = ctx.fragments.proto deps = [] - runtime = ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo].runtime + runtime = proto_toolchain.runtime if runtime != None: deps.append(runtime) deps.extend(getattr(ctx.rule.attr, "deps", [])) @@ -98,7 +100,7 @@ def _aspect_impl(target, ctx): textual_hdrs = [] additional_exported_hdrs = [] - if _are_srcs_excluded(ctx, target): + if _are_srcs_excluded(proto_toolchain, target): header_provider = None # Hack: This is a proto_library for descriptor.proto or similar. @@ -153,7 +155,7 @@ def _aspect_impl(target, ctx): proto_common.compile( actions = ctx.actions, proto_info = proto_info, - proto_lang_toolchain_info = ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo], + proto_lang_toolchain_info = proto_toolchain, generated_files = outputs, experimental_output_files = "multiple", ) @@ -248,12 +250,11 @@ cc_proto_aspect = aspect( required_providers = [ProtoInfo], provides = [CcInfo], attrs = { - "_aspect_cc_proto_toolchain": attr.label( - default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"), - ), "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"), - }, - toolchains = cc_helper.use_cpp_toolchain(), + } | toolchains.if_legacy_toolchain({"_aspect_cc_proto_toolchain": attr.label( + default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"), + )}), + toolchains = cc_helper.use_cpp_toolchain() + toolchains.use_toolchain(semantics.CC_PROTO_TOOLCHAIN), ) def _impl(ctx): @@ -267,7 +268,8 @@ def _impl(ctx): attr = "deps", ) dep = ctx.attr.deps[0] - proto_common.check_collocated(ctx.label, dep[ProtoInfo], ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo]) + proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", semantics.CC_PROTO_TOOLCHAIN) + proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain) cc_info = dep[CcInfo] output_groups = dep[OutputGroupInfo] return [cc_info, DefaultInfo(files = dep[ProtoCcFilesInfo].files), output_groups] @@ -280,9 +282,11 @@ cc_proto_library = rule( allow_rules = ["proto_library"], allow_files = False, ), + } | toolchains.if_legacy_toolchain({ "_aspect_cc_proto_toolchain": attr.label( default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"), ), - }, + }), provides = [CcInfo], + toolchains = toolchains.use_toolchain(semantics.CC_PROTO_TOOLCHAIN), ) diff --git a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl index c2a47233409c18..fde88aab6ad3df 100644 --- a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl @@ -200,4 +200,5 @@ semantics = struct( get_experimental_link_static_libraries_once = _get_experimental_link_static_libraries_once, check_cc_shared_library_tags = _check_cc_shared_library_tags, BUILD_INFO_TRANLATOR_LABEL = "@bazel_tools//tools/build_defs/build_info:cc_build_info", + CC_PROTO_TOOLCHAIN = "@rules_cc//cc/proto:toolchain_type", ) diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java index 22a7f260bff52b..d7399caf3c7460 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java @@ -56,6 +56,9 @@ public class CcProtoLibraryTest extends BuildViewTestCase { @Before public void setUp() throws Exception { MockProtoSupport.setup(mockToolsConfig); + scratch.file( + "third_party/bazel_rules/rules_cc/cc/proto/BUILD", + "toolchain_type(name = 'toolchain_type', visibility = ['//visibility:public'])"); scratch.file("protobuf/WORKSPACE"); scratch.overwriteFile( "protobuf/BUILD", @@ -82,6 +85,31 @@ public void setUp() throws Exception { invalidatePackages(); // A dash of magic to re-evaluate the WORKSPACE file. } + @Test + public void protoToolchainResolution_enabled() throws Exception { + setBuildLanguageOptions("--incompatible_enable_proto_toolchain_resolution"); + getAnalysisMock() + .ccSupport() + .setupCcToolchainConfig( + mockToolsConfig, + CcToolchainConfig.builder() + .withFeatures( + CppRuleClasses.SUPPORTS_DYNAMIC_LINKER, + CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES)); + scratch.file( + "x/BUILD", + TestConstants.LOAD_PROTO_LIBRARY, + "cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])", + "proto_library(name = 'foo_proto', srcs = ['foo.proto'])"); + assertThat(prettyArtifactNames(getFilesToBuild(getConfiguredTarget("//x:foo_cc_proto")))) + .containsExactly( + "x/foo.pb.h", + "x/foo.pb.cc", + "x/libfoo_proto.a", + "x/libfoo_proto.ifso", + "x/libfoo_proto.so"); + } + @Test public void basic() throws Exception { getAnalysisMock()