fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
078c6908
0.10.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 431experimental_use_cc_common_link = ctx . attr . experimental_use_cc_common_link [ BuildSettingInfo ]. value 432if experimental_use_cc_common_link and not ctx . attr . allocator_library : 433fail ( "rust_toolchain.experimental_use_cc_common_link requires rust_toolchain.allocator_library to be set" ) 434 435if ctx . attr . rust_lib : 436# buildifier: disable=print 437( "`rust_toolchain.rust_lib` is deprecated. Please update {} to use `rust_toolchain.rust_std`" . format ( 438ctx . label , 439)) 440rust_std = ctx . attr . rust_lib 441else : 442rust_std = ctx . attr . rust_std 443 444sysroot = _generate_sysroot ( 445ctx = ctx , 446rustc = ctx . file . rustc , 447rustdoc = ctx . file . rust_doc , 448rustc_lib = ctx . attr . rustc_lib , 449rust_std = rust_std , 450rustfmt = ctx . file . rustfmt , 451clippy = ctx . file . clippy_driver , 452cargo = ctx . file . cargo , 453llvm_tools = ctx . attr . llvm_tools , 454) 455 456expanded_stdlib_linkflags = [] 457for flag in ctx . attr . stdlib_linkflags : 458expanded_stdlib_linkflags . append ( 459ctx . expand_location ( 460flag , 461targets = rust_std [ rust_common . stdlib_info ]. srcs , 462), 463) 464 465linking_context = cc_common . create_linking_context ( 466linker_inputs = depset ([ 467cc_common . create_linker_input ( 468owner = ctx . label , 469user_link_flags = depset ( expanded_stdlib_linkflags ), 470), 471]), 472) 473 474# Contains linker flags needed to link Rust standard library. 475# These need to be added to linker command lines when the linker is not rustc 476# (rustc does this automatically). Linker flags wrapped in an otherwise empty 477# `CcInfo` to provide the flags in a way that doesn't duplicate them per target 478# providing a `CcInfo`. 479stdlib_linkflags_cc_info = CcInfo ( 480compilation_context = cc_common . create_compilation_context (), 481linking_context = linking_context , 482) 483 484# Determine the path and short_path of the sysroot 485sysroot_path = sysroot . sysroot_anchor . dirname 486sysroot_short_path , _ , _ = sysroot . sysroot_anchor . short_path . rpartition ( "/" ) 487 488# Variables for make variable expansion 489make_variables = { 490"RUSTC" : sysroot . rustc . path , 491"RUSTDOC" : sysroot . rustdoc . path , 492"RUST_DEFAULT_EDITION" : ctx . attr . default_edition or "" , 493"RUST_SYSROOT" : sysroot_path , 494} 495 496if sysroot . cargo : 497make_variables . update ({ 498"CARGO" : sysroot . cargo . path , 499}) 500 501if sysroot . rustfmt : 502make_variables . update ({ 503"RUSTFMT" : sysroot . rustfmt . path , 504}) 505 506make_variable_info = platform_common . TemplateVariableInfo ( make_variables ) 507 508toolchain = platform_common . ToolchainInfo ( 509all_files = sysroot . all_files , 510binary_ext = ctx . attr . binary_ext , 511cargo = sysroot . cargo , 512clippy_driver = sysroot . clippy , 513compilation_mode_opts = compilation_mode_opts , 514crosstool_files = ctx . files . _cc_toolchain , 515default_edition = ctx . attr . default_edition , 516dylib_ext = ctx . attr . dylib_ext , 517env = ctx . attr . env , 518exec_triple = ctx . attr . exec_triple , 519libstd_and_allocator_ccinfo = _make_libstd_and_allocator_ccinfo ( ctx , rust_std , ctx . attr . allocator_library ), 520llvm_cov = ctx . file . llvm_cov , 521llvm_profdata = ctx . file . llvm_profdata , 522make_variables = make_variable_info , 523os = ctx . attr . os , 524rust_doc = sysroot . rustdoc , 525rust_lib = sysroot . rust_std , #`rust_lib` is deprecated and only exists for legacy support . 526rust_std = sysroot . rust_std , 527rust_std_paths = depset ([ file . dirname for file in sysroot . rust_std . to_list ()]), 528rustc = sysroot . rustc , 529rustc_lib = sysroot . rustc_lib , 530rustc_srcs = ctx . attr . rustc_srcs , 531rustfmt = sysroot . rustfmt , 532staticlib_ext = ctx . attr . staticlib_ext , 533stdlib_linkflags = stdlib_linkflags_cc_info , 534sysroot = sysroot_path , 535sysroot_short_path = sysroot_short_path , 536target_arch = ctx . attr . target_triple . split ( "-" )[ 0 ], 537target_flag_value = ctx . file . target_json . path if ctx . file . target_json else ctx . attr . target_triple , 538target_json = ctx . file . target_json , 539target_triple = ctx . attr . target_triple , 540 541# Experimental and incompatible flags 542_rename_first_party_crates = rename_first_party_crates , 543_third_party_dir = third_party_dir , 544_pipelined_compilation = pipelined_compilation , 545_experimental_use_cc_common_link = experimental_use_cc_common_link , 546) 547return [ 548toolchain , 549make_variable_info , 550] 551 552rust_toolchain = rule ( 553implementation = _rust_toolchain_impl , 554fragments = [ "cpp" ], 555attrs = { 556"allocator_library" : attr . label ( 557doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." , 558), 559"binary_ext" : attr . string ( 560doc = "The extension for binaries created from rustc." , 561mandatory = True , 562), 563"cargo" : attr . label ( 564doc = "The location of the `cargo` binary. Can be a direct source or a filegroup containing one item." , 565allow_single_file = True , 566cfg = "exec" , 567), 568"clippy_driver" : attr . label ( 569doc = "The location of the `clippy-driver` binary. Can be a direct source or a filegroup containing one item." , 570allow_single_file = True , 571cfg = "exec" , 572), 573"debug_info" : attr . string_dict ( 574doc = "Rustc debug info levels per opt level" , 575default = { 576"dbg" : "2" , 577"fastbuild" : "0" , 578"opt" : "0" , 579}, 580), 581"default_edition" : attr . string ( 582doc = ( 583"The edition to use for rust_* rules that don't specify an edition. " + 584"If absent, every rule is required to specify its `edition` attribute." 585), 586), 587"dylib_ext" : attr . string ( 588doc = "The extension for dynamic libraries created from rustc." , 589mandatory = True , 590), 591"env" : attr . string_dict ( 592doc = "Environment variables to set in actions." , 593), 594"exec_triple" : attr . string ( 595doc = ( 596"The platform triple for the toolchains execution environment. " + 597"For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations" 598), 599mandatory = True , 600), 601"experimental_use_cc_common_link" : attr . label ( 602default = Label ( "//rust/settings:experimental_use_cc_common_link" ), 603doc = "Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries." , 604), 605"llvm_cov" : attr . label ( 606doc = "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." , 607allow_single_file = True , 608cfg = "exec" , 609), 610"llvm_profdata" : attr . label ( 611doc = "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." , 612allow_single_file = True , 613cfg = "exec" , 614), 615"llvm_tools" : attr . label ( 616doc = "LLVM tools that are shipped with the Rust toolchain." , 617allow_files = True , 618), 619"opt_level" : attr . string_dict ( 620doc = "Rustc optimization levels." , 621default = { 622"dbg" : "0" , 623"fastbuild" : "0" , 624"opt" : "3" , 625}, 626), 627"os" : attr . string ( 628doc = "The operating system for the current toolchain" , 629mandatory = True , 630), 631"rust_doc" : attr . label ( 632doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item." , 633allow_single_file = True , 634cfg = "exec" , 635mandatory = True , 636), 637"rust_lib" : attr . label ( 638doc = "**Deprecated**: Use `rust_std`" , 639), 640"rust_std" : attr . label ( 641doc = "The Rust standard library." , 642), 643"rustc" : attr . label ( 644doc = "The location of the `rustc` binary. Can be a direct source or a filegroup containing one item." , 645allow_single_file = True , 646cfg = "exec" , 647mandatory = True , 648), 649"rustc_lib" : attr . label ( 650doc = "The libraries used by rustc during compilation." , 651cfg = "exec" , 652), 653"rustc_srcs" : attr . label ( 654doc = "The source code of rustc." , 655), 656"rustfmt" : attr . label ( 657doc = "The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item." , 658allow_single_file = True , 659cfg = "exec" , 660), 661"staticlib_ext" : attr . string ( 662doc = "The extension for static libraries created from rustc." , 663mandatory = True , 664), 665"stdlib_linkflags" : attr . string_list ( 666doc = ( 667"Additional linker flags to use when Rust standard library is linked by a C++ linker " + 668"(rustc will deal with these automatically). Subject to location expansion with respect " + 669"to the srcs of the `rust_std` attribute." 670), 671mandatory = True , 672), 673"target_json" : attr . label ( 674doc = ( "Override the target_triple with a custom target specification. " + 675"For more details see: https://doc.rust-lang.org/rustc/targets/custom.html" ), 676allow_single_file = True , 677), 678"target_triple" : attr . string ( 679doc = ( 680"The platform triple for the toolchains target environment. " + 681"For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations" 682), 683), 684"_cc_toolchain" : attr . label ( 685default = Label ( "@bazel_tools//tools/cpp:current_cc_toolchain" ), 686), 687"_pipelined_compilation" : attr . label ( 688default = "@rules_rust//rust/settings:pipelined_compilation" , 689), 690"_rename_first_party_crates" : attr . label ( 691default = Label ( "//rust/settings:rename_first_party_crates" ), 692), 693"_third_party_dir" : attr . label ( 694default = Label ( "//rust/settings:third_party_dir" ), 695), 696}, 697toolchains = [ 698"@bazel_tools//tools/cpp:toolchain_type" , 699], 700incompatible_use_toolchain_transition = True , 701doc = """Declares a Rust toolchain for use. 702 703This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform. 704 705Example: 706 707Suppose the core rust team has ported the compiler to a new target CPU, called `cpuX`. This \ 708support can be used in Bazel by defining a new toolchain definition and declaration: 709 710```python 711load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') 712 713rust_toolchain( 714name = "rust_cpuX_impl", 715rustc = "@rust_cpuX//:rustc", 716rustc_lib = "@rust_cpuX//:rustc_lib", 717rust_std = "@rust_cpuX//:rust_std", 718rust_doc = "@rust_cpuX//:rustdoc", 719binary_ext = "", 720staticlib_ext = ".a", 721dylib_ext = ".so", 722stdlib_linkflags = ["-lpthread", "-ldl"], 723os = "linux", 724) 725 726toolchain( 727name = "rust_cpuX", 728exec_compatible_with = [ 729"@platforms//cpu:cpuX", 730], 731target_compatible_with = [ 732"@platforms//cpu:cpuX", 733], 734toolchain = ":rust_cpuX_impl", 735) 736``` 737 738Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass \ 739it to the `"--extra_toolchains"` flag for Bazel, and it will be used. 740 741See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository \ 742with the actual binaries and libraries. 743""" , 744)