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