Skip to content

Commit 7e6ef66

Browse files
habermancopybara-github
authored andcommitted
Refactor protobuf flag abstraction layer to custom flag targets
Instead of having rules depend on both native and Starlark versions of flags and reconciling them inside the rule implementation, this change moves the resolution logic into custom flag targets (proto_bool_flag and proto_string_list_flag). PiperOrigin-RevId: 952990495
1 parent 732618d commit 7e6ef66

12 files changed

Lines changed: 279 additions & 199 deletions

bazel/flags/BUILD

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag")
2+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
3+
load("//bazel/private:compat_flag.bzl", "compat_bool_flag", "compat_string_list_flag")
34

45
package(
56
default_applicable_licenses = ["//:license"],
@@ -25,9 +26,10 @@ filegroup(
2526
],
2627
)
2728

28-
bool_flag(
29+
compat_bool_flag(
2930
name = "experimental_proto_descriptor_sets_include_source_info",
3031
build_setting_default = False,
32+
native_flag = "experimental_proto_descriptor_sets_include_source_info",
3133
scope = "universal",
3234
)
3335

@@ -36,9 +38,10 @@ label_flag(
3638
build_setting_default = "@bazel_tools//tools/proto:protoc",
3739
)
3840

39-
string_list_flag(
41+
compat_string_list_flag(
4042
name = "protocopt",
4143
build_setting_default = [],
44+
fragment_field = "experimental_protoc_opts",
4245
scope = "universal",
4346
)
4447

@@ -75,39 +78,45 @@ config_setting(
7578
)
7679

7780
# TODO: deprecate this flag.
78-
string_flag(
81+
compat_bool_flag(
7982
name = "strict_proto_deps",
80-
build_setting_default = "error",
83+
build_setting_default = True,
84+
native_flag = "strict_proto_deps",
8185
scope = "universal",
82-
values = [
83-
"off",
84-
"OFF",
85-
"warn",
86-
"WARN",
87-
"error",
88-
"ERROR",
89-
"strict",
90-
"STRICT",
91-
"default",
92-
"DEFAULT",
93-
],
86+
values = {
87+
False: [
88+
"off",
89+
"OFF",
90+
],
91+
True: [
92+
"warn",
93+
"WARN",
94+
"error",
95+
"ERROR",
96+
"strict",
97+
"STRICT",
98+
],
99+
},
94100
)
95101

96102
# TODO: deprecate this flag.
97-
string_flag(
103+
compat_bool_flag(
98104
name = "strict_public_imports",
99-
build_setting_default = "off",
105+
build_setting_default = False,
106+
native_flag = "strict_public_imports",
100107
scope = "universal",
101-
values = [
102-
"off",
103-
"OFF",
104-
"warn",
105-
"WARN",
106-
"error",
107-
"ERROR",
108-
"strict",
109-
"STRICT",
110-
"default",
111-
"DEFAULT",
112-
],
108+
values = {
109+
False: [
110+
"off",
111+
"OFF",
112+
],
113+
True: [
114+
"warn",
115+
"WARN",
116+
"error",
117+
"ERROR",
118+
"strict",
119+
"STRICT",
120+
],
121+
},
113122
)

bazel/flags/cc/BUILD

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
22
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
3+
load("//bazel/private:compat_flag.bzl", "compat_string_list_flag")
34

45
package(
56
default_applicable_licenses = ["//:license"],
@@ -19,14 +20,16 @@ alias(
1920
deprecation = "Use //bazel/flags:protocopt instead.",
2021
)
2122

22-
string_list_flag(
23+
compat_string_list_flag(
2324
name = "cc_proto_library_header_suffixes",
2425
build_setting_default = [".pb.h"],
26+
fragment_field = "cc_proto_library_header_suffixes",
2527
scope = "universal",
2628
)
2729

28-
string_list_flag(
30+
compat_string_list_flag(
2931
name = "cc_proto_library_source_suffixes",
3032
build_setting_default = [".pb.cc"],
33+
fragment_field = "cc_proto_library_source_suffixes",
3134
scope = "universal",
3235
)

bazel/flags/flags.bzl

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,22 @@ visibility([
88
"//third_party/grpc/bazel",
99
])
1010

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: "//:java_toolchain",
40-
default = "//:java_toolchain",
41-
),
42-
"proto_toolchain_for_javalite": struct(
43-
native = lambda ctx: "//:javalite_toolchain",
44-
default = "//:javalite_toolchain",
45-
),
46-
"proto_toolchain_for_cc": struct(
47-
native = lambda ctx: "//:cc_toolchain",
48-
default = "//:cc_toolchain",
49-
),
50-
}
51-
5211
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, if the proto fragment exists.
12+
"""Returns the value of the given flag attribute from rule context.
5413
5514
Args:
5615
ctx: The rule context.
5716
flag_name: The name of the flag to get the value for.
5817
5918
Returns:
60-
The value of the flag.
19+
The value of the flag. If the value is a list, returns a mutable copy.
6120
"""
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-
starlark_flag = getattr(ctx.attr, "_" + flag_name)
68-
69-
# Label flags don't have a BuildSettingInfo, just get the value.
70-
if "toolchain" in flag_name:
71-
starlark_flag_is_set = starlark_flag.label != _FLAGS[flag_name].default
72-
starlark_value = starlark_flag
73-
else:
74-
starlark_flag_is_set = starlark_flag[BuildSettingInfo].value != _FLAGS[flag_name].default
75-
starlark_value = starlark_flag[BuildSettingInfo].value
76-
77-
# Starlark flags take precedence over native flags.
78-
# Also of course, use the Starlark value if the proto fragment no longer exists.
79-
if starlark_flag_is_set or not hasattr(ctx.fragments, "proto"):
80-
return starlark_value
81-
else:
82-
return _FLAGS[flag_name].native(ctx)
21+
attr_val = getattr(ctx.attr, "_" + flag_name)
22+
if BuildSettingInfo in attr_val:
23+
val = attr_val[BuildSettingInfo].value
24+
if type(val) == "list":
25+
return list(val)
26+
return val
27+
if type(attr_val) == "list":
28+
return list(attr_val)
29+
return attr_val

bazel/private/BUILD

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load(":native_bool_flag.bzl", "native_bool_flag")
32

43
package(default_applicable_licenses = ["//:license"])
54

@@ -168,32 +167,9 @@ bzl_library(
168167
],
169168
)
170169

171-
native_bool_flag(
172-
name = "experimental_proto_descriptor_sets_include_source_info",
173-
flag = "experimental_proto_descriptor_sets_include_source_info",
174-
match_value = "true",
175-
visibility = ["//bazel:__subpackages__"],
176-
)
177-
178-
native_bool_flag(
179-
name = "strict_proto_deps",
180-
flag = "strict_proto_deps",
181-
match_value = "off",
182-
result = False,
183-
visibility = ["//bazel:__subpackages__"],
184-
)
185-
186-
native_bool_flag(
187-
name = "strict_public_imports",
188-
flag = "strict_public_imports",
189-
match_value = "off",
190-
result = False,
191-
visibility = ["//bazel:__subpackages__"],
192-
)
193-
194170
bzl_library(
195-
name = "native_bool_flag_bzl",
196-
srcs = ["native_bool_flag.bzl"],
171+
name = "compat_flag_bzl",
172+
srcs = ["compat_flag.bzl"],
197173
visibility = ["//visibility:private"],
198174
deps = ["@bazel_skylib//rules:common_settings"],
199175
)
@@ -203,8 +179,8 @@ filegroup(
203179
testonly = True,
204180
srcs = [
205181
"BUILD",
182+
":compat_flag_bzl",
206183
":java_proto_library_bzl",
207-
":native_bool_flag_bzl",
208184
":toolchain_helpers_bzl",
209185
"//bazel:for_bazel_tests",
210186
"//bazel/private/oss/toolchains:for_bazel_tests",

0 commit comments

Comments
 (0)