Skip to content

Commit d1f4b2f

Browse files
protobuf-github-bottonyliaoss
authored andcommitted
Introduce Starlark versions of Protobuf flags.
PiperOrigin-RevId: 859848490
1 parent ea5534f commit d1f4b2f

File tree

15 files changed

+285
-26
lines changed

15 files changed

+285
-26
lines changed

bazel/flags/BUILD

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag")
3+
4+
package(
5+
default_applicable_licenses = ["//:license"],
6+
default_visibility = ["//visibility:public"],
7+
)
8+
9+
bzl_library(
10+
name = "flags_bzl",
11+
srcs = ["flags.bzl"],
12+
deps = [
13+
"@bazel_skylib//rules:common_settings",
14+
],
15+
)
16+
17+
filegroup(
18+
name = "for_bazel_tests",
19+
testonly = True,
20+
srcs = [
21+
"BUILD",
22+
"flags.bzl",
23+
"//bazel/flags/cc:BUILD",
24+
"//bazel/flags/java:BUILD",
25+
],
26+
)
27+
28+
bool_flag(
29+
name = "experimental_proto_descriptor_sets_include_source_info",
30+
build_setting_default = False,
31+
scope = "universal",
32+
)
33+
34+
label_flag(
35+
name = "proto_compiler",
36+
build_setting_default = "@bazel_tools//tools/proto:protoc",
37+
)
38+
39+
# TODO: deprecate this flag.
40+
string_flag(
41+
name = "strict_proto_deps",
42+
build_setting_default = "error",
43+
scope = "universal",
44+
values = [
45+
"off",
46+
"OFF",
47+
"warn",
48+
"WARN",
49+
"error",
50+
"ERROR",
51+
"strict",
52+
"STRICT",
53+
"default",
54+
"DEFAULT",
55+
],
56+
)
57+
58+
# TODO: deprecate this flag.
59+
string_flag(
60+
name = "strict_public_imports",
61+
build_setting_default = "off",
62+
scope = "universal",
63+
values = [
64+
"off",
65+
"OFF",
66+
"warn",
67+
"WARN",
68+
"error",
69+
"ERROR",
70+
"strict",
71+
"STRICT",
72+
"default",
73+
"DEFAULT",
74+
],
75+
)

bazel/flags/cc/BUILD

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")
2+
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
3+
4+
package(
5+
default_applicable_licenses = ["//:license"],
6+
default_visibility = ["//visibility:public"],
7+
)
8+
9+
exports_files(["BUILD"])
10+
11+
label_flag(
12+
name = "proto_toolchain_for_cc",
13+
build_setting_default = "@bazel_tools//tools/proto:cc_toolchain",
14+
)
15+
16+
string_list_flag(
17+
name = "protocopt",
18+
build_setting_default = [],
19+
scope = "universal",
20+
)
21+
22+
string_list_flag(
23+
name = "cc_proto_library_header_suffixes",
24+
build_setting_default = [".pb.h"],
25+
scope = "universal",
26+
)
27+
28+
string_list_flag(
29+
name = "cc_proto_library_source_suffixes",
30+
build_setting_default = [".pb.cc"],
31+
scope = "universal",
32+
)

google3/third_party/protobuf/bazel/flags/cc/empty.sh renamed to bazel/flags/cc/empty.sh

File renamed without changes.

bazel/flags/flags.bzl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""An indirection layer for referencing flags whether they are native or defined in Starlark."""
2+
3+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
4+
5+
visibility([
6+
"//third_party/grpc/bazel",
7+
"//bazel/private",
8+
"//bazel/flags",
9+
])
10+
11+
# Maps flag names to their native reference
12+
_FLAGS = {
13+
"protocopt": struct(
14+
native = lambda ctx: getattr(ctx.fragments.proto, "experimental_protoc_opts"),
15+
default = [],
16+
),
17+
"experimental_proto_descriptor_sets_include_source_info": struct(
18+
native = lambda ctx: getattr(ctx.attr, "_experimental_proto_descriptor_sets_include_source_info_native")[BuildSettingInfo].value,
19+
default = False,
20+
),
21+
"proto_compiler": struct(native = lambda ctx: getattr(ctx.attr, "_proto_compiler_native")[BuildSettingInfo].value, default = "@bazel_tools//tools/proto:protoc"),
22+
"strict_proto_deps": struct(
23+
native = lambda ctx: getattr(ctx.attr, "_strict_proto_deps_native")[BuildSettingInfo].value,
24+
default = "error",
25+
),
26+
"strict_public_imports": struct(
27+
native = lambda ctx: getattr(ctx.attr, "_strict_public_imports_native")[BuildSettingInfo].value,
28+
default = "off",
29+
),
30+
"cc_proto_library_header_suffixes": struct(
31+
native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_header_suffixes"),
32+
default = [".pb.h"],
33+
),
34+
"cc_proto_library_source_suffixes": struct(
35+
native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_source_suffixes"),
36+
default = [".pb.cc"],
37+
),
38+
"proto_toolchain_for_java": struct(
39+
native = lambda ctx: getattr(ctx.attr, "_aspect_java_proto_toolchain"),
40+
default = "@bazel_tools//tools/proto:java_toolchain",
41+
),
42+
"proto_toolchain_for_javalite": struct(
43+
native = lambda ctx: getattr(ctx.attr, "_aspect_proto_toolchain_for_javalite"),
44+
default = "@bazel_tools//tools/proto:javalite_toolchain",
45+
),
46+
"proto_toolchain_for_cc": struct(
47+
native = lambda ctx: getattr(ctx.attr, "_aspect_cc_proto_toolchain"),
48+
default = "@bazel_tools//tools/proto:cc_toolchain",
49+
),
50+
}
51+
52+
def get_flag_value(ctx, flag_name):
53+
"""Returns the value of the given flag in Starlark if it's set, otherwise reads the Java flag value.
54+
55+
Args:
56+
ctx: The rule context.
57+
flag_name: The name of the flag to get the value for.
58+
59+
Returns:
60+
The value of the flag.
61+
"""
62+
63+
# We probably got here from toolchains.find_toolchain. Leave the attribute alone.
64+
if flag_name not in _FLAGS:
65+
return getattr(ctx.attr, "_" + flag_name)
66+
67+
# Label flags don't have a BuildSettingInfo, just get the value.
68+
if "toolchain" in flag_name and getattr(ctx.attr, "_" + flag_name).label != _FLAGS[flag_name].default:
69+
return getattr(ctx.attr, "_" + flag_name)
70+
elif getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value != _FLAGS[flag_name].default:
71+
return getattr(ctx.attr, "_" + flag_name)[BuildSettingInfo].value
72+
else:
73+
return _FLAGS[flag_name].native(ctx)

bazel/flags/java/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package(
2+
default_applicable_licenses = ["//:license"],
3+
default_visibility = [
4+
"//bazel/private:__pkg__",
5+
"//devtools/blaze/exoblaze/mac/integration:__subpackages__",
6+
"//devtools/blaze/integration:__subpackages__",
7+
],
8+
)
9+
10+
exports_files(
11+
["BUILD"],
12+
)
13+
14+
label_flag(
15+
name = "proto_toolchain_for_java",
16+
build_setting_default = "@bazel_tools//tools/proto:java_toolchain",
17+
)
18+
19+
label_flag(
20+
name = "proto_toolchain_for_javalite",
21+
build_setting_default = "@bazel_tools//tools/proto:javalite_toolchain",
22+
)

bazel/private/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ bzl_library(
182182
deps = [
183183
":native_bzl",
184184
"//bazel/common:proto_lang_toolchain_info_bzl",
185+
"//bazel/flags:flags_bzl",
185186
],
186187
)
187188

bazel/private/bazel_cc_proto_library.bzl

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
1111
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
1212
load("//bazel/common:proto_common.bzl", "proto_common")
1313
load("//bazel/common:proto_info.bzl", "ProtoInfo")
14+
load("//bazel/flags:flags.bzl", "get_flag_value")
1415
load("//bazel/private:cc_proto_support.bzl", "cc_proto_compile_and_link")
1516
load("//bazel/private:toolchain_helpers.bzl", "toolchains")
1617

@@ -48,25 +49,24 @@ def _get_strip_include_prefix(ctx, proto_info):
4849

4950
def _aspect_impl(target, ctx):
5051
proto_info = target[ProtoInfo]
51-
proto_configuration = ctx.fragments.proto
5252

5353
sources = []
5454
headers = []
5555
textual_hdrs = []
5656

57-
proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", _CC_PROTO_TOOLCHAIN)
57+
proto_toolchain = toolchains.find_toolchain(ctx, "proto_toolchain_for_cc", _CC_PROTO_TOOLCHAIN)
5858
should_generate_code = proto_common.experimental_should_generate_code(proto_info, proto_toolchain, "cc_proto_library", target.label)
5959

6060
if should_generate_code:
6161
if len(proto_info.direct_sources) != 0:
6262
# Bazel 7 didn't expose cc_proto_library_source_suffixes used by Kythe
6363
# gradually falling back to .pb.cc
64-
if type(proto_configuration.cc_proto_library_source_suffixes) == "builtin_function_or_method":
64+
if type(get_flag_value(ctx, "cc_proto_library_source_suffixes")) == "builtin_function_or_method":
6565
source_suffixes = [".pb.cc"]
6666
header_suffixes = [".pb.h"]
6767
else:
68-
source_suffixes = proto_configuration.cc_proto_library_source_suffixes
69-
header_suffixes = proto_configuration.cc_proto_library_header_suffixes
68+
source_suffixes = get_flag_value(ctx, "cc_proto_library_source_suffixes")
69+
header_suffixes = get_flag_value(ctx, "cc_proto_library_header_suffixes")
7070
sources = _get_output_files(ctx.actions, proto_info, source_suffixes)
7171
headers = _get_output_files(ctx.actions, proto_info, header_suffixes)
7272
header_provider = _ProtoCcHeaderInfo(headers = depset(headers))
@@ -122,9 +122,22 @@ cc_proto_aspect = aspect(
122122
fragments = ["cpp", "proto"],
123123
required_providers = [ProtoInfo],
124124
provides = [CcInfo],
125-
attrs = toolchains.if_legacy_toolchain({"_aspect_cc_proto_toolchain": attr.label(
126-
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"),
127-
)}),
125+
attrs = {
126+
"_cc_proto_library_header_suffixes": attr.label(
127+
default = "//bazel/flags/cc:cc_proto_library_header_suffixes",
128+
),
129+
"_cc_proto_library_source_suffixes": attr.label(
130+
default = "//bazel/flags/cc:cc_proto_library_source_suffixes",
131+
),
132+
} |
133+
toolchains.if_legacy_toolchain({
134+
"_aspect_cc_proto_toolchain": attr.label(
135+
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"),
136+
),
137+
"_proto_toolchain_for_cc": attr.label(
138+
default = Label("//bazel/flags/cc:proto_toolchain_for_cc"),
139+
),
140+
}),
128141
toolchains = use_cc_toolchain() + toolchains.use_toolchain(_CC_PROTO_TOOLCHAIN),
129142
)
130143

@@ -140,7 +153,7 @@ def _cc_proto_library_impl(ctx):
140153
)
141154
dep = ctx.attr.deps[0]
142155

143-
proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_cc_proto_toolchain", _CC_PROTO_TOOLCHAIN)
156+
proto_toolchain = toolchains.find_toolchain(ctx, "proto_toolchain_for_cc", _CC_PROTO_TOOLCHAIN)
144157
proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain)
145158

146159
return [DefaultInfo(files = dep[_ProtoCcFilesInfo].files), dep[CcInfo], dep[OutputGroupInfo]]
@@ -192,6 +205,9 @@ rules to generate C++ code for.""",
192205
"_aspect_cc_proto_toolchain": attr.label(
193206
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_cc"),
194207
),
208+
"_proto_toolchain_for_cc": attr.label(
209+
default = "//bazel/flags/cc:proto_toolchain_for_cc",
210+
),
195211
}),
196212
provides = [CcInfo],
197213
toolchains = toolchains.use_toolchain(_CC_PROTO_TOOLCHAIN),

bazel/private/bazel_java_proto_library_rule.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _bazel_java_proto_aspect_impl(target, ctx):
3737
runtime jars.
3838
"""
3939
_proto_library = ctx.rule.attr
40-
proto_toolchain_info = toolchains.find_toolchain(ctx, "_aspect_java_proto_toolchain", _JAVA_PROTO_TOOLCHAIN)
40+
proto_toolchain_info = toolchains.find_toolchain(ctx, "proto_toolchain_for_java", _JAVA_PROTO_TOOLCHAIN)
4141
source_jar = None
4242
if proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain_info, "java_proto_library", target.label):
4343
# Generate source jar using proto compiler.
@@ -76,6 +76,9 @@ bazel_java_proto_aspect = aspect(
7676
"_aspect_java_proto_toolchain": attr.label(
7777
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java"),
7878
),
79+
"_proto_toolchain_for_java": attr.label(
80+
default = "//bazel/flags/java:proto_toolchain_for_java",
81+
),
7982
})
8083
),
8184
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"] + toolchains.use_toolchain(_JAVA_PROTO_TOOLCHAIN),
@@ -93,7 +96,7 @@ def bazel_java_proto_library_rule(ctx):
9396
Returns:
9497
([JavaInfo, DefaultInfo, OutputGroupInfo])
9598
"""
96-
proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_java_proto_toolchain", _JAVA_PROTO_TOOLCHAIN)
99+
proto_toolchain = toolchains.find_toolchain(ctx, "proto_toolchain_for_java", _JAVA_PROTO_TOOLCHAIN)
97100
for dep in ctx.attr.deps:
98101
proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain)
99102

@@ -160,6 +163,9 @@ rules to generate Java code for.
160163
"_aspect_java_proto_toolchain": attr.label(
161164
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java"),
162165
),
166+
"_proto_toolchain_for_java": attr.label(
167+
default = "//bazel/flags/java:proto_toolchain_for_java",
168+
),
163169
}), # buildifier: disable=attr-licenses (attribute called licenses)
164170
provides = [JavaInfo],
165171
toolchains = toolchains.use_toolchain(_JAVA_PROTO_TOOLCHAIN),

bazel/private/java_lite_proto_library.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _aspect_impl(target, ctx):
3535
exports = [exp[JavaInfo] for exp in ctx.rule.attr.exports]
3636
proto_toolchain_info = toolchains.find_toolchain(
3737
ctx,
38-
"_aspect_proto_toolchain_for_javalite",
38+
"proto_toolchain_for_javalite",
3939
_JAVA_LITE_PROTO_TOOLCHAIN,
4040
)
4141
source_jar = None
@@ -75,6 +75,9 @@ _java_lite_proto_aspect = aspect(
7575
_PROTO_TOOLCHAIN_ATTR: attr.label(
7676
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java_lite"),
7777
),
78+
"_proto_toolchain_for_javalite": attr.label(
79+
default = Label("//bazel/flags/java:proto_toolchain_for_javalite"),
80+
),
7881
}),
7982
fragments = ["java"],
8083
required_providers = [ProtoInfo],
@@ -97,7 +100,7 @@ def _rule_impl(ctx):
97100
"""
98101
proto_toolchain_info = toolchains.find_toolchain(
99102
ctx,
100-
"_aspect_proto_toolchain_for_javalite",
103+
"proto_toolchain_for_javalite",
101104
_JAVA_LITE_PROTO_TOOLCHAIN,
102105
)
103106
for dep in ctx.attr.deps:
@@ -171,6 +174,9 @@ rules to generate Java code for.
171174
_PROTO_TOOLCHAIN_ATTR: attr.label(
172175
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java_lite"),
173176
),
177+
"_proto_toolchain_for_javalite": attr.label(
178+
default = Label("//bazel/flags/java:proto_toolchain_for_javalite"),
179+
),
174180
}),
175181
provides = [JavaInfo],
176182
toolchains = toolchains.use_toolchain(_JAVA_LITE_PROTO_TOOLCHAIN),

bazel/private/proto_lang_toolchain_rule.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ load("@proto_bazel_features//:features.bzl", "bazel_features")
1111
load("//bazel/common:proto_common.bzl", "proto_common")
1212
load("//bazel/common:proto_info.bzl", "ProtoInfo")
1313
load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
14+
load("//bazel/flags:flags.bzl", "get_flag_value")
1415
load("//bazel/private:toolchain_helpers.bzl", "toolchains")
1516

1617
def _rule_impl(ctx):
@@ -33,7 +34,7 @@ def _rule_impl(ctx):
3334
protoc_opts = ctx.toolchains[toolchains.PROTO_TOOLCHAIN].proto.protoc_opts
3435
else:
3536
proto_compiler = ctx.attr._proto_compiler.files_to_run
36-
protoc_opts = ctx.fragments.proto.experimental_protoc_opts
37+
protoc_opts = get_flag_value(ctx, "protocopt")
3738

3839
if ctx.attr.protoc_minimal_do_not_use:
3940
proto_compiler = ctx.attr.protoc_minimal_do_not_use.files_to_run
@@ -148,6 +149,9 @@ Deprecated. Alias for <code>denylisted_protos</code>. Will be removed in a futur
148149
cfg = "exec",
149150
executable = True,
150151
),
152+
"_protocopt": attr.label(
153+
default = "//bazel/flags/cc:protocopt",
154+
),
151155
} | ({} if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
152156
"_proto_compiler": attr.label(
153157
cfg = "exec",

0 commit comments

Comments
 (0)