xref: /aosp_15_r20/external/bazelbuild-rules_cc/cc/toolchains/tool.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1*eed53cd4SHONG Yifan# Copyright 2023 The Bazel Authors. All rights reserved.
2*eed53cd4SHONG Yifan#
3*eed53cd4SHONG Yifan# Licensed under the Apache License, Version 2.0 (the "License");
4*eed53cd4SHONG Yifan# you may not use this file except in compliance with the License.
5*eed53cd4SHONG Yifan# You may obtain a copy of the License at
6*eed53cd4SHONG Yifan#
7*eed53cd4SHONG Yifan#     http://www.apache.org/licenses/LICENSE-2.0
8*eed53cd4SHONG Yifan#
9*eed53cd4SHONG Yifan# Unless required by applicable law or agreed to in writing, software
10*eed53cd4SHONG Yifan# distributed under the License is distributed on an "AS IS" BASIS,
11*eed53cd4SHONG Yifan# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*eed53cd4SHONG Yifan# See the License for the specific language governing permissions and
13*eed53cd4SHONG Yifan# limitations under the License.
14*eed53cd4SHONG Yifan"""Implementation of cc_tool"""
15*eed53cd4SHONG Yifan
16*eed53cd4SHONG Yifanload("//cc/toolchains/impl:collect.bzl", "collect_data", "collect_provider")
17*eed53cd4SHONG Yifanload(
18*eed53cd4SHONG Yifan    ":cc_toolchain_info.bzl",
19*eed53cd4SHONG Yifan    "FeatureConstraintInfo",
20*eed53cd4SHONG Yifan    "ToolInfo",
21*eed53cd4SHONG Yifan)
22*eed53cd4SHONG Yifan
23*eed53cd4SHONG Yifandef _cc_tool_impl(ctx):
24*eed53cd4SHONG Yifan    exe_info = ctx.attr.src[DefaultInfo]
25*eed53cd4SHONG Yifan    if exe_info.files_to_run != None and exe_info.files_to_run.executable != None:
26*eed53cd4SHONG Yifan        exe = exe_info.files_to_run.executable
27*eed53cd4SHONG Yifan    elif len(exe_info.files.to_list()) == 1:
28*eed53cd4SHONG Yifan        exe = exe_info.files.to_list()[0]
29*eed53cd4SHONG Yifan    else:
30*eed53cd4SHONG Yifan        fail("Expected cc_tool's src attribute to be either an executable or a single file")
31*eed53cd4SHONG Yifan
32*eed53cd4SHONG Yifan    runfiles = collect_data(ctx, ctx.attr.data + [ctx.attr.src])
33*eed53cd4SHONG Yifan    tool = ToolInfo(
34*eed53cd4SHONG Yifan        label = ctx.label,
35*eed53cd4SHONG Yifan        exe = exe,
36*eed53cd4SHONG Yifan        runfiles = runfiles,
37*eed53cd4SHONG Yifan        requires_any_of = tuple(collect_provider(
38*eed53cd4SHONG Yifan            ctx.attr.requires_any_of,
39*eed53cd4SHONG Yifan            FeatureConstraintInfo,
40*eed53cd4SHONG Yifan        )),
41*eed53cd4SHONG Yifan        execution_requirements = tuple(ctx.attr.execution_requirements),
42*eed53cd4SHONG Yifan    )
43*eed53cd4SHONG Yifan
44*eed53cd4SHONG Yifan    link = ctx.actions.declare_file(ctx.label.name)
45*eed53cd4SHONG Yifan    ctx.actions.symlink(
46*eed53cd4SHONG Yifan        output = link,
47*eed53cd4SHONG Yifan        target_file = exe,
48*eed53cd4SHONG Yifan        is_executable = True,
49*eed53cd4SHONG Yifan    )
50*eed53cd4SHONG Yifan    return [
51*eed53cd4SHONG Yifan        tool,
52*eed53cd4SHONG Yifan        # This isn't required, but now we can do "bazel run <tool>", which can
53*eed53cd4SHONG Yifan        # be very helpful when debugging toolchains.
54*eed53cd4SHONG Yifan        DefaultInfo(
55*eed53cd4SHONG Yifan            files = depset([link]),
56*eed53cd4SHONG Yifan            runfiles = runfiles,
57*eed53cd4SHONG Yifan            executable = link,
58*eed53cd4SHONG Yifan        ),
59*eed53cd4SHONG Yifan    ]
60*eed53cd4SHONG Yifan
61*eed53cd4SHONG Yifancc_tool = rule(
62*eed53cd4SHONG Yifan    implementation = _cc_tool_impl,
63*eed53cd4SHONG Yifan    # @unsorted-dict-items
64*eed53cd4SHONG Yifan    attrs = {
65*eed53cd4SHONG Yifan        "src": attr.label(
66*eed53cd4SHONG Yifan            allow_files = True,
67*eed53cd4SHONG Yifan            cfg = "exec",
68*eed53cd4SHONG Yifan            doc = """The underlying binary that this tool represents.
69*eed53cd4SHONG Yifan
70*eed53cd4SHONG YifanUsually just a single prebuilt (eg. @sysroot//:bin/clang), but may be any
71*eed53cd4SHONG Yifanexecutable label.
72*eed53cd4SHONG Yifan""",
73*eed53cd4SHONG Yifan        ),
74*eed53cd4SHONG Yifan        "data": attr.label_list(
75*eed53cd4SHONG Yifan            allow_files = True,
76*eed53cd4SHONG Yifan            doc = "Additional files that are required for this tool to run.",
77*eed53cd4SHONG Yifan        ),
78*eed53cd4SHONG Yifan        "execution_requirements": attr.string_list(
79*eed53cd4SHONG Yifan            doc = "A list of strings that provide hints for execution environment compatibility (e.g. `requires-network`).",
80*eed53cd4SHONG Yifan        ),
81*eed53cd4SHONG Yifan        "requires_any_of": attr.label_list(
82*eed53cd4SHONG Yifan            providers = [FeatureConstraintInfo],
83*eed53cd4SHONG Yifan            doc = """This will be enabled when any of the constraints are met.
84*eed53cd4SHONG Yifan
85*eed53cd4SHONG YifanIf omitted, this tool will be enabled unconditionally.
86*eed53cd4SHONG Yifan""",
87*eed53cd4SHONG Yifan        ),
88*eed53cd4SHONG Yifan    },
89*eed53cd4SHONG Yifan    provides = [ToolInfo],
90*eed53cd4SHONG Yifan    doc = """Declares a tool that can be bound to action configs.
91*eed53cd4SHONG Yifan
92*eed53cd4SHONG YifanA tool is a binary with extra metadata for the action config rule to consume
93*eed53cd4SHONG Yifan(eg. execution_requirements).
94*eed53cd4SHONG Yifan
95*eed53cd4SHONG YifanExample:
96*eed53cd4SHONG Yifan```
97*eed53cd4SHONG Yifancc_tool(
98*eed53cd4SHONG Yifan    name = "clang_tool",
99*eed53cd4SHONG Yifan    executable = "@llvm_toolchain//:bin/clang",
100*eed53cd4SHONG Yifan    # Suppose clang needs libc to run.
101*eed53cd4SHONG Yifan    data = ["@llvm_toolchain//:lib/x86_64-linux-gnu/libc.so.6"]
102*eed53cd4SHONG Yifan)
103*eed53cd4SHONG Yifan```
104*eed53cd4SHONG Yifan""",
105*eed53cd4SHONG Yifan    executable = True,
106*eed53cd4SHONG Yifan)
107