fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
2fc02f03
rust-analyzer
1"""Utility macros for use in rules_rust repository rules""" 2 3load ( "//rust:known_shas.bzl" , "FILE_KEY_TO_SHA" ) 4load ( 5"//rust/platform:triple_mappings.bzl" , 6"system_to_binary_ext" , 7"system_to_dylib_ext" , 8"system_to_staticlib_ext" , 9"system_to_stdlib_linkflags" , 10) 11 12DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for" 13DEFAULT_STATIC_RUST_URL_TEMPLATES = [ "https://static.rust-lang.org/dist/{}.tar.gz" ] 14 15_build_file_for_compiler_template = """\ 16filegroup( 17name = "rustc", 18srcs = ["bin/rustc{binary_ext}"], 19visibility = ["//visibility:public"], 20) 21 22filegroup( 23name = "rustc_lib", 24srcs = glob( 25[ 26"bin/*{dylib_ext}", 27"lib/*{dylib_ext}", 28"lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}", 29"lib/rustlib/{target_triple}/bin/rust-lld{binary_ext}", 30"lib/rustlib/{target_triple}/lib/*{dylib_ext}", 31], 32allow_empty = True, 33), 34visibility = ["//visibility:public"], 35) 36 37filegroup( 38name = "rustdoc", 39srcs = ["bin/rustdoc{binary_ext}"], 40visibility = ["//visibility:public"], 41) 42""" 43 44def BUILD_for_compiler ( target_triple ): 45"""Emits a BUILD file the compiler archive. 46 47Args: 48target_triple (str): The triple of the target platform 49 50Returns: 51str: The contents of a BUILD file 52""" 53return _build_file_for_compiler_template . format ( 54binary_ext = system_to_binary_ext ( target_triple . system ), 55staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 56dylib_ext = system_to_dylib_ext ( target_triple . system ), 57target_triple = target_triple . str , 58) 59 60_build_file_for_cargo_template = """\ 61filegroup( 62name = "cargo", 63srcs = ["bin/cargo{binary_ext}"], 64visibility = ["//visibility:public"], 65)""" 66 67def BUILD_for_cargo ( target_triple ): 68"""Emits a BUILD file the cargo archive. 69 70Args: 71target_triple (str): The triple of the target platform 72 73Returns: 74str: The contents of a BUILD file 75""" 76return _build_file_for_cargo_template . format ( 77binary_ext = system_to_binary_ext ( target_triple . system ), 78) 79 80_build_file_for_rustfmt_template = """\ 81filegroup( 82name = "rustfmt_bin", 83srcs = ["bin/rustfmt{binary_ext}"], 84visibility = ["//visibility:public"], 85) 86 87sh_binary( 88name = "rustfmt", 89srcs = [":rustfmt_bin"], 90visibility = ["//visibility:public"], 91) 92""" 93 94def BUILD_for_rustfmt ( target_triple ): 95"""Emits a BUILD file the rustfmt archive. 96 97Args: 98target_triple (str): The triple of the target platform 99 100Returns: 101str: The contents of a BUILD file 102""" 103return _build_file_for_rustfmt_template . format ( 104binary_ext = system_to_binary_ext ( target_triple . system ), 105) 106 107_build_file_for_clippy_template = """\ 108filegroup( 109name = "clippy_driver_bin", 110srcs = ["bin/clippy-driver{binary_ext}"], 111visibility = ["//visibility:public"], 112) 113""" 114 115_build_file_for_rust_analyzer_proc_macro_srv = """\ 116filegroup( 117name = "rust_analyzer_proc_macro_srv", 118srcs = ["libexec/rust-analyzer-proc-macro-srv{binary_ext}"], 119visibility = ["//visibility:public"], 120) 121""" 122 123def BUILD_for_rust_analyzer_proc_macro_srv ( exec_triple ): 124"""Emits a BUILD file the rust_analyzer_proc_macro_srv archive. 125 126Args: 127exec_triple (str): The triple of the exec platform 128Returns: 129str: The contents of a BUILD file 130""" 131return _build_file_for_rust_analyzer_proc_macro_srv . format ( 132binary_ext = system_to_binary_ext ( exec_triple . system ), 133) 134 135def BUILD_for_clippy ( target_triple ): 136"""Emits a BUILD file the clippy archive. 137 138Args: 139target_triple (str): The triple of the target platform 140 141Returns: 142str: The contents of a BUILD file 143""" 144return _build_file_for_clippy_template . format ( 145binary_ext = system_to_binary_ext ( target_triple . system ), 146) 147 148_build_file_for_llvm_tools = """\ 149filegroup( 150name = "llvm_cov_bin", 151srcs = ["lib/rustlib/{target_triple}/bin/llvm-cov{binary_ext}"], 152visibility = ["//visibility:public"], 153) 154 155filegroup( 156name = "llvm_profdata_bin", 157srcs = ["lib/rustlib/{target_triple}/bin/llvm-profdata{binary_ext}"], 158visibility = ["//visibility:public"], 159) 160""" 161 162def BUILD_for_llvm_tools ( target_triple ): 163"""Emits a BUILD file the llvm-tools binaries. 164 165Args: 166target_triple (struct): The triple of the target platform 167 168Returns: 169str: The contents of a BUILD file 170""" 171return _build_file_for_llvm_tools . format ( 172binary_ext = system_to_binary_ext ( target_triple . system ), 173target_triple = target_triple . str , 174) 175 176_build_file_for_stdlib_template = """\ 177load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup") 178 179rust_stdlib_filegroup( 180name = "rust_std-{target_triple}", 181srcs = glob( 182[ 183"lib/rustlib/{target_triple}/lib/*.rlib", 184"lib/rustlib/{target_triple}/lib/*{dylib_ext}", 185"lib/rustlib/{target_triple}/lib/*{staticlib_ext}", 186"lib/rustlib/{target_triple}/lib/self-contained/**", 187], 188# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245 189allow_empty = True, 190), 191visibility = ["//visibility:public"], 192) 193 194# For legacy support 195alias( 196name = "rust_lib-{target_triple}", 197actual = "rust_std-{target_triple}", 198visibility = ["//visibility:public"], 199) 200""" 201 202def BUILD_for_stdlib ( target_triple ): 203"""Emits a BUILD file the stdlib archive. 204 205Args: 206target_triple (triple): The triple of the target platform 207 208Returns: 209str: The contents of a BUILD file 210""" 211return _build_file_for_stdlib_template . format ( 212binary_ext = system_to_binary_ext ( target_triple . system ), 213staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 214dylib_ext = system_to_dylib_ext ( target_triple . system ), 215target_triple = target_triple . str , 216) 217 218_build_file_for_rust_toolchain_template = """\ 219load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") 220 221rust_toolchain( 222name = "{toolchain_name}", 223rust_doc = "@{workspace_name}//:rustdoc", 224rust_std = "@{workspace_name}//:rust_std-{target_triple}", 225rustc = "@{workspace_name}//:rustc", 226rustfmt = {rustfmt_label}, 227cargo = "@{workspace_name}//:cargo", 228clippy_driver = "@{workspace_name}//:clippy_driver_bin", 229llvm_cov = {llvm_cov_label}, 230llvm_profdata = {llvm_profdata_label}, 231rustc_lib = "@{workspace_name}//:rustc_lib", 232allocator_library = {allocator_library}, 233binary_ext = "{binary_ext}", 234staticlib_ext = "{staticlib_ext}", 235dylib_ext = "{dylib_ext}", 236stdlib_linkflags = [{stdlib_linkflags}], 237os = "{system}", 238default_edition = "{default_edition}", 239exec_triple = "{exec_triple}", 240target_triple = "{target_triple}", 241visibility = ["//visibility:public"], 242) 243""" 244 245def BUILD_for_rust_toolchain ( 246workspace_name , 247name , 248exec_triple , 249target_triple , 250allocator_library , 251default_edition , 252include_rustfmt , 253include_llvm_tools , 254stdlib_linkflags = None ): 255"""Emits a toolchain declaration to match an existing compiler and stdlib. 256 257Args: 258workspace_name (str): The name of the workspace that this toolchain resides in 259name (str): The name of the toolchain declaration 260exec_triple (triple): The rust-style target that this compiler runs on 261target_triple (triple): The rust-style target triple of the tool 262allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 263default_edition (str): Default Rust edition. 264include_rustfmt (bool): Whether rustfmt is present in the toolchain. 265include_llvm_tools (bool): Whether llvm-tools are present in the toolchain. 266stdlib_linkflags (list, optional): Overriden flags needed for linking to rust 267stdlib, akin to BAZEL_LINKLIBS. Defaults to 268None. 269 270 271Returns: 272str: A rendered template of a `rust_toolchain` declaration 273""" 274if stdlib_linkflags == None : 275stdlib_linkflags = ", " . join ([ '"%s"' % x for x in system_to_stdlib_linkflags ( target_triple . system )]) 276 277rustfmt_label = "None" 278if include_rustfmt : 279rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"" . format ( workspace_name = workspace_name ) 280llvm_cov_label = "None" 281llvm_profdata_label = "None" 282if include_llvm_tools : 283llvm_cov_label = "\"@{workspace_name}//:llvm_cov_bin\"" . format ( workspace_name = workspace_name ) 284llvm_profdata_label = "\"@{workspace_name}//:llvm_profdata_bin\"" . format ( workspace_name = workspace_name ) 285allocator_library_label = "None" 286if allocator_library : 287allocator_library_label = "\"{allocator_library}\"" . format ( allocator_library = allocator_library ) 288 289return _build_file_for_rust_toolchain_template . format ( 290toolchain_name = name , 291workspace_name = workspace_name , 292binary_ext = system_to_binary_ext ( target_triple . system ), 293staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 294dylib_ext = system_to_dylib_ext ( target_triple . system ), 295allocator_library = allocator_library_label , 296stdlib_linkflags = stdlib_linkflags , 297system = target_triple . system , 298default_edition = default_edition , 299exec_triple = exec_triple . str , 300target_triple = target_triple . str , 301rustfmt_label = rustfmt_label , 302llvm_cov_label = llvm_cov_label , 303llvm_profdata_label = llvm_profdata_label , 304) 305 306_build_file_for_toolchain_template = """\ 307toolchain( 308name = "{name}", 309exec_compatible_with = {exec_constraint_sets_serialized}, 310target_compatible_with = {target_constraint_sets_serialized}, 311toolchain = "{toolchain}", 312toolchain_type = "{toolchain_type}", 313{target_settings} 314) 315""" 316 317def BUILD_for_toolchain ( 318name , 319toolchain , 320toolchain_type , 321target_settings , 322target_compatible_with , 323exec_compatible_with ): 324target_settings_value = "target_settings = {}," . format ( json . encode ( target_settings )) if target_settings else "# target_settings = []" 325 326return _build_file_for_toolchain_template . format ( 327name = name , 328exec_constraint_sets_serialized = json . encode ( exec_compatible_with ), 329target_constraint_sets_serialized = json . encode ( target_compatible_with ), 330toolchain = toolchain , 331toolchain_type = toolchain_type , 332target_settings = target_settings_value , 333) 334 335def load_rustfmt ( ctx , target_triple , version , iso_date ): 336"""Loads a rustfmt binary and yields corresponding BUILD for it 337 338Args: 339ctx (repository_ctx): The repository rule's context object. 340target_triple (struct): The platform triple to download rustfmt for. 341version (str): The version or channel of rustfmt. 342iso_date (str): The date of the tool (or None, if the version is a specific version). 343 344Returns: 345str: The BUILD file contents for this rustfmt binary 346""" 347 348load_arbitrary_tool ( 349ctx , 350iso_date = iso_date , 351target_triple = target_triple , 352tool_name = "rustfmt" , 353tool_subdirectories = [ "rustfmt-preview" ], 354version = version , 355) 356 357return BUILD_for_rustfmt ( target_triple ) 358 359def load_rust_compiler ( ctx , iso_date , target_triple , version ): 360 """Loads a rust compiler and yields corresponding BUILD for it 361 362 Args: 363 ctx (repository_ctx): A repository_ctx. 364 iso_date (str): The date of the tool (or None, if the version is a specific version). 365 target_triple (struct): The Rust-style target that this compiler runs on. 366 version (str): The version of the tool among \"nightly\", \"beta\", or an exact version . 367 368Returns : 369str : The BUILD file contents for this compiler and compiler library 370""" 371 372load_arbitrary_tool( 373ctx, 374iso_date = iso_date, 375target_triple = target_triple, 376tool_name = "rustc", 377tool_subdirectories = ["rustc"], 378version = version, 379) 380 381return BUILD_for_compiler(target_triple) 382 383def load_clippy(ctx, iso_date, target_triple, version): 384""" Loads Clippy and yields corresponding BUILD for it 385 386Args : 387ctx ( repository_ctx ): A repository_ctx . 388iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 389target_triple ( struct ): The Rust - style target that this compiler runs on . 390version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 391 392Returns : 393str : The BUILD file contents for Clippy 394""" 395load_arbitrary_tool( 396ctx, 397iso_date = iso_date, 398target_triple = target_triple, 399tool_name = "clippy", 400tool_subdirectories = ["clippy-preview"], 401version = version, 402) 403 404return BUILD_for_clippy(target_triple) 405 406def load_cargo(ctx, iso_date, target_triple, version): 407""" Loads Cargo and yields corresponding BUILD for it 408 409Args : 410ctx ( repository_ctx ): A repository_ctx . 411iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 412target_triple ( struct ): The Rust - style target that this compiler runs on . 413version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 414 415Returns : 416str : The BUILD file contents for Cargo 417""" 418 419load_arbitrary_tool( 420ctx, 421iso_date = iso_date, 422target_triple = target_triple, 423tool_name = "cargo", 424tool_subdirectories = ["cargo"], 425version = version, 426) 427 428return BUILD_for_cargo(target_triple) 429 430def includes_rust_analyzer_proc_macro_srv(version, iso_date): 431""" Determine whether or not the rust_analyzer_proc_macro_srv binary in available in the given version of Rust . 432 433Args : 434version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 435iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 436 437Returns : 438bool : Whether or not the binary is expected to be included 439""" 440 441if version == "nightly": 442return iso_date >= "2022-09-21" 443elif version == "beta": 444return False 445elif version >= "1.64.0": 446return True 447 448return False 449 450def load_rust_src(ctx, iso_date, version, sha256 = ""): 451""" Loads the rust source code . Used by the rust - analyzer rust - project . json generator . 452 453Args : 454ctx ( ctx ): A repository_ctx . 455version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 456iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 457sha256 ( str ): The sha256 value for the `rust-src` artifact 458""" 459tool_suburl = produce_tool_suburl("rust-src", None, version, iso_date) 460url = ctx.attr.urls[0].format(tool_suburl) 461 462tool_path = produce_tool_path("rust-src", version, None) 463archive_path = tool_path + _get_tool_extension(getattr(ctx.attr, "urls", None)) 464sha256 = sha256 or getattr(ctx.attr, "sha256s", {}).get(archive_path) or FILE_KEY_TO_SHA.get(archive_path) or "" 465ctx.download_and_extract( 466url, 467output = "lib/rustlib/src", 468sha256 = sha256, 469auth = _make_auth_dict(ctx, [url]), 470stripPrefix = "{}/rust-src/lib/rustlib/src/rust".format(tool_path), 471) 472ctx.file( 473"lib/rustlib/src/BUILD.bazel", 474""" \ 475filegroup ( 476name = "rustc_srcs" , 477srcs = glob ([ "**/*" ]), 478visibility = [ "//visibility:public" ], 479) """, 480) 481 482_build_file_for_rust_analyzer_toolchain_template = """ \ 483load ( "@rules_rust//rust:toolchain.bzl" , "rust_analyzer_toolchain" ) 484 485rust_analyzer_toolchain ( 486name = "{name}" , 487proc_macro_srv = { proc_macro_srv }, 488rustc = "{rustc}" , 489rustc_srcs = "//lib/rustlib/src:rustc_srcs" , 490visibility = [ "//visibility:public" ], 491) 492""" 493 494def BUILD_for_rust_analyzer_toolchain(name, rustc, proc_macro_srv): 495return _build_file_for_rust_analyzer_toolchain_template.format( 496name = name, 497rustc = rustc, 498proc_macro_srv = repr(proc_macro_srv), 499) 500 501_build_file_for_rustfmt_toolchain_template = """ \ 502load ( "@rules_rust//rust:toolchain.bzl" , "rustfmt_toolchain" ) 503 504rustfmt_toolchain ( 505name = "{name}" , 506rustfmt = "{rustfmt}" , 507rustc = "{rustc}" , 508rustc_lib = "{rustc_lib}" , 509visibility = [ "//visibility:public" ], 510) 511""" 512 513def BUILD_for_rustfmt_toolchain(name, rustfmt, rustc, rustc_lib): 514return _build_file_for_rustfmt_toolchain_template.format( 515name = name, 516rustfmt = rustfmt, 517rustc = rustc, 518rustc_lib = rustc_lib, 519) 520 521def load_rust_stdlib(ctx, target_triple): 522""" Loads a rust standard library and yields corresponding BUILD for it 523 524Args : 525ctx ( repository_ctx ): A repository_ctx . 526target_triple ( struct ): The rust - style target triple of the tool 527 528Returns : 529str : The BUILD file contents for this stdlib 530""" 531 532load_arbitrary_tool( 533ctx, 534iso_date = ctx.attr.iso_date, 535target_triple = target_triple, 536tool_name = "rust-std", 537tool_subdirectories = ["rust-std-{}".format(target_triple.str)], 538version = ctx.attr.version, 539) 540 541return BUILD_for_stdlib(target_triple) 542 543def load_rustc_dev_nightly(ctx, target_triple): 544""" Loads the nightly rustc dev component 545 546Args : 547ctx : A repository_ctx . 548target_triple : The rust - style target triple of the tool 549""" 550 551subdir_name = "rustc-dev" 552if ctx.attr.iso_date < "2020-12-24": 553subdir_name = "rustc-dev-{}".format(target_triple) 554 555load_arbitrary_tool( 556ctx, 557iso_date = ctx.attr.iso_date, 558target_triple = target_triple, 559tool_name = "rustc-dev", 560tool_subdirectories = [subdir_name], 561version = ctx.attr.version, 562) 563 564def load_llvm_tools(ctx, target_triple): 565""" Loads the llvm tools 566 567Args : 568ctx : A repository_ctx . 569target_triple : The rust - style target triple of the tool 570""" 571load_arbitrary_tool( 572ctx, 573iso_date = ctx.attr.iso_date, 574target_triple = target_triple, 575tool_name = "llvm-tools", 576tool_subdirectories = ["llvm-tools-preview"], 577version = ctx.attr.version, 578) 579 580return BUILD_for_llvm_tools(target_triple) 581 582def check_version_valid(version, iso_date, param_prefix = ""): 583""" Verifies that the provided rust version and iso_date make sense . 584 585Args : 586version ( str ): The rustc version 587iso_date ( str ): The rustc nightly version 's iso date 588param_prefix ( str , optional ): The name of the tool who 's version is being checked . 589""" 590 591if not version and iso_date: 592fail("{param_prefix}iso_date must be paired with a {param_prefix}version".format(param_prefix = param_prefix)) 593 594if version in ("beta", "nightly") and not iso_date: 595fail("{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'".format(param_prefix = param_prefix)) 596 597def produce_tool_suburl(tool_name, target_triple, version, iso_date = None): 598""" Produces a fully qualified Rust tool name for URL 599 600Args : 601tool_name ( str ): The name of the tool per `static.rust-lang.org` . 602target_triple ( struct ): The rust - style target triple of the tool . 603version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 604iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 605 606Returns : 607str : The fully qualified url path for the specified tool . 608""" 609path = produce_tool_path(tool_name, version, target_triple) 610return iso_date + "/" + path if (iso_date and version in ("beta", "nightly")) else path 611 612def produce_tool_path(tool_name, version, target_triple = None): 613""" Produces a qualified Rust tool name 614 615Args : 616tool_name ( str ): The name of the tool per static . rust - lang . org 617version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 618target_triple ( struct , optional ): The rust - style target triple of the tool 619 620Returns : 621str : The qualified path for the specified tool . 622""" 623if not tool_name: 624fail("No tool name was provided") 625if not version: 626fail("No tool version was provided") 627 628# Not all tools require a triple. E.g. `rustc_src` (Rust source files for rust-analyzer). 629platform_triple = None 630if target_triple: 631platform_triple = target_triple.str 632 633return "-".join([e for e in [tool_name, version, platform_triple] if e]) 634 635def lookup_tool_sha256(ctx, tool_name, target_triple, version, iso_date, sha256): 636""" Looks up the sha256 hash of a specific tool archive . 637 638The lookup order is : 639 6401. The sha256s dict in the context attributes ; 6412. The list of sha256 hashes populated in // rust : known_shas . bzl ; 6423. The sha256 argument to the function 643 644Args : 645ctx ( repository_ctx ): A repository_ctx ( no attrs required ). 646tool_name ( str ): The name of the given tool per the archive naming . 647target_triple ( struct ): The rust - style target triple of the tool . 648version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 649iso_date ( str ): The date of the tool ( ignored if the version is a specific version ). 650sha256 ( str ): The expected hash of hash of the Rust tool . 651 652Returns : 653str : The sha256 of the tool archive , or an empty string if the hash could not be found . 654""" 655tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date) 656archive_path = tool_suburl + _get_tool_extension(getattr(ctx.attr, "urls", None)) 657return getattr(ctx.attr, "sha256s", dict()).get(archive_path) or FILE_KEY_TO_SHA.get(archive_path) or sha256 658 659def load_arbitrary_tool(ctx, tool_name, tool_subdirectories, version, iso_date, target_triple, sha256 = ""): 660""" Loads a Rust tool , downloads , and extracts into the common workspace . 661 662This function sources the tool from the Rust - lang static file server . The index is available at : 663- https : // static . rust - lang . org / dist / channel - rust - stable . toml 664- https : // static . rust - lang . org / dist / channel - rust - beta . toml 665- https : // static . rust - lang . org / dist / channel - rust - nightly . toml 666 667Args : 668ctx ( repository_ctx ): A repository_ctx ( no attrs required ). 669tool_name ( str ): The name of the given tool per the archive naming . 670tool_subdirectories ( str ): The subdirectories of the tool files ( at a level below the root directory of 671the archive ). The root directory of the archive is expected to match 672 $TOOL_NAME - $VERSION - $TARGET_TRIPLE . 673Example : 674tool_name 675| version 676| | target_triple 677v v v 678rust - 1.39 . 0 - x86_64 - unknown - linux - gnu / clippy - preview 679... / rustc 680... / etc 681tool_subdirectories = [ "clippy-preview" , "rustc" ] 682version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 683iso_date ( str ): The date of the tool ( ignored if the version is a specific version ). 684target_triple ( struct ): The rust - style target triple of the tool . 685sha256 ( str , optional ): The expected hash of hash of the Rust tool . Defaults to "" . 686""" 687check_version_valid(version, iso_date, param_prefix = tool_name + "_") 688 689# View the indices mentioned in the docstring to find the tool_suburl for a given 690# tool. 691tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date) 692urls = [] 693 694for url in getattr(ctx.attr, "urls", DEFAULT_STATIC_RUST_URL_TEMPLATES): 695new_url = url.format(tool_suburl) 696if new_url not in urls: 697urls.append(new_url) 698 699tool_path = produce_tool_path(tool_name, version, target_triple) 700 701sha256 = lookup_tool_sha256(ctx, tool_name, target_triple, version, iso_date, sha256) 702 703for subdirectory in tool_subdirectories: 704# As long as the sha256 value is consistent accross calls here the 705# cost of downloading an artifact is negated as by Bazel's caching. 706result = ctx.download_and_extract( 707urls, 708sha256 = sha256, 709auth = _make_auth_dict(ctx, urls), 710stripPrefix = "{}/{}".format(tool_path, subdirectory), 711) 712 713# In the event no sha256 was provided, set it to the value of the first 714# downloaded item so subsequent downloads use a cached artifact. 715if not sha256: 716sha256 = result.sha256 717 718def _make_auth_dict(ctx, urls): 719auth = getattr(ctx.attr, "auth", {}) 720if not auth: 721return {} 722ret = {} 723for url in urls: 724ret[url] = auth 725return ret 726 727def _get_tool_extension(urls = None): 728if urls == None: 729urls = DEFAULT_STATIC_RUST_URL_TEMPLATES 730if urls[0][-7:] == ".tar.gz": 731return ".tar.gz" 732elif urls[0][-7:] == ".tar.xz": 733return ".tar.xz" 734else: 735return "" 736 737def select_rust_version(versions): 738""" Select the highest priorty version for a list of Rust versions 739 740Priority order : `stable > nightly > beta` 741 742Note that duplicate channels are unexpected in `versions` . 743 744Args : 745versions ( list ): A list of Rust versions . E . g . [ `1.66.0` , `nightly/2022-12-15` ] 746 747Returns : 748str : The highest ranking value from `versions` 749 """ 750 if not versions: 751 fail("No versions were provided") 752 753 current = versions[0] 754 755 for ver in versions: 756 if ver.startswith("beta"): 757 if current[0].isdigit() or current.startswith("nightly"): 758 continue 759 if current.startswith("beta") and ver > current: 760 current = ver 761 continue 762 763 current = ver 764 elif ver.startswith("nightly"): 765 if current[0].isdigit(): 766 continue 767 if current.startswith("nightly") and ver > current: 768 current = ver 769 continue 770 771 current = ver 772 773 else: 774 current = ver 775 776 return current