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