• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2024 Google Inc.  All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7#
8"""proto_lang_toolchain rule"""
9
10load("@proto_bazel_features//:features.bzl", "bazel_features")
11load("//bazel/common:proto_common.bzl", "proto_common")
12load("//bazel/private:proto_lang_toolchain_rule.bzl", _proto_lang_toolchain_rule = "proto_lang_toolchain")
13
14def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs):
15    """Creates a proto_lang_toolchain and corresponding toolchain target.
16
17    Toolchain target is only created when toolchain_type is set.
18
19    https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain
20
21    Args:
22
23      name: name of the toolchain
24      toolchain_type: The toolchain type
25      exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with.
26      target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with.
27      **attrs: Rule attributes
28    """
29
30    if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
31        attrs["toolchain_type"] = toolchain_type
32
33    # This condition causes Starlark rules to be used only on Bazel >=7.0.0
34    if bazel_features.proto.starlark_proto_info:
35        _proto_lang_toolchain_rule(name = name, **attrs)
36    else:
37        # On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen
38        native.proto_lang_toolchain(name = name, **attrs)
39
40    if toolchain_type:
41        native.toolchain(
42            name = name + "_toolchain",
43            toolchain_type = toolchain_type,
44            exec_compatible_with = exec_compatible_with,
45            target_compatible_with = target_compatible_with,
46            toolchain = name,
47        )
48