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