diff --git a/bazel/common/BUILD.bazel b/bazel/common/BUILD.bazel new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/bazel/common/proto_common.bzl b/bazel/common/proto_common.bzl new file mode 100644 index 000000000000..963aabde6ddb --- /dev/null +++ b/bazel/common/proto_common.bzl @@ -0,0 +1,5 @@ +"""proto_common""" + +load("//bazel/private:native.bzl", "native_proto_common") + +proto_common = native_proto_common diff --git a/bazel/common/proto_info.bzl b/bazel/common/proto_info.bzl new file mode 100644 index 000000000000..19fb7294e56c --- /dev/null +++ b/bazel/common/proto_info.bzl @@ -0,0 +1,5 @@ +"""ProtoInfo""" + +load("//bazel/private:native.bzl", "NativeProtoInfo") + +ProtoInfo = NativeProtoInfo diff --git a/bazel/common/proto_lang_toolchain_info.bzl b/bazel/common/proto_lang_toolchain_info.bzl new file mode 100644 index 000000000000..714c9f288c0f --- /dev/null +++ b/bazel/common/proto_lang_toolchain_info.bzl @@ -0,0 +1,5 @@ +"""ProtoLangToolchainInfo""" + +load("//bazel/common:proto_common.bzl", "proto_common") + +ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo diff --git a/bazel/private/native.bzl b/bazel/private/native.bzl new file mode 100644 index 000000000000..56856e3720c8 --- /dev/null +++ b/bazel/private/native.bzl @@ -0,0 +1,5 @@ +"""Renames toplevel symbols so they can be exported in Starlark under the same name""" + +NativeProtoInfo = ProtoInfo + +native_proto_common = proto_common_do_not_use diff --git a/bazel/private/proto_toolchain_rule.bzl b/bazel/private/proto_toolchain_rule.bzl new file mode 100644 index 000000000000..27527a612afd --- /dev/null +++ b/bazel/private/proto_toolchain_rule.bzl @@ -0,0 +1,47 @@ +"""A Starlark implementation of the proto_toolchain rule.""" + +load("//bazel/common:proto_common.bzl", "proto_common") +load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") + +def _impl(ctx): + kwargs = {} + if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False): + kwargs["toolchain_type"] = "//third_party/bazel_rules/rules_proto/proto:toolchain_type" + + return [ + DefaultInfo( + files = depset(), + runfiles = ctx.runfiles(), + ), + platform_common.ToolchainInfo( + proto = ProtoLangToolchainInfo( + out_replacement_format_flag = ctx.attr.command_line, + output_files = ctx.attr.output_files, + plugin = None, + runtime = None, + proto_compiler = ctx.attr.proto_compiler.files_to_run, + protoc_opts = ctx.fragments.proto.experimental_protoc_opts, + progress_message = ctx.attr.progress_message, + mnemonic = ctx.attr.mnemonic, + **kwargs + ), + ), + ] + +proto_toolchain = rule( + _impl, + attrs = + { + "progress_message": attr.string(default = "Generating Descriptor Set proto_library %{label}"), + "mnemonic": attr.string(default = "GenProtoDescriptorSet"), + "command_line": attr.string(default = "--descriptor_set_out=%s"), + "output_files": attr.string(values = ["single", "multiple", "legacy"], default = "single"), + "proto_compiler": attr.label( + cfg = "exec", + executable = True, + allow_files = True, # Used by mocks in tests. Consider fixing tests and removing it. + ), + }, + provides = [platform_common.ToolchainInfo], + fragments = ["proto"], +) diff --git a/bazel/proto_library.bzl b/bazel/proto_library.bzl new file mode 100644 index 000000000000..3006e3c0c29d --- /dev/null +++ b/bazel/proto_library.bzl @@ -0,0 +1,3 @@ +"""proto_library rule""" + +proto_library = native.proto_library diff --git a/bazel/toolchains/BUILD.bazel b/bazel/toolchains/BUILD.bazel new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/bazel/toolchains/proto_lang_toolchain.bzl b/bazel/toolchains/proto_lang_toolchain.bzl new file mode 100644 index 000000000000..7af6537e4df5 --- /dev/null +++ b/bazel/toolchains/proto_lang_toolchain.bzl @@ -0,0 +1,34 @@ +"""proto_lang_toolchain rule""" + +load("//bazel/common:proto_common.bzl", "proto_common") + +def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs): + """Creates a proto_lang_toolchain and corresponding toolchain target. + + Toolchain target is only created when toolchain_type is set. + + https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain + + Args: + + name: name of the toolchain + toolchain_type: The toolchain type + exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with. + target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with. + **attrs: Rule attributes + """ + + if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False): + attrs["toolchain_type"] = toolchain_type + + # buildifier: disable=native-proto + native.proto_lang_toolchain(name = name, **attrs) + + if toolchain_type: + native.toolchain( + name = name + "_toolchain", + toolchain_type = toolchain_type, + exec_compatible_with = exec_compatible_with, + target_compatible_with = target_compatible_with, + toolchain = name, + ) diff --git a/bazel/toolchains/proto_toolchain.bzl b/bazel/toolchains/proto_toolchain.bzl new file mode 100644 index 000000000000..2b267fed6179 --- /dev/null +++ b/bazel/toolchains/proto_toolchain.bzl @@ -0,0 +1,26 @@ +"""Macro wrapping the proto_toolchain implementation. + +The macro additionally creates toolchain target when toolchain_type is given. +""" + +load("//bazel/private:proto_toolchain_rule.bzl", _proto_toolchain_rule = "proto_toolchain") + +def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []): + """Creates a proto_toolchain and toolchain target for proto_library. + + Toolchain target is suffixed with "_toolchain". + + Args: + name: name of the toolchain + proto_compiler: (Label) of either proto compiler sources or prebuild binaries + exec_compatible_with: ([constraints]) List of constraints the prebuild binary is compatible with. + """ + _proto_toolchain_rule(name = name, proto_compiler = proto_compiler) + + native.toolchain( + name = name + "_toolchain", + toolchain_type = "//third_party/bazel_rules/rules_proto/proto:toolchain_type", + exec_compatible_with = exec_compatible_with, + target_compatible_with = [], + toolchain = name, + )