fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
bef8d2d4
patching
1"""Utility macros for use in rules_rust repository rules""" 2 3load ( 4"@bazel_tools//tools/build_defs/repo:utils.bzl" , 5"read_netrc" , 6"read_user_netrc" , 7"use_netrc" , 8) 9load ( "//rust:known_shas.bzl" , "FILE_KEY_TO_SHA" ) 10load ( 11"//rust/platform:triple_mappings.bzl" , 12"system_to_binary_ext" , 13"system_to_dylib_ext" , 14"system_to_staticlib_ext" , 15"system_to_stdlib_linkflags" , 16) 17load ( "//rust/private:common.bzl" , "DEFAULT_NIGHTLY_ISO_DATE" ) 18 19DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for" 20DEFAULT_STATIC_RUST_URL_TEMPLATES = [ "https://static.rust-lang.org/dist/{}.tar.xz" ] 21DEFAULT_NIGHTLY_VERSION = "nightly/{}" . format ( DEFAULT_NIGHTLY_ISO_DATE ) 22DEFAULT_EXTRA_TARGET_TRIPLES = [ "wasm32-unknown-unknown" , "wasm32-wasi" ] 23 24TINYJSON_KWARGS = dict ( 25name = "rules_rust_tinyjson" , 26sha256 = "9ab95735ea2c8fd51154d01e39cf13912a78071c2d89abc49a7ef102a7dd725a" , 27url = "https://static.crates.io/crates/tinyjson/tinyjson-2.5.1.crate" , 28strip_prefix = "tinyjson-2.5.1" , 29type = "tar.gz" , 30build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel" , 31) 32 33_build_file_for_compiler_template = """\ 34filegroup( 35name = "rustc", 36srcs = ["bin/rustc{binary_ext}"], 37visibility = ["//visibility:public"], 38) 39 40filegroup( 41name = "rustc_lib", 42srcs = glob( 43[ 44"bin/*{dylib_ext}", 45"lib/*{dylib_ext}*", 46"lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}", 47"lib/rustlib/{target_triple}/bin/rust-lld{binary_ext}", 48"lib/rustlib/{target_triple}/lib/*{dylib_ext}*", 49], 50allow_empty = True, 51), 52visibility = ["//visibility:public"], 53) 54 55filegroup( 56name = "rustdoc", 57srcs = ["bin/rustdoc{binary_ext}"], 58visibility = ["//visibility:public"], 59) 60""" 61 62def BUILD_for_compiler ( target_triple ): 63"""Emits a BUILD file the compiler archive. 64 65Args: 66target_triple (str): The triple of the target platform 67 68Returns: 69str: The contents of a BUILD file 70""" 71return _build_file_for_compiler_template . format ( 72binary_ext = system_to_binary_ext ( target_triple . system ), 73staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 74dylib_ext = system_to_dylib_ext ( target_triple . system ), 75target_triple = target_triple . str , 76) 77 78_build_file_for_cargo_template = """\ 79filegroup( 80name = "cargo", 81srcs = ["bin/cargo{binary_ext}"], 82visibility = ["//visibility:public"], 83)""" 84 85def BUILD_for_cargo ( target_triple ): 86"""Emits a BUILD file the cargo archive. 87 88Args: 89target_triple (str): The triple of the target platform 90 91Returns: 92str: The contents of a BUILD file 93""" 94return _build_file_for_cargo_template . format ( 95binary_ext = system_to_binary_ext ( target_triple . system ), 96) 97 98_build_file_for_rustfmt_template = """\ 99filegroup( 100name = "rustfmt_bin", 101srcs = ["bin/rustfmt{binary_ext}"], 102visibility = ["//visibility:public"], 103) 104 105sh_binary( 106name = "rustfmt", 107srcs = [":rustfmt_bin"], 108visibility = ["//visibility:public"], 109) 110""" 111 112def BUILD_for_rustfmt ( target_triple ): 113"""Emits a BUILD file the rustfmt archive. 114 115Args: 116target_triple (str): The triple of the target platform 117 118Returns: 119str: The contents of a BUILD file 120""" 121return _build_file_for_rustfmt_template . format ( 122binary_ext = system_to_binary_ext ( target_triple . system ), 123) 124 125_build_file_for_rust_analyzer_proc_macro_srv = """\ 126filegroup( 127name = "rust_analyzer_proc_macro_srv", 128srcs = ["libexec/rust-analyzer-proc-macro-srv{binary_ext}"], 129visibility = ["//visibility:public"], 130) 131""" 132 133def BUILD_for_rust_analyzer_proc_macro_srv ( exec_triple ): 134"""Emits a BUILD file the rust_analyzer_proc_macro_srv archive. 135 136Args: 137exec_triple (str): The triple of the exec platform 138Returns: 139str: The contents of a BUILD file 140""" 141return _build_file_for_rust_analyzer_proc_macro_srv . format ( 142binary_ext = system_to_binary_ext ( exec_triple . system ), 143) 144 145_build_file_for_clippy_template = """\ 146filegroup( 147name = "clippy_driver_bin", 148srcs = ["bin/clippy-driver{binary_ext}"], 149visibility = ["//visibility:public"], 150) 151filegroup( 152name = "cargo_clippy_bin", 153srcs = ["bin/cargo-clippy{binary_ext}"], 154visibility = ["//visibility:public"], 155) 156""" 157 158def BUILD_for_clippy ( target_triple ): 159"""Emits a BUILD file the clippy archive. 160 161Args: 162target_triple (str): The triple of the target platform 163 164Returns: 165str: The contents of a BUILD file 166""" 167return _build_file_for_clippy_template . format ( 168binary_ext = system_to_binary_ext ( target_triple . system ), 169) 170 171_build_file_for_llvm_tools = """\ 172filegroup( 173name = "llvm_cov_bin", 174srcs = ["lib/rustlib/{target_triple}/bin/llvm-cov{binary_ext}"], 175visibility = ["//visibility:public"], 176) 177 178filegroup( 179name = "llvm_profdata_bin", 180srcs = ["lib/rustlib/{target_triple}/bin/llvm-profdata{binary_ext}"], 181visibility = ["//visibility:public"], 182) 183""" 184 185def BUILD_for_llvm_tools ( target_triple ): 186"""Emits a BUILD file the llvm-tools binaries. 187 188Args: 189target_triple (struct): The triple of the target platform 190 191Returns: 192str: The contents of a BUILD file 193""" 194return _build_file_for_llvm_tools . format ( 195binary_ext = system_to_binary_ext ( target_triple . system ), 196target_triple = target_triple . str , 197) 198 199_build_file_for_stdlib_template = """\ 200load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup") 201 202rust_stdlib_filegroup( 203name = "rust_std-{target_triple}", 204srcs = glob( 205[ 206"lib/rustlib/{target_triple}/lib/*.rlib", 207"lib/rustlib/{target_triple}/lib/*{dylib_ext}*", 208"lib/rustlib/{target_triple}/lib/*{staticlib_ext}", 209"lib/rustlib/{target_triple}/lib/self-contained/**", 210], 211# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245 212allow_empty = True, 213), 214visibility = ["//visibility:public"], 215) 216 217# For legacy support 218alias( 219name = "rust_lib-{target_triple}", 220actual = "rust_std-{target_triple}", 221visibility = ["//visibility:public"], 222) 223""" 224 225def BUILD_for_stdlib ( target_triple ): 226"""Emits a BUILD file the stdlib archive. 227 228Args: 229target_triple (triple): The triple of the target platform 230 231Returns: 232str: The contents of a BUILD file 233""" 234return _build_file_for_stdlib_template . format ( 235binary_ext = system_to_binary_ext ( target_triple . system ), 236staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 237dylib_ext = system_to_dylib_ext ( target_triple . system ), 238target_triple = target_triple . str , 239) 240 241_build_file_for_rust_toolchain_template = """\ 242load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") 243 244rust_toolchain( 245name = "{toolchain_name}", 246rust_doc = "//:rustdoc", 247rust_std = "//:rust_std-{target_triple}", 248rustc = "//:rustc", 249rustfmt = {rustfmt_label}, 250cargo = "//:cargo", 251clippy_driver = "//:clippy_driver_bin", 252cargo_clippy = "//:cargo_clippy_bin", 253llvm_cov = {llvm_cov_label}, 254llvm_profdata = {llvm_profdata_label}, 255rustc_lib = "//:rustc_lib", 256allocator_library = {allocator_library}, 257global_allocator_library = {global_allocator_library}, 258binary_ext = "{binary_ext}", 259staticlib_ext = "{staticlib_ext}", 260dylib_ext = "{dylib_ext}", 261stdlib_linkflags = [{stdlib_linkflags}], 262default_edition = "{default_edition}", 263exec_triple = "{exec_triple}", 264target_triple = "{target_triple}", 265visibility = ["//visibility:public"], 266extra_rustc_flags = {extra_rustc_flags}, 267extra_exec_rustc_flags = {extra_exec_rustc_flags}, 268opt_level = {opt_level}, 269) 270""" 271 272def BUILD_for_rust_toolchain ( 273name , 274exec_triple , 275target_triple , 276allocator_library , 277global_allocator_library , 278default_edition , 279include_rustfmt , 280include_llvm_tools , 281stdlib_linkflags = None , 282extra_rustc_flags = None , 283extra_exec_rustc_flags = None , 284opt_level = None ): 285"""Emits a toolchain declaration to match an existing compiler and stdlib. 286 287Args: 288name (str): The name of the toolchain declaration 289exec_triple (triple): The rust-style target that this compiler runs on 290target_triple (triple): The rust-style target triple of the tool 291allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 292global_allocator_library (str, optional): Target that provides allocator functions when a global allocator is used with cc_common_link. 293This target is only used in the target configuration; exec builds still use the symbols provided 294by the `allocator_library` target. 295default_edition (str): Default Rust edition. 296include_rustfmt (bool): Whether rustfmt is present in the toolchain. 297include_llvm_tools (bool): Whether llvm-tools are present in the toolchain. 298stdlib_linkflags (list, optional): Overriden flags needed for linking to rust 299stdlib, akin to BAZEL_LINKLIBS. Defaults to 300None. 301extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration. 302extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. 303opt_level (dict, optional): Optimization level config for this toolchain. 304 305Returns: 306str: A rendered template of a `rust_toolchain` declaration 307""" 308if stdlib_linkflags == None : 309stdlib_linkflags = ", " . join ([ '"%s"' % x for x in system_to_stdlib_linkflags ( target_triple . system )]) 310 311rustfmt_label = "None" 312if include_rustfmt : 313rustfmt_label = "\"//:rustfmt_bin\"" 314llvm_cov_label = "None" 315llvm_profdata_label = "None" 316if include_llvm_tools : 317llvm_cov_label = "\"//:llvm_cov_bin\"" 318llvm_profdata_label = "\"//:llvm_profdata_bin\"" 319allocator_library_label = "None" 320if allocator_library : 321allocator_library_label = "\"{allocator_library}\"" . format ( allocator_library = allocator_library ) 322global_allocator_library_label = "None" 323if global_allocator_library : 324global_allocator_library_label = "\"{global_allocator_library}\"" . format ( global_allocator_library = global_allocator_library ) 325 326return _build_file_for_rust_toolchain_template . format ( 327toolchain_name = name , 328binary_ext = system_to_binary_ext ( target_triple . system ), 329staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 330dylib_ext = system_to_dylib_ext ( target_triple . system ), 331allocator_library = allocator_library_label , 332global_allocator_library = global_allocator_library_label , 333stdlib_linkflags = stdlib_linkflags , 334default_edition = default_edition , 335exec_triple = exec_triple . str , 336target_triple = target_triple . str , 337rustfmt_label = rustfmt_label , 338llvm_cov_label = llvm_cov_label , 339llvm_profdata_label = llvm_profdata_label , 340extra_rustc_flags = extra_rustc_flags , 341extra_exec_rustc_flags = extra_exec_rustc_flags , 342opt_level = opt_level , 343) 344 345_build_file_for_toolchain_template = """\ 346toolchain( 347name = "{name}", 348exec_compatible_with = {exec_constraint_sets_serialized}, 349target_compatible_with = {target_constraint_sets_serialized}, 350toolchain = "{toolchain}", 351toolchain_type = "{toolchain_type}", 352{target_settings} 353) 354""" 355 356def BUILD_for_toolchain ( 357name , 358toolchain , 359toolchain_type , 360target_settings , 361target_compatible_with , 362exec_compatible_with ): 363target_settings_value = "target_settings = {}," . format ( json . encode ( target_settings )) if target_settings else "# target_settings = []" 364 365return _build_file_for_toolchain_template . format ( 366name = name , 367exec_constraint_sets_serialized = json . encode ( exec_compatible_with ), 368target_constraint_sets_serialized = json . encode ( target_compatible_with ), 369toolchain = toolchain , 370toolchain_type = toolchain_type , 371target_settings = target_settings_value , 372) 373 374def load_rustfmt ( ctx , target_triple , version , iso_date ): 375"""Loads a rustfmt binary and yields corresponding BUILD for it 376 377Args: 378ctx (repository_ctx): The repository rule's context object. 379target_triple (struct): The platform triple to download rustfmt for. 380version (str): The version or channel of rustfmt. 381iso_date (str): The date of the tool (or None, if the version is a specific version). 382 383Returns: 384Tuple[str, Dict[str, str]]: The BUILD file contents for this rustfmt binary and sha256 of it's artifact. 385""" 386 387sha256 = load_arbitrary_tool ( 388ctx , 389iso_date = iso_date , 390target_triple = target_triple , 391tool_name = "rustfmt" , 392tool_subdirectories = [ "rustfmt-preview" ], 393version = version , 394) 395 396return BUILD_for_rustfmt ( target_triple ), sha256 397 398def load_rust_compiler ( ctx , iso_date , target_triple , version ): 399 """Loads a rust compiler and yields corresponding BUILD for it 400 401 Args: 402 ctx (repository_ctx): A repository_ctx. 403 iso_date (str): The date of the tool (or None, if the version is a specific version). 404 target_triple (struct): The Rust-style target that this compiler runs on. 405 version (str): The version of the tool among \"nightly\", \"beta\", or an exact version . 406 407Returns : 408Tuple [ str , Dict [ str , str ]]: The BUILD file contents for this compiler and compiler library 409and sha256 of it 's artifact . 410""" 411 412sha256 = load_arbitrary_tool( 413ctx, 414iso_date = iso_date, 415target_triple = target_triple, 416tool_name = "rustc", 417tool_subdirectories = ["rustc"], 418version = version, 419) 420 421return BUILD_for_compiler(target_triple), sha256 422 423def load_clippy(ctx, iso_date, target_triple, version): 424""" Loads Clippy and yields corresponding BUILD for it 425 426Args : 427ctx ( repository_ctx ): A repository_ctx . 428iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 429target_triple ( struct ): The Rust - style target that this compiler runs on . 430version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 431 432Returns : 433Tuple [ str , str ]: The BUILD file contents for Clippy and the sha256 of it 's artifact 434""" 435sha256 = load_arbitrary_tool( 436ctx, 437iso_date = iso_date, 438target_triple = target_triple, 439tool_name = "clippy", 440tool_subdirectories = ["clippy-preview"], 441version = version, 442) 443 444return BUILD_for_clippy(target_triple), sha256 445 446def load_cargo(ctx, iso_date, target_triple, version): 447""" Loads Cargo and yields corresponding BUILD for it 448 449Args : 450ctx ( repository_ctx ): A repository_ctx . 451iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 452target_triple ( struct ): The Rust - style target that this compiler runs on . 453version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 454 455Returns : 456Tuple [ str , Dict [ str , str ]]: The BUILD file contents for Cargo and the sha256 of its artifact . 457""" 458 459sha256 = load_arbitrary_tool( 460ctx, 461iso_date = iso_date, 462target_triple = target_triple, 463tool_name = "cargo", 464tool_subdirectories = ["cargo"], 465version = version, 466) 467 468return BUILD_for_cargo(target_triple), sha256 469 470def includes_rust_analyzer_proc_macro_srv(version, iso_date): 471""" Determine whether or not the rust_analyzer_proc_macro_srv binary in available in the given version of Rust . 472 473Args : 474version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 475iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 476 477Returns : 478bool : Whether or not the binary is expected to be included 479""" 480 481if version == "nightly": 482return iso_date >= "2022-09-21" 483elif version == "beta": 484return False 485elif version >= "1.64.0": 486return True 487 488return False 489 490def load_rust_src(ctx, iso_date, version, sha256 = None): 491""" Loads the rust source code . Used by the rust - analyzer rust - project . json generator . 492 493Args : 494ctx ( ctx ): A repository_ctx . 495version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 496iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 497sha256 ( str ): The sha256 value for the `rust-src` artifact 498 499Returns : 500Dict [ str , str ]: A mapping of the artifact name to sha256 501""" 502tool_suburl = produce_tool_suburl("rust-src", None, version, iso_date) 503url = ctx.attr.urls[0].format(tool_suburl) 504 505tool_path = produce_tool_path("rust-src", version, None) 506archive_path = tool_path + _get_tool_extension(getattr(ctx.attr, "urls", None)) 507 508is_reproducible = True 509if sha256 == None: 510sha256s = getattr(ctx.attr, "sha256s", {}) 511sha256 = sha256s.get(archive_path, None) or FILE_KEY_TO_SHA.get(archive_path, None) 512if not sha256: 513sha256 = "" 514is_reproducible = False 515 516result = ctx.download_and_extract( 517url, 518output = "lib/rustlib/src", 519sha256 = sha256, 520auth = _make_auth_dict(ctx, [url]), 521stripPrefix = "{}/rust-src/lib/rustlib/src/rust".format(tool_path), 522) 523ctx.file( 524"lib/rustlib/src/BUILD.bazel", 525""" \ 526filegroup ( 527name = "rustc_srcs" , 528srcs = glob ([ "**/*" ]), 529visibility = [ "//visibility:public" ], 530) """, 531) 532 533if is_reproducible: 534return {} 535 536return {archive_path: result.sha256} 537 538_build_file_for_rust_analyzer_toolchain_template = """ \ 539load ( "@rules_rust//rust:toolchain.bzl" , "rust_analyzer_toolchain" ) 540 541rust_analyzer_toolchain ( 542name = "{name}" , 543proc_macro_srv = { proc_macro_srv }, 544rustc = "{rustc}" , 545rustc_srcs = "//lib/rustlib/src:rustc_srcs" , 546visibility = [ "//visibility:public" ], 547) 548""" 549 550def BUILD_for_rust_analyzer_toolchain(name, rustc, proc_macro_srv): 551return _build_file_for_rust_analyzer_toolchain_template.format( 552name = name, 553rustc = rustc, 554proc_macro_srv = repr(proc_macro_srv), 555) 556 557_build_file_for_rustfmt_toolchain_template = """ \ 558load ( "@rules_rust//rust:toolchain.bzl" , "rustfmt_toolchain" ) 559 560rustfmt_toolchain ( 561name = "{name}" , 562rustfmt = "{rustfmt}" , 563rustc = "{rustc}" , 564rustc_lib = "{rustc_lib}" , 565visibility = [ "//visibility:public" ], 566) 567""" 568 569def BUILD_for_rustfmt_toolchain(name, rustfmt, rustc, rustc_lib): 570return _build_file_for_rustfmt_toolchain_template.format( 571name = name, 572rustfmt = rustfmt, 573rustc = rustc, 574rustc_lib = rustc_lib, 575) 576 577def load_rust_stdlib(ctx, target_triple, version, iso_date = None): 578""" Loads a rust standard library and yields corresponding BUILD for it 579 580Args : 581ctx ( repository_ctx ): A repository_ctx . 582target_triple ( struct ): The rust - style target triple of the tool 583version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 584iso_date ( str ): The iso_date to use with \"nightly\" or \"beta\"versions . 585 586Returns : 587Tuple [ str , Dict [ str , str ]]: The BUILD file contents for this stdlib and the sha256 of the artifact . 588""" 589 590sha256 = load_arbitrary_tool( 591ctx, 592iso_date = iso_date, 593target_triple = target_triple, 594tool_name = "rust-std", 595tool_subdirectories = ["rust-std-{}".format(target_triple.str)], 596version = version, 597) 598 599return BUILD_for_stdlib(target_triple), sha256 600 601def load_rustc_dev_nightly(ctx, target_triple, version, iso_date = None): 602""" Loads the nightly rustc dev component 603 604Args : 605ctx : A repository_ctx . 606target_triple : The rust - style target triple of the tool 607version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 608iso_date ( str ): The iso_date to use with \"nightly\" or \"beta\"versions . 609 610Returns : 611Dict [ str , str ]: The sha256 value of the rustc - dev artifact . 612""" 613 614subdir_name = "rustc-dev" 615if iso_date and iso_date < "2020-12-24": 616subdir_name = "rustc-dev-{}".format(target_triple) 617 618sha256 = load_arbitrary_tool( 619ctx, 620iso_date = iso_date, 621target_triple = target_triple, 622tool_name = "rustc-dev", 623tool_subdirectories = [subdir_name], 624version = version, 625) 626 627return sha256 628 629def load_llvm_tools(ctx, target_triple, version, iso_date = None): 630""" Loads the llvm tools 631 632Args : 633ctx ( repository_ctx ): A repository_ctx . 634target_triple ( struct ): The rust - style target triple of the tool 635version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 636iso_date ( str ): The iso_date to use with \"nightly\" or \"beta\"versions . 637 638Returns : 639Tuple [ str , Dict [ str , str ]]: The BUILD . bazel content and sha256 value of the llvm tools artifact . 640""" 641sha256 = load_arbitrary_tool( 642ctx, 643iso_date = iso_date, 644target_triple = target_triple, 645tool_name = "llvm-tools", 646tool_subdirectories = ["llvm-tools-preview"], 647version = version, 648) 649 650return BUILD_for_llvm_tools(target_triple), sha256 651 652def check_version_valid(version, iso_date, param_prefix = ""): 653""" Verifies that the provided rust version and iso_date make sense . 654 655Args : 656version ( str ): The rustc version 657iso_date ( str ): The rustc nightly version 's iso date 658param_prefix ( str , optional ): The name of the tool who 's version is being checked . 659""" 660 661if not version and iso_date: 662fail("{param_prefix}iso_date must be paired with a {param_prefix}version".format(param_prefix = param_prefix)) 663 664if version in ("beta", "nightly") and not iso_date: 665fail("{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'".format(param_prefix = param_prefix)) 666 667def produce_tool_suburl(tool_name, target_triple, version, iso_date = None): 668""" Produces a fully qualified Rust tool name for URL 669 670Args : 671tool_name ( str ): The name of the tool per `static.rust-lang.org` . 672target_triple ( struct ): The rust - style target triple of the tool . 673version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 674iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 675 676Returns : 677str : The fully qualified url path for the specified tool . 678""" 679path = produce_tool_path(tool_name, version, target_triple) 680return iso_date + "/" + path if (iso_date and version in ("beta", "nightly")) else path 681 682def produce_tool_path(tool_name, version, target_triple = None): 683""" Produces a qualified Rust tool name 684 685Args : 686tool_name ( str ): The name of the tool per static . rust - lang . org 687version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 688target_triple ( struct , optional ): The rust - style target triple of the tool 689 690Returns : 691str : The qualified path for the specified tool . 692""" 693if not tool_name: 694fail("No tool name was provided") 695if not version: 696fail("No tool version was provided") 697 698# Not all tools require a triple. E.g. `rustc_src` (Rust source files for rust-analyzer). 699platform_triple = None 700if target_triple: 701platform_triple = target_triple.str 702 703return "-".join([e for e in [tool_name, version, platform_triple] if e]) 704 705def lookup_tool_sha256( 706repository_ctx, 707tool_name, 708target_triple, 709version, 710iso_date): 711""" Looks up the sha256 hash of a specific tool archive . 712 713The lookup order is : 714 7151. The sha256s dict in the context attributes ; 7162. The list of sha256 hashes populated in `//rust:known_shas.bzl` ; 717 718Args : 719repository_ctx ( repository_ctx ): A repository_ctx ( no attrs required ). 720tool_name ( str ): The name of the given tool per the archive naming . 721target_triple ( struct ): The rust - style target triple of the tool . 722version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 723iso_date ( str ): The date of the tool ( ignored if the version is a specific version ). 724 725Returns : 726str : The sha256 of the tool archive , or an empty string if the hash could not be found . 727""" 728tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date) 729urls = getattr(repository_ctx.attr, "urls", None) 730archive_path = tool_suburl + _get_tool_extension(urls) 731sha256s = getattr(repository_ctx.attr, "sha256s", dict()) 732 733from_attr = sha256s.get(archive_path, None) 734if from_attr: 735return archive_path, from_attr 736 737from_builtin = FILE_KEY_TO_SHA.get(archive_path, None) 738if from_builtin: 739return archive_path, from_builtin 740 741return archive_path, "" 742 743def load_arbitrary_tool( 744ctx, 745tool_name, 746tool_subdirectories, 747version, 748iso_date, 749target_triple, 750sha256 = None): 751""" Loads a Rust tool , downloads , and extracts into the common workspace . 752 753This function sources the tool from the Rust - lang static file server . The index is available at : 754- https : // static . rust - lang . org / dist / channel - rust - stable . toml 755- https : // static . rust - lang . org / dist / channel - rust - beta . toml 756- https : // static . rust - lang . org / dist / channel - rust - nightly . toml 757 758Args : 759ctx ( repository_ctx ): A repository_ctx ( no attrs required ). 760tool_name ( str ): The name of the given tool per the archive naming . 761tool_subdirectories ( str ): The subdirectories of the tool files ( at a level below the root directory of 762the archive ). The root directory of the archive is expected to match 763 $TOOL_NAME - $VERSION - $TARGET_TRIPLE . 764Example : 765tool_name 766| version 767| | target_triple 768v v v 769rust - 1.39 . 0 - x86_64 - unknown - linux - gnu / clippy - preview 770... / rustc 771... / etc 772tool_subdirectories = [ "clippy-preview" , "rustc" ] 773version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 774iso_date ( str ): The date of the tool ( ignored if the version is a specific version ). 775target_triple ( struct ): The rust - style target triple of the tool . 776sha256 ( str , optional ): The expected hash of hash of the Rust tool . Defaults to "" . 777 778Returns : 779Dict [ str , str ]: A mapping of the tool name to it 's sha256 value if the requested tool does not have 780enough information in the repository_ctx to be reproducible . 781""" 782check_version_valid(version, iso_date, param_prefix = tool_name + "_") 783 784# View the indices mentioned in the docstring to find the tool_suburl for a given 785# tool. 786tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date) 787urls = [] 788 789for url in getattr(ctx.attr, "urls", DEFAULT_STATIC_RUST_URL_TEMPLATES): 790new_url = url.format(tool_suburl) 791if new_url not in urls: 792urls.append(new_url) 793 794tool_path = produce_tool_path(tool_name, version, target_triple) 795 796archive_path, ctx_sha256 = lookup_tool_sha256(ctx, tool_name, target_triple, version, iso_date) 797 798is_reproducible = True 799if sha256 == None: 800sha256 = ctx_sha256 801is_reproducible = bool(ctx_sha256) 802 803for subdirectory in tool_subdirectories: 804# As long as the sha256 value is consistent accross calls here the 805# cost of downloading an artifact is negated as by Bazel's caching. 806result = ctx.download_and_extract( 807urls, 808sha256 = sha256, 809auth = _make_auth_dict(ctx, urls), 810stripPrefix = "{}/{}".format(tool_path, subdirectory), 811) 812 813# In the event no sha256 was provided, set it to the value of the first 814# downloaded item so subsequent downloads use a cached artifact. 815if not sha256: 816sha256 = result.sha256 817 818# If the artifact is reproducibly downloadable then return an 819# empty dict to inform consumers no attributes require updating. 820if is_reproducible: 821return {} 822 823return {archive_path: sha256} 824 825# The function is copied from the main branch of bazel_tools. 826# It should become available there from version 7.1.0, 827# We should remove this function when we upgrade minimum supported 828# version to 7.1.0. 829# https://github.com/bazelbuild/bazel/blob/d37762b494a4e122d46a5a71e3a8cc77fa15aa25/tools/build_defs/repo/utils.bzl#L424-L446 830def _get_auth(ctx, urls): 831""" Utility function to obtain the correct auth dict for a list of urls from . netrc file . 832 833Support optional netrc and auth_patterns attributes if available . 834 835Args : 836ctx : The repository context of the repository rule calling this utility 837function . 838urls : the list of urls to read 839 840Returns : 841the auth dict which can be passed to repository_ctx . download 842""" 843if hasattr(ctx.attr, "netrc") and ctx.attr.netrc: 844netrc = read_netrc(ctx, ctx.attr.netrc) 845elif "NETRC" in ctx.os.environ: 846netrc = read_netrc(ctx, ctx.os.environ["NETRC"]) 847else: 848netrc = read_user_netrc(ctx) 849auth_patterns = {} 850if hasattr(ctx.attr, "auth_patterns") and ctx.attr.auth_patterns: 851auth_patterns = ctx.attr.auth_patterns 852return use_netrc(netrc, urls, auth_patterns) 853 854def _make_auth_dict(ctx, urls): 855auth = getattr(ctx.attr, "auth", {}) 856if not auth: 857return _get_auth(ctx, urls) 858ret = {} 859for url in urls: 860ret[url] = auth 861return ret 862 863def _get_tool_extension(urls = None): 864if urls == None: 865urls = DEFAULT_STATIC_RUST_URL_TEMPLATES 866if urls[0][-7:] == ".tar.gz": 867return ".tar.gz" 868elif urls[0][-7:] == ".tar.xz": 869return ".tar.xz" 870else: 871return "" 872 873def select_rust_version(versions): 874""" Select the highest priorty version for a list of Rust versions 875 876Priority order : `stable > nightly > beta` 877 878Note that duplicate channels are unexpected in `versions` . 879 880Args : 881versions ( list ): A list of Rust versions . E . g . [ `1.66.0` , `nightly/2022-12-15` ] 882 883Returns : 884str : The highest ranking value from `versions` 885""" 886if not versions: 887fail("No versions were provided") 888 889current = versions[0] 890 891for ver in versions: 892if ver.startswith("beta"): 893if current[0].isdigit() or current.startswith("nightly"): 894continue 895if current.startswith("beta") and ver > current: 896current = ver 897continue 898 899current = ver 900elif ver.startswith("nightly"): 901if current[0].isdigit(): 902continue 903if current.startswith("nightly") and ver > current: 904current = ver 905continue 906 907current = ver 908 909else: 910current = ver 911 912return current 913 914_build_file_for_toolchain_hub_template = """ 915toolchain ( 916name = "{name}" , 917exec_compatible_with = { exec_constraint_sets_serialized }, 918target_compatible_with = { target_constraint_sets_serialized }, 919toolchain = "{toolchain}" , 920toolchain_type = "{toolchain_type}" , 921visibility = [ "//visibility:public" ], 922) 923""" 924 925def BUILD_for_toolchain_hub( 926 toolchain_names, 927 toolchain_labels, 928 toolchain_types, 929 target_compatible_with, 930 exec_compatible_with): 931 return "\n ". join ([ _build_file_for_toolchain_hub_template . format ( 932name = toolchain_name , 933exec_constraint_sets_serialized = json . encode ( exec_compatible_with [ toolchain_name ]), 934target_constraint_sets_serialized = json . encode ( target_compatible_with [ toolchain_name ]), 935toolchain = toolchain_labels [ toolchain_name ], 936toolchain_type = toolchain_types [ toolchain_name ], 937) for toolchain_name in toolchain_names ]) 938 939def _toolchain_repository_hub_impl ( repository_ctx ): 940repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 941repository_ctx . name , 942)) 943 944repository_ctx . file ( "BUILD.bazel" , BUILD_for_toolchain_hub ( 945toolchain_names = repository_ctx . attr . toolchain_names , 946toolchain_labels = repository_ctx . attr . toolchain_labels , 947toolchain_types = repository_ctx . attr . toolchain_types , 948target_compatible_with = repository_ctx . attr . target_compatible_with , 949exec_compatible_with = repository_ctx . attr . exec_compatible_with , 950)) 951 952toolchain_repository_hub = repository_rule ( 953doc = ( 954"Generates a toolchain-bearing repository that declares a set of other toolchains from other " + 955"repositories. This exists to allow registering a set of toolchains in one go with the `:all` target." 956), 957attrs = { 958"exec_compatible_with" : attr . string_list_dict ( 959doc = "A list of constraints for the execution platform for this toolchain, keyed by toolchain name." , 960mandatory = True , 961), 962"target_compatible_with" : attr . string_list_dict ( 963doc = "A list of constraints for the target platform for this toolchain, keyed by toolchain name." , 964mandatory = True , 965), 966"toolchain_labels" : attr . string_dict ( 967doc = "The name of the toolchain implementation target, keyed by toolchain name." , 968mandatory = True , 969), 970"toolchain_names" : attr . string_list ( 971mandatory = True , 972), 973"toolchain_types" : attr . string_dict ( 974doc = "The toolchain type of the toolchain to declare, keyed by toolchain name." , 975mandatory = True , 976), 977}, 978implementation = _toolchain_repository_hub_impl , 979)