xref: /aosp_15_r20/external/angle/build/config/siso/rust_linux.star (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# -*- bazel-starlark -*-
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Siso configuration for rust/linux."""
6
7load("@builtin//path.star", "path")
8load("@builtin//struct.star", "module")
9load("./config.star", "config")
10load("./fuchsia.star", "fuchsia")
11
12def __filegroups(ctx):
13    fg = {
14        "third_party/rust-toolchain:toolchain": {
15            "type": "glob",
16            "includes": [
17                "bin/rustc",
18                "lib/*.so",
19                "lib/libclang.so.*",
20                "lib/rustlib/src/rust/library/std/src/lib.rs",
21                "lib/rustlib/x86_64-unknown-linux-gnu/lib/*",
22            ],
23        },
24        "third_party/rust:rustlib": {
25            "type": "glob",
26            "includes": [
27                "*.rs",
28            ],
29        },
30        "build/linux/debian_bullseye_amd64-sysroot:rustlink": {
31            "type": "glob",
32            "includes": [
33                "*.so",
34                "*.so.*",
35                "*.o",
36                "*.a",
37            ],
38        },
39        "third_party/llvm-build/Release+Asserts:rustlink": {
40            "type": "glob",
41            "includes": [
42                "bin/clang",
43                "bin/clang++",
44                "bin/*lld",
45                "libclang*.a",
46            ],
47        },
48    }
49    if fuchsia.enabled(ctx):
50        fg.update(fuchsia.filegroups(ctx))
51    return fg
52
53def __rust_link_handler(ctx, cmd):
54    inputs = []
55    use_android_toolchain = None
56    target = None
57    args = cmd.args
58
59    # there is a case that command line sets environment variable
60    # like `TOOL_VERSION=xxxx "python3" ..`
61    if args[0] == "/bin/sh":
62        args = args[2].split(" ")
63    for i, arg in enumerate(args):
64        if arg.startswith("--sysroot=../../third_party/fuchsia-sdk/sdk"):
65            sysroot = ctx.fs.canonpath(arg.removeprefix("--sysroot="))
66            libpath = path.join(path.dir(sysroot), "lib")
67            inputs.extend([
68                sysroot + ":link",
69                libpath + ":link",
70            ])
71        elif arg.startswith("--sysroot=../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot"):
72            use_android_toolchain = True
73            inputs.append("third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot:headers")
74        if arg == "-isysroot":
75            sysroot = ctx.fs.canonpath(args[i + 1])
76            inputs.extend([
77                sysroot + ":link",
78            ])
79        if arg.startswith("--target="):
80            target = arg.removeprefix("--target=")
81        if arg.startswith("-Clinker="):
82            linker = arg.removeprefix("-Clinker=")
83            if linker.startswith("\""):
84                linker = linker[1:len(linker) - 1]
85
86            # TODO(crbug.com/380798907): expand input_deps, instead of using label?
87            inputs.append(ctx.fs.canonpath(linker) + ":link")
88    if use_android_toolchain and target:
89        # e.g. target=aarch64-linux-android26
90        android_ver = ""
91        i = target.find("android")
92        if i >= 0:
93            android_ver = target[i:].removeprefix("android").removeprefix("eabi")
94        if android_ver:
95            android_arch = target.removesuffix(android_ver)
96            filegroup = "third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/%s/%s:link" % (android_arch, android_ver)
97            inputs.append(filegroup)
98    ctx.actions.fix(inputs = cmd.inputs + inputs)
99
100def __rust_build_handler(ctx, cmd):
101    inputs = []
102    for i, arg in enumerate(cmd.args):
103        if arg == "--src-dir":
104            inputs.append(ctx.fs.canonpath(cmd.args[i + 1]))
105    ctx.actions.fix(inputs = cmd.inputs + inputs)
106
107__handlers = {
108    "rust_link_handler": __rust_link_handler,
109    "rust_build_handler": __rust_build_handler,
110}
111
112def __step_config(ctx, step_config):
113    platform_ref = "large"  # Rust actions run faster on large workers.
114    clang_inputs = [
115        "build/linux/debian_bullseye_amd64-sysroot:rustlink",
116        "third_party/llvm-build/Release+Asserts:rustlink",
117    ]
118    rust_toolchain = [
119        # TODO(b/285225184): use precomputed subtree
120        "third_party/rust-toolchain:toolchain",
121    ]
122    rust_inputs = [
123        "build/action_helpers.py",
124        "build/gn_helpers.py",
125        "build/rust/rustc_wrapper.py",
126    ] + rust_toolchain
127    rust_indirect_inputs = {
128        "includes": [
129            "*.h",
130            "*.o",
131            "*.rlib",
132            "*.rs",
133            "*.so",
134        ],
135    }
136    step_config["rules"].extend([
137        {
138            "name": "rust_bin",
139            "action": "(.*_)?rust_bin",
140            "inputs": rust_inputs + clang_inputs,
141            "indirect_inputs": rust_indirect_inputs,
142            "handler": "rust_link_handler",
143            "deps": "none",  # disable gcc scandeps
144            "remote": True,
145            # "canonicalize_dir": True,  # TODO(b/300352286)
146            "timeout": "2m",
147            "platform_ref": platform_ref,
148        },
149        {
150            "name": "rust_cdylib",
151            "action": "(.*_)?rust_cdylib",
152            "inputs": rust_inputs + clang_inputs,
153            "indirect_inputs": rust_indirect_inputs,
154            "handler": "rust_link_handler",
155            "deps": "none",  # disable gcc scandeps
156            "remote": True,
157            # "canonicalize_dir": True,  # TODO(b/300352286)
158            "timeout": "2m",
159            "platform_ref": platform_ref,
160        },
161        {
162            "name": "rust_macro",
163            "action": "(.*_)?rust_macro",
164            "inputs": rust_inputs + clang_inputs,
165            "indirect_inputs": rust_indirect_inputs,
166            "handler": "rust_link_handler",
167            "deps": "none",  # disable gcc scandeps
168            # "canonicalize_dir": True,  # TODO(b/300352286)
169            "remote": True,
170            "timeout": "2m",
171            "platform_ref": platform_ref,
172        },
173        {
174            "name": "rust_rlib",
175            "action": "(.*_)?rust_rlib",
176            "inputs": rust_inputs,
177            "indirect_inputs": rust_indirect_inputs,
178            "deps": "none",  # disable gcc scandeps
179            "remote": True,
180            # "canonicalize_dir": True,  # TODO(b/300352286)
181            "timeout": "2m",
182            "platform_ref": platform_ref,
183        },
184        {
185            "name": "rust_staticlib",
186            "action": "(.*_)?rust_staticlib",
187            "inputs": rust_inputs,
188            "indirect_inputs": rust_indirect_inputs,
189            "deps": "none",  # disable gcc scandeps
190            "remote": True,
191            # "canonicalize_dir": True,  # TODO(b/300352286)
192            "timeout": "2m",
193            "platform_ref": platform_ref,
194        },
195        {
196            "name": "rust/run_build_script",
197            "command_prefix": "python3 ../../build/rust/run_build_script.py",
198            "inputs": [
199                "third_party/rust-toolchain:toolchain",
200                "third_party/rust:rustlib",
201            ],
202            "handler": "rust_build_handler",
203            "remote": config.get(ctx, "cog"),
204            "input_root_absolute_path": True,
205            "timeout": "2m",
206        },
207        {
208            "name": "rust/find_std_rlibs",
209            "command_prefix": "python3 ../../build/rust/std/find_std_rlibs.py",
210            "inputs": [
211                "third_party/rust-toolchain:toolchain",
212                "third_party/rust-toolchain/lib/rustlib:rlib",
213            ],
214            "remote": config.get(ctx, "cog"),
215            "input_root_absolute_path": True,
216            "timeout": "2m",
217        },
218        {
219            # rust/bindgen fails remotely when *.d does not exist.
220            # TODO(b/356496947): need to run scandeps?
221            "name": "rust/bindgen",
222            "command_prefix": "python3 ../../build/rust/run_bindgen.py",
223            "inputs": rust_toolchain + clang_inputs,
224            "remote": False,
225            "timeout": "2m",
226        },
227    ])
228    return step_config
229
230rust = module(
231    "rust",
232    filegroups = __filegroups,
233    handlers = __handlers,
234    step_config = __step_config,
235)
236