Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions bazel/flags/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag", "string_list_flag")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//bazel/private:compat_flag.bzl", "compat_bool_flag", "compat_string_list_flag")

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -25,9 +26,10 @@ filegroup(
],
)

bool_flag(
compat_bool_flag(
name = "experimental_proto_descriptor_sets_include_source_info",
build_setting_default = False,
native_flag = "experimental_proto_descriptor_sets_include_source_info",
scope = "universal",
)

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

string_list_flag(
compat_string_list_flag(
name = "protocopt",
build_setting_default = [],
fragment_field = "experimental_protoc_opts",
scope = "universal",
)

Expand Down Expand Up @@ -75,39 +78,45 @@ config_setting(
)

# TODO: deprecate this flag.
string_flag(
compat_bool_flag(
name = "strict_proto_deps",
build_setting_default = "error",
build_setting_default = True,
native_flag = "strict_proto_deps",
scope = "universal",
values = [
"off",
"OFF",
"warn",
"WARN",
"error",
"ERROR",
"strict",
"STRICT",
"default",
"DEFAULT",
],
values = {
False: [
"off",
"OFF",
],
True: [
"warn",
"WARN",
"error",
"ERROR",
"strict",
"STRICT",
],
},
)

# TODO: deprecate this flag.
string_flag(
compat_bool_flag(
name = "strict_public_imports",
build_setting_default = "off",
build_setting_default = False,
native_flag = "strict_public_imports",
scope = "universal",
values = [
"off",
"OFF",
"warn",
"WARN",
"error",
"ERROR",
"strict",
"STRICT",
"default",
"DEFAULT",
],
values = {
False: [
"off",
"OFF",
],
True: [
"warn",
"WARN",
"error",
"ERROR",
"strict",
"STRICT",
],
},
)
9 changes: 6 additions & 3 deletions bazel/flags/cc/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//bazel/private:compat_flag.bzl", "compat_string_list_flag")

package(
default_applicable_licenses = ["//:license"],
Expand All @@ -19,14 +20,16 @@ alias(
deprecation = "Use //bazel/flags:protocopt instead.",
)

string_list_flag(
compat_string_list_flag(
name = "cc_proto_library_header_suffixes",
build_setting_default = [".pb.h"],
fragment_field = "cc_proto_library_header_suffixes",
scope = "universal",
)

string_list_flag(
compat_string_list_flag(
name = "cc_proto_library_source_suffixes",
build_setting_default = [".pb.cc"],
fragment_field = "cc_proto_library_source_suffixes",
scope = "universal",
)
75 changes: 11 additions & 64 deletions bazel/flags/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,22 @@ visibility([
"//third_party/grpc/bazel",
])

# Maps flag names to their native reference
_FLAGS = {
"protocopt": struct(
native = lambda ctx: getattr(ctx.fragments.proto, "experimental_protoc_opts"),
default = [],
),
"experimental_proto_descriptor_sets_include_source_info": struct(
native = lambda ctx: getattr(ctx.attr, "_experimental_proto_descriptor_sets_include_source_info_native")[BuildSettingInfo].value,
default = False,
),
"proto_compiler": struct(native = lambda ctx: getattr(ctx.attr, "_proto_compiler_native")[BuildSettingInfo].value, default = "@bazel_tools//tools/proto:protoc"),
"strict_proto_deps": struct(
native = lambda ctx: getattr(ctx.attr, "_strict_proto_deps_native")[BuildSettingInfo].value,
default = "error",
),
"strict_public_imports": struct(
native = lambda ctx: getattr(ctx.attr, "_strict_public_imports_native")[BuildSettingInfo].value,
default = "off",
),
"cc_proto_library_header_suffixes": struct(
native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_header_suffixes"),
default = [".pb.h"],
),
"cc_proto_library_source_suffixes": struct(
native = lambda ctx: getattr(ctx.fragments.proto, "cc_proto_library_source_suffixes"),
default = [".pb.cc"],
),
"proto_toolchain_for_java": struct(
native = lambda ctx: "//:java_toolchain",
default = "//:java_toolchain",
),
"proto_toolchain_for_javalite": struct(
native = lambda ctx: "//:javalite_toolchain",
default = "//:javalite_toolchain",
),
"proto_toolchain_for_cc": struct(
native = lambda ctx: "//:cc_toolchain",
default = "//:cc_toolchain",
),
}

def get_flag_value(ctx, flag_name):
"""Returns the value of the given flag in Starlark if it's set, otherwise reads the Java flag value, if the proto fragment exists.
"""Returns the value of the given flag attribute from rule context.

Args:
ctx: The rule context.
flag_name: The name of the flag to get the value for.

Returns:
The value of the flag.
The value of the flag. If the value is a list, returns a mutable copy.
"""

# We probably got here from toolchains.find_toolchain. Leave the attribute alone.
if flag_name not in _FLAGS:
return getattr(ctx.attr, "_" + flag_name)

starlark_flag = getattr(ctx.attr, "_" + flag_name)

# Label flags don't have a BuildSettingInfo, just get the value.
if "toolchain" in flag_name:
starlark_flag_is_set = starlark_flag.label != _FLAGS[flag_name].default
starlark_value = starlark_flag
else:
starlark_flag_is_set = starlark_flag[BuildSettingInfo].value != _FLAGS[flag_name].default
starlark_value = starlark_flag[BuildSettingInfo].value

# Starlark flags take precedence over native flags.
# Also of course, use the Starlark value if the proto fragment no longer exists.
if starlark_flag_is_set or not hasattr(ctx.fragments, "proto"):
return starlark_value
else:
return _FLAGS[flag_name].native(ctx)
attr_val = getattr(ctx.attr, "_" + flag_name)
if BuildSettingInfo in attr_val:
val = attr_val[BuildSettingInfo].value
if type(val) == "list":
return list(val)
return val
if type(attr_val) == "list":
return list(attr_val)
return attr_val
30 changes: 3 additions & 27 deletions bazel/private/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":native_bool_flag.bzl", "native_bool_flag")

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

Expand Down Expand Up @@ -168,32 +167,9 @@ bzl_library(
],
)

native_bool_flag(
name = "experimental_proto_descriptor_sets_include_source_info",
flag = "experimental_proto_descriptor_sets_include_source_info",
match_value = "true",
visibility = ["//bazel:__subpackages__"],
)

native_bool_flag(
name = "strict_proto_deps",
flag = "strict_proto_deps",
match_value = "off",
result = False,
visibility = ["//bazel:__subpackages__"],
)

native_bool_flag(
name = "strict_public_imports",
flag = "strict_public_imports",
match_value = "off",
result = False,
visibility = ["//bazel:__subpackages__"],
)

bzl_library(
name = "native_bool_flag_bzl",
srcs = ["native_bool_flag.bzl"],
name = "compat_flag_bzl",
srcs = ["compat_flag.bzl"],
visibility = ["//visibility:private"],
deps = ["@bazel_skylib//rules:common_settings"],
)
Expand All @@ -203,8 +179,8 @@ filegroup(
testonly = True,
srcs = [
"BUILD",
":compat_flag_bzl",
":java_proto_library_bzl",
":native_bool_flag_bzl",
":toolchain_helpers_bzl",
"//bazel:for_bazel_tests",
"//bazel/private/oss/toolchains:for_bazel_tests",
Expand Down
Loading
Loading