Skip to content

Commit 3e1e061

Browse files
comiuscopybara-github
authored andcommitted
Use proto toolchains in cc_proto_library
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
1 parent b34ea66 commit 3e1e061

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

src/main/starlark/builtins_bzl/common/cc/cc_proto_library.bzl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@
1414

1515
"""Starlark implementation of cc_proto_library"""
1616

17-
load(":common/proto/proto_info.bzl", "ProtoInfo")
17+
load(":common/cc/cc_common.bzl", "cc_common")
1818
load(":common/cc/cc_helper.bzl", "cc_helper")
19-
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", proto_common = "proto_common_do_not_use")
2019
load(":common/cc/cc_info.bzl", "CcInfo")
21-
load(":common/cc/cc_common.bzl", "cc_common")
20+
load(":common/cc/semantics.bzl", "semantics")
21+
load(":common/proto/proto_common.bzl", "toolchains", proto_common = "proto_common_do_not_use")
22+
load(":common/proto/proto_info.bzl", "ProtoInfo")
2223

2324
ProtoCcFilesInfo = provider(fields = ["files"], doc = "Provide cc proto files.")
2425
ProtoCcHeaderInfo = provider(fields = ["headers"], doc = "Provide cc proto headers.")
2526

26-
def _are_srcs_excluded(ctx, target):
27-
return not proto_common.experimental_should_generate_code(target[ProtoInfo], ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo], "cc_proto_library", target.label)
27+
def _are_srcs_excluded(proto_toolchain, target):
28+
return not proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain, "cc_proto_library", target.label)
2829

29-
def _get_feature_configuration(ctx, target, cc_toolchain, proto_info):
30+
def _get_feature_configuration(ctx, target, cc_toolchain, proto_info, proto_toolchain):
3031
requested_features = list(ctx.features)
3132
unsupported_features = list(ctx.disabled_features)
3233
unsupported_features.append("parse_headers")
3334
unsupported_features.append("layering_check")
34-
if not _are_srcs_excluded(ctx, target) and len(proto_info.direct_sources) != 0:
35+
if not _are_srcs_excluded(proto_toolchain, target) and len(proto_info.direct_sources) != 0:
3536
requested_features.append("header_modules")
3637
else:
3738
unsupported_features.append("header_modules")
@@ -77,12 +78,13 @@ def _get_strip_include_prefix(ctx, proto_info):
7778

7879
def _aspect_impl(target, ctx):
7980
cc_toolchain = cc_helper.find_cpp_toolchain(ctx)
81+
proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", semantics.CC_PROTO_TOOLCHAIN)
8082
proto_info = target[ProtoInfo]
81-
feature_configuration = _get_feature_configuration(ctx, target, cc_toolchain, proto_info)
83+
feature_configuration = _get_feature_configuration(ctx, target, cc_toolchain, proto_info, proto_toolchain)
8284
proto_configuration = ctx.fragments.proto
8385

8486
deps = []
85-
runtime = ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo].runtime
87+
runtime = proto_toolchain.runtime
8688
if runtime != None:
8789
deps.append(runtime)
8890
deps.extend(getattr(ctx.rule.attr, "deps", []))
@@ -98,7 +100,7 @@ def _aspect_impl(target, ctx):
98100
textual_hdrs = []
99101
additional_exported_hdrs = []
100102

101-
if _are_srcs_excluded(ctx, target):
103+
if _are_srcs_excluded(proto_toolchain, target):
102104
header_provider = None
103105

104106
# Hack: This is a proto_library for descriptor.proto or similar.
@@ -153,7 +155,7 @@ def _aspect_impl(target, ctx):
153155
proto_common.compile(
154156
actions = ctx.actions,
155157
proto_info = proto_info,
156-
proto_lang_toolchain_info = ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo],
158+
proto_lang_toolchain_info = proto_toolchain,
157159
generated_files = outputs,
158160
experimental_output_files = "multiple",
159161
)
@@ -248,12 +250,11 @@ cc_proto_aspect = aspect(
248250
required_providers = [ProtoInfo],
249251
provides = [CcInfo],
250252
attrs = {
251-
"_aspect_cc_proto_toolchain": attr.label(
252-
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"),
253-
),
254253
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
255-
},
256-
toolchains = cc_helper.use_cpp_toolchain(),
254+
} | toolchains.if_legacy_toolchain({"_aspect_cc_proto_toolchain": attr.label(
255+
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"),
256+
)}),
257+
toolchains = cc_helper.use_cpp_toolchain() + toolchains.use_toolchain(semantics.CC_PROTO_TOOLCHAIN),
257258
)
258259

259260
def _impl(ctx):
@@ -267,7 +268,8 @@ def _impl(ctx):
267268
attr = "deps",
268269
)
269270
dep = ctx.attr.deps[0]
270-
proto_common.check_collocated(ctx.label, dep[ProtoInfo], ctx.attr._aspect_cc_proto_toolchain[ProtoLangToolchainInfo])
271+
proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", semantics.CC_PROTO_TOOLCHAIN)
272+
proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain)
271273
cc_info = dep[CcInfo]
272274
output_groups = dep[OutputGroupInfo]
273275
return [cc_info, DefaultInfo(files = dep[ProtoCcFilesInfo].files), output_groups]
@@ -280,9 +282,11 @@ cc_proto_library = rule(
280282
allow_rules = ["proto_library"],
281283
allow_files = False,
282284
),
285+
} | toolchains.if_legacy_toolchain({
283286
"_aspect_cc_proto_toolchain": attr.label(
284287
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"),
285288
),
286-
},
289+
}),
287290
provides = [CcInfo],
291+
toolchains = toolchains.use_toolchain(semantics.CC_PROTO_TOOLCHAIN),
288292
)

src/main/starlark/builtins_bzl/common/cc/semantics.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,5 @@ semantics = struct(
200200
get_experimental_link_static_libraries_once = _get_experimental_link_static_libraries_once,
201201
check_cc_shared_library_tags = _check_cc_shared_library_tags,
202202
BUILD_INFO_TRANLATOR_LABEL = "@bazel_tools//tools/build_defs/build_info:cc_build_info",
203+
CC_PROTO_TOOLCHAIN = "@rules_cc//cc/proto:toolchain_type",
203204
)

src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public class CcProtoLibraryTest extends BuildViewTestCase {
5656
@Before
5757
public void setUp() throws Exception {
5858
MockProtoSupport.setup(mockToolsConfig);
59+
scratch.file(
60+
"third_party/bazel_rules/rules_cc/cc/proto/BUILD",
61+
"toolchain_type(name = 'toolchain_type', visibility = ['//visibility:public'])");
5962
scratch.file("protobuf/WORKSPACE");
6063
scratch.overwriteFile(
6164
"protobuf/BUILD",
@@ -82,6 +85,31 @@ public void setUp() throws Exception {
8285
invalidatePackages(); // A dash of magic to re-evaluate the WORKSPACE file.
8386
}
8487

88+
@Test
89+
public void protoToolchainResolution_enabled() throws Exception {
90+
setBuildLanguageOptions("--incompatible_enable_proto_toolchain_resolution");
91+
getAnalysisMock()
92+
.ccSupport()
93+
.setupCcToolchainConfig(
94+
mockToolsConfig,
95+
CcToolchainConfig.builder()
96+
.withFeatures(
97+
CppRuleClasses.SUPPORTS_DYNAMIC_LINKER,
98+
CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES));
99+
scratch.file(
100+
"x/BUILD",
101+
TestConstants.LOAD_PROTO_LIBRARY,
102+
"cc_proto_library(name = 'foo_cc_proto', deps = ['foo_proto'])",
103+
"proto_library(name = 'foo_proto', srcs = ['foo.proto'])");
104+
assertThat(prettyArtifactNames(getFilesToBuild(getConfiguredTarget("//x:foo_cc_proto"))))
105+
.containsExactly(
106+
"x/foo.pb.h",
107+
"x/foo.pb.cc",
108+
"x/libfoo_proto.a",
109+
"x/libfoo_proto.ifso",
110+
"x/libfoo_proto.so");
111+
}
112+
85113
@Test
86114
public void basic() throws Exception {
87115
getAnalysisMock()

0 commit comments

Comments
 (0)