blob: c11c02cee33bb921e92c5ac4be4aae88b81a8cc2 [file] [edit]
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for managing tools for different platforms."""
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
visibility("//tools/...")
BZLMOD_ENABLED = str(Label("@bazel_tools//:foo")).startswith("@@")
IS_HOST_WINDOWS = Label("@platforms//os:windows") in [Label(label) for label in HOST_CONSTRAINTS]
def _single_binary_toolchain_rule_impl(ctx):
return platform_common.ToolchainInfo(
binary = ctx.file.binary,
)
_single_target_binary_toolchain_rule = rule(
implementation = _single_binary_toolchain_rule_impl,
attrs = {
"binary": attr.label(
allow_single_file = True,
mandatory = True,
),
},
)
_single_exec_binary_toolchain_rule = rule(
implementation = _single_binary_toolchain_rule_impl,
attrs = {
"binary": attr.label(
cfg = "exec",
allow_single_file = True,
mandatory = True,
),
},
)
def single_binary_toolchain(
*,
name,
toolchain_type,
target_binary = None,
exec_binary = None,
target_compatible_with = [],
exec_compatible_with = []):
"""Declares a toolchain together with its implementation for an optional single binary."""
impl_name = name + "_impl"
if exec_binary and target_binary:
fail("Cannot specify both exec_binary and target_binary for a single_binary_toolchain.")
elif target_binary:
binary = target_binary
_single_binary_toolchain_rule = _single_target_binary_toolchain_rule
elif exec_binary:
binary = exec_binary
_single_binary_toolchain_rule = _single_exec_binary_toolchain_rule
else:
fail("Must specify either exec_binary or target_binary for a single_binary_toolchain.")
_single_binary_toolchain_rule(
name = impl_name,
binary = binary,
# Avoid eager loading of the binary, which may come from a remote
# repository, in wildcard builds.
tags = ["manual"],
visibility = ["//visibility:private"],
)
native.toolchain(
name = name,
toolchain_type = toolchain_type,
toolchain = ":" + impl_name,
target_compatible_with = target_compatible_with,
exec_compatible_with = exec_compatible_with,
visibility = ["//visibility:private"],
)
def _current_toolchain_base_impl(ctx, *, toolchain_type):
executable = ctx.actions.declare_file(ctx.label.name)
ctx.actions.symlink(
output = executable,
target_file = ctx.toolchains[toolchain_type].binary,
)
return DefaultInfo(executable = executable)
def _make_current_toolchain_rule(toolchain_type):
return rule(
implementation = lambda ctx: _current_toolchain_base_impl(ctx, toolchain_type = toolchain_type),
toolchains = [toolchain_type],
executable = True,
)
current_launcher_binary = _make_current_toolchain_rule("//tools/launcher:launcher_toolchain_type")