fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
43e5279e
cleanup
1"""Repository rules for defining Rust dependencies and toolchains""" 2 3load ( "@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" ) 4load ( "@bazel_tools//tools/build_defs/repo:utils.bzl" , "maybe" ) 5load ( "//rust/platform:triple.bzl" , "get_host_triple" , "triple" ) 6load ( "//rust/platform:triple_mappings.bzl" , "triple_to_constraint_set" ) 7load ( "//rust/private:common.bzl" , "rust_common" ) 8load ( 9"//rust/private:repository_utils.bzl" , 10"BUILD_for_rust_analyzer_proc_macro_srv" , 11"BUILD_for_rust_analyzer_toolchain" , 12"BUILD_for_rust_toolchain" , 13"BUILD_for_rustfmt_toolchain" , 14"BUILD_for_toolchain" , 15"DEFAULT_EXTRA_TARGET_TRIPLES" , 16"DEFAULT_NIGHTLY_VERSION" , 17"DEFAULT_STATIC_RUST_URL_TEMPLATES" , 18"TINYJSON_KWARGS" , 19"check_version_valid" , 20"includes_rust_analyzer_proc_macro_srv" , 21"load_cargo" , 22"load_clippy" , 23"load_llvm_tools" , 24"load_rust_compiler" , 25"load_rust_src" , 26"load_rust_stdlib" , 27"load_rustc_dev_nightly" , 28"load_rustfmt" , 29"select_rust_version" , 30"toolchain_repository_hub" , 31_load_arbitrary_tool = "load_arbitrary_tool" , 32) 33 34# Re-export `load_arbitrary_tool` as it's historically been used in external repositories. 35load_arbitrary_tool = _load_arbitrary_tool 36 37# Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if you remove it or change its format, you will also need to update that code. 38DEFAULT_TOOLCHAIN_TRIPLES = { 39"aarch64-apple-darwin" : "rust_darwin_aarch64" , 40"aarch64-pc-windows-msvc" : "rust_windows_aarch64" , 41"aarch64-unknown-linux-gnu" : "rust_linux_aarch64" , 42"s390x-unknown-linux-gnu" : "rust_linux_s390x" , 43"x86_64-apple-darwin" : "rust_darwin_x86_64" , 44"x86_64-pc-windows-msvc" : "rust_windows_x86_64" , 45"x86_64-unknown-freebsd" : "rust_freebsd_x86_64" , 46"x86_64-unknown-linux-gnu" : "rust_linux_x86_64" , 47} 48 49def rules_rust_dependencies (): 50"""Dependencies used in the implementation of `rules_rust`.""" 51 52maybe ( 53http_archive , 54name = "platforms" , 55urls = [ 56"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.10/platforms-0.0.10.tar.gz" , 57"https://github.com/bazelbuild/platforms/releases/download/0.0.10/platforms-0.0.10.tar.gz" , 58], 59sha256 = "218efe8ee736d26a3572663b374a253c012b716d8af0c07e842e82f238a0a7ee" , 60) 61 62# Avoid the following issue https://github.com/bazelbuild/rules_cc/issues/274 63rules_cc_kwargs = {} 64if native . bazel_version . startswith (( "6" , "7" )): 65rules_cc_kwargs . update ({ 66"patch_args" : [ "-p1" ], 67"patches" : [ Label ( "//rust/private/3rdparty:rules_cc.patch" )], 68}) 69maybe ( 70http_archive , 71name = "rules_cc" , 72urls = [ "https://github.com/bazelbuild/rules_cc/releases/download/0.0.17/rules_cc-0.0.17.tar.gz" ], 73sha256 = "abc605dd850f813bb37004b77db20106a19311a96b2da1c92b789da529d28fe1" , 74strip_prefix = "rules_cc-0.0.17" , 75** rules_cc_kwargs 76) 77maybe ( 78http_archive , 79name = "rules_license" , 80urls = [ 81"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/1.0.0/rules_license-1.0.0.tar.gz" , 82"https://github.com/bazelbuild/rules_license/releases/download/1.0.0/rules_license-1.0.0.tar.gz" , 83], 84sha256 = "26d4021f6898e23b82ef953078389dd49ac2b5618ac564ade4ef87cced147b38" , 85) 86maybe ( 87http_archive , 88name = "bazel_features" , 89sha256 = "af3d4fb1cf4f25942cb4a933b1ad93a0ea9fe9ee70c2af7f369fb72a67c266e5" , 90strip_prefix = "bazel_features-1.21.0" , 91url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.21.0/bazel_features-v1.21.0.tar.gz" , 92) 93 94maybe ( 95http_archive , 96name = "bazel_skylib" , 97sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f" , 98urls = [ 99"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz" , 100"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz" , 101], 102) 103 104# Make the iOS simulator constraint available, which is referenced in abi_to_constraints() 105# rules_rust does not require this dependency; it is just imported as a convenience for users. 106maybe ( 107http_archive , 108name = "build_bazel_apple_support" , 109sha256 = "b53f6491e742549f13866628ddffcc75d1f3b2d6987dc4f14a16b242113c890b" , 110url = "https://github.com/bazelbuild/apple_support/releases/download/1.17.1/apple_support.1.17.1.tar.gz" , 111) 112 113# process_wrapper needs a low-dependency way to process json. 114maybe ( 115http_archive , 116** TINYJSON_KWARGS 117) 118 119_RUST_TOOLCHAIN_VERSIONS = [ 120rust_common . default_version , 121DEFAULT_NIGHTLY_VERSION , 122] 123 124# buildifier: disable=unnamed-macro 125def rust_register_toolchains ( 126dev_components = False , 127edition = None , 128allocator_library = None , 129global_allocator_library = None , 130register_toolchains = True , 131rustfmt_version = None , 132rust_analyzer_version = None , 133sha256s = None , 134extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES , 135extra_rustc_flags = None , 136extra_exec_rustc_flags = None , 137urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 138versions = _RUST_TOOLCHAIN_VERSIONS , 139aliases = {}): 140"""Emits a default set of toolchains for Linux, MacOS, and Freebsd 141 142Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \ 143other hosts or for additional target triples. 144 145The `sha256s` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example: 146```python 147{ 148"rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5", 149"rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0", 150"rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc", 151} 152``` 153This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive \ 154list of known checksums.. 155 156See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. 157 158Args: 159dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". 160edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every target is required to specify its `edition` attribute. 161allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 162global_allocator_library (str, optional): Target that provides allocator functions when global allocator is used with cc_common.link. 163register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets. 164rustfmt_version (str, optional): The version of rustfmt. If none is supplied and only a single version in `versions` is given, then this defaults to that version, otherwise will default to the default nightly version. 165rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer. 166sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. 167extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support. 168extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. 169extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. 170urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). 171versions (list, optional): A list of toolchain versions to download. This parameter only accepts one versions 172per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`. 173aliases (dict, optional): A mapping of "full" repository name to another name to use instead. 174""" 175if not rustfmt_version : 176if len ( versions ) == 1 : 177rustfmt_version = versions [ 0 ] 178else : 179rustfmt_version = DEFAULT_NIGHTLY_VERSION 180 181if dev_components : 182has_nightly = False 183for ver in versions : 184if ver . startswith ( "nightly" ): 185has_nightly = True 186 break 187if not has_nightly : 188fail ( "rustc-dev components were requested but no \"nightly\" is being registered. Please update `versions` to include a nightly version." ) 189 190if not rust_analyzer_version : 191rust_analyzer_version = select_rust_version ( versions ) 192 193# Convert to an unfrozen dict to remove mappings as they are used. This will allow us to determine if there 194# are any unused aliases requested. 195aliases = dict ( aliases ) 196 197rust_analyzer_repo_name = "rust_analyzer_{}" . format ( rust_analyzer_version . replace ( "/" , "-" )) 198 199toolchain_names = [] 200toolchain_labels = {} 201toolchain_types = {} 202exec_compatible_with_by_toolchain = {} 203target_compatible_with_by_toolchain = {} 204 205maybe ( 206rust_analyzer_toolchain_repository , 207name = rust_analyzer_repo_name , 208version = rust_analyzer_version , 209urls = urls , 210sha256s = sha256s , 211) 212 213toolchain_names . append ( rust_analyzer_repo_name ) 214toolchain_labels [ rust_analyzer_repo_name ] = "@{}_tools//:rust_analyzer_toolchain" . format ( 215rust_analyzer_repo_name , 216) 217exec_compatible_with_by_toolchain [ rust_analyzer_repo_name ] = [] 218target_compatible_with_by_toolchain [ rust_analyzer_repo_name ] = [] 219toolchain_types [ rust_analyzer_repo_name ] = "@rules_rust//rust/rust_analyzer:toolchain_type" 220 221if register_toolchains : 222native . register_toolchains ( "@{}//:toolchain" . format ( 223rust_analyzer_repo_name , 224)) 225 226for exec_triple , name in DEFAULT_TOOLCHAIN_TRIPLES . items (): 227maybe ( 228rust_repository_set , 229name = name , 230dev_components = dev_components , 231edition = edition , 232exec_triple = exec_triple , 233extra_target_triples = extra_target_triples , 234allocator_library = allocator_library , 235global_allocator_library = global_allocator_library , 236register_toolchain = register_toolchains , 237rustfmt_version = rustfmt_version , 238extra_rustc_flags = extra_rustc_flags , 239extra_exec_rustc_flags = extra_exec_rustc_flags , 240sha256s = sha256s , 241urls = urls , 242versions = versions , 243aliases = aliases , 244) 245 246rustfmt_repo_name = "rustfmt_{}__{}" . format ( rustfmt_version . replace ( "/" , "-" ), exec_triple ) 247if rustfmt_repo_name in aliases : 248rustfmt_repo_name = aliases . pop ( rustfmt_repo_name ) 249 250maybe ( 251rustfmt_toolchain_repository , 252name = rustfmt_repo_name , 253version = rustfmt_version , 254urls = urls , 255sha256s = sha256s , 256exec_triple = exec_triple , 257) 258 259if register_toolchains : 260native . register_toolchains ( "@{}//:toolchain" . format ( 261rustfmt_repo_name , 262)) 263 264for toolchain in _get_toolchain_repositories ( name , exec_triple , extra_target_triples , versions , fallback_target_compatible_with = None , aliases = aliases ): 265toolchain_names . append ( toolchain . name ) 266toolchain_labels [ toolchain . name ] = "@{}//:{}" . format ( toolchain . name + "_tools" , "rust_toolchain" ) 267exec_compatible_with_by_toolchain [ toolchain . name ] = triple_to_constraint_set ( exec_triple ) 268target_compatible_with_by_toolchain [ toolchain . name ] = toolchain . target_constraints 269toolchain_types [ toolchain . name ] = "@rules_rust//rust:toolchain" 270 271toolchain_names . append ( rustfmt_repo_name ) 272toolchain_labels [ rustfmt_repo_name ] = "@{}_tools//:rustfmt_toolchain" . format ( rustfmt_repo_name ) 273exec_compatible_with_by_toolchain [ rustfmt_repo_name ] = triple_to_constraint_set ( exec_triple ) 274target_compatible_with_by_toolchain [ rustfmt_repo_name ] = [] 275toolchain_types [ rustfmt_repo_name ] = "@rules_rust//rust/rustfmt:toolchain_type" 276 277if aliases : 278fail ( "No repositories were created matching the requested names to alias:\n {} ".format(" \n ". join ( sorted ( aliases )))) 279 280toolchain_repository_hub ( 281name = "rust_toolchains" , 282toolchain_names = toolchain_names , 283toolchain_labels = toolchain_labels , 284toolchain_types = toolchain_types , 285exec_compatible_with = exec_compatible_with_by_toolchain , 286target_compatible_with = target_compatible_with_by_toolchain , 287) 288 289# buildifier: disable=unnamed-macro 290def rust_repositories ( ** kwargs ): 291"""**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \ 292and [rust_register_toolchains](#rust_register_toolchains) directly. 293 294Args: 295**kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro. 296""" 297rules_rust_dependencies () 298 299rust_register_toolchains ( ** kwargs ) 300 301_RUST_TOOLCHAIN_REPOSITORY_ATTRS = { 302"allocator_library" : attr . string ( 303doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." , 304default = "@rules_rust//ffi/cc/allocator_library" , 305), 306"auth" : attr . string_dict ( 307doc = ( 308"Auth object compatible with repository_ctx.download to use when downloading files. " + 309"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 310), 311), 312"auth_patterns" : attr . string_list ( 313doc = "A list of patterns to match against urls for which the auth object should be used." , 314), 315"dev_components" : attr . bool ( 316doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\"." , 317default = False , 318), 319"edition" : attr . string ( 320doc = ( 321"The rust edition to be used by default (2015, 2018, or 2021). " + 322"If absent, every rule is required to specify its `edition` attribute." 323), 324), 325"exec_triple" : attr . string ( 326doc = "The Rust-style target that this compiler runs on" , 327mandatory = True , 328), 329"extra_exec_rustc_flags" : attr . string_list ( 330doc = "Extra flags to pass to rustc in exec configuration" , 331), 332"extra_rustc_flags" : attr . string_list ( 333doc = "Extra flags to pass to rustc in non-exec configuration" , 334), 335"global_allocator_library" : attr . string ( 336doc = "Target that provides allocator functions when a global allocator is used with cc_common.link." , 337default = "@rules_rust//ffi/cc/global_allocator_library" , 338), 339"netrc" : attr . string ( 340doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive" , 341), 342"opt_level" : attr . string_dict ( 343doc = "Rustc optimization levels. For more details see the documentation for `rust_toolchain.opt_level`." , 344), 345"rustfmt_version" : attr . string ( 346doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 347), 348"sha256s" : attr . string_dict ( 349doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details." , 350), 351"target_triple" : attr . string ( 352doc = "The Rust-style target that this compiler builds for." , 353mandatory = True , 354), 355"urls" : attr . string_list ( 356doc = "A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format)." , 357default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 358), 359"version" : attr . string ( 360doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 361mandatory = True , 362), 363} 364 365def _rust_toolchain_tools_repository_impl ( ctx ): 366"""The implementation of the rust toolchain tools repository rule.""" 367sha256s = dict ( ctx . attr . sha256s ) 368iso_date = None 369version = ctx . attr . version 370version_array = version . split ( "/" ) 371if len ( version_array ) > 1 : 372version = version_array [ 0 ] 373iso_date = version_array [ 1 ] 374 375check_version_valid ( ctx . attr . version , iso_date ) 376 377exec_triple = triple ( ctx . attr . exec_triple ) 378 379rustc_content , rustc_sha256 = load_rust_compiler ( 380ctx = ctx , 381iso_date = iso_date , 382target_triple = exec_triple , 383version = version , 384) 385clippy_content , clippy_sha256 = load_clippy ( 386ctx = ctx , 387iso_date = iso_date , 388target_triple = exec_triple , 389version = version , 390) 391cargo_content , cargo_sha256 = load_cargo ( 392ctx = ctx , 393iso_date = iso_date , 394target_triple = exec_triple , 395version = version , 396) 397 398build_components = [ 399rustc_content , 400clippy_content , 401cargo_content , 402] 403sha256s . update ( rustc_sha256 | clippy_sha256 | cargo_sha256 ) 404 405if ctx . attr . rustfmt_version : 406rustfmt_version = ctx . attr . rustfmt_version 407rustfmt_iso_date = None 408if rustfmt_version in ( "nightly" , "beta" ): 409if iso_date : 410rustfmt_iso_date = iso_date 411else : 412fail ( "`rustfmt_version` does not include an iso_date. The following reposiotry should either set `iso_date` or update `rustfmt_version` to include an iso_date suffix: {}" . format ( 413ctx . name , 414)) 415elif rustfmt_version . startswith (( "nightly" , "beta" )): 416rustfmt_version , _ , rustfmt_iso_date = rustfmt_version . partition ( "/" ) 417rustfmt_content , rustfmt_sha256 = load_rustfmt ( 418ctx = ctx , 419target_triple = triple ( ctx . attr . exec_triple ), 420version = rustfmt_version , 421iso_date = rustfmt_iso_date , 422) 423build_components . append ( rustfmt_content ) 424sha256s . update ( rustfmt_sha256 ) 425 426# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib 427include_llvm_tools = version >= "1.45.0" or ( version == "nightly" and iso_date > "2020-05-22" ) 428if include_llvm_tools : 429llvm_tools_content , llvm_tools_sha256 = load_llvm_tools ( 430ctx = ctx , 431target_triple = exec_triple , 432version = version , 433iso_date = iso_date , 434) 435build_components . append ( llvm_tools_content ) 436sha256s . update ( llvm_tools_sha256 ) 437 438target_triple = triple ( ctx . attr . target_triple ) 439rust_stdlib_content , rust_stdlib_sha256 = load_rust_stdlib ( 440ctx = ctx , 441target_triple = target_triple , 442version = version , 443iso_date = iso_date , 444) 445build_components . append ( rust_stdlib_content ) 446sha256s . update ( rust_stdlib_sha256 ) 447 448stdlib_linkflags = None 449if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx . os . environ : 450stdlib_linkflags = ctx . os . environ [ "BAZEL_RUST_STDLIB_LINKFLAGS" ]. split ( ":" ) 451 452build_components . append ( BUILD_for_rust_toolchain ( 453name = "rust_toolchain" , 454exec_triple = exec_triple , 455allocator_library = ctx . attr . allocator_library , 456global_allocator_library = ctx . attr . global_allocator_library , 457target_triple = target_triple , 458stdlib_linkflags = stdlib_linkflags , 459default_edition = ctx . attr . edition , 460include_rustfmt = not ( not ctx . attr . rustfmt_version ), 461include_llvm_tools = include_llvm_tools , 462extra_rustc_flags = ctx . attr . extra_rustc_flags , 463extra_exec_rustc_flags = ctx . attr . extra_exec_rustc_flags , 464opt_level = ctx . attr . opt_level if ctx . attr . opt_level else None , 465)) 466 467# Not all target triples are expected to have dev components 468if ctx . attr . dev_components : 469rustc_dev_sha256 = load_rustc_dev_nightly ( 470ctx = ctx , 471target_triple = target_triple , 472version = version , 473iso_date = iso_date , 474) 475sha256s . update ( rustc_dev_sha256 ) 476 477ctx . file ( "WORKSPACE.bazel" , "" ) 478ctx . file ( "BUILD.bazel" , "\n ". join ( build_components )) 479 480repro = { "name" : ctx . name } 481for key in _RUST_TOOLCHAIN_REPOSITORY_ATTRS : 482repro [ key ] = getattr ( ctx . attr , key ) 483repro [ "sha256s" ] = sha256s 484 485return repro 486 487rust_toolchain_tools_repository = repository_rule ( 488doc = ( 489"Composes a single workspace containing the toolchain components for compiling on a given " + 490 "platform to a series of target platforms.\n "+ 491 "\n "+ 492"A given instance of this rule should be accompanied by a toolchain_repository_proxy " + 493"invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " + 494"selection from toolchain fetching." 495), 496attrs = _RUST_TOOLCHAIN_REPOSITORY_ATTRS , 497implementation = _rust_toolchain_tools_repository_impl , 498) 499 500def _toolchain_repository_proxy_impl ( repository_ctx ): 501repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 502repository_ctx . name , 503)) 504 505repository_ctx . file ( "BUILD.bazel" , BUILD_for_toolchain ( 506name = "toolchain" , 507toolchain = repository_ctx . attr . toolchain , 508target_settings = repository_ctx . attr . target_settings , 509toolchain_type = repository_ctx . attr . toolchain_type , 510target_compatible_with = repository_ctx . attr . target_compatible_with , 511exec_compatible_with = repository_ctx . attr . exec_compatible_with , 512)) 513 514toolchain_repository_proxy = repository_rule ( 515doc = ( 516"Generates a toolchain-bearing repository that declares the toolchains from some other " + 517"rust_toolchain_repository." 518), 519attrs = { 520"exec_compatible_with" : attr . string_list ( 521doc = "A list of constraints for the execution platform for this toolchain." , 522), 523"target_compatible_with" : attr . string_list ( 524doc = "A list of constraints for the target platform for this toolchain." , 525), 526"target_settings" : attr . string_list ( 527doc = "A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution." , 528), 529"toolchain" : attr . string ( 530doc = "The name of the toolchain implementation target." , 531mandatory = True , 532), 533"toolchain_type" : attr . string ( 534doc = "The toolchain type of the toolchain to declare" , 535mandatory = True , 536), 537}, 538implementation = _toolchain_repository_proxy_impl , 539) 540 541# For legacy support 542rust_toolchain_repository_proxy = toolchain_repository_proxy 543 544# N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) 545# without actually downloading the toolchain. 546def rust_toolchain_repository ( 547name , 548version , 549exec_triple , 550target_triple , 551exec_compatible_with = None , 552target_compatible_with = None , 553target_settings = [], 554channel = None , 555allocator_library = None , 556global_allocator_library = None , 557rustfmt_version = None , 558edition = None , 559dev_components = False , 560extra_rustc_flags = None , 561extra_exec_rustc_flags = None , 562opt_level = None , 563sha256s = None , 564urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 565auth = None , 566netrc = None , 567auth_patterns = None ): 568"""Assembles a remote repository for the given toolchain params, produces a proxy repository \ 569to contain the toolchain declaration, and registers the toolchains. 570 571Args: 572name (str): The name of the generated repository 573version (str): The version of the tool among "nightly", "beta", or an exact version. 574exec_triple (str): The Rust-style target that this compiler runs on. 575target_triple (str): The Rust-style target to build for. 576channel (str, optional): The channel of the Rust toolchain. 577exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 578target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain. 579target_settings (list, optional): A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution. 580allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 581global_allocator_library (str, optional): Target that provides allocator functions when a global allocator is used with cc_common.link. 582rustfmt_version (str, optional): The version of rustfmt to be associated with the 583toolchain. 584edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute. 585dev_components (bool, optional): Whether to download the rustc-dev components. 586Requires version to be "nightly". Defaults to False. 587extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration. 588extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. 589opt_level (dict, optional): Optimization level config for this toolchain. 590sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 591[rust_register_toolchains](#rust_register_toolchains) for more details. 592urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.xz'] 593auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 594See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 595netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive 596auth_patterns (list, optional): A list of patterns to match against urls for which the auth object should be used. 597 598Returns: 599str: The name of the registerable toolchain created by this rule. 600""" 601if exec_compatible_with == None : 602exec_compatible_with = triple_to_constraint_set ( exec_triple ) 603 604if target_compatible_with == None : 605target_compatible_with = triple_to_constraint_set ( target_triple ) 606 607tools_repo_name = "{}_tools" . format ( name ) 608 609rust_toolchain_tools_repository ( 610name = tools_repo_name , 611exec_triple = exec_triple , 612allocator_library = allocator_library , 613global_allocator_library = global_allocator_library , 614target_triple = target_triple , 615version = version , 616rustfmt_version = rustfmt_version , 617edition = edition , 618dev_components = dev_components , 619extra_rustc_flags = extra_rustc_flags , 620extra_exec_rustc_flags = extra_exec_rustc_flags , 621opt_level = opt_level , 622sha256s = sha256s , 623urls = urls , 624auth = auth , 625netrc = netrc , 626auth_patterns = auth_patterns , 627) 628 629channel_target_settings = [ "@rules_rust//rust/toolchain/channel:{}" . format ( channel )] if channel else [] 630 631toolchain_repository_proxy ( 632name = name , 633toolchain = "@{}//:rust_toolchain" . format ( tools_repo_name ), 634target_settings = channel_target_settings + target_settings , 635toolchain_type = "@rules_rust//rust:toolchain" , 636exec_compatible_with = exec_compatible_with , 637target_compatible_with = target_compatible_with , 638) 639 640return "@{name}//:toolchain" . format ( 641name = name , 642) 643 644_RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS = { 645"auth" : attr . string_dict ( 646doc = ( 647"Auth object compatible with repository_ctx.download to use when downloading files. " + 648"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 649), 650), 651"auth_patterns" : attr . string_list ( 652doc = "A list of patterns to match against urls for which the auth object should be used." , 653), 654"netrc" : attr . string ( 655doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive" , 656), 657"sha256s" : attr . string_dict ( 658doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details." , 659), 660"urls" : attr . string_list ( 661doc = "A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format)." , 662default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 663), 664"version" : attr . string ( 665doc = "The version of the tool among \"nightly\", \"beta\" with an iso date, or an exact version." , 666mandatory = True , 667), 668} 669 670def _rust_analyzer_toolchain_tools_repository_impl ( repository_ctx ): 671sha256s = dict ( repository_ctx . attr . sha256s ) 672 673iso_date = None 674version = repository_ctx . attr . version 675version_array = version . split ( "/" ) 676if len ( version_array ) > 1 : 677version = version_array [ 0 ] 678iso_date = version_array [ 1 ] 679 680sha256s . update ( load_rust_src ( 681ctx = repository_ctx , 682iso_date = iso_date , 683version = version , 684)) 685 686repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 687repository_ctx . name , 688)) 689 690host_triple = get_host_triple ( repository_ctx ) 691rustc_content , rustc_sha256 = load_rust_compiler ( 692ctx = repository_ctx , 693iso_date = iso_date , 694target_triple = host_triple , 695version = version , 696) 697build_contents = [ rustc_content ] 698sha256s . update ( rustc_sha256 ) 699rustc = "//:rustc" 700 701proc_macro_srv = None 702if includes_rust_analyzer_proc_macro_srv ( version , iso_date ): 703build_contents . append ( BUILD_for_rust_analyzer_proc_macro_srv ( host_triple )) 704proc_macro_srv = "//:rust_analyzer_proc_macro_srv" 705 706build_contents . append ( BUILD_for_rust_analyzer_toolchain ( 707name = "rust_analyzer_toolchain" , 708rustc = rustc , 709proc_macro_srv = proc_macro_srv , 710)) 711 712repository_ctx . file ( "BUILD.bazel" , "\n ". join ( build_contents )) 713repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 714repository_ctx . name , 715)) 716 717repro = { "name" : repository_ctx . name } 718for key in _RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS : 719repro [ key ] = getattr ( repository_ctx . attr , key ) 720repro [ "sha256s" ] = sha256s 721 722return repro 723 724rust_analyzer_toolchain_tools_repository = repository_rule ( 725doc = "A repository rule for defining a rust_analyzer_toolchain with a `rust-src` artifact." , 726implementation = _rust_analyzer_toolchain_tools_repository_impl , 727attrs = _RUST_ANALYZER_TOOLCHAIN_TOOLS_REPOSITORY_ATTRS , 728) 729 730def rust_analyzer_toolchain_repository ( 731name , 732version , 733exec_compatible_with = [], 734target_compatible_with = [], 735sha256s = None , 736urls = None , 737auth = None , 738netrc = None , 739auth_patterns = None ): 740"""Assemble a remote rust_analyzer_toolchain target based on the given params. 741 742Args: 743name (str): The name of the toolchain proxy repository contianing the registerable toolchain. 744version (str): The version of the tool among "nightly", "beta', or an exact version. 745exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 746target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain. 747sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 748[rust_register_toolchains](#rust_register_toolchains) for more details. 749urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.xz'] 750auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 751See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 752netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive 753auth_patterns (dict, optional): Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive 754 755Returns: 756str: The name of a registerable rust_analyzer_toolchain. 757""" 758rust_analyzer_toolchain_tools_repository ( 759name = name + "_tools" , 760version = version , 761sha256s = sha256s , 762urls = urls , 763auth = auth , 764netrc = netrc , 765auth_patterns = auth_patterns , 766) 767 768toolchain_repository_proxy ( 769name = name , 770toolchain = "@{}//:{}" . format ( name + "_tools" , "rust_analyzer_toolchain" ), 771toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type" , 772exec_compatible_with = exec_compatible_with , 773target_compatible_with = target_compatible_with , 774) 775 776return "@{}//:toolchain" . format ( 777name , 778) 779 780_RUSTFMT_TOOLCHAIN_TOOLS_ATTRS = { 781"auth" : attr . string_dict ( 782doc = ( 783"Auth object compatible with repository_ctx.download to use when downloading files. " + 784"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 785), 786), 787"auth_patterns" : attr . string_dict ( 788doc = "Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive" , 789), 790"exec_triple" : attr . string ( 791doc = "The Rust-style triple Rustfmt is expected to run on." , 792mandatory = True , 793), 794"netrc" : attr . string ( 795doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive" , 796), 797"sha256s" : attr . string_dict ( 798doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_register_toolchains](#rust_register_toolchains) for more details." , 799), 800"urls" : attr . string_list ( 801doc = "A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format)." , 802default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 803), 804"version" : attr . string ( 805doc = "The version of the tool among \"nightly\", \"beta\" with an iso date, or an exact version." , 806mandatory = True , 807), 808} 809 810def _rustfmt_toolchain_tools_repository_impl ( repository_ctx ): 811sha256s = dict ( repository_ctx . attr . sha256s ) 812repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 813repository_ctx . name , 814)) 815 816iso_date = None 817version = repository_ctx . attr . version 818version_array = version . split ( "/" ) 819if len ( version_array ) > 1 : 820version = version_array [ 0 ] 821iso_date = version_array [ 1 ] 822 823rustfmt = "//:rustfmt_bin" 824rustc = "//:rustc" 825rustc_lib = "//:rustc_lib" 826 827exec_triple = triple ( repository_ctx . attr . exec_triple ) 828 829rustc_content , rustc_sha256 = load_rust_compiler ( 830ctx = repository_ctx , 831iso_date = iso_date , 832target_triple = exec_triple , 833version = version , 834) 835rustfmt_content , rustfmt_sha256 = load_rustfmt ( 836ctx = repository_ctx , 837iso_date = iso_date , 838target_triple = exec_triple , 839version = version , 840) 841 842build_contents = [ 843rustc_content , 844rustfmt_content , 845BUILD_for_rustfmt_toolchain ( 846name = "rustfmt_toolchain" , 847rustfmt = rustfmt , 848rustc = rustc , 849rustc_lib = rustc_lib , 850), 851] 852sha256s . update ( rustc_sha256 | rustfmt_sha256 ) 853 854repository_ctx . file ( "BUILD.bazel" , "\n ". join ( build_contents )) 855repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 856repository_ctx . name , 857)) 858 859repro = { "name" : repository_ctx . name } 860for key in _RUSTFMT_TOOLCHAIN_TOOLS_ATTRS : 861repro [ key ] = getattr ( repository_ctx . attr , key ) 862repro [ "sha256s" ] = sha256s 863 864return repro 865 866rustfmt_toolchain_tools_repository = repository_rule ( 867doc = "A repository rule for defining a rustfmt_toolchain." , 868attrs = _RUSTFMT_TOOLCHAIN_TOOLS_ATTRS , 869implementation = _rustfmt_toolchain_tools_repository_impl , 870) 871 872def rustfmt_toolchain_repository ( 873name , 874version , 875exec_triple , 876exec_compatible_with = None , 877target_compatible_with = None , 878channel = None , 879sha256s = None , 880urls = None , 881auth = None , 882netrc = None , 883auth_patterns = None ): 884"""Assemble a remote rustfmt_toolchain target based on the given params. 885 886Args: 887name (str): The name of the toolchain proxy repository contianing the registerable toolchain. 888version (str): The version of the tool among "nightly", "beta', or an exact version. 889exec_triple (str): The platform triple Rustfmt is expected to run on. 890exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 891target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain. 892channel (str, optional): The channel value to with which to constrain the toolchain. 893sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 894[rust_register_toolchains](#rust_register_toolchains) for more details. 895urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.xz'] 896auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 897See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 898netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive 899auth_patterns (dict, optional): Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive 900 901Returns: 902str: The name of a registerable rustfmt_toolchain. 903""" 904if exec_compatible_with == None : 905exec_compatible_with = triple_to_constraint_set ( exec_triple ) 906 907rustfmt_toolchain_tools_repository ( 908name = name + "_tools" , 909version = version , 910sha256s = sha256s , 911urls = urls , 912auth = auth , 913netrc = netrc , 914auth_patterns = auth_patterns , 915exec_triple = exec_triple , 916) 917 918toolchain_repository_proxy ( 919name = name , 920toolchain = "@{}//:{}" . format ( name + "_tools" , "rustfmt_toolchain" ), 921toolchain_type = "@rules_rust//rust/rustfmt:toolchain_type" , 922target_settings = [ "@rules_rust//rust/toolchain/channel:{}" . format ( channel )] if channel else None , 923exec_compatible_with = exec_compatible_with , 924target_compatible_with = target_compatible_with , 925) 926 927return "@{}//:toolchain" . format ( 928name , 929) 930 931def _rust_toolchain_set_repository_impl ( repository_ctx ): 932repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 933repository_ctx . name , 934)) 935 936repository_ctx . file ( "BUILD.bazel" , """exports_files(["defs.bzl"])""" ) 937repository_ctx . file ( "defs.bzl" , "ALL_TOOLCHAINS = {}\n ". format ( 938json . encode_indent ( repository_ctx . attr . toolchains , indent = " " * 4 ), 939)) 940 941rust_toolchain_set_repository = repository_rule ( 942doc = ( 943"Generates a toolchain-bearing repository that declares the toolchains from some other " + 944"rust_toolchain_repository." 945), 946attrs = { 947"toolchains" : attr . string_list ( 948doc = "The list of all toolchains created by the current `rust_toolchain_set`" , 949mandatory = True , 950), 951}, 952implementation = _rust_toolchain_set_repository_impl , 953) 954 955def _get_toolchain_repositories ( name , exec_triple , extra_target_triples , versions , fallback_target_compatible_with , aliases = {}): 956extra_target_triples_list = extra_target_triples . keys () if type ( extra_target_triples ) == "dict" else extra_target_triples 957 958toolchain_repos = [] 959 960for target_triple in depset ([ exec_triple ] + extra_target_triples_list ). to_list (): 961# Parse all provided versions while checking for duplicates 962channels = {} 963for version in versions : 964if version . startswith (( "beta" , "nightly" )): 965channel , _ , _ = version . partition ( "/" ) 966else : 967channel = "stable" 968 969if channel in channels : 970fail ( "Duplicate {} channels provided for {}: {}" . format ( channel , name , versions )) 971 972channels . update ({ channel : struct ( 973name = channel , 974version = version , 975)}) 976 977if type ( extra_target_triples ) == "dict" and target_triple in extra_target_triples : 978target_constraints = extra_target_triples [ target_triple ] 979elif fallback_target_compatible_with != None : 980target_constraints = fallback_target_compatible_with 981else : 982target_constraints = triple_to_constraint_set ( target_triple ) 983 984# Define toolchains for each requested version 985for channel in channels . values (): 986# Check if this toolchain is requested to be aliased. 987full_name = "{}__{}__{}" . format ( name , target_triple , channel . name ) 988if full_name in aliases : 989full_name = aliases . pop ( full_name ) 990toolchain_repos . append ( struct ( 991name = full_name , 992target_triple = target_triple , 993channel = channel , 994target_constraints = target_constraints , 995)) 996 997return toolchain_repos 998 999def rust_repository_set ( 1000name , 1001versions , 1002exec_triple , 1003target_settings = [], 1004allocator_library = None , 1005global_allocator_library = None , 1006extra_target_triples = {}, 1007rustfmt_version = None , 1008edition = None , 1009dev_components = False , 1010extra_rustc_flags = None , 1011extra_exec_rustc_flags = None , 1012opt_level = None , 1013sha256s = None , 1014urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 1015auth = None , 1016netrc = None , 1017auth_patterns = None , 1018register_toolchain = True , 1019exec_compatible_with = None , 1020default_target_compatible_with = None , 1021aliases = {}): 1022"""Assembles a remote repository for the given toolchain params, produces a proxy repository \ 1023to contain the toolchain declaration, and registers the toolchains. 1024 1025Args: 1026name (str): The name of the generated repository 1027versions (list, optional): A list of toolchain versions to download. This paramter only accepts one versions 1028per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`. 1029exec_triple (str): The Rust-style target that this compiler runs on 1030target_settings (list, optional): A list of config_settings that must be satisfied by the target configuration in order for this set of toolchains to be selected during toolchain resolution. 1031allocator_library (str, optional): Target that provides allocator functions when rust_library targets are 1032embedded in a cc_binary. 1033global_allocator_library (str, optional): Target that provides allocator functions a global allocator is used with cc_common.link. 1034extra_target_triples (list or map, optional): Additional rust-style targets that this set of 1035toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. 1036rustfmt_version (str, optional): The version of rustfmt to be associated with the 1037toolchain. 1038edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is 1039required to specify its `edition` attribute. 1040dev_components (bool, optional): Whether to download the rustc-dev components. 1041Requires version to be "nightly". 1042extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. 1043extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. 1044opt_level (dict, dict, optional): Dictionary of target triples to optimiztion config. 1045sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 1046[rust_register_toolchains](#rust_register_toolchains) for more details. 1047urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These 1048must contain the '{}' used to substitute the tool being fetched (using .format). 1049auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 1050See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 1051netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive 1052auth_patterns (dict, optional): Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive 1053 1054register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain. 1055exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 1056default_target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. 1057aliases (dict): Replacement names to use for toolchains created by this macro. 1058""" 1059 1060all_toolchain_names = [] 1061for toolchain in _get_toolchain_repositories ( name , exec_triple , extra_target_triples , versions , default_target_compatible_with , aliases ): 1062# Infer toolchain-specific rustc flags depending on the type (list, dict, optional) of extra_rustc_flags 1063if extra_rustc_flags == None : 1064toolchain_extra_rustc_flags = [] 1065elif type ( extra_rustc_flags ) == "list" : 1066toolchain_extra_rustc_flags = extra_rustc_flags 1067elif type ( extra_rustc_flags ) == "dict" : 1068toolchain_extra_rustc_flags = extra_rustc_flags . get ( toolchain . target_triple ) 1069else : 1070fail ( "extra_rustc_flags should be a list or a dict" ) 1071 1072all_toolchain_names . append ( rust_toolchain_repository ( 1073name = toolchain . name , 1074allocator_library = allocator_library , 1075global_allocator_library = global_allocator_library , 1076auth = auth , 1077netrc = netrc , 1078auth_patterns = auth_patterns , 1079channel = toolchain . channel . name , 1080dev_components = dev_components , 1081edition = edition , 1082exec_triple = exec_triple , 1083extra_exec_rustc_flags = extra_exec_rustc_flags , 1084extra_rustc_flags = toolchain_extra_rustc_flags , 1085opt_level = opt_level . get ( toolchain . target_triple ) if opt_level != None else None , 1086target_settings = target_settings , 1087rustfmt_version = rustfmt_version , 1088sha256s = sha256s , 1089target_triple = toolchain . target_triple , 1090urls = urls , 1091version = toolchain . channel . version , 1092exec_compatible_with = exec_compatible_with , 1093target_compatible_with = toolchain . target_constraints , 1094)) 1095 1096# This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper. 1097rust_toolchain_set_repository ( 1098name = name , 1099toolchains = all_toolchain_names , 1100) 1101 1102# Register toolchains 1103if register_toolchain : 1104native . register_toolchains ( * all_toolchain_names ) 1105native . register_toolchains ( str ( Label ( "//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain" ))) 1106native . register_toolchains ( str ( Label ( "//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain" )))