mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
Prepare Protobuf for a future version of Bazel which will lack the proto fragment
The ultimate effect of this change will be that both the native flags and Starlark flags will continue to work with future versions of Bazel that lack the proto fragment, until such time as we deprecate or remove the flags in a Protobuf release. This decouples Bazel releases from Protobuf behavior changes. 1. Builds on the Bazel 8-compatible abstraction layer we submitted in cl/955468205 by setting `HAS_NATIVE_PROTO_FLAGS` properly in Bazel. We do this by adding a `rules.has_proto_fragment` setting to the `proto_bazel_features.bzl` mechanism: we know that the fragment no longer needs to be read in Bazel 9+, because we can use `flag_alias()` instead. 2. Adds `flag_alias()` declarations in MODULE.bazel for all compat layer flags. This will work in Bazel 9, but is ignored in Bazel 8. This leads to a slight inconsistency. Consider the following set of flags set by a user: ``` --@protobuf//bazel/flags:protocopt=foo --protocopt=bar ``` In Bazel 9+, where the flag alias is respected, the final value of the flag will be `bar`. In Bazel 8, where we emulate `flag_alias()` by reconciling the two different values at analysis time, we always prefer the Starlark version (and cannot tell the relative order), so the final value of the flag will be `foo`. To avoid this, prefer setting the Starlark version of the flag whenever possible. PiperOrigin-RevId: 970834999
This commit is contained in:
parent
ea0f2968ed
commit
f1c23cb216
14 changed files with 466 additions and 21 deletions
60
MODULE.bazel
60
MODULE.bazel
|
|
@ -25,7 +25,10 @@ bazel_dep(name = "rules_cc", version = "0.2.18")
|
|||
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")
|
||||
|
||||
# other dependencies
|
||||
bazel_dep(name = "bazel_features", version = "1.33.0", repo_name = "proto_bazel_features")
|
||||
proto_bazel_features = use_repo_rule("//bazel/private/oss:proto_bazel_features.bzl", "proto_bazel_features")
|
||||
|
||||
proto_bazel_features(name = "proto_bazel_features")
|
||||
|
||||
bazel_dep(name = "bazel_skylib", version = "1.9.0")
|
||||
bazel_dep(name = "jsoncpp", version = "1.9.6.bcr.2")
|
||||
bazel_dep(name = "rules_java", version = "8.6.1")
|
||||
|
|
@ -343,3 +346,58 @@ bazel_dep(name = "jq.bzl", version = "0.6.1", dev_dependency = True)
|
|||
|
||||
jq = use_extension("@jq.bzl//jq:extensions.bzl", "toolchains", dev_dependency = True)
|
||||
use_repo(jq, "jq_toolchains")
|
||||
|
||||
flag_alias(
|
||||
name = "experimental_proto_descriptor_sets_include_source_info",
|
||||
starlark_flag = "@//bazel/flags:experimental_proto_descriptor_sets_include_source_info",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "experimental_protoc_opts",
|
||||
starlark_flag = "@//bazel/flags:protocopt",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "protocopt",
|
||||
starlark_flag = "@//bazel/flags:protocopt",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "proto_compiler",
|
||||
starlark_flag = "@//bazel/flags:proto_compiler",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "proto_toolchain_for_cc",
|
||||
starlark_flag = "@//bazel/flags/cc:proto_toolchain_for_cc",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "proto_toolchain_for_java",
|
||||
starlark_flag = "@//bazel/flags/java:proto_toolchain_for_java",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "proto_toolchain_for_javalite",
|
||||
starlark_flag = "@//bazel/flags/java:proto_toolchain_for_javalite",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "strict_proto_deps",
|
||||
starlark_flag = "@//bazel/flags:strict_proto_deps",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "strict_public_imports",
|
||||
starlark_flag = "@//bazel/flags:strict_public_imports",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "cc_proto_library_header_suffixes",
|
||||
starlark_flag = "@//bazel/flags/cc:cc_proto_library_header_suffixes",
|
||||
)
|
||||
|
||||
flag_alias(
|
||||
name = "cc_proto_library_source_suffixes",
|
||||
starlark_flag = "@//bazel/flags/cc:cc_proto_library_source_suffixes",
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
|
||||
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
|
||||
load("//bazel/private:compat_flag.bzl", "compat_bool_flag", "compat_string_list_flag")
|
||||
load("//bazel/private:compat_flag.bzl", "compat_bool_flag", "compat_label_flag", "compat_string_list_flag")
|
||||
|
||||
package(
|
||||
default_applicable_licenses = ["//:license"],
|
||||
|
|
@ -33,9 +33,11 @@ compat_bool_flag(
|
|||
scope = "universal",
|
||||
)
|
||||
|
||||
label_flag(
|
||||
compat_label_flag(
|
||||
name = "proto_compiler",
|
||||
build_setting_default = "@bazel_tools//tools/proto:protoc",
|
||||
executable = True,
|
||||
fragment_field = "proto_compiler",
|
||||
)
|
||||
|
||||
compat_string_list_flag(
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
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")
|
||||
load("//bazel/private:compat_flag.bzl", "compat_label_flag", "compat_string_list_flag")
|
||||
|
||||
package(
|
||||
default_applicable_licenses = ["//:license"],
|
||||
|
|
@ -9,9 +9,10 @@ package(
|
|||
|
||||
exports_files(["BUILD"])
|
||||
|
||||
label_flag(
|
||||
compat_label_flag(
|
||||
name = "proto_toolchain_for_cc",
|
||||
build_setting_default = "@bazel_tools//tools/proto:cc_toolchain",
|
||||
fragment_field = "proto_toolchain_for_cc",
|
||||
)
|
||||
|
||||
alias(
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
load("//bazel/private:compat_flag.bzl", "compat_label_flag")
|
||||
|
||||
package(
|
||||
default_applicable_licenses = ["//:license"],
|
||||
default_visibility = [
|
||||
"//bazel/private:__pkg__",
|
||||
"//bazel/tests:__subpackages__",
|
||||
"//devtools/blaze/exoblaze/mac/integration:__subpackages__",
|
||||
"//devtools/blaze/integration:__subpackages__",
|
||||
],
|
||||
|
|
@ -11,12 +14,14 @@ exports_files(
|
|||
["BUILD"],
|
||||
)
|
||||
|
||||
label_flag(
|
||||
compat_label_flag(
|
||||
name = "proto_toolchain_for_java",
|
||||
build_setting_default = "@bazel_tools//tools/proto:java_toolchain",
|
||||
fragment_field = "proto_toolchain_for_java",
|
||||
)
|
||||
|
||||
label_flag(
|
||||
compat_label_flag(
|
||||
name = "proto_toolchain_for_javalite",
|
||||
build_setting_default = "@bazel_tools//tools/proto:javalite_toolchain",
|
||||
fragment_field = "proto_toolchain_for_javalite",
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@
|
|||
"""Helper rules and macros for custom proto flags."""
|
||||
|
||||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
|
||||
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
|
||||
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
|
||||
load("@rules_java//java/common:proguard_spec_info.bzl", "ProguardSpecInfo")
|
||||
load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
|
||||
load("//bazel/private:native.bzl", "HAS_NATIVE_PROTO_FLAGS")
|
||||
|
||||
def _compat_bool_rule_impl(ctx):
|
||||
|
|
@ -91,12 +95,21 @@ def compat_bool_flag(
|
|||
|
||||
def _compat_string_list_rule_impl(ctx):
|
||||
starlark_val = ctx.build_setting_value
|
||||
|
||||
# 1. Starlark flag takes precedence if explicitly passed (not empty)
|
||||
if starlark_val:
|
||||
return [BuildSettingInfo(value = starlark_val)]
|
||||
|
||||
# 2. Native flag fallback (if HAS_NATIVE_PROTO_FLAGS and present on ctx.fragments.proto)
|
||||
if HAS_NATIVE_PROTO_FLAGS and hasattr(ctx.fragments, "proto") and ctx.attr.fragment_field:
|
||||
if hasattr(ctx.fragments.proto, ctx.attr.fragment_field):
|
||||
val = getattr(ctx.fragments.proto, ctx.attr.fragment_field)
|
||||
return [BuildSettingInfo(value = val)]
|
||||
if type(val) == "list":
|
||||
return [BuildSettingInfo(value = val)]
|
||||
else:
|
||||
return [BuildSettingInfo(value = val())]
|
||||
|
||||
# 3. Default fallback
|
||||
return [BuildSettingInfo(value = ctx.attr.default_value)]
|
||||
|
||||
_compat_string_list_rule = rule(
|
||||
|
|
@ -133,3 +146,147 @@ def compat_string_list_flag(
|
|||
default_value = default_vals,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def _forward_providers(ctx, label_val, target = None, extra_providers = []):
|
||||
if target == None:
|
||||
target = ctx.attr.default_value
|
||||
providers = [BuildSettingInfo(value = label_val)] + extra_providers
|
||||
if ProtoLangToolchainInfo in target:
|
||||
providers.append(target[ProtoLangToolchainInfo])
|
||||
if target[ProtoLangToolchainInfo].runtime and JavaInfo in target[ProtoLangToolchainInfo].runtime:
|
||||
providers.append(target[ProtoLangToolchainInfo].runtime[JavaInfo])
|
||||
if target[ProtoLangToolchainInfo].runtime and ProguardSpecInfo in target[ProtoLangToolchainInfo].runtime:
|
||||
providers.append(target[ProtoLangToolchainInfo].runtime[ProguardSpecInfo])
|
||||
|
||||
if JavaInfo in target:
|
||||
providers.append(target[JavaInfo])
|
||||
|
||||
if ProguardSpecInfo in target:
|
||||
providers.append(target[ProguardSpecInfo])
|
||||
|
||||
if CcInfo in target:
|
||||
providers.append(target[CcInfo])
|
||||
|
||||
return providers
|
||||
|
||||
def _compat_label_rule_impl(ctx):
|
||||
target = ctx.attr.default_value
|
||||
label_val = target.label
|
||||
|
||||
starlark_val = ctx.build_setting_value
|
||||
|
||||
# 1. Starlark flag takes precedence if explicitly passed (not "default")
|
||||
if starlark_val != "default":
|
||||
label_val = Label(starlark_val)
|
||||
|
||||
# 2. Native flag fallback (if HAS_NATIVE_PROTO_FLAGS and _native_target present on ctx.attr)
|
||||
elif HAS_NATIVE_PROTO_FLAGS and hasattr(ctx.attr, "_native_target") and ctx.attr._native_target:
|
||||
native_target = ctx.attr._native_target
|
||||
if native_target.label != target.label:
|
||||
target = native_target
|
||||
label_val = native_target.label
|
||||
|
||||
return _forward_providers(ctx, label_val, target = target)
|
||||
|
||||
def _compat_executable_label_rule_impl(ctx):
|
||||
target = ctx.attr.default_value
|
||||
label_val = target.label
|
||||
|
||||
starlark_val = ctx.build_setting_value
|
||||
if starlark_val != "default":
|
||||
label_val = Label(starlark_val)
|
||||
elif HAS_NATIVE_PROTO_FLAGS and hasattr(ctx.attr, "_native_target") and ctx.attr._native_target:
|
||||
native_target = ctx.attr._native_target
|
||||
if native_target.label != target.label:
|
||||
target = native_target
|
||||
label_val = native_target.label
|
||||
|
||||
extra_providers = []
|
||||
if DefaultInfo in target:
|
||||
def_info = target[DefaultInfo]
|
||||
if def_info.files_to_run and def_info.files_to_run.executable:
|
||||
orig_exec = def_info.files_to_run.executable
|
||||
symlink = ctx.actions.declare_file(orig_exec.basename)
|
||||
ctx.actions.symlink(
|
||||
output = symlink,
|
||||
target_file = orig_exec,
|
||||
is_executable = True,
|
||||
)
|
||||
extra_providers.append(DefaultInfo(
|
||||
files = depset([symlink]),
|
||||
runfiles = def_info.default_runfiles,
|
||||
executable = symlink,
|
||||
))
|
||||
|
||||
return _forward_providers(ctx, label_val, target = target, extra_providers = extra_providers)
|
||||
|
||||
_COMMON_LABEL_FLAG_ATTRS = {
|
||||
"fragment_field": attr.string(),
|
||||
"default_value": attr.label(),
|
||||
"runtime": attr.label(),
|
||||
"scope": attr.string(),
|
||||
}
|
||||
|
||||
def _make_label_rule(fragment_name, executable = False):
|
||||
attrs = dict(_COMMON_LABEL_FLAG_ATTRS)
|
||||
if HAS_NATIVE_PROTO_FLAGS and fragment_name:
|
||||
attrs["_native_target"] = attr.label(
|
||||
default = configuration_field(fragment = "proto", name = fragment_name),
|
||||
)
|
||||
return rule(
|
||||
implementation = _compat_executable_label_rule_impl if executable else _compat_label_rule_impl,
|
||||
build_setting = config.string(flag = True),
|
||||
executable = executable,
|
||||
fragments = ["proto"] if HAS_NATIVE_PROTO_FLAGS and fragment_name else [],
|
||||
attrs = attrs,
|
||||
)
|
||||
|
||||
_compat_label_rule_compiler = _make_label_rule("proto_compiler", executable = False)
|
||||
_compat_executable_label_rule_compiler = _make_label_rule("proto_compiler", executable = True)
|
||||
_compat_label_rule_cc = _make_label_rule("proto_toolchain_for_cc", executable = False)
|
||||
_compat_label_rule_java = _make_label_rule("proto_toolchain_for_java", executable = False)
|
||||
_compat_label_rule_javalite = _make_label_rule("proto_toolchain_for_java_lite", executable = False)
|
||||
_compat_label_rule_default = _make_label_rule(None, executable = False)
|
||||
_compat_executable_label_rule_default = _make_label_rule(None, executable = True)
|
||||
|
||||
_LABEL_RULES = {
|
||||
("proto_compiler", False): _compat_label_rule_compiler,
|
||||
("proto_compiler", True): _compat_executable_label_rule_compiler,
|
||||
("proto_toolchain_for_cc", False): _compat_label_rule_cc,
|
||||
("proto_toolchain_for_java", False): _compat_label_rule_java,
|
||||
("proto_toolchain_for_javalite", False): _compat_label_rule_javalite,
|
||||
(None, False): _compat_label_rule_default,
|
||||
(None, True): _compat_executable_label_rule_default,
|
||||
}
|
||||
|
||||
def compat_label_flag(
|
||||
*,
|
||||
name,
|
||||
fragment_field = None,
|
||||
build_setting_default = None,
|
||||
executable = False,
|
||||
**kwargs):
|
||||
"""Creates a custom label build setting reconciling Starlark/fragments.
|
||||
|
||||
Args:
|
||||
name: The target name for the Starlark build setting flag.
|
||||
fragment_field: The field name in ctx.fragments.proto, if any.
|
||||
build_setting_default: Fallback default label target.
|
||||
executable: Whether the label setting points to an executable target.
|
||||
**kwargs: Additional rule arguments (such as `scope`).
|
||||
"""
|
||||
if fragment_field != None and (fragment_field, executable) not in _LABEL_RULES:
|
||||
fail("Unsupported fragment_field '%s' for compat_label_flag. Supported values are: %s" % (
|
||||
fragment_field,
|
||||
sorted([k[0] for k in _LABEL_RULES.keys() if k[0] != None]),
|
||||
))
|
||||
|
||||
rule_func = _LABEL_RULES.get((fragment_field, executable), _LABEL_RULES[(None, executable)])
|
||||
rule_func(
|
||||
name = name,
|
||||
build_setting_default = "default",
|
||||
fragment_field = fragment_field or "",
|
||||
default_value = build_setting_default,
|
||||
runtime = build_setting_default,
|
||||
**kwargs
|
||||
)
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ java_proto_aspect = aspect(
|
|||
attrs = (
|
||||
toolchains.if_legacy_toolchain({
|
||||
"_aspect_java_proto_toolchain": attr.label(
|
||||
default = "//bazel/flags/java:proto_toolchain_for_java",
|
||||
default = Label("//bazel/flags/java:proto_toolchain_for_java"),
|
||||
),
|
||||
})
|
||||
),
|
||||
|
|
@ -161,7 +161,7 @@ rules to generate Java code for.
|
|||
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
|
||||
} | toolchains.if_legacy_toolchain({
|
||||
"_aspect_java_proto_toolchain": attr.label(
|
||||
default = "//bazel/flags/java:proto_toolchain_for_java",
|
||||
default = Label("//bazel/flags/java:proto_toolchain_for_java"),
|
||||
),
|
||||
}), # buildifier: disable=attr-licenses (attribute called licenses)
|
||||
provides = [JavaInfo],
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
"""Renames toplevel symbols so they can be exported in Starlark under the same name"""
|
||||
|
||||
native_proto_common = proto_common_do_not_use
|
||||
HAS_NATIVE_PROTO_FLAGS = True
|
||||
load("@proto_bazel_features//:features.bzl", "bazel_features")
|
||||
|
||||
native_proto_common = getattr(native, "proto_common", None)
|
||||
HAS_NATIVE_PROTO_FLAGS = bazel_features.rules.has_proto_fragment
|
||||
|
|
|
|||
|
|
@ -193,7 +193,7 @@ rules to generate C++ code for.""",
|
|||
),
|
||||
} | toolchains.if_legacy_toolchain({
|
||||
"_proto_toolchain_for_cc": attr.label(
|
||||
default = "//bazel/flags/cc:proto_toolchain_for_cc",
|
||||
default = Label("//bazel/flags/cc:proto_toolchain_for_cc"),
|
||||
),
|
||||
}),
|
||||
provides = [CcInfo],
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ _PROTO_BAZEL_FEATURES = """bazel_features = struct(
|
|||
),
|
||||
rules = struct(
|
||||
analysis_tests_can_transition_on_experimental_incompatible_flags = {analysis_tests_can_transition_on_experimental_incompatible_flags},
|
||||
has_proto_fragment = {has_proto_fragment},
|
||||
),
|
||||
globals = struct(
|
||||
PackageSpecificationInfo = {PackageSpecificationInfo},
|
||||
|
|
@ -40,6 +41,7 @@ def _proto_bazel_features_impl(rctx):
|
|||
protobuf_on_allowlist = major_version_int > 7
|
||||
ProtoInfo = "ProtoInfo" if major_version_int < 8 else "None"
|
||||
cc_proto_aspect = "cc_proto_aspect" if major_version_int < 8 else "None"
|
||||
has_proto_fragment = major_version_int < 9
|
||||
|
||||
rctx.file("BUILD.bazel", """
|
||||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
|
||||
|
|
@ -58,6 +60,7 @@ exports_files(["features.bzl"])
|
|||
cc_proto_aspect = cc_proto_aspect,
|
||||
analysis_tests_can_transition_on_experimental_incompatible_flags =
|
||||
"True" if major_version_int > 8 or (major_version_int == 8 and minor_version_int >= 2) else "False",
|
||||
has_proto_fragment = repr(has_proto_fragment),
|
||||
))
|
||||
|
||||
proto_bazel_features = repository_rule(
|
||||
|
|
|
|||
|
|
@ -150,14 +150,14 @@ Deprecated. Alias for <code>denylisted_protos</code>. Will be removed in a futur
|
|||
executable = True,
|
||||
),
|
||||
"_protocopt": attr.label(
|
||||
default = "//bazel/flags:protocopt",
|
||||
default = Label("//bazel/flags:protocopt"),
|
||||
),
|
||||
} | ({} if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
|
||||
"_proto_compiler": attr.label(
|
||||
cfg = "exec",
|
||||
executable = True,
|
||||
allow_files = True,
|
||||
default = "//bazel/flags:proto_compiler",
|
||||
default = Label("//bazel/flags:proto_compiler"),
|
||||
),
|
||||
}),
|
||||
provides = [ProtoLangToolchainInfo],
|
||||
|
|
|
|||
|
|
@ -386,20 +386,20 @@ for use with MessageSet.
|
|||
# buildifier: disable=attr-license (calling attr.license())
|
||||
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
|
||||
"_experimental_proto_descriptor_sets_include_source_info": attr.label(
|
||||
default = "//bazel/flags:experimental_proto_descriptor_sets_include_source_info",
|
||||
default = Label("//bazel/flags:experimental_proto_descriptor_sets_include_source_info"),
|
||||
),
|
||||
"_strict_proto_deps": attr.label(
|
||||
default = "//bazel/flags:strict_proto_deps",
|
||||
default = Label("//bazel/flags:strict_proto_deps"),
|
||||
),
|
||||
"_strict_public_imports": attr.label(
|
||||
default = "//bazel/flags:strict_public_imports",
|
||||
default = Label("//bazel/flags:strict_public_imports"),
|
||||
),
|
||||
} | toolchains.if_legacy_toolchain({
|
||||
"_proto_compiler": attr.label(
|
||||
cfg = "exec",
|
||||
executable = True,
|
||||
allow_files = True,
|
||||
default = "//src/google/protobuf/compiler:protoc_minimal",
|
||||
default = Label("//src/google/protobuf/compiler:protoc_minimal"),
|
||||
),
|
||||
}), # buildifier: disable=attr-licenses (attribute called licenses)
|
||||
fragments = [
|
||||
|
|
|
|||
|
|
@ -669,7 +669,10 @@ def _test_experimental_proto_descriptor_sets_include_source_info(name):
|
|||
name = name,
|
||||
target = name + "_a_proto",
|
||||
impl = _test_experimental_proto_descriptor_sets_include_source_info_impl,
|
||||
config_settings = {"@@//bazel/flags:experimental_proto_descriptor_sets_include_source_info": "true"},
|
||||
config_settings = {
|
||||
"//command_line_option:experimental_proto_descriptor_sets_include_source_info": "true",
|
||||
"@@//bazel/flags:experimental_proto_descriptor_sets_include_source_info": "true",
|
||||
},
|
||||
)
|
||||
|
||||
def _test_experimental_proto_descriptor_sets_include_source_info_impl(env, target):
|
||||
|
|
|
|||
|
|
@ -7,11 +7,13 @@
|
|||
#
|
||||
"""Tests for `proto_common.compile` function."""
|
||||
|
||||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
|
||||
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
|
||||
load("@rules_testing//lib:truth.bzl", "matching")
|
||||
load("@rules_testing//lib:util.bzl", "util")
|
||||
load("//bazel:proto_library.bzl", "proto_library")
|
||||
load("//bazel/tests/testdata:compile_rule.bzl", "compile_rule")
|
||||
load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
|
||||
|
||||
protocol_compiler = "/protoc"
|
||||
|
||||
|
|
@ -34,9 +36,18 @@ def proto_common_compile_test_suite(name):
|
|||
_test_compile_additional_inputs,
|
||||
_test_compile_resource_set,
|
||||
_test_compile_protoc_opts,
|
||||
_test_compile_protoc_opts_starlark,
|
||||
_test_compile_direct_generated_protos,
|
||||
_test_compile_indirect_generated_protos,
|
||||
_test_compile_override_progress_message,
|
||||
_test_proto_compiler_flag_override,
|
||||
_test_proto_compiler_native_flag_override,
|
||||
_test_proto_toolchain_for_cc_flag_override,
|
||||
_test_proto_toolchain_for_cc_native_flag_override,
|
||||
_test_proto_toolchain_for_java_flag_override,
|
||||
_test_proto_toolchain_for_java_native_flag_override,
|
||||
_test_proto_toolchain_for_javalite_flag_override,
|
||||
_test_proto_toolchain_for_javalite_native_flag_override,
|
||||
],
|
||||
)
|
||||
|
||||
|
|
@ -291,7 +302,25 @@ def _test_compile_protoc_opts(name):
|
|||
analysis_test(
|
||||
name = name,
|
||||
target = name + "_compile",
|
||||
config_settings = {"//command_line_option:protocopt": ["--foo", "--bar"]},
|
||||
config_settings = {
|
||||
"//command_line_option:protocopt": ["--foo", "--bar"],
|
||||
},
|
||||
impl = _test_compile_protoc_opts_impl,
|
||||
)
|
||||
|
||||
def _test_compile_protoc_opts_starlark(name):
|
||||
util.helper_target(
|
||||
compile_rule,
|
||||
name = name + "_compile",
|
||||
proto_dep = ":simple_proto",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = name + "_compile",
|
||||
config_settings = {
|
||||
"@@//bazel/flags:protocopt": ["--foo", "--bar"],
|
||||
},
|
||||
impl = _test_compile_protoc_opts_impl,
|
||||
)
|
||||
|
||||
|
|
@ -393,3 +422,185 @@ def _test_compile_override_progress_message(name):
|
|||
def _test_compile_override_progress_message_impl(env, target):
|
||||
action = env.expect.that_target(target).action_named("MyMnemonic")
|
||||
env.expect.that_str(repr(action.actual)).contains("My custom progress message //")
|
||||
|
||||
def _dummy_compiler_impl(ctx):
|
||||
exe = ctx.actions.declare_file(ctx.label.name)
|
||||
ctx.actions.write(exe, "#!/bin/sh\nexit 0\n", is_executable = True)
|
||||
return [DefaultInfo(executable = exe, files = depset([exe]))]
|
||||
|
||||
_dummy_compiler = rule(
|
||||
implementation = _dummy_compiler_impl,
|
||||
executable = True,
|
||||
)
|
||||
|
||||
def _test_proto_compiler_flag_override(name):
|
||||
util.helper_target(
|
||||
_dummy_compiler,
|
||||
name = name + "_custom_compiler",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags:proto_compiler",
|
||||
impl = _test_proto_compiler_flag_override_impl,
|
||||
config_settings = {
|
||||
"@@//bazel/flags:proto_compiler": "//bazel/tests:" + name + "_custom_compiler",
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_compiler_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_compiler")
|
||||
|
||||
def _test_proto_compiler_native_flag_override(name):
|
||||
util.helper_target(
|
||||
_dummy_compiler,
|
||||
name = name + "_custom_compiler_native",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags:proto_compiler",
|
||||
impl = _test_proto_compiler_native_flag_override_impl,
|
||||
config_settings = {
|
||||
"//command_line_option:proto_compiler": str(Label("//bazel/tests:" + name + "_custom_compiler_native")),
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_compiler_native_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_compiler_native")
|
||||
|
||||
def _test_proto_toolchain_for_cc_flag_override(name):
|
||||
util.helper_target(
|
||||
proto_lang_toolchain,
|
||||
name = name + "_custom_cc_toolchain",
|
||||
command_line = "$(OUT)",
|
||||
mnemonic = "CustomCcMnemonic",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags/cc:proto_toolchain_for_cc",
|
||||
impl = _test_proto_toolchain_for_cc_flag_override_impl,
|
||||
config_settings = {
|
||||
"@@//bazel/flags/cc:proto_toolchain_for_cc": "//bazel/tests:" + name + "_custom_cc_toolchain",
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_toolchain_for_cc_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_cc_toolchain")
|
||||
|
||||
def _test_proto_toolchain_for_cc_native_flag_override(name):
|
||||
util.helper_target(
|
||||
proto_lang_toolchain,
|
||||
name = name + "_custom_cc_toolchain_native",
|
||||
command_line = "$(OUT)",
|
||||
mnemonic = "CustomCcMnemonicNative",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags/cc:proto_toolchain_for_cc",
|
||||
impl = _test_proto_toolchain_for_cc_native_flag_override_impl,
|
||||
config_settings = {
|
||||
"//command_line_option:proto_toolchain_for_cc": str(Label("//bazel/tests:" + name + "_custom_cc_toolchain_native")),
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_toolchain_for_cc_native_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_cc_toolchain_native")
|
||||
|
||||
def _test_proto_toolchain_for_java_flag_override(name):
|
||||
util.helper_target(
|
||||
proto_lang_toolchain,
|
||||
name = name + "_custom_java_toolchain",
|
||||
command_line = "$(OUT)",
|
||||
mnemonic = "CustomJavaMnemonic",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags/java:proto_toolchain_for_java",
|
||||
impl = _test_proto_toolchain_for_java_flag_override_impl,
|
||||
config_settings = {
|
||||
"@@//bazel/flags/java:proto_toolchain_for_java": "//bazel/tests:" + name + "_custom_java_toolchain",
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_toolchain_for_java_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_java_toolchain")
|
||||
|
||||
def _test_proto_toolchain_for_java_native_flag_override(name):
|
||||
util.helper_target(
|
||||
proto_lang_toolchain,
|
||||
name = name + "_custom_java_toolchain_native",
|
||||
command_line = "$(OUT)",
|
||||
mnemonic = "CustomJavaMnemonicNative",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags/java:proto_toolchain_for_java",
|
||||
impl = _test_proto_toolchain_for_java_native_flag_override_impl,
|
||||
config_settings = {
|
||||
"//command_line_option:proto_toolchain_for_java": str(Label("//bazel/tests:" + name + "_custom_java_toolchain_native")),
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_toolchain_for_java_native_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_java_toolchain_native")
|
||||
|
||||
def _test_proto_toolchain_for_javalite_flag_override(name):
|
||||
util.helper_target(
|
||||
proto_lang_toolchain,
|
||||
name = name + "_custom_javalite_toolchain",
|
||||
command_line = "$(OUT)",
|
||||
mnemonic = "CustomJavaLiteMnemonic",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags/java:proto_toolchain_for_javalite",
|
||||
impl = _test_proto_toolchain_for_javalite_flag_override_impl,
|
||||
config_settings = {
|
||||
"@@//bazel/flags/java:proto_toolchain_for_javalite": "//bazel/tests:" + name + "_custom_javalite_toolchain",
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_toolchain_for_javalite_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_javalite_toolchain")
|
||||
|
||||
def _test_proto_toolchain_for_javalite_native_flag_override(name):
|
||||
util.helper_target(
|
||||
proto_lang_toolchain,
|
||||
name = name + "_custom_javalite_toolchain_native",
|
||||
command_line = "$(OUT)",
|
||||
mnemonic = "CustomJavaLiteMnemonicNative",
|
||||
)
|
||||
|
||||
analysis_test(
|
||||
name = name,
|
||||
target = "@//bazel/flags/java:proto_toolchain_for_javalite",
|
||||
impl = _test_proto_toolchain_for_javalite_native_flag_override_impl,
|
||||
config_settings = {
|
||||
"//command_line_option:proto_toolchain_for_javalite": str(Label("//bazel/tests:" + name + "_custom_javalite_toolchain_native")),
|
||||
},
|
||||
)
|
||||
|
||||
def _test_proto_toolchain_for_javalite_native_flag_override_impl(env, target):
|
||||
env.expect.that_target(target).has_provider(BuildSettingInfo)
|
||||
val = target[BuildSettingInfo].value
|
||||
env.expect.that_str(str(val)).contains("_custom_javalite_toolchain_native")
|
||||
|
|
|
|||
|
|
@ -87,6 +87,9 @@ class ModuleFileFunctions(object):
|
|||
def register_toolchains(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def flag_alias(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def use_repo(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue