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