fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
2fc02f03
rust-analyzer
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" , "DEFAULT_NIGHTLY_ISO_DATE" , "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_STATIC_RUST_URL_TEMPLATES" , 16"check_version_valid" , 17"includes_rust_analyzer_proc_macro_srv" , 18"load_cargo" , 19"load_clippy" , 20"load_llvm_tools" , 21"load_rust_compiler" , 22"load_rust_src" , 23"load_rust_stdlib" , 24"load_rustc_dev_nightly" , 25"load_rustfmt" , 26"select_rust_version" , 27_load_arbitrary_tool = "load_arbitrary_tool" , 28) 29 30# Reexport `load_arbitrary_tool` as it's currently in use in https://github.com/google/cargo-raze 31load_arbitrary_tool = _load_arbitrary_tool 32 33# 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. 34DEFAULT_TOOLCHAIN_TRIPLES = { 35"aarch64-apple-darwin" : "rust_darwin_aarch64" , 36"aarch64-pc-windows-msvc" : "rust_windows_aarch64" , 37"aarch64-unknown-linux-gnu" : "rust_linux_aarch64" , 38"x86_64-apple-darwin" : "rust_darwin_x86_64" , 39"x86_64-pc-windows-msvc" : "rust_windows_x86_64" , 40"x86_64-unknown-freebsd" : "rust_freebsd_x86_64" , 41"x86_64-unknown-linux-gnu" : "rust_linux_x86_64" , 42} 43 44def rules_rust_dependencies (): 45"""Dependencies used in the implementation of `rules_rust`.""" 46 47maybe ( 48http_archive , 49name = "platforms" , 50urls = [ 51"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz" , 52"https://github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz" , 53], 54sha256 = "379113459b0feaf6bfbb584a91874c065078aa673222846ac765f86661c27407" , 55) 56maybe ( 57http_archive , 58name = "rules_cc" , 59urls = [ "https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz" ], 60sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241" , 61) 62 63maybe ( 64http_archive , 65name = "bazel_skylib" , 66urls = [ 67"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz" , 68"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz" , 69], 70sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de" , 71) 72 73# Make the iOS simulator constraint available, which is referenced in abi_to_constraints() 74# rules_rust does not require this dependency; it is just imported as a convenience for users. 75maybe ( 76http_archive , 77name = "build_bazel_apple_support" , 78sha256 = "d94b7a0f49d735f196e1f36d2e6ef79c4e8e8b82132848dd8cd93cd82d9b12a8" , 79url = "https://github.com/bazelbuild/apple_support/releases/download/1.3.0/apple_support.1.3.0.tar.gz" , 80) 81 82# process_wrapper needs a low-dependency way to process json. 83maybe ( 84http_archive , 85name = "rules_rust_tinyjson" , 86sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16" , 87url = "https://crates.io/api/v1/crates/tinyjson/2.3.0/download" , 88strip_prefix = "tinyjson-2.3.0" , 89type = "tar.gz" , 90build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel" , 91) 92 93_DEFAULT_NIGHTLY_VERSION = "nightly/{}" . format ( DEFAULT_NIGHTLY_ISO_DATE ) 94 95_RUST_TOOLCHAIN_VERSIONS = [ 96rust_common . default_version , 97_DEFAULT_NIGHTLY_VERSION , 98] 99 100# buildifier: disable=unnamed-macro 101def rust_register_toolchains ( 102dev_components = False , 103edition = None , 104allocator_library = None , 105iso_date = None , 106register_toolchains = True , 107rustfmt_version = _DEFAULT_NIGHTLY_VERSION , 108rust_analyzer_version = None , 109sha256s = None , 110extra_target_triples = [ "wasm32-unknown-unknown" , "wasm32-wasi" ], 111urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 112version = None , 113versions = []): 114"""Emits a default set of toolchains for Linux, MacOS, and Freebsd 115 116Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \ 117other hosts or for additional target triples. 118 119The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example: 120```python 121{ 122"rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5", 123"rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0", 124"rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc", 125} 126``` 127This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive \ 128list of known checksums.. 129 130See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. 131 132Args: 133dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". 134edition (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. 135allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 136iso_date (str, optional): **Deprecated**: Use `versions` instead. 137register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets. 138rustfmt_version (str, optional): The version of rustfmt. 139rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer. 140sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. 141extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support. 142urls (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). 143version (str, optional): **Deprecated**: Use `versions` instead. 144versions (list, optional): A list of toolchain versions to download. This paramter only accepts one versions 145per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`. 146""" 147if version : 148# buildifier: disable=print 149( "`rust_register.toolchains.version` is deprecated. Please use `versions` instead: https://bazelbuild.github.io/rules_rust/flatten.html#rust_register_toolchains-versions" ) 150 151if iso_date : 152# buildifier: disable=print 153( "`rust_register.toolchains.iso_date` is deprecated. Please use `versions` instead: https://bazelbuild.github.io/rules_rust/flatten.html#rust_register_toolchains-versions" ) 154 155if rustfmt_version in ( "nightly" , "beta" ): 156# buildifier: disable=print 157( "`rust_register.toolchains.rustfmt_version` now requires iso date to be included in the string. E.g. `nightly/2022-12-15`. This version will be assumed until this value is updated" ) 158rustfmt_version = "{}/{}" . format ( rustfmt_version , DEFAULT_NIGHTLY_ISO_DATE ) 159 160if not versions : 161if version : 162versions = [ version ] 163else : 164versions = _RUST_TOOLCHAIN_VERSIONS 165 166if dev_components : 167has_nightly = False 168for ver in versions : 169if ver . startswith ( "nightly" ): 170has_nightly = True 171 break 172if not has_nightly : 173fail ( "rustc-dev components were requested but no \"nightly\" is being registered. Please update `versions` to include a nightly version." ) 174 175if not rust_analyzer_version : 176rust_analyzer_version = select_rust_version ( versions ) 177 178rust_analyzer_repo_name = "rust_analyzer_{}" . format ( rust_analyzer_version . replace ( "/" , "-" )) 179rust_analyzer_iso_date = None 180if rust_analyzer_version . startswith (( "beta" , "nightly" )): 181rust_analyzer_version , _ , rust_analyzer_iso_date = rustfmt_version . partition ( "/" ) 182 183maybe ( 184rust_analyzer_toolchain_repository , 185name = rust_analyzer_repo_name , 186version = rust_analyzer_version , 187urls = urls , 188sha256s = sha256s , 189iso_date = rust_analyzer_iso_date , 190) 191 192if register_toolchains : 193native . register_toolchains ( "@{}//:toolchain" . format ( 194rust_analyzer_repo_name , 195)) 196 197rustfmt_iso_date = None 198rustfmt_version_or_channel = rustfmt_version 199if rustfmt_version . startswith (( "beta" , "nightly" )): 200rustfmt_version_or_channel , _ , rustfmt_iso_date = rustfmt_version . partition ( "/" ) 201 202for exec_triple , name in DEFAULT_TOOLCHAIN_TRIPLES . items (): 203maybe ( 204rust_repository_set , 205name = name , 206dev_components = dev_components , 207edition = edition , 208exec_triple = exec_triple , 209extra_target_triples = extra_target_triples , 210allocator_library = allocator_library , 211iso_date = iso_date , 212register_toolchain = register_toolchains , 213rustfmt_version = rustfmt_version , 214sha256s = sha256s , 215urls = urls , 216version = version , 217versions = versions , 218) 219 220rustfmt_repo_name = "rustfmt_{}__{}" . format ( rustfmt_version . replace ( "/" , "-" ), exec_triple ) 221 222maybe ( 223rustfmt_toolchain_repository , 224name = rustfmt_repo_name , 225version = rustfmt_version_or_channel , 226urls = urls , 227sha256s = sha256s , 228iso_date = rustfmt_iso_date , 229exec_triple = exec_triple , 230) 231 232if register_toolchains : 233native . register_toolchains ( "@{}//:toolchain" . format ( 234rustfmt_repo_name , 235)) 236 237# buildifier: disable=unnamed-macro 238def rust_repositories ( ** kwargs ): 239"""**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \ 240and [rust_register_toolchains](#rust_register_toolchains) directly. 241 242Args: 243**kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro. 244""" 245rules_rust_dependencies () 246 247rust_register_toolchains ( ** kwargs ) 248 249def _rust_toolchain_tools_repository_impl ( ctx ): 250"""The implementation of the rust toolchain tools repository rule.""" 251 252check_version_valid ( ctx . attr . version , ctx . attr . iso_date ) 253 254exec_triple = triple ( ctx . attr . exec_triple ) 255 256build_components = [ 257load_rust_compiler ( 258ctx = ctx , 259iso_date = ctx . attr . iso_date , 260target_triple = exec_triple , 261version = ctx . attr . version , 262), 263load_clippy ( 264ctx = ctx , 265iso_date = ctx . attr . iso_date , 266target_triple = exec_triple , 267version = ctx . attr . version , 268), 269load_cargo ( 270ctx = ctx , 271iso_date = ctx . attr . iso_date , 272target_triple = exec_triple , 273version = ctx . attr . version , 274), 275] 276 277if ctx . attr . rustfmt_version : 278rustfmt_version = ctx . attr . rustfmt_version 279rustfmt_iso_date = None 280if rustfmt_version in ( "nightly" , "beta" ): 281if ctx . attr . iso_date : 282rustfmt_iso_date = ctx . attr . iso_date 283else : 284fail ( "`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 ( 285ctx . name , 286)) 287elif rustfmt_version . startswith (( "nightly" , "beta" )): 288rustfmt_version , _ , rustfmt_iso_date = rustfmt_version . partition ( "/" ) 289build_components . append ( load_rustfmt ( 290ctx = ctx , 291target_triple = triple ( ctx . attr . exec_triple ), 292version = rustfmt_version , 293iso_date = rustfmt_iso_date , 294)) 295 296# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib 297include_llvm_tools = ctx . attr . version >= "1.45.0" or ( ctx . attr . version == "nightly" and ctx . attr . iso_date > "2020-05-22" ) 298if include_llvm_tools : 299build_components . append ( load_llvm_tools ( 300ctx = ctx , 301target_triple = exec_triple , 302)) 303 304target_triple = triple ( ctx . attr . target_triple ) 305build_components . append ( load_rust_stdlib ( 306ctx = ctx , 307target_triple = target_triple , 308)) 309 310stdlib_linkflags = None 311if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx . os . environ : 312stdlib_linkflags = ctx . os . environ [ "BAZEL_RUST_STDLIB_LINKFLAGS" ]. split ( ":" ) 313 314build_components . append ( BUILD_for_rust_toolchain ( 315name = "rust_toolchain" , 316exec_triple = exec_triple , 317allocator_library = ctx . attr . allocator_library , 318target_triple = target_triple , 319stdlib_linkflags = stdlib_linkflags , 320workspace_name = ctx . attr . name , 321default_edition = ctx . attr . edition , 322include_rustfmt = not ( not ctx . attr . rustfmt_version ), 323include_llvm_tools = include_llvm_tools , 324)) 325 326# Not all target triples are expected to have dev components 327if ctx . attr . dev_components : 328load_rustc_dev_nightly ( ctx , ctx . attr . target_triple ) 329 330ctx . file ( "WORKSPACE.bazel" , "" ) 331ctx . file ( "BUILD.bazel" , "\n ". join ( build_components )) 332 333rust_toolchain_tools_repository = repository_rule ( 334doc = ( 335"Composes a single workspace containing the toolchain components for compiling on a given " + 336 "platform to a series of target platforms.\n "+ 337 "\n "+ 338"A given instance of this rule should be accompanied by a toolchain_repository_proxy " + 339"invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " + 340"selection from toolchain fetching." 341), 342attrs = { 343"allocator_library" : attr . string ( 344doc = "Target that provides allocator functions when rust_library targets are embedded in a cc_binary." , 345), 346"auth" : attr . string_dict ( 347doc = ( 348"Auth object compatible with repository_ctx.download to use when downloading files. " + 349"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 350), 351), 352"dev_components" : attr . bool ( 353doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\"." , 354default = False , 355), 356"edition" : attr . string ( 357doc = ( 358"The rust edition to be used by default (2015, 2018, or 2021). " + 359"If absent, every rule is required to specify its `edition` attribute." 360), 361), 362"exec_triple" : attr . string ( 363doc = "The Rust-style target that this compiler runs on" , 364mandatory = True , 365), 366"iso_date" : attr . string ( 367doc = "The date of the tool (or None, if the version is a specific version)." , 368), 369"rustfmt_version" : attr . string ( 370doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 371), 372"sha256s" : attr . string_dict ( 373doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 374), 375"target_triple" : attr . string ( 376doc = "The Rust-style target that this compiler builds for." , 377mandatory = True , 378), 379"urls" : attr . string_list ( 380doc = "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)." , 381default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 382), 383"version" : attr . string ( 384doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 385mandatory = True , 386), 387}, 388implementation = _rust_toolchain_tools_repository_impl , 389) 390 391def _toolchain_repository_proxy_impl ( repository_ctx ): 392repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 393repository_ctx . name , 394)) 395 396repository_ctx . file ( "BUILD.bazel" , BUILD_for_toolchain ( 397name = "toolchain" , 398toolchain = repository_ctx . attr . toolchain , 399target_settings = repository_ctx . attr . target_settings , 400toolchain_type = repository_ctx . attr . toolchain_type , 401target_compatible_with = repository_ctx . attr . target_compatible_with , 402exec_compatible_with = repository_ctx . attr . exec_compatible_with , 403)) 404 405toolchain_repository_proxy = repository_rule ( 406doc = ( 407"Generates a toolchain-bearing repository that declares the toolchains from some other " + 408"rust_toolchain_repository." 409), 410attrs = { 411"exec_compatible_with" : attr . string_list ( 412doc = "A list of constraints for the execution platform for this toolchain." , 413), 414"target_compatible_with" : attr . string_list ( 415doc = "A list of constraints for the target platform for this toolchain." , 416), 417"target_settings" : attr . string_list ( 418doc = "A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution." , 419), 420"toolchain" : attr . string ( 421doc = "The name of the toolchain implementation target." , 422mandatory = True , 423), 424"toolchain_type" : attr . string ( 425doc = "The toolchain type of the toolchain to declare" , 426mandatory = True , 427), 428}, 429implementation = _toolchain_repository_proxy_impl , 430) 431 432# For legacy support 433rust_toolchain_repository_proxy = toolchain_repository_proxy 434 435# N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) 436# without actually downloading the toolchain. 437def rust_toolchain_repository ( 438name , 439version , 440exec_triple , 441target_triple , 442exec_compatible_with = None , 443target_compatible_with = None , 444target_settings = [], 445channel = None , 446allocator_library = None , 447iso_date = None , 448rustfmt_version = None , 449edition = None , 450dev_components = False , 451sha256s = None , 452urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 453auth = None ): 454"""Assembles a remote repository for the given toolchain params, produces a proxy repository \ 455to contain the toolchain declaration, and registers the toolchains. 456 457Args: 458name (str): The name of the generated repository 459version (str): The version of the tool among "nightly", "beta", or an exact version. 460exec_triple (str): The Rust-style target that this compiler runs on. 461target_triple (str): The Rust-style target to build for. 462channel (str, optional): The channel of the Rust toolchain. 463exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 464target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain. 465target_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. 466allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 467iso_date (str, optional): The date of the tool. 468rustfmt_version (str, optional): The version of rustfmt to be associated with the 469toolchain. 470edition (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. 471dev_components (bool, optional): Whether to download the rustc-dev components. 472Requires version to be "nightly". Defaults to False. 473sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 474[rust_repositories](#rust_repositories) for more details. 475urls (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.gz'] 476auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 477See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 478 479Returns: 480str: The name of the registerable toolchain created by this rule. 481""" 482 483if rustfmt_version in ( "nightly" , "beta" ): 484# buildifier: disable=print 485( "`rust_toolchain_repository.rustfmt_version` now requires iso date to be included in the string. E.g. `nightly/2022-12-15`. This version will be assumed until this value is updated" ) 486rustfmt_version = "{}/{}" . format ( rustfmt_version , DEFAULT_NIGHTLY_ISO_DATE ) 487 488if exec_compatible_with == None : 489exec_compatible_with = triple_to_constraint_set ( exec_triple ) 490 491if target_compatible_with == None : 492target_compatible_with = triple_to_constraint_set ( target_triple ) 493 494tools_repo_name = "{}_tools" . format ( name ) 495 496rust_toolchain_tools_repository ( 497name = tools_repo_name , 498exec_triple = exec_triple , 499allocator_library = allocator_library , 500target_triple = target_triple , 501iso_date = iso_date , 502version = version , 503rustfmt_version = rustfmt_version , 504edition = edition , 505dev_components = dev_components , 506sha256s = sha256s , 507urls = urls , 508auth = auth , 509) 510 511channel_target_settings = [ "@rules_rust//rust/toolchain/channel:{}" . format ( channel )] if channel else [] 512 513toolchain_repository_proxy ( 514name = name , 515toolchain = "@{}//:rust_toolchain" . format ( tools_repo_name ), 516target_settings = channel_target_settings + target_settings , 517toolchain_type = "@rules_rust//rust:toolchain" , 518exec_compatible_with = exec_compatible_with , 519target_compatible_with = target_compatible_with , 520) 521 522return "@{name}//:toolchain" . format ( 523name = name , 524) 525 526def _rust_analyzer_toolchain_tools_repository_impl ( repository_ctx ): 527load_rust_src ( 528ctx = repository_ctx , 529iso_date = repository_ctx . attr . iso_date , 530version = repository_ctx . attr . version , 531) 532 533repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 534repository_ctx . name , 535)) 536 537host_triple = get_host_triple ( repository_ctx ) 538build_contents = [ 539load_rust_compiler ( 540ctx = repository_ctx , 541iso_date = repository_ctx . attr . iso_date , 542target_triple = host_triple , 543version = repository_ctx . attr . version , 544), 545] 546rustc = "//:rustc" 547 548proc_macro_srv = None 549if includes_rust_analyzer_proc_macro_srv ( repository_ctx . attr . version , repository_ctx . attr . iso_date ): 550build_contents . append ( BUILD_for_rust_analyzer_proc_macro_srv ( host_triple )) 551proc_macro_srv = "//:rust_analyzer_proc_macro_srv" 552 553build_contents . append ( BUILD_for_rust_analyzer_toolchain ( 554name = "rust_analyzer_toolchain" , 555rustc = rustc , 556proc_macro_srv = proc_macro_srv , 557)) 558 559repository_ctx . file ( "BUILD.bazel" , "\n ". join ( build_contents )) 560repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 561repository_ctx . name , 562)) 563 564rust_analyzer_toolchain_tools_repository = repository_rule ( 565doc = "A repository rule for defining a rust_analyzer_toolchain with a `rust-src` artifact." , 566attrs = { 567"auth" : attr . string_dict ( 568doc = ( 569"Auth object compatible with repository_ctx.download to use when downloading files. " + 570"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 571), 572), 573"iso_date" : attr . string ( 574doc = "The date of the tool (or None, if the version is a specific version)." , 575), 576"sha256s" : attr . string_dict ( 577doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 578), 579"urls" : attr . string_list ( 580doc = "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)." , 581default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 582), 583"version" : attr . string ( 584doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 585mandatory = True , 586), 587}, 588implementation = _rust_analyzer_toolchain_tools_repository_impl , 589) 590 591def rust_analyzer_toolchain_repository ( 592name , 593version , 594exec_compatible_with = [], 595target_compatible_with = [], 596iso_date = None , 597sha256s = None , 598urls = None , 599auth = None ): 600"""Assemble a remote rust_analyzer_toolchain target based on the given params. 601 602Args: 603name (str): The name of the toolchain proxy repository contianing the registerable toolchain. 604version (str): The version of the tool among "nightly", "beta', or an exact version. 605exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 606target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain. 607iso_date (str, optional): The date of the tool. 608sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 609[rust_repositories](#rust_repositories) for more details. 610urls (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.gz'] 611auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 612See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 613 614Returns: 615str: The name of a registerable rust_analyzer_toolchain. 616""" 617rust_analyzer_toolchain_tools_repository ( 618name = name + "_tools" , 619version = version , 620iso_date = iso_date , 621sha256s = sha256s , 622urls = urls , 623auth = auth , 624) 625 626toolchain_repository_proxy ( 627name = name , 628toolchain = "@{}//:{}" . format ( name + "_tools" , "rust_analyzer_toolchain" ), 629toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type" , 630exec_compatible_with = exec_compatible_with , 631target_compatible_with = target_compatible_with , 632) 633 634return "@{}//:toolchain" . format ( 635name , 636) 637 638def _rustfmt_toolchain_tools_repository_impl ( repository_ctx ): 639repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 640repository_ctx . name , 641)) 642 643rustfmt = "//:rustfmt_bin" 644rustc = "//:rustc" 645rustc_lib = "//:rustc_lib" 646 647exec_triple = triple ( repository_ctx . attr . exec_triple ) 648 649build_contents = [ 650load_rust_compiler ( 651ctx = repository_ctx , 652iso_date = repository_ctx . attr . iso_date , 653target_triple = exec_triple , 654version = repository_ctx . attr . version , 655), 656load_rustfmt ( 657ctx = repository_ctx , 658iso_date = repository_ctx . attr . iso_date , 659target_triple = exec_triple , 660version = repository_ctx . attr . version , 661), 662BUILD_for_rustfmt_toolchain ( 663name = "rustfmt_toolchain" , 664rustfmt = rustfmt , 665rustc = rustc , 666rustc_lib = rustc_lib , 667), 668] 669 670repository_ctx . file ( "BUILD.bazel" , "\n ". join ( build_contents )) 671repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 672repository_ctx . name , 673)) 674 675rustfmt_toolchain_tools_repository = repository_rule ( 676doc = "A repository rule for defining a rustfmt_toolchain." , 677attrs = { 678"auth" : attr . string_dict ( 679doc = ( 680"Auth object compatible with repository_ctx.download to use when downloading files. " + 681"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 682), 683), 684"exec_triple" : attr . string ( 685doc = "The Rust-style triple Rustfmt is expected to run on." , 686mandatory = True , 687), 688"iso_date" : attr . string ( 689doc = "The date of the tool (or None, if the version is a specific version)." , 690), 691"sha256s" : attr . string_dict ( 692doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 693), 694"urls" : attr . string_list ( 695doc = "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)." , 696default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 697), 698"version" : attr . string ( 699doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 700mandatory = True , 701), 702}, 703implementation = _rustfmt_toolchain_tools_repository_impl , 704) 705 706def rustfmt_toolchain_repository ( 707name , 708version , 709exec_triple , 710exec_compatible_with = None , 711target_compatible_with = None , 712iso_date = None , 713channel = None , 714sha256s = None , 715urls = None , 716auth = None ): 717"""Assemble a remote rustfmt_toolchain target based on the given params. 718 719Args: 720name (str): The name of the toolchain proxy repository contianing the registerable toolchain. 721version (str): The version of the tool among "nightly", "beta', or an exact version. 722exec_triple (str): The platform triple Rustfmt is expected to run on. 723exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. 724target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain. 725iso_date (str, optional): The date of the tool. 726channel (str, optional): The channel value to with which to constrain the toolchain. 727sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 728[rust_repositories](#rust_repositories) for more details. 729urls (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.gz'] 730auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 731See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 732 733Returns: 734str: The name of a registerable rustfmt_toolchain. 735""" 736if exec_compatible_with == None : 737exec_compatible_with = triple_to_constraint_set ( exec_triple ) 738 739rustfmt_toolchain_tools_repository ( 740name = name + "_tools" , 741version = version , 742iso_date = iso_date , 743sha256s = sha256s , 744urls = urls , 745auth = auth , 746exec_triple = exec_triple , 747) 748 749toolchain_repository_proxy ( 750name = name , 751toolchain = "@{}//:{}" . format ( name + "_tools" , "rustfmt_toolchain" ), 752toolchain_type = "@rules_rust//rust/rustfmt:toolchain_type" , 753target_settings = [ "@rules_rust//rust/toolchain/channel:{}" . format ( channel )] if channel else None , 754exec_compatible_with = exec_compatible_with , 755target_compatible_with = target_compatible_with , 756) 757 758return "@{}//:toolchain" . format ( 759name , 760) 761 762def _rust_toolchain_set_repository_impl ( repository_ctx ): 763repository_ctx . file ( "WORKSPACE.bazel" , """workspace(name = "{}")""" . format ( 764repository_ctx . name , 765)) 766 767repository_ctx . file ( "BUILD.bazel" , """exports_files(["defs.bzl"])""" ) 768repository_ctx . file ( "defs.bzl" , "ALL_TOOLCHAINS = {}\n ". format ( 769json . encode_indent ( repository_ctx . attr . toolchains , indent = " " * 4 ), 770)) 771 772rust_toolchain_set_repository = repository_rule ( 773doc = ( 774"Generates a toolchain-bearing repository that declares the toolchains from some other " + 775"rust_toolchain_repository." 776), 777attrs = { 778"toolchains" : attr . string_list ( 779doc = "The list of all toolchains created by the current `rust_toolchain_set`" , 780mandatory = True , 781), 782}, 783implementation = _rust_toolchain_set_repository_impl , 784) 785 786def rust_repository_set ( 787name , 788exec_triple , 789target_settings = [], 790version = None , 791versions = [], 792allocator_library = None , 793extra_target_triples = [], 794iso_date = None , 795rustfmt_version = None , 796edition = None , 797dev_components = False , 798sha256s = None , 799urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 800auth = None , 801register_toolchain = True ): 802"""Assembles a remote repository for the given toolchain params, produces a proxy repository \ 803to contain the toolchain declaration, and registers the toolchains. 804 805Args: 806name (str): The name of the generated repository 807exec_triple (str): The Rust-style target that this compiler runs on 808target_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. 809version (str): The version of the tool among "nightly", "beta', or an exact version. 810versions (list, optional): A list of toolchain versions to download. This paramter only accepts one versions 811per channel. E.g. `["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]`. 812allocator_library (str, optional): Target that provides allocator functions when rust_library targets are 813embedded in a cc_binary. 814extra_target_triples (list, optional): Additional rust-style targets that this set of 815toolchains should support. 816iso_date (str, optional): The date of the tool. 817rustfmt_version (str, optional): The version of rustfmt to be associated with the 818toolchain. 819edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is 820required to specify its `edition` attribute. 821dev_components (bool, optional): Whether to download the rustc-dev components. 822Requires version to be "nightly". 823sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 824[rust_repositories](#rust_repositories) for more details. 825urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These 826must contain the '{}' used to substitute the tool being fetched (using .format). 827auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 828See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 829register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain. 830""" 831 832if version and versions : 833fail ( "`version` and `versions` attributes are mutually exclusive. Update {} to use one" . format ( 834name , 835)) 836 837if not version and not versions : 838fail ( "`version` or `versions` attributes are required. Update {} to use one" . format ( 839name , 840)) 841 842if version : 843# buildifier: disable=print 844( "`rust_repository_set.version` is deprecated. Instead use `rust_repository_set.versions`" ) 845 846if version and not versions : 847versions = [ version ] 848 849all_toolchain_names = [] 850for target_triple in [ exec_triple ] + extra_target_triples : 851# Parse all provided versions while checking for duplicates 852channels = {} 853for version in versions : 854if version . startswith (( "beta" , "nightly" )): 855channel , _ , date = version . partition ( "/" ) 856ver = channel 857else : 858channel = "stable" 859date = iso_date 860ver = version 861 862if channel in channels : 863fail ( "Duplicate {} channels provided for {}: {}" . format ( channel , name , versions )) 864 865channels . update ({ channel : struct ( 866iso_date = date , 867version = ver , 868)}) 869 870# Define toolchains for each requested version 871for channel , info in channels . items (): 872toolchain_name = "{}__{}__{}" . format ( name , target_triple , channel ) 873 874all_toolchain_names . append ( rust_toolchain_repository ( 875name = toolchain_name , 876allocator_library = allocator_library , 877auth = auth , 878channel = channel , 879dev_components = dev_components , 880edition = edition , 881exec_triple = exec_triple , 882target_settings = target_settings , 883iso_date = info . iso_date , 884rustfmt_version = rustfmt_version , 885sha256s = sha256s , 886target_triple = target_triple , 887urls = urls , 888version = info . version , 889)) 890 891# This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper. 892rust_toolchain_set_repository ( 893name = name , 894toolchains = all_toolchain_names , 895) 896 897# Register toolchains 898if register_toolchain : 899native . register_toolchains ( * all_toolchain_names ) 900native . register_toolchains ( str ( Label ( "//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain" )))