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""" 9Toolchain helpers. 10 11The helpers here should be used for a migration to toolchain in proto rules. 12 13Anybody that needs them in another repository should copy them, because after 14the migration is finished, the helpers can be removed. 15""" 16 17load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") 18load("//bazel/private:native.bzl", "native_proto_common") 19 20_incompatible_toolchain_resolution = getattr(native_proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False) 21 22def _find_toolchain(ctx, legacy_attr, toolchain_type): 23 if _incompatible_toolchain_resolution: 24 toolchain = ctx.toolchains[toolchain_type] 25 if not toolchain: 26 fail("No toolchains registered for '%s'." % toolchain_type) 27 return toolchain.proto 28 else: 29 return getattr(ctx.attr, legacy_attr)[ProtoLangToolchainInfo] 30 31def _use_toolchain(toolchain_type): 32 if _incompatible_toolchain_resolution: 33 return [config_common.toolchain_type(toolchain_type, mandatory = False)] 34 else: 35 return [] 36 37def _if_legacy_toolchain(legacy_attr_dict): 38 if _incompatible_toolchain_resolution: 39 return {} 40 else: 41 return legacy_attr_dict 42 43toolchains = struct( 44 use_toolchain = _use_toolchain, 45 find_toolchain = _find_toolchain, 46 if_legacy_toolchain = _if_legacy_toolchain, 47 INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = _incompatible_toolchain_resolution, 48 PROTO_TOOLCHAIN = Label("//bazel/private:proto_toolchain_type"), 49) 50