Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame] | 1 | """The rust_toolchain rule definition and implementation.""" |
| 2 | |
| 3 | load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") |
| 4 | load("//rust/private:common.bzl", "rust_common") |
| 5 | load("//rust/private:utils.bzl", "dedent", "find_cc_toolchain", "make_static_lib_symlink") |
| 6 | |
| 7 | def _rust_stdlib_filegroup_impl(ctx): |
| 8 | rust_std = ctx.files.srcs |
| 9 | dot_a_files = [] |
| 10 | between_alloc_and_core_files = [] |
| 11 | core_files = [] |
| 12 | between_core_and_std_files = [] |
| 13 | std_files = [] |
| 14 | alloc_files = [] |
| 15 | self_contained_files = [ |
| 16 | file |
| 17 | for file in rust_std |
| 18 | if file.basename.endswith(".o") and "self-contained" in file.path |
| 19 | ] |
| 20 | |
| 21 | std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")] |
| 22 | if std_rlibs: |
| 23 | # std depends on everything |
| 24 | # |
| 25 | # core only depends on alloc, but we poke adler in there |
| 26 | # because that needs to be before miniz_oxide |
| 27 | # |
| 28 | # alloc depends on the allocator_library if it's configured, but we |
| 29 | # do that later. |
| 30 | dot_a_files = [make_static_lib_symlink(ctx.actions, f) for f in std_rlibs] |
| 31 | |
| 32 | alloc_files = [f for f in dot_a_files if "alloc" in f.basename and "std" not in f.basename] |
| 33 | between_alloc_and_core_files = [f for f in dot_a_files if "compiler_builtins" in f.basename] |
| 34 | core_files = [f for f in dot_a_files if ("core" in f.basename or "adler" in f.basename) and "std" not in f.basename] |
| 35 | between_core_and_std_files = [ |
| 36 | f |
| 37 | for f in dot_a_files |
| 38 | if "alloc" not in f.basename and "compiler_builtins" not in f.basename and "core" not in f.basename and "adler" not in f.basename and "std" not in f.basename |
| 39 | ] |
| 40 | std_files = [f for f in dot_a_files if "std" in f.basename] |
| 41 | |
| 42 | partitioned_files_len = len(alloc_files) + len(between_alloc_and_core_files) + len(core_files) + len(between_core_and_std_files) + len(std_files) |
| 43 | if partitioned_files_len != len(dot_a_files): |
| 44 | partitioned = alloc_files + between_alloc_and_core_files + core_files + between_core_and_std_files + std_files |
| 45 | for f in sorted(partitioned): |
| 46 | # buildifier: disable=print |
| 47 | print("File partitioned: {}".format(f.basename)) |
| 48 | fail("rust_toolchain couldn't properly partition rlibs in rust_std. Partitioned {} out of {} files. This is probably a bug in the rule implementation.".format(partitioned_files_len, len(dot_a_files))) |
| 49 | |
| 50 | return [ |
| 51 | DefaultInfo( |
| 52 | files = depset(ctx.files.srcs), |
| 53 | ), |
| 54 | rust_common.stdlib_info( |
| 55 | std_rlibs = std_rlibs, |
| 56 | dot_a_files = dot_a_files, |
| 57 | between_alloc_and_core_files = between_alloc_and_core_files, |
| 58 | core_files = core_files, |
| 59 | between_core_and_std_files = between_core_and_std_files, |
| 60 | std_files = std_files, |
| 61 | alloc_files = alloc_files, |
| 62 | self_contained_files = self_contained_files, |
| 63 | srcs = ctx.attr.srcs, |
| 64 | ), |
| 65 | ] |
| 66 | |
| 67 | rust_stdlib_filegroup = rule( |
| 68 | doc = "A dedicated filegroup-like rule for Rust stdlib artifacts.", |
| 69 | implementation = _rust_stdlib_filegroup_impl, |
| 70 | attrs = { |
| 71 | "srcs": attr.label_list( |
| 72 | allow_files = True, |
| 73 | doc = "The list of targets/files that are components of the rust-stdlib file group", |
| 74 | mandatory = True, |
| 75 | ), |
| 76 | }, |
| 77 | ) |
| 78 | |
| 79 | def _ltl(library, ctx, cc_toolchain, feature_configuration): |
| 80 | """A helper to generate `LibraryToLink` objects |
| 81 | |
| 82 | Args: |
| 83 | library (File): A rust library file to link. |
| 84 | ctx (ctx): The rule's context object. |
| 85 | cc_toolchain (CcToolchainInfo): A cc toolchain provider to be used. |
| 86 | feature_configuration (feature_configuration): feature_configuration to be queried. |
| 87 | |
| 88 | Returns: |
| 89 | LibraryToLink: A provider containing information about libraries to link. |
| 90 | """ |
| 91 | return cc_common.create_library_to_link( |
| 92 | actions = ctx.actions, |
| 93 | feature_configuration = feature_configuration, |
| 94 | cc_toolchain = cc_toolchain, |
| 95 | static_library = library, |
| 96 | pic_static_library = library, |
| 97 | ) |
| 98 | |
| 99 | def _make_libstd_and_allocator_ccinfo(ctx, rust_std, allocator_library): |
| 100 | """Make the CcInfo (if possible) for libstd and allocator libraries. |
| 101 | |
| 102 | Args: |
| 103 | ctx (ctx): The rule's context object. |
| 104 | rust_std: The Rust standard library. |
| 105 | allocator_library: The target to use for providing allocator functions. |
| 106 | |
| 107 | |
| 108 | Returns: |
| 109 | A CcInfo object for the required libraries, or None if no such libraries are available. |
| 110 | """ |
| 111 | cc_toolchain, feature_configuration = find_cc_toolchain(ctx) |
| 112 | cc_infos = [] |
| 113 | |
| 114 | if not rust_common.stdlib_info in rust_std: |
| 115 | fail(dedent("""\ |
| 116 | {} -- |
| 117 | The `rust_lib` ({}) must be a target providing `rust_common.stdlib_info` |
| 118 | (typically `rust_stdlib_filegroup` rule from @rules_rust//rust:defs.bzl). |
| 119 | See https://github.com/bazelbuild/rules_rust/pull/802 for more information. |
| 120 | """).format(ctx.label, rust_std)) |
| 121 | rust_stdlib_info = rust_std[rust_common.stdlib_info] |
| 122 | |
| 123 | if rust_stdlib_info.self_contained_files: |
| 124 | compilation_outputs = cc_common.create_compilation_outputs( |
| 125 | objects = depset(rust_stdlib_info.self_contained_files), |
| 126 | ) |
| 127 | |
| 128 | linking_context, _linking_outputs = cc_common.create_linking_context_from_compilation_outputs( |
| 129 | name = ctx.label.name, |
| 130 | actions = ctx.actions, |
| 131 | feature_configuration = feature_configuration, |
| 132 | cc_toolchain = cc_toolchain, |
| 133 | compilation_outputs = compilation_outputs, |
| 134 | ) |
| 135 | |
| 136 | cc_infos.append(CcInfo( |
| 137 | linking_context = linking_context, |
| 138 | )) |
| 139 | |
| 140 | if rust_stdlib_info.std_rlibs: |
| 141 | alloc_inputs = depset( |
| 142 | [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.alloc_files], |
| 143 | ) |
| 144 | between_alloc_and_core_inputs = depset( |
| 145 | [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.between_alloc_and_core_files], |
| 146 | transitive = [alloc_inputs], |
| 147 | order = "topological", |
| 148 | ) |
| 149 | core_inputs = depset( |
| 150 | [_ltl(f, ctx, cc_toolchain, feature_configuration) for f in rust_stdlib_info.core_files], |
| 151 | transitive = [between_alloc_and_core_inputs], |
| 152 | order = "topological", |
| 153 | ) |
| 154 | |
| 155 | # The libraries panic_abort and panic_unwind are alternatives. |
| 156 | # The std by default requires panic_unwind. |
| 157 | # Exclude panic_abort if panic_unwind is present. |
| 158 | # TODO: Provide a setting to choose between panic_abort and panic_unwind. |
| 159 | filtered_between_core_and_std_files = rust_stdlib_info.between_core_and_std_files |
| 160 | has_panic_unwind = [ |
| 161 | f |
| 162 | for f in filtered_between_core_and_std_files |
| 163 | if "panic_unwind" in f.basename |
| 164 | ] |
| 165 | if has_panic_unwind: |
| 166 | filtered_between_core_and_std_files = [ |
| 167 | f |
| 168 | for f in filtered_between_core_and_std_files |
| 169 | if "panic_abort" not in f.basename |
| 170 | ] |
| 171 | between_core_and_std_inputs = depset( |
| 172 | [ |
| 173 | _ltl(f, ctx, cc_toolchain, feature_configuration) |
| 174 | for f in filtered_between_core_and_std_files |
| 175 | ], |
| 176 | transitive = [core_inputs], |
| 177 | order = "topological", |
| 178 | ) |
| 179 | std_inputs = depset( |
| 180 | [ |
| 181 | _ltl(f, ctx, cc_toolchain, feature_configuration) |
| 182 | for f in rust_stdlib_info.std_files |
| 183 | ], |
| 184 | transitive = [between_core_and_std_inputs], |
| 185 | order = "topological", |
| 186 | ) |
| 187 | |
| 188 | link_inputs = cc_common.create_linker_input( |
| 189 | owner = rust_std.label, |
| 190 | libraries = std_inputs, |
| 191 | ) |
| 192 | |
| 193 | allocator_inputs = None |
| 194 | if allocator_library: |
| 195 | allocator_inputs = [allocator_library[CcInfo].linking_context.linker_inputs] |
| 196 | |
| 197 | cc_infos.append(CcInfo( |
| 198 | linking_context = cc_common.create_linking_context( |
| 199 | linker_inputs = depset( |
| 200 | [link_inputs], |
| 201 | transitive = allocator_inputs, |
| 202 | order = "topological", |
| 203 | ), |
| 204 | ), |
| 205 | )) |
| 206 | |
| 207 | if cc_infos: |
| 208 | return cc_common.merge_cc_infos( |
| 209 | direct_cc_infos = cc_infos, |
| 210 | ) |
| 211 | return None |
| 212 | |
| 213 | def _symlink_sysroot_tree(ctx, name, target): |
| 214 | """Generate a set of symlinks to files from another target |
| 215 | |
| 216 | Args: |
| 217 | ctx (ctx): The toolchain's context object |
| 218 | name (str): The name of the sysroot directory (typically `ctx.label.name`) |
| 219 | target (Target): A target owning files to symlink |
| 220 | |
| 221 | Returns: |
| 222 | depset[File]: A depset of the generated symlink files |
| 223 | """ |
| 224 | tree_files = [] |
| 225 | for file in target.files.to_list(): |
| 226 | # Parse the path to the file relative to the workspace root so a |
| 227 | # symlink matching this path can be created within the sysroot. |
| 228 | |
| 229 | # The code blow attempts to parse any workspace names out of the |
| 230 | # path. For local targets, this code is a noop. |
| 231 | if target.label.workspace_root: |
| 232 | file_path = file.path.split(target.label.workspace_root, 1)[-1] |
| 233 | else: |
| 234 | file_path = file.path |
| 235 | |
| 236 | symlink = ctx.actions.declare_file("{}/{}".format(name, file_path.lstrip("/"))) |
| 237 | |
| 238 | ctx.actions.symlink( |
| 239 | output = symlink, |
| 240 | target_file = file, |
| 241 | ) |
| 242 | |
| 243 | tree_files.append(symlink) |
| 244 | |
| 245 | return depset(tree_files) |
| 246 | |
| 247 | def _symlink_sysroot_bin(ctx, name, directory, target): |
| 248 | """Crete a symlink to a target file. |
| 249 | |
| 250 | Args: |
| 251 | ctx (ctx): The rule's context object |
| 252 | name (str): A common name for the output directory |
| 253 | directory (str): The directory under `name` to put the file in |
| 254 | target (File): A File object to symlink to |
| 255 | |
| 256 | Returns: |
| 257 | File: A newly generated symlink file |
| 258 | """ |
| 259 | symlink = ctx.actions.declare_file("{}/{}/{}".format( |
| 260 | name, |
| 261 | directory, |
| 262 | target.basename, |
| 263 | )) |
| 264 | |
| 265 | ctx.actions.symlink( |
| 266 | output = symlink, |
| 267 | target_file = target, |
| 268 | is_executable = True, |
| 269 | ) |
| 270 | |
| 271 | return symlink |
| 272 | |
| 273 | def _generate_sysroot( |
| 274 | ctx, |
| 275 | rustc, |
| 276 | rustdoc, |
| 277 | rustc_lib, |
| 278 | cargo = None, |
| 279 | clippy = None, |
| 280 | llvm_tools = None, |
| 281 | rust_std = None, |
| 282 | rustfmt = None): |
| 283 | """Generate a rust sysroot from collection of toolchain components |
| 284 | |
| 285 | Args: |
| 286 | ctx (ctx): A context object from a `rust_toolchain` rule. |
| 287 | rustc (File): The path to a `rustc` executable. |
| 288 | rustdoc (File): The path to a `rustdoc` executable. |
| 289 | rustc_lib (Target): A collection of Files containing dependencies of `rustc`. |
| 290 | cargo (File, optional): The path to a `cargo` executable. |
| 291 | clippy (File, optional): The path to a `clippy-driver` executable. |
| 292 | llvm_tools (Target, optional): A collection of llvm tools used by `rustc`. |
| 293 | rust_std (Target, optional): A collection of Files containing Rust standard library components. |
| 294 | rustfmt (File, optional): The path to a `rustfmt` executable. |
| 295 | |
| 296 | Returns: |
| 297 | struct: A struct of generated files representing the new sysroot |
| 298 | """ |
| 299 | name = ctx.label.name |
| 300 | |
| 301 | # Define runfiles |
| 302 | direct_files = [] |
| 303 | transitive_file_sets = [] |
| 304 | |
| 305 | # Rustc |
| 306 | sysroot_rustc = _symlink_sysroot_bin(ctx, name, "bin", rustc) |
| 307 | direct_files.extend([sysroot_rustc]) |
| 308 | |
| 309 | # Rustc dependencies |
| 310 | sysroot_rustc_lib = None |
| 311 | if rustc_lib: |
| 312 | sysroot_rustc_lib = _symlink_sysroot_tree(ctx, name, rustc_lib) |
| 313 | transitive_file_sets.extend([sysroot_rustc_lib]) |
| 314 | |
| 315 | # Rustdoc |
| 316 | sysroot_rustdoc = _symlink_sysroot_bin(ctx, name, "bin", rustdoc) |
| 317 | direct_files.extend([sysroot_rustdoc]) |
| 318 | |
| 319 | # Clippy |
| 320 | sysroot_clippy = None |
| 321 | if clippy: |
| 322 | sysroot_clippy = _symlink_sysroot_bin(ctx, name, "bin", clippy) |
| 323 | direct_files.extend([sysroot_clippy]) |
| 324 | |
| 325 | # Cargo |
| 326 | sysroot_cargo = None |
| 327 | if cargo: |
| 328 | sysroot_cargo = _symlink_sysroot_bin(ctx, name, "bin", cargo) |
| 329 | direct_files.extend([sysroot_cargo]) |
| 330 | |
| 331 | # Rustfmt |
| 332 | sysroot_rustfmt = None |
| 333 | if rustfmt: |
| 334 | sysroot_rustfmt = _symlink_sysroot_bin(ctx, name, "bin", rustfmt) |
| 335 | direct_files.extend([sysroot_rustfmt]) |
| 336 | |
| 337 | # Llvm tools |
| 338 | sysroot_llvm_tools = None |
| 339 | if llvm_tools: |
| 340 | sysroot_llvm_tools = _symlink_sysroot_tree(ctx, name, llvm_tools) |
| 341 | transitive_file_sets.extend([sysroot_llvm_tools]) |
| 342 | |
| 343 | # Rust standard library |
| 344 | sysroot_rust_std = None |
| 345 | if rust_std: |
| 346 | sysroot_rust_std = _symlink_sysroot_tree(ctx, name, rust_std) |
| 347 | transitive_file_sets.extend([sysroot_rust_std]) |
| 348 | |
| 349 | # Declare a file in the root of the sysroot to make locating the sysroot easy |
| 350 | sysroot_anchor = ctx.actions.declare_file("{}/rust.sysroot".format(name)) |
| 351 | ctx.actions.write( |
| 352 | output = sysroot_anchor, |
| 353 | content = "\n".join([ |
| 354 | "cargo: {}".format(cargo), |
| 355 | "clippy: {}".format(clippy), |
| 356 | "llvm_tools: {}".format(llvm_tools), |
| 357 | "rust_std: {}".format(rust_std), |
| 358 | "rustc_lib: {}".format(rustc_lib), |
| 359 | "rustc: {}".format(rustc), |
| 360 | "rustdoc: {}".format(rustdoc), |
| 361 | "rustfmt: {}".format(rustfmt), |
| 362 | ]), |
| 363 | ) |
| 364 | |
| 365 | # Create a depset of all sysroot files (symlinks and their real paths) |
| 366 | all_files = depset(direct_files, transitive = transitive_file_sets) |
| 367 | |
| 368 | return struct( |
| 369 | all_files = all_files, |
| 370 | cargo = sysroot_cargo, |
| 371 | clippy = sysroot_clippy, |
| 372 | rust_std = sysroot_rust_std, |
| 373 | rustc = sysroot_rustc, |
| 374 | rustc_lib = sysroot_rustc_lib, |
| 375 | rustdoc = sysroot_rustdoc, |
| 376 | rustfmt = sysroot_rustfmt, |
| 377 | sysroot_anchor = sysroot_anchor, |
| 378 | ) |
| 379 | |
| 380 | def _rust_toolchain_impl(ctx): |
| 381 | """The rust_toolchain implementation |
| 382 | |
| 383 | Args: |
| 384 | ctx (ctx): The rule's context object |
| 385 | |
| 386 | Returns: |
| 387 | list: A list containing the target's toolchain Provider info |
| 388 | """ |
| 389 | compilation_mode_opts = {} |
| 390 | for k, v in ctx.attr.opt_level.items(): |
| 391 | if not k in ctx.attr.debug_info: |
| 392 | fail("Compilation mode {} is not defined in debug_info but is defined opt_level".format(k)) |
| 393 | compilation_mode_opts[k] = struct(debug_info = ctx.attr.debug_info[k], opt_level = v) |
| 394 | for k, v in ctx.attr.debug_info.items(): |
| 395 | if not k in ctx.attr.opt_level: |
| 396 | fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k)) |
| 397 | |
| 398 | if ctx.attr.target_triple and ctx.file.target_json: |
| 399 | fail("Do not specify both target_triple and target_json, either use a builtin triple or provide a custom specification file.") |
| 400 | |
| 401 | rename_first_party_crates = ctx.attr._rename_first_party_crates[BuildSettingInfo].value |
| 402 | third_party_dir = ctx.attr._third_party_dir[BuildSettingInfo].value |
| 403 | |
| 404 | if ctx.attr.rust_lib: |
| 405 | # buildifier: disable=print |
| 406 | print("`rust_toolchain.rust_lib` is deprecated. Please update {} to use `rust_toolchain.rust_std`".format( |
| 407 | ctx.label, |
| 408 | )) |
| 409 | rust_std = ctx.attr.rust_lib |
| 410 | else: |
| 411 | rust_std = ctx.attr.rust_std |
| 412 | |
| 413 | sysroot = _generate_sysroot( |
| 414 | ctx = ctx, |
| 415 | rustc = ctx.file.rustc, |
| 416 | rustdoc = ctx.file.rust_doc, |
| 417 | rustc_lib = ctx.attr.rustc_lib, |
| 418 | rust_std = rust_std, |
| 419 | rustfmt = ctx.file.rustfmt, |
| 420 | clippy = ctx.file.clippy_driver, |
| 421 | cargo = ctx.file.cargo, |
| 422 | llvm_tools = ctx.attr.llvm_tools, |
| 423 | ) |
| 424 | |
| 425 | expanded_stdlib_linkflags = [] |
| 426 | for flag in ctx.attr.stdlib_linkflags: |
| 427 | expanded_stdlib_linkflags.append( |
| 428 | ctx.expand_location( |
| 429 | flag, |
| 430 | targets = rust_std[rust_common.stdlib_info].srcs, |
| 431 | ), |
| 432 | ) |
| 433 | |
| 434 | linking_context = cc_common.create_linking_context( |
| 435 | linker_inputs = depset([ |
| 436 | cc_common.create_linker_input( |
| 437 | owner = ctx.label, |
| 438 | user_link_flags = depset(expanded_stdlib_linkflags), |
| 439 | ), |
| 440 | ]), |
| 441 | ) |
| 442 | |
| 443 | # Contains linker flags needed to link Rust standard library. |
| 444 | # These need to be added to linker command lines when the linker is not rustc |
| 445 | # (rustc does this automatically). Linker flags wrapped in an otherwise empty |
| 446 | # `CcInfo` to provide the flags in a way that doesn't duplicate them per target |
| 447 | # providing a `CcInfo`. |
| 448 | stdlib_linkflags_cc_info = CcInfo( |
| 449 | compilation_context = cc_common.create_compilation_context(), |
| 450 | linking_context = linking_context, |
| 451 | ) |
| 452 | |
| 453 | # Determine the path and short_path of the sysroot |
| 454 | sysroot_path = sysroot.sysroot_anchor.dirname |
| 455 | sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/") |
| 456 | |
| 457 | toolchain = platform_common.ToolchainInfo( |
| 458 | all_files = sysroot.all_files, |
| 459 | binary_ext = ctx.attr.binary_ext, |
| 460 | cargo = sysroot.cargo, |
| 461 | clippy_driver = sysroot.clippy, |
| 462 | compilation_mode_opts = compilation_mode_opts, |
| 463 | crosstool_files = ctx.files._cc_toolchain, |
| 464 | default_edition = ctx.attr.default_edition, |
| 465 | dylib_ext = ctx.attr.dylib_ext, |
| 466 | exec_triple = ctx.attr.exec_triple, |
| 467 | libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo(ctx, rust_std, ctx.attr.allocator_library), |
| 468 | os = ctx.attr.os, |
| 469 | rust_doc = sysroot.rustdoc, |
| 470 | rust_lib = sysroot.rust_std, # `rust_lib` is deprecated and only exists for legacy support. |
| 471 | rust_std = sysroot.rust_std, |
| 472 | rust_std_paths = depset([file.dirname for file in sysroot.rust_std.to_list()]), |
| 473 | rustc = sysroot.rustc, |
| 474 | rustc_lib = sysroot.rustc_lib, |
| 475 | rustc_srcs = ctx.attr.rustc_srcs, |
| 476 | rustfmt = sysroot.rustfmt, |
| 477 | staticlib_ext = ctx.attr.staticlib_ext, |
| 478 | stdlib_linkflags = stdlib_linkflags_cc_info, |
| 479 | sysroot = sysroot_path, |
| 480 | sysroot_short_path = sysroot_short_path, |
| 481 | target_arch = ctx.attr.target_triple.split("-")[0], |
| 482 | target_flag_value = ctx.file.target_json.path if ctx.file.target_json else ctx.attr.target_triple, |
| 483 | target_json = ctx.file.target_json, |
| 484 | target_triple = ctx.attr.target_triple, |
| 485 | |
| 486 | # Experimental and incompatible flags |
| 487 | _rename_first_party_crates = rename_first_party_crates, |
| 488 | _third_party_dir = third_party_dir, |
| 489 | ) |
| 490 | return [toolchain] |
| 491 | |
| 492 | rust_toolchain = rule( |
| 493 | implementation = _rust_toolchain_impl, |
| 494 | fragments = ["cpp"], |
| 495 | attrs = { |
| 496 | "allocator_library": attr.label( |
| 497 | doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary.", |
| 498 | ), |
| 499 | "binary_ext": attr.string( |
| 500 | doc = "The extension for binaries created from rustc.", |
| 501 | mandatory = True, |
| 502 | ), |
| 503 | "cargo": attr.label( |
| 504 | doc = "The location of the `cargo` binary. Can be a direct source or a filegroup containing one item.", |
| 505 | allow_single_file = True, |
| 506 | cfg = "exec", |
| 507 | ), |
| 508 | "clippy_driver": attr.label( |
| 509 | doc = "The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item.", |
| 510 | allow_single_file = True, |
| 511 | cfg = "exec", |
| 512 | ), |
| 513 | "debug_info": attr.string_dict( |
| 514 | doc = "Rustc debug info levels per opt level", |
| 515 | default = { |
| 516 | "dbg": "2", |
| 517 | "fastbuild": "0", |
| 518 | "opt": "0", |
| 519 | }, |
| 520 | ), |
| 521 | "default_edition": attr.string( |
| 522 | doc = "The edition to use for rust_* rules that don't specify an edition.", |
| 523 | default = rust_common.default_edition, |
| 524 | ), |
| 525 | "dylib_ext": attr.string( |
| 526 | doc = "The extension for dynamic libraries created from rustc.", |
| 527 | mandatory = True, |
| 528 | ), |
| 529 | "exec_triple": attr.string( |
| 530 | doc = ( |
| 531 | "The platform triple for the toolchains execution environment. " + |
| 532 | "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations" |
| 533 | ), |
| 534 | mandatory = True, |
| 535 | ), |
| 536 | "llvm_tools": attr.label( |
| 537 | doc = "LLVM tools that are shipped with the Rust toolchain.", |
| 538 | allow_files = True, |
| 539 | ), |
| 540 | "opt_level": attr.string_dict( |
| 541 | doc = "Rustc optimization levels.", |
| 542 | default = { |
| 543 | "dbg": "0", |
| 544 | "fastbuild": "0", |
| 545 | "opt": "3", |
| 546 | }, |
| 547 | ), |
| 548 | "os": attr.string( |
| 549 | doc = "The operating system for the current toolchain", |
| 550 | mandatory = True, |
| 551 | ), |
| 552 | "rust_doc": attr.label( |
| 553 | doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item.", |
| 554 | allow_single_file = True, |
| 555 | cfg = "exec", |
| 556 | mandatory = True, |
| 557 | ), |
| 558 | "rust_lib": attr.label( |
| 559 | doc = "**Deprecated**: Use `rust_std`", |
| 560 | ), |
| 561 | "rust_std": attr.label( |
| 562 | doc = "The Rust standard library.", |
| 563 | ), |
| 564 | "rustc": attr.label( |
| 565 | doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.", |
| 566 | allow_single_file = True, |
| 567 | cfg = "exec", |
| 568 | mandatory = True, |
| 569 | ), |
| 570 | "rustc_lib": attr.label( |
| 571 | doc = "The libraries used by rustc during compilation.", |
| 572 | cfg = "exec", |
| 573 | ), |
| 574 | "rustc_srcs": attr.label( |
| 575 | doc = "The source code of rustc.", |
| 576 | ), |
| 577 | "rustfmt": attr.label( |
| 578 | doc = "The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item.", |
| 579 | allow_single_file = True, |
| 580 | cfg = "exec", |
| 581 | ), |
| 582 | "staticlib_ext": attr.string( |
| 583 | doc = "The extension for static libraries created from rustc.", |
| 584 | mandatory = True, |
| 585 | ), |
| 586 | "stdlib_linkflags": attr.string_list( |
| 587 | doc = ( |
| 588 | "Additional linker flags to use when Rust standard library is linked by a C++ linker " + |
| 589 | "(rustc will deal with these automatically). Subject to location expansion with respect " + |
| 590 | "to the srcs of the `rust_std` attribute." |
| 591 | ), |
| 592 | mandatory = True, |
| 593 | ), |
| 594 | "target_json": attr.label( |
| 595 | doc = ("Override the target_triple with a custom target specification. " + |
| 596 | "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"), |
| 597 | allow_single_file = True, |
| 598 | ), |
| 599 | "target_triple": attr.string( |
| 600 | doc = ( |
| 601 | "The platform triple for the toolchains target environment. " + |
| 602 | "For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations" |
| 603 | ), |
| 604 | ), |
| 605 | "_cc_toolchain": attr.label( |
| 606 | default = "@bazel_tools//tools/cpp:current_cc_toolchain", |
| 607 | ), |
| 608 | "_rename_first_party_crates": attr.label( |
| 609 | default = "@rules_rust//rust/settings:rename_first_party_crates", |
| 610 | ), |
| 611 | "_third_party_dir": attr.label( |
| 612 | default = "@rules_rust//rust/settings:third_party_dir", |
| 613 | ), |
| 614 | }, |
| 615 | toolchains = [ |
| 616 | "@bazel_tools//tools/cpp:toolchain_type", |
| 617 | ], |
| 618 | incompatible_use_toolchain_transition = True, |
| 619 | doc = """Declares a Rust toolchain for use. |
| 620 | |
| 621 | This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. |
| 622 | |
| 623 | Example: |
| 624 | |
| 625 | Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This \ |
| 626 | support can be used in Bazel by defining a new toolchain definition and declaration: |
| 627 | |
| 628 | ```python |
| 629 | load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') |
| 630 | |
| 631 | rust_toolchain( |
| 632 | name = "rust_cpuX_impl", |
| 633 | rustc = "@rust_cpuX//:rustc", |
| 634 | rustc_lib = "@rust_cpuX//:rustc_lib", |
| 635 | rust_std = "@rust_cpuX//:rust_std", |
| 636 | rust_doc = "@rust_cpuX//:rustdoc", |
| 637 | binary_ext = "", |
| 638 | staticlib_ext = ".a", |
| 639 | dylib_ext = ".so", |
| 640 | stdlib_linkflags = ["-lpthread", "-ldl"], |
| 641 | os = "linux", |
| 642 | ) |
| 643 | |
| 644 | toolchain( |
| 645 | name = "rust_cpuX", |
| 646 | exec_compatible_with = [ |
| 647 | "@platforms//cpu:cpuX", |
| 648 | ], |
| 649 | target_compatible_with = [ |
| 650 | "@platforms//cpu:cpuX", |
| 651 | ], |
| 652 | toolchain = ":rust_cpuX_impl", |
| 653 | ) |
| 654 | ``` |
| 655 | |
| 656 | Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass \ |
| 657 | it to the `"--extra_toolchains"` flag for Bazel, and it will be used. |
| 658 | |
| 659 | See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository \ |
| 660 | with the actual binaries and libraries. |
| 661 | """, |
| 662 | ) |