fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
8e962a16
main
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-wasip1" ] 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}, 269tags = ["rust_version={version}"], 270) 271""" 272 273def BUILD_for_rust_toolchain ( 274name , 275exec_triple , 276target_triple , 277version , 278allocator_library , 279global_allocator_library , 280default_edition , 281include_rustfmt , 282include_llvm_tools , 283stdlib_linkflags = None , 284extra_rustc_flags = None , 285extra_exec_rustc_flags = None , 286opt_level = None ): 287"""Emits a toolchain declaration to match an existing compiler and stdlib. 288 289Args: 290name (str): The name of the toolchain declaration 291exec_triple (triple): The rust-style target that this compiler runs on 292target_triple (triple): The rust-style target triple of the tool 293version (str): The Rust version for the toolchain. 294allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 295global_allocator_library (str, optional): Target that provides allocator functions when a global allocator is used with cc_common_link. 296This target is only used in the target configuration; exec builds still use the symbols provided 297by the `allocator_library` target. 298default_edition (str): Default Rust edition. 299include_rustfmt (bool): Whether rustfmt is present in the toolchain. 300include_llvm_tools (bool): Whether llvm-tools are present in the toolchain. 301stdlib_linkflags (list, optional): Overriden flags needed for linking to rust 302stdlib, akin to BAZEL_LINKLIBS. Defaults to 303None. 304extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration. 305extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. 306opt_level (dict, optional): Optimization level config for this toolchain. 307 308Returns: 309str: A rendered template of a `rust_toolchain` declaration 310""" 311if stdlib_linkflags == None : 312stdlib_linkflags = ", " . join ([ '"%s"' % x for x in system_to_stdlib_linkflags ( target_triple . system )]) 313 314rustfmt_label = "None" 315if include_rustfmt : 316rustfmt_label = "\"//:rustfmt_bin\"" 317llvm_cov_label = "None" 318llvm_profdata_label = "None" 319if include_llvm_tools : 320llvm_cov_label = "\"//:llvm_cov_bin\"" 321llvm_profdata_label = "\"//:llvm_profdata_bin\"" 322allocator_library_label = "None" 323if allocator_library : 324allocator_library_label = "\"{allocator_library}\"" . format ( allocator_library = allocator_library ) 325global_allocator_library_label = "None" 326if global_allocator_library : 327global_allocator_library_label = "\"{global_allocator_library}\"" . format ( global_allocator_library = global_allocator_library ) 328 329return _build_file_for_rust_toolchain_template . format ( 330toolchain_name = name , 331binary_ext = system_to_binary_ext ( target_triple . system ), 332staticlib_ext = system_to_staticlib_ext ( target_triple . system ), 333dylib_ext = system_to_dylib_ext ( target_triple . system ), 334allocator_library = allocator_library_label , 335global_allocator_library = global_allocator_library_label , 336stdlib_linkflags = stdlib_linkflags , 337default_edition = default_edition , 338exec_triple = exec_triple . str , 339target_triple = target_triple . str , 340rustfmt_label = rustfmt_label , 341llvm_cov_label = llvm_cov_label , 342llvm_profdata_label = llvm_profdata_label , 343extra_rustc_flags = extra_rustc_flags , 344extra_exec_rustc_flags = extra_exec_rustc_flags , 345opt_level = opt_level , 346version = version , 347) 348 349_build_file_for_toolchain_template = """\ 350toolchain( 351name = "{name}", 352exec_compatible_with = {exec_constraint_sets_serialized}, 353target_compatible_with = {target_constraint_sets_serialized}, 354toolchain = "{toolchain}", 355toolchain_type = "{toolchain_type}", 356{target_settings} 357) 358""" 359 360def BUILD_for_toolchain ( 361name , 362toolchain , 363toolchain_type , 364target_settings , 365target_compatible_with , 366exec_compatible_with ): 367target_settings_value = "target_settings = {}," . format ( json . encode ( target_settings )) if target_settings else "# target_settings = []" 368 369return _build_file_for_toolchain_template . format ( 370name = name , 371exec_constraint_sets_serialized = json . encode ( exec_compatible_with ), 372target_constraint_sets_serialized = json . encode ( target_compatible_with ), 373toolchain = toolchain , 374toolchain_type = toolchain_type , 375target_settings = target_settings_value , 376) 377 378def load_rustfmt ( ctx , target_triple , version , iso_date ): 379"""Loads a rustfmt binary and yields corresponding BUILD for it 380 381Args: 382ctx (repository_ctx): The repository rule's context object. 383target_triple (struct): The platform triple to download rustfmt for. 384version (str): The version or channel of rustfmt. 385iso_date (str): The date of the tool (or None, if the version is a specific version). 386 387Returns: 388Tuple[str, Dict[str, str]]: The BUILD file contents for this rustfmt binary and sha256 of it's artifact. 389""" 390 391sha256 = load_arbitrary_tool ( 392ctx , 393iso_date = iso_date , 394target_triple = target_triple , 395tool_name = "rustfmt" , 396tool_subdirectories = [ "rustfmt-preview" ], 397version = version , 398) 399 400return BUILD_for_rustfmt ( target_triple ), sha256 401 402def load_rust_compiler ( ctx , iso_date , target_triple , version ): 403 """Loads a rust compiler and yields corresponding BUILD for it 404 405 Args: 406 ctx (repository_ctx): A repository_ctx. 407 iso_date (str): The date of the tool (or None, if the version is a specific version). 408 target_triple (struct): The Rust-style target that this compiler runs on. 409 version (str): The version of the tool among \"nightly\", \"beta\", or an exact version . 410 411Returns : 412Tuple [ str , Dict [ str , str ]]: The BUILD file contents for this compiler and compiler library 413and sha256 of it 's artifact . 414""" 415 416sha256 = load_arbitrary_tool( 417ctx, 418iso_date = iso_date, 419target_triple = target_triple, 420tool_name = "rustc", 421tool_subdirectories = ["rustc"], 422version = version, 423) 424 425return BUILD_for_compiler(target_triple), sha256 426 427def load_clippy(ctx, iso_date, target_triple, version): 428""" Loads Clippy and yields corresponding BUILD for it 429 430Args : 431ctx ( repository_ctx ): A repository_ctx . 432iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 433target_triple ( struct ): The Rust - style target that this compiler runs on . 434version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 435 436Returns : 437Tuple [ str , str ]: The BUILD file contents for Clippy and the sha256 of it 's artifact 438""" 439sha256 = load_arbitrary_tool( 440ctx, 441iso_date = iso_date, 442target_triple = target_triple, 443tool_name = "clippy", 444tool_subdirectories = ["clippy-preview"], 445version = version, 446) 447 448return BUILD_for_clippy(target_triple), sha256 449 450def load_cargo(ctx, iso_date, target_triple, version): 451""" Loads Cargo and yields corresponding BUILD for it 452 453Args : 454ctx ( repository_ctx ): A repository_ctx . 455iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 456target_triple ( struct ): The Rust - style target that this compiler runs on . 457version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 458 459Returns : 460Tuple [ str , Dict [ str , str ]]: The BUILD file contents for Cargo and the sha256 of its artifact . 461""" 462 463sha256 = load_arbitrary_tool( 464ctx, 465iso_date = iso_date, 466target_triple = target_triple, 467tool_name = "cargo", 468tool_subdirectories = ["cargo"], 469version = version, 470) 471 472return BUILD_for_cargo(target_triple), sha256 473 474def includes_rust_analyzer_proc_macro_srv(version, iso_date): 475""" Determine whether or not the rust_analyzer_proc_macro_srv binary in available in the given version of Rust . 476 477Args : 478version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 479iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 480 481Returns : 482bool : Whether or not the binary is expected to be included 483""" 484 485if version == "nightly": 486return iso_date >= "2022-09-21" 487elif version == "beta": 488return False 489elif version >= "1.64.0": 490return True 491 492return False 493 494def load_rust_src(ctx, iso_date, version, sha256 = None): 495""" Loads the rust source code . Used by the rust - analyzer rust - project . json generator . 496 497Args : 498ctx ( ctx ): A repository_ctx . 499version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 500iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 501sha256 ( str ): The sha256 value for the `rust-src` artifact 502 503Returns : 504Dict [ str , str ]: A mapping of the artifact name to sha256 505""" 506tool_suburl = produce_tool_suburl("rust-src", None, version, iso_date) 507url = ctx.attr.urls[0].format(tool_suburl) 508 509tool_path = produce_tool_path("rust-src", version, None) 510archive_path = tool_path + _get_tool_extension(getattr(ctx.attr, "urls", None)) 511 512is_reproducible = True 513if sha256 == None: 514sha256s = getattr(ctx.attr, "sha256s", {}) 515sha256 = sha256s.get(archive_path, None) or FILE_KEY_TO_SHA.get(archive_path, None) 516if not sha256: 517sha256 = "" 518is_reproducible = False 519 520result = ctx.download_and_extract( 521url, 522output = "lib/rustlib/src", 523sha256 = sha256, 524auth = _make_auth_dict(ctx, [url]), 525stripPrefix = "{}/rust-src/lib/rustlib/src/rust".format(tool_path), 526) 527ctx.file( 528"lib/rustlib/src/BUILD.bazel", 529""" \ 530filegroup ( 531name = "rustc_srcs" , 532srcs = glob ([ "**/*" ]), 533visibility = [ "//visibility:public" ], 534) """, 535) 536 537if is_reproducible: 538return {} 539 540return {archive_path: result.sha256} 541 542_build_file_for_rust_analyzer_toolchain_template = """ \ 543load ( "@rules_rust//rust:toolchain.bzl" , "rust_analyzer_toolchain" ) 544 545rust_analyzer_toolchain ( 546name = "{name}" , 547proc_macro_srv = { proc_macro_srv }, 548rustc = "{rustc}" , 549rustc_srcs = "//lib/rustlib/src:rustc_srcs" , 550visibility = [ "//visibility:public" ], 551) 552""" 553 554def BUILD_for_rust_analyzer_toolchain(name, rustc, proc_macro_srv): 555return _build_file_for_rust_analyzer_toolchain_template.format( 556name = name, 557rustc = rustc, 558proc_macro_srv = repr(proc_macro_srv), 559) 560 561_build_file_for_rustfmt_toolchain_template = """ \ 562load ( "@rules_rust//rust:toolchain.bzl" , "rustfmt_toolchain" ) 563 564rustfmt_toolchain ( 565name = "{name}" , 566rustfmt = "{rustfmt}" , 567rustc = "{rustc}" , 568rustc_lib = "{rustc_lib}" , 569visibility = [ "//visibility:public" ], 570) 571""" 572 573def BUILD_for_rustfmt_toolchain(name, rustfmt, rustc, rustc_lib): 574return _build_file_for_rustfmt_toolchain_template.format( 575name = name, 576rustfmt = rustfmt, 577rustc = rustc, 578rustc_lib = rustc_lib, 579) 580 581def load_rust_stdlib(ctx, target_triple, version, iso_date = None): 582""" Loads a rust standard library and yields corresponding BUILD for it 583 584Args : 585ctx ( repository_ctx ): A repository_ctx . 586target_triple ( struct ): The rust - style target triple of the tool 587version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 588iso_date ( str ): The iso_date to use with \"nightly\" or \"beta\"versions . 589 590Returns : 591Tuple [ str , Dict [ str , str ]]: The BUILD file contents for this stdlib and the sha256 of the artifact . 592""" 593 594sha256 = load_arbitrary_tool( 595ctx, 596iso_date = iso_date, 597target_triple = target_triple, 598tool_name = "rust-std", 599tool_subdirectories = ["rust-std-{}".format(target_triple.str)], 600version = version, 601) 602 603return BUILD_for_stdlib(target_triple), sha256 604 605def load_rustc_dev_nightly(ctx, target_triple, version, iso_date = None): 606""" Loads the nightly rustc dev component 607 608Args : 609ctx : A repository_ctx . 610target_triple : The rust - style target triple of the tool 611version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 612iso_date ( str ): The iso_date to use with \"nightly\" or \"beta\"versions . 613 614Returns : 615Dict [ str , str ]: The sha256 value of the rustc - dev artifact . 616""" 617 618subdir_name = "rustc-dev" 619if iso_date and iso_date < "2020-12-24": 620subdir_name = "rustc-dev-{}".format(target_triple) 621 622sha256 = load_arbitrary_tool( 623ctx, 624iso_date = iso_date, 625target_triple = target_triple, 626tool_name = "rustc-dev", 627tool_subdirectories = [subdir_name], 628version = version, 629) 630 631return sha256 632 633def load_llvm_tools(ctx, target_triple, version, iso_date = None): 634""" Loads the llvm tools 635 636Args : 637ctx ( repository_ctx ): A repository_ctx . 638target_triple ( struct ): The rust - style target triple of the tool 639version ( str ): The version of the tool among \"nightly\", \"beta\", or an exact version . 640iso_date ( str ): The iso_date to use with \"nightly\" or \"beta\"versions . 641 642Returns : 643Tuple [ str , Dict [ str , str ]]: The BUILD . bazel content and sha256 value of the llvm tools artifact . 644""" 645sha256 = load_arbitrary_tool( 646ctx, 647iso_date = iso_date, 648target_triple = target_triple, 649tool_name = "llvm-tools", 650tool_subdirectories = ["llvm-tools-preview"], 651version = version, 652) 653 654return BUILD_for_llvm_tools(target_triple), sha256 655 656def check_version_valid(version, iso_date, param_prefix = ""): 657""" Verifies that the provided rust version and iso_date make sense . 658 659Args : 660version ( str ): The rustc version 661iso_date ( str ): The rustc nightly version 's iso date 662param_prefix ( str , optional ): The name of the tool who 's version is being checked . 663""" 664 665if not version and iso_date: 666fail("{param_prefix}iso_date must be paired with a {param_prefix}version".format(param_prefix = param_prefix)) 667 668if version in ("beta", "nightly") and not iso_date: 669fail("{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'".format(param_prefix = param_prefix)) 670 671def produce_tool_suburl(tool_name, target_triple, version, iso_date = None): 672""" Produces a fully qualified Rust tool name for URL 673 674Args : 675tool_name ( str ): The name of the tool per `static.rust-lang.org` . 676target_triple ( struct ): The rust - style target triple of the tool . 677version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 678iso_date ( str ): The date of the tool ( or None , if the version is a specific version ). 679 680Returns : 681str : The fully qualified url path for the specified tool . 682""" 683path = produce_tool_path(tool_name, version, target_triple) 684return iso_date + "/" + path if (iso_date and version in ("beta", "nightly")) else path 685 686def produce_tool_path(tool_name, version, target_triple = None): 687""" Produces a qualified Rust tool name 688 689Args : 690tool_name ( str ): The name of the tool per static . rust - lang . org 691version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 692target_triple ( struct , optional ): The rust - style target triple of the tool 693 694Returns : 695str : The qualified path for the specified tool . 696""" 697if not tool_name: 698fail("No tool name was provided") 699if not version: 700fail("No tool version was provided") 701 702# Not all tools require a triple. E.g. `rustc_src` (Rust source files for rust-analyzer). 703platform_triple = None 704if target_triple: 705platform_triple = target_triple.str 706 707return "-".join([e for e in [tool_name, version, platform_triple] if e]) 708 709def lookup_tool_sha256( 710repository_ctx, 711tool_name, 712target_triple, 713version, 714iso_date): 715""" Looks up the sha256 hash of a specific tool archive . 716 717The lookup order is : 718 7191. The sha256s dict in the context attributes ; 7202. The list of sha256 hashes populated in `//rust:known_shas.bzl` ; 721 722Args : 723repository_ctx ( repository_ctx ): A repository_ctx ( no attrs required ). 724tool_name ( str ): The name of the given tool per the archive naming . 725target_triple ( struct ): The rust - style target triple of the tool . 726version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 727iso_date ( str ): The date of the tool ( ignored if the version is a specific version ). 728 729Returns : 730str : The sha256 of the tool archive , or an empty string if the hash could not be found . 731""" 732tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date) 733urls = getattr(repository_ctx.attr, "urls", None) 734archive_path = tool_suburl + _get_tool_extension(urls) 735sha256s = getattr(repository_ctx.attr, "sha256s", dict()) 736 737from_attr = sha256s.get(archive_path, None) 738if from_attr: 739return archive_path, from_attr 740 741from_builtin = FILE_KEY_TO_SHA.get(archive_path, None) 742if from_builtin: 743return archive_path, from_builtin 744 745return archive_path, "" 746 747def load_arbitrary_tool( 748ctx, 749tool_name, 750tool_subdirectories, 751version, 752iso_date, 753target_triple, 754sha256 = None): 755""" Loads a Rust tool , downloads , and extracts into the common workspace . 756 757This function sources the tool from the Rust - lang static file server . The index is available at : 758- https : // static . rust - lang . org / dist / channel - rust - stable . toml 759- https : // static . rust - lang . org / dist / channel - rust - beta . toml 760- https : // static . rust - lang . org / dist / channel - rust - nightly . toml 761 762Args : 763ctx ( repository_ctx ): A repository_ctx ( no attrs required ). 764tool_name ( str ): The name of the given tool per the archive naming . 765tool_subdirectories ( str ): The subdirectories of the tool files ( at a level below the root directory of 766the archive ). The root directory of the archive is expected to match 767 $TOOL_NAME - $VERSION - $TARGET_TRIPLE . 768Example : 769tool_name 770| version 771| | target_triple 772v v v 773rust - 1.39 . 0 - x86_64 - unknown - linux - gnu / clippy - preview 774... / rustc 775... / etc 776tool_subdirectories = [ "clippy-preview" , "rustc" ] 777version ( str ): The version of the tool among "nightly" , "beta ', or an exact version . 778iso_date ( str ): The date of the tool ( ignored if the version is a specific version ). 779target_triple ( struct ): The rust - style target triple of the tool . 780sha256 ( str , optional ): The expected hash of hash of the Rust tool . Defaults to "" . 781 782Returns : 783Dict [ str , str ]: A mapping of the tool name to it 's sha256 value if the requested tool does not have 784enough information in the repository_ctx to be reproducible . 785""" 786check_version_valid(version, iso_date, param_prefix = tool_name + "_") 787 788# View the indices mentioned in the docstring to find the tool_suburl for a given 789# tool. 790tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date) 791urls = [] 792 793for url in getattr(ctx.attr, "urls", DEFAULT_STATIC_RUST_URL_TEMPLATES): 794new_url = url.format(tool_suburl) 795if new_url not in urls: 796urls.append(new_url) 797 798tool_path = produce_tool_path(tool_name, version, target_triple) 799 800archive_path, ctx_sha256 = lookup_tool_sha256(ctx, tool_name, target_triple, version, iso_date) 801 802is_reproducible = True 803if sha256 == None: 804sha256 = ctx_sha256 805is_reproducible = bool(ctx_sha256) 806 807for subdirectory in tool_subdirectories: 808# As long as the sha256 value is consistent accross calls here the 809# cost of downloading an artifact is negated as by Bazel's caching. 810result = ctx.download_and_extract( 811urls, 812sha256 = sha256, 813auth = _make_auth_dict(ctx, urls), 814stripPrefix = "{}/{}".format(tool_path, subdirectory), 815) 816 817# In the event no sha256 was provided, set it to the value of the first 818# downloaded item so subsequent downloads use a cached artifact. 819if not sha256: 820sha256 = result.sha256 821 822# If the artifact is reproducibly downloadable then return an 823# empty dict to inform consumers no attributes require updating. 824if is_reproducible: 825return {} 826 827return {archive_path: sha256} 828 829# The function is copied from the main branch of bazel_tools. 830# It should become available there from version 7.1.0, 831# We should remove this function when we upgrade minimum supported 832# version to 7.1.0. 833# https://github.com/bazelbuild/bazel/blob/d37762b494a4e122d46a5a71e3a8cc77fa15aa25/tools/build_defs/repo/utils.bzl#L424-L446 834def _get_auth(ctx, urls): 835""" Utility function to obtain the correct auth dict for a list of urls from . netrc file . 836 837Support optional netrc and auth_patterns attributes if available . 838 839Args : 840ctx : The repository context of the repository rule calling this utility 841function . 842urls : the list of urls to read 843 844Returns : 845the auth dict which can be passed to repository_ctx . download 846""" 847if hasattr(ctx.attr, "netrc") and ctx.attr.netrc: 848netrc = read_netrc(ctx, ctx.attr.netrc) 849elif "NETRC" in ctx.os.environ: 850netrc = read_netrc(ctx, ctx.os.environ["NETRC"]) 851else: 852netrc = read_user_netrc(ctx) 853auth_patterns = {} 854if hasattr(ctx.attr, "auth_patterns") and ctx.attr.auth_patterns: 855auth_patterns = ctx.attr.auth_patterns 856return use_netrc(netrc, urls, auth_patterns) 857 858def _make_auth_dict(ctx, urls): 859auth = getattr(ctx.attr, "auth", {}) 860if not auth: 861return _get_auth(ctx, urls) 862ret = {} 863for url in urls: 864ret[url] = auth 865return ret 866 867def _get_tool_extension(urls = None): 868if urls == None: 869urls = DEFAULT_STATIC_RUST_URL_TEMPLATES 870if urls[0][-7:] == ".tar.gz": 871return ".tar.gz" 872elif urls[0][-7:] == ".tar.xz": 873return ".tar.xz" 874else: 875return "" 876 877def select_rust_version(versions): 878""" Select the highest priorty version for a list of Rust versions 879 880Priority order : `stable > nightly > beta` 881 882Note that duplicate channels are unexpected in `versions` . 883 884Args : 885versions ( list ): A list of Rust versions . E . g . [ `1.66.0` , `nightly/2022-12-15` ] 886 887Returns : 888str : The highest ranking value from `versions` 889""" 890if not versions: 891fail("No versions were provided") 892 893current = versions[0] 894 895for ver in versions: 896if ver.startswith("beta"): 897if current[0].isdigit() or current.startswith("nightly"): 898continue 899if current.startswith("beta") and ver > current: 900current = ver 901continue 902 903current = ver 904elif ver.startswith("nightly"): 905if current[0].isdigit(): 906continue 907if current.startswith("nightly") and ver > current: 908current = ver 909continue 910 911current = ver 912 913else: 914current = ver 915 916return current 917 918_build_file_for_toolchain_hub_template = """ 919toolchain ( 920name = "{name}" , 921exec_compatible_with = { exec_constraint_sets_serialized }, 922target_compatible_with = { target_constraint_sets_serialized }, 923target_settings = { target_settings_serialized }, 924toolchain = "{toolchain}" , 925toolchain_type = "{toolchain_type}" , 926visibility = [ "//visibility:public" ], 927) 928""" 929 930def BUILD_for_toolchain_hub( 931 toolchain_names, 932 toolchain_labels, 933 toolchain_types, 934 target_settings, 935 target_compatible_with, 936 exec_compatible_with): 937 return "\n ". join ([ _build_file_for_toolchain_hub_template . format ( 938name = toolchain_name , 939exec_constraint_sets_serialized = json . encode ( exec_compatible_with [ toolchain_name ]), 940target_constraint_sets_serialized = json . encode ( target_compatible_with [ toolchain_name ]), 941target_settings_serialized = json . encode ( target_settings [ toolchain_name ]) if toolchain_name in target_settings else "None" , 942toolchain = toolchain_labels [ toolchain_name ], 943toolchain_type = toolchain_types [ toolchain_name ], 944) for toolchain_name in toolchain_names ]) 945 946def _toolchain_repository_hub_impl ( repository_ctx ): 947repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 948repository_ctx . name , 949)) 950 951repository_ctx . file ( "BUILD.bazel" , BUILD_for_toolchain_hub ( 952toolchain_names = repository_ctx . attr . toolchain_names , 953toolchain_labels = repository_ctx . attr . toolchain_labels , 954toolchain_types = repository_ctx . attr . toolchain_types , 955target_settings = repository_ctx . attr . target_settings , 956target_compatible_with = repository_ctx . attr . target_compatible_with , 957exec_compatible_with = repository_ctx . attr . exec_compatible_with , 958)) 959 960toolchain_repository_hub = repository_rule ( 961doc = ( 962"Generates a toolchain-bearing repository that declares a set of other toolchains from other " + 963"repositories. This exists to allow registering a set of toolchains in one go with the `:all` target." 964), 965attrs = { 966"exec_compatible_with" : attr . string_list_dict ( 967doc = "A list of constraints for the execution platform for this toolchain, keyed by toolchain name." , 968mandatory = True , 969), 970"target_compatible_with" : attr . string_list_dict ( 971doc = "A list of constraints for the target platform for this toolchain, keyed by toolchain name." , 972mandatory = True , 973), 974"target_settings" : attr . string_list_dict ( 975doc = "A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution." , 976mandatory = True , 977), 978"toolchain_labels" : attr . string_dict ( 979doc = "The name of the toolchain implementation target, keyed by toolchain name." , 980mandatory = True , 981), 982"toolchain_names" : attr . string_list ( 983mandatory = True , 984), 985"toolchain_types" : attr . string_dict ( 986doc = "The toolchain type of the toolchain to declare, keyed by toolchain name." , 987mandatory = True , 988), 989}, 990implementation = _toolchain_repository_hub_impl , 991)