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