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 12# TODO: b/323091468 - Propagate fuchsia arch and version from GN, 13# and remove the hardcoded filegroups. 14fuchsia_archs = [ 15 "arm64", 16 "riscv64", 17 "x64", 18] 19 20fuchsia_versions = [12, 14, 15, 16, 17, 18] 21 22def __filegroups(ctx): 23 fg = { 24 "third_party/rust-toolchain:toolchain": { 25 "type": "glob", 26 "includes": [ 27 "bin/rustc", 28 "lib/*.so", 29 "lib/rustlib/src/rust/library/std/src/lib.rs", 30 "lib/rustlib/x86_64-unknown-linux-gnu/lib/*", 31 ], 32 }, 33 "third_party/rust:rustlib": { 34 "type": "glob", 35 "includes": [ 36 "*.rs", 37 ], 38 }, 39 "build/linux/debian_bullseye_amd64-sysroot:rustlink": { 40 "type": "glob", 41 "includes": [ 42 "*.so", 43 "*.so.*", 44 "*.o", 45 "*.a", 46 ], 47 }, 48 "third_party/llvm-build/Release+Asserts:rustlink": { 49 "type": "glob", 50 "includes": [ 51 "bin/clang", 52 "bin/clang++", 53 "bin/*lld", 54 "libclang*.a", 55 ], 56 }, 57 } 58 if fuchsia.enabled(ctx): 59 for arch in fuchsia_archs: 60 group = "third_party/fuchsia-sdk/sdk/arch/%s:rustlink" % arch 61 fg[group] = { 62 "type": "glob", 63 "includes": [ 64 "lib/*", 65 "sysroot/lib/*", 66 ], 67 } 68 for ver in fuchsia_versions: 69 group = "third_party/fuchsia-sdk/sdk/obj/%s-api-%s:rustlink" % (arch, ver) 70 fg[group] = { 71 "type": "glob", 72 "includes": [ 73 "lib/*", 74 "sysroot/lib/*", 75 ], 76 } 77 return fg 78 79def __rust_bin_handler(ctx, cmd): 80 inputs = [] 81 use_android_toolchain = None 82 target = None 83 for i, arg in enumerate(cmd.args): 84 if arg.startswith("--sysroot=../../third_party/fuchsia-sdk/sdk"): 85 # Get the corresponding sdk filegroup from --sysroot. 86 # e.g. --sysroot=../../third_party/fuchsia-sdk/sdk/obj/x64-api-16/sysroot -> third_party/fuchsia-sdk/sdk/obj/x64-api-16:rustlink 87 filegroup = "%s:rustlink" % path.dir(ctx.fs.canonpath(arg.removeprefix("--sysroot="))) 88 inputs.append(filegroup) 89 elif arg.startswith("--sysroot=../../third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot"): 90 use_android_toolchain = True 91 if arg.startswith("--target="): 92 target = arg.removeprefix("--target=") 93 if use_android_toolchain and target: 94 # e.g. target=aarch64-linux-android26 95 android_ver = "" 96 i = target.find("android") 97 if i >= 0: 98 android_ver = target[i:].removeprefix("android").removeprefix("eabi") 99 if android_ver: 100 android_arch = target.removesuffix(android_ver) 101 filegroup = "third_party/android_toolchain/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/%s/%s:link" % (android_arch, android_ver) 102 inputs.append(filegroup) 103 104 ctx.actions.fix(inputs = cmd.inputs + inputs) 105 106def __rust_build_handler(ctx, cmd): 107 inputs = [] 108 for i, arg in enumerate(cmd.args): 109 if arg == "--src-dir": 110 inputs.append(ctx.fs.canonpath(cmd.args[i + 1])) 111 ctx.actions.fix(inputs = cmd.inputs + inputs) 112 113__handlers = { 114 "rust_bin_handler": __rust_bin_handler, 115 "rust_build_handler": __rust_build_handler, 116} 117 118def __step_config(ctx, step_config): 119 platform_ref = "large" # Rust actions run faster on large workers. 120 clang_inputs = [ 121 "build/linux/debian_bullseye_amd64-sysroot:rustlink", 122 "third_party/llvm-build/Release+Asserts:rustlink", 123 ] 124 rust_inputs = [ 125 "build/action_helpers.py", 126 "build/gn_helpers.py", 127 "build/rust/rustc_wrapper.py", 128 # TODO(b/285225184): use precomputed subtree 129 "third_party/rust-toolchain:toolchain", 130 ] 131 rust_indirect_inputs = { 132 "includes": [ 133 "*.h", 134 "*.o", 135 "*.rlib", 136 "*.rs", 137 "*.so", 138 ], 139 } 140 step_config["rules"].extend([ 141 { 142 "name": "rust_bin", 143 "action": "(.*_)?rust_bin", 144 "inputs": rust_inputs + clang_inputs, 145 "indirect_inputs": rust_indirect_inputs, 146 "handler": "rust_bin_handler", 147 "deps": "none", # disable gcc scandeps 148 "remote": True, 149 # "canonicalize_dir": True, # TODO(b/300352286) 150 "timeout": "2m", 151 "platform_ref": platform_ref, 152 }, 153 { 154 "name": "rust_cdylib", 155 "action": "(.*_)?rust_cdylib", 156 "inputs": rust_inputs + clang_inputs, 157 "indirect_inputs": rust_indirect_inputs, 158 "deps": "none", # disable gcc scandeps 159 "remote": True, 160 # "canonicalize_dir": True, # TODO(b/300352286) 161 "timeout": "2m", 162 "platform_ref": platform_ref, 163 }, 164 { 165 "name": "rust_macro", 166 "action": "(.*_)?rust_macro", 167 "inputs": rust_inputs + clang_inputs, 168 "indirect_inputs": rust_indirect_inputs, 169 "deps": "none", # disable gcc scandeps 170 # "canonicalize_dir": True, # TODO(b/300352286) 171 "remote": True, 172 "timeout": "2m", 173 "platform_ref": platform_ref, 174 }, 175 { 176 "name": "rust_rlib", 177 "action": "(.*_)?rust_rlib", 178 "inputs": rust_inputs, 179 "indirect_inputs": rust_indirect_inputs, 180 "deps": "none", # disable gcc scandeps 181 "remote": True, 182 # "canonicalize_dir": True, # TODO(b/300352286) 183 "timeout": "2m", 184 "platform_ref": platform_ref, 185 }, 186 { 187 "name": "rust_staticlib", 188 "action": "(.*_)?rust_staticlib", 189 "inputs": rust_inputs, 190 "indirect_inputs": rust_indirect_inputs, 191 "deps": "none", # disable gcc scandeps 192 "remote": True, 193 # "canonicalize_dir": True, # TODO(b/300352286) 194 "timeout": "2m", 195 "platform_ref": platform_ref, 196 }, 197 { 198 "name": "rust/run_build_script", 199 "command_prefix": "python3 ../../build/rust/run_build_script.py", 200 "inputs": [ 201 "build/action_helpers.py", 202 "build/gn_helpers.py", 203 "third_party/rust-toolchain:toolchain", 204 "third_party/rust:rustlib", 205 ], 206 "handler": "rust_build_handler", 207 "remote": config.get(ctx, "cog"), 208 "input_root_absolute_path": True, 209 "timeout": "2m", 210 }, 211 { 212 "name": "rust/find_std_rlibs", 213 "command_prefix": "python3 ../../build/rust/std/find_std_rlibs.py", 214 "inputs": [ 215 "third_party/rust-toolchain:toolchain", 216 "third_party/rust-toolchain/lib/rustlib:rlib", 217 ], 218 "remote": config.get(ctx, "cog"), 219 "input_root_absolute_path": True, 220 "timeout": "2m", 221 }, 222 ]) 223 return step_config 224 225rust = module( 226 "rust", 227 filegroups = __filegroups, 228 handlers = __handlers, 229 step_config = __step_config, 230) 231