Skip to content

Commit

Permalink
Use proto toolchains in cc_proto_library
Browse files Browse the repository at this point in the history
Retrieve proto_lang_toolchain using toolchains in cc_proto_library when proto toolchain resolution is enabled.

Issue: bazelbuild/rules_proto#179
PiperOrigin-RevId: 570334276
Change-Id: If7b7e25254bf3036fdcad67d36474b8fe64a8ffd
  • Loading branch information
comius authored and copybara-github committed Oct 3, 2023
1 parent b34ea66 commit 3e1e061
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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", []))
Expand All @@ -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.
Expand Down Expand Up @@ -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",
)
Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand All @@ -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),
)
1 change: 1 addition & 0 deletions src/main/starlark/builtins_bzl/common/cc/semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down

0 comments on commit 3e1e061

Please sign in to comment.