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