Skip to content

Commit 753dd82

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 e96bfb2 commit 753dd82

11 files changed

Lines changed: 262 additions & 203 deletions

bazel/flags/BUILD

Lines changed: 47 additions & 37 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,21 +26,26 @@ filegroup(
2526
],
2627
)
2728

28-
bool_flag(
29+
compat_bool_flag(
2930
name = "experimental_proto_descriptor_sets_include_source_info",
30-
build_setting_default = False,
31-
scope = "universal",
31+
default = False,
32+
native_flag = "experimental_proto_descriptor_sets_include_source_info",
33+
type = "bool",
34+
values = {True: [
35+
"true",
36+
"TRUE",
37+
"1",
38+
]},
3239
)
3340

3441
label_flag(
3542
name = "proto_compiler",
3643
build_setting_default = "@bazel_tools//tools/proto:protoc",
3744
)
3845

39-
string_list_flag(
46+
compat_string_list_flag(
4047
name = "protocopt",
41-
build_setting_default = [],
42-
scope = "universal",
48+
fragment_field = "experimental_protoc_opts",
4349
)
4450

4551
# When set to true, we will use a prebuilt protoc binary from GitHub if it's available.
@@ -75,39 +81,43 @@ config_setting(
7581
)
7682

7783
# TODO: deprecate this flag.
78-
string_flag(
84+
compat_bool_flag(
7985
name = "strict_proto_deps",
80-
build_setting_default = "error",
81-
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+
default = True,
87+
native_flag = "strict_proto_deps",
88+
values = {
89+
False: [
90+
"off",
91+
"OFF",
92+
],
93+
True: [
94+
"warn",
95+
"WARN",
96+
"error",
97+
"ERROR",
98+
"strict",
99+
"STRICT",
100+
],
101+
},
94102
)
95103

96104
# TODO: deprecate this flag.
97-
string_flag(
105+
compat_bool_flag(
98106
name = "strict_public_imports",
99-
build_setting_default = "off",
100-
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-
],
107+
default = False,
108+
native_flag = "strict_public_imports",
109+
values = {
110+
False: [
111+
"off",
112+
"OFF",
113+
],
114+
True: [
115+
"warn",
116+
"WARN",
117+
"error",
118+
"ERROR",
119+
"strict",
120+
"STRICT",
121+
],
122+
},
113123
)

bazel/flags/cc/BUILD

Lines changed: 8 additions & 7 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,14 @@ 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",
24-
build_setting_default = [".pb.h"],
25-
scope = "universal",
25+
default = [".pb.h"],
26+
fragment_field = "cc_proto_library_header_suffixes",
2627
)
2728

28-
string_list_flag(
29+
compat_string_list_flag(
2930
name = "cc_proto_library_source_suffixes",
30-
build_setting_default = [".pb.cc"],
31-
scope = "universal",
31+
default = [".pb.cc"],
32+
fragment_field = "cc_proto_library_source_suffixes",
3233
)

bazel/flags/flags.bzl

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,8 @@ 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.
@@ -59,24 +18,7 @@ def get_flag_value(ctx, flag_name):
5918
Returns:
6019
The value of the flag.
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+
return attr_val[BuildSettingInfo].value
24+
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)