fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
7465c1aa
0.7.0
1# buildifier: disable=module-docstring 2load ( "@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" ) 3load ( "@bazel_tools//tools/build_defs/repo:utils.bzl" , "maybe" ) 4load ( "//rust/private:common.bzl" , "rust_common" ) 5load ( 6"//rust/private:repository_utils.bzl" , 7"BUILD_for_toolchain" , 8"DEFAULT_STATIC_RUST_URL_TEMPLATES" , 9"DEFAULT_TOOLCHAIN_NAME_PREFIX" , 10"check_version_valid" , 11"load_llvm_tools" , 12"load_rust_compiler" , 13"load_rust_src" , 14"load_rust_stdlib" , 15"load_rustc_dev_nightly" , 16"load_rustfmt" , 17"should_include_rustc_srcs" , 18_load_arbitrary_tool = "load_arbitrary_tool" , 19) 20 21# Reexport `load_arbitrary_tool` as it's currently in use in https://github.com/google/cargo-raze 22load_arbitrary_tool = _load_arbitrary_tool 23 24# 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. 25DEFAULT_TOOLCHAIN_TRIPLES = { 26"aarch64-apple-darwin" : "rust_darwin_aarch64" , 27"aarch64-unknown-linux-gnu" : "rust_linux_aarch64" , 28"x86_64-apple-darwin" : "rust_darwin_x86_64" , 29"x86_64-pc-windows-msvc" : "rust_windows_x86_64" , 30"x86_64-unknown-freebsd" : "rust_freebsd_x86_64" , 31"x86_64-unknown-linux-gnu" : "rust_linux_x86_64" , 32} 33 34def rules_rust_dependencies (): 35"""Dependencies used in the implementation of `rules_rust`.""" 36 37maybe ( 38http_archive , 39name = "platforms" , 40urls = [ 41"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz" , 42"https://github.com/bazelbuild/platforms/releases/download/0.0.5/platforms-0.0.5.tar.gz" , 43], 44sha256 = "379113459b0feaf6bfbb584a91874c065078aa673222846ac765f86661c27407" , 45) 46maybe ( 47http_archive , 48name = "rules_cc" , 49urls = [ "https://github.com/bazelbuild/rules_cc/releases/download/0.0.1/rules_cc-0.0.1.tar.gz" ], 50sha256 = "4dccbfd22c0def164c8f47458bd50e0c7148f3d92002cdb459c2a96a68498241" , 51) 52 53maybe ( 54http_archive , 55name = "bazel_skylib" , 56urls = [ 57"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz" , 58"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz" , 59], 60sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de" , 61) 62 63# Make the iOS simulator constraint available, which is referenced in abi_to_constraints() 64# rules_rust does not require this dependency; it is just imported as a convenience for users. 65maybe ( 66http_archive , 67name = "build_bazel_apple_support" , 68sha256 = "5bbce1b2b9a3d4b03c0697687023ef5471578e76f994363c641c5f50ff0c7268" , 69url = "https://github.com/bazelbuild/apple_support/releases/download/0.13.0/apple_support.0.13.0.tar.gz" , 70) 71 72# process_wrapper needs a low-dependency way to process json. 73maybe ( 74http_archive , 75name = "rules_rust_tinyjson" , 76sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16" , 77url = "https://crates.io/api/v1/crates/tinyjson/2.3.0/download" , 78strip_prefix = "tinyjson-2.3.0" , 79type = "tar.gz" , 80build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel" , 81) 82 83# buildifier: disable=unnamed-macro 84def rust_register_toolchains ( 85dev_components = False , 86edition = None , 87include_rustc_srcs = False , 88iso_date = None , 89register_toolchains = True , 90rustfmt_version = None , 91sha256s = None , 92extra_target_triples = [ "wasm32-unknown-unknown" , "wasm32-wasi" ], 93urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 94version = rust_common . default_version ): 95"""Emits a default set of toolchains for Linux, MacOS, and Freebsd 96 97Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \ 98other hosts or for additional target triples. 99 100The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example: 101```python 102{ 103"rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5", 104"rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0", 105"rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc", 106} 107``` 108This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive \ 109list of known checksums.. 110 111See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more details. 112 113Args: 114dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". 115edition (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. 116include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. 117See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details 118iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version). 119register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets. 120rustfmt_version (str, optional): The version of rustfmt. Either "nightly", "beta", or an exact version. Defaults to `version` if not specified. 121sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. 122extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support. 123urls (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). 124version (str, optional): The version of Rust. Either "nightly", "beta", or an exact version. Defaults to a modern version. 125""" 126if dev_components and version != "nightly" : 127fail ( "Rust version must be set to \"nightly\" to enable rustc-dev components" ) 128 129if not rustfmt_version : 130rustfmt_version = version 131 132for exec_triple , name in DEFAULT_TOOLCHAIN_TRIPLES . items (): 133maybe ( 134rust_repository_set , 135name = name , 136dev_components = dev_components , 137edition = edition , 138exec_triple = exec_triple , 139extra_target_triples = extra_target_triples , 140include_rustc_srcs = include_rustc_srcs , 141iso_date = iso_date , 142register_toolchain = register_toolchains , 143rustfmt_version = rustfmt_version , 144sha256s = sha256s , 145urls = urls , 146version = version , 147) 148 149# buildifier: disable=unnamed-macro 150def rust_repositories ( ** kwargs ): 151"""**Deprecated**: Use [rules_rust_dependencies](#rules_rust_dependencies) \ 152and [rust_register_toolchains](#rust_register_toolchains) directly. 153 154Args: 155**kwargs (dict): Keyword arguments for the `rust_register_toolchains` macro. 156""" 157rules_rust_dependencies () 158 159rust_register_toolchains ( ** kwargs ) 160 161def _rust_toolchain_repository_impl ( ctx ): 162"""The implementation of the rust toolchain repository rule.""" 163 164check_version_valid ( ctx . attr . version , ctx . attr . iso_date ) 165 166# Conditionally download rustc sources. Generally used for `rust-analyzer` 167if should_include_rustc_srcs ( ctx ): 168load_rust_src ( ctx ) 169 170build_components = [ load_rust_compiler ( ctx )] 171 172if ctx . attr . rustfmt_version : 173build_components . append ( load_rustfmt ( ctx )) 174 175# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib 176include_llvm_tools = ctx . attr . version >= "1.45.0" or ( ctx . attr . version == "nightly" and ctx . attr . iso_date > "2020-05-22" ) 177if include_llvm_tools : 178build_components . append ( load_llvm_tools ( ctx , ctx . attr . exec_triple )) 179 180for target_triple in [ ctx . attr . exec_triple ] + ctx . attr . extra_target_triples : 181build_components . append ( load_rust_stdlib ( ctx , target_triple , include_llvm_tools )) 182 183# extra_target_triples contains targets such as wasm, which don't have rustc_dev components 184if ctx . attr . dev_components and target_triple not in ctx . attr . extra_target_triples : 185load_rustc_dev_nightly ( ctx , target_triple ) 186 187ctx . file ( "WORKSPACE.bazel" , "" ) 188ctx . file ( "BUILD.bazel" , "\n ". join ( build_components )) 189 190def _rust_toolchain_repository_proxy_impl ( ctx ): 191build_components = [] 192for target_triple in [ ctx . attr . exec_triple ] + ctx . attr . extra_target_triples : 193build_components . append ( BUILD_for_toolchain ( 194name = "{toolchain_prefix}_{target_triple}" . format ( 195toolchain_prefix = ctx . attr . toolchain_name_prefix , 196target_triple = target_triple , 197), 198exec_triple = ctx . attr . exec_triple , 199parent_workspace_name = ctx . attr . parent_workspace_name , 200target_triple = target_triple , 201)) 202 203ctx . file ( "WORKSPACE.bazel" , "" ) 204ctx . file ( "BUILD.bazel" , "\n ". join ( build_components )) 205 206rust_toolchain_repository = repository_rule ( 207doc = ( 208"Composes a single workspace containing the toolchain components for compiling on a given " + 209 "platform to a series of target platforms.\n "+ 210 "\n "+ 211"A given instance of this rule should be accompanied by a rust_toolchain_repository_proxy " + 212"invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " + 213"selection from toolchain fetching." 214), 215attrs = { 216"auth" : attr . string_dict ( 217doc = ( 218"Auth object compatible with repository_ctx.download to use when downloading files. " + 219"See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details." 220), 221), 222"dev_components" : attr . bool ( 223doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\"." , 224default = False , 225), 226"edition" : attr . string ( 227doc = ( 228"The rust edition to be used by default (2015, 2018, or 2021). " + 229"If absent, every rule is required to specify its `edition` attribute." 230), 231), 232"exec_triple" : attr . string ( 233doc = "The Rust-style target that this compiler runs on" , 234mandatory = True , 235), 236"extra_target_triples" : attr . string_list ( 237doc = "Additional rust-style targets that this set of toolchains should support." , 238), 239"include_rustc_srcs" : attr . bool ( 240doc = ( 241"Whether to download and unpack the rustc source files. These are very large, and " + 242"slow to unpack, but are required to support rust analyzer. An environment variable " + 243"`RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS` can also be used to control this attribute. " + 244"This variable will take precedence over the hard coded attribute. Setting it to `true` to " + 245"activates this attribute where all other values deactivate it." 246), 247default = False , 248), 249"iso_date" : attr . string ( 250doc = "The date of the tool (or None, if the version is a specific version)." , 251), 252"rustfmt_version" : attr . string ( 253doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 254), 255"sha256s" : attr . string_dict ( 256doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details." , 257), 258"toolchain_name_prefix" : attr . string ( 259doc = "The per-target prefix expected for the rust_toolchain declarations in the parent workspace." , 260), 261"urls" : attr . string_list ( 262doc = "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)." , 263default = DEFAULT_STATIC_RUST_URL_TEMPLATES , 264), 265"version" : attr . string ( 266doc = "The version of the tool among \"nightly\", \"beta\", or an exact version." , 267mandatory = True , 268), 269}, 270implementation = _rust_toolchain_repository_impl , 271environ = [ "RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS" ], 272) 273 274rust_toolchain_repository_proxy = repository_rule ( 275doc = ( 276"Generates a toolchain-bearing repository that declares the toolchains from some other " + 277"rust_toolchain_repository." 278), 279attrs = { 280"exec_triple" : attr . string ( 281doc = "The Rust-style target triple for the compilation platform" , 282mandatory = True , 283), 284"extra_target_triples" : attr . string_list ( 285doc = "The Rust-style triples for extra compilation targets" , 286), 287"parent_workspace_name" : attr . string ( 288doc = "The name of the other rust_toolchain_repository" , 289mandatory = True , 290), 291"toolchain_name_prefix" : attr . string ( 292doc = "The per-target prefix expected for the rust_toolchain declarations in the parent workspace." , 293), 294}, 295implementation = _rust_toolchain_repository_proxy_impl , 296) 297 298def rust_repository_set ( 299name , 300version , 301exec_triple , 302include_rustc_srcs = False , 303extra_target_triples = [], 304iso_date = None , 305rustfmt_version = None , 306edition = None , 307dev_components = False , 308sha256s = None , 309urls = DEFAULT_STATIC_RUST_URL_TEMPLATES , 310auth = None , 311register_toolchain = True ): 312"""Assembles a remote repository for the given toolchain params, produces a proxy repository \ 313to contain the toolchain declaration, and registers the toolchains. 314 315N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) \ 316without actually downloading the toolchain. 317 318Args: 319name (str): The name of the generated repository 320version (str): The version of the tool among "nightly", "beta', or an exact version. 321exec_triple (str): The Rust-style target that this compiler runs on 322include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. 323extra_target_triples (list, optional): Additional rust-style targets that this set of 324toolchains should support. Defaults to []. 325iso_date (str, optional): The date of the tool. Defaults to None. 326rustfmt_version (str, optional): The version of rustfmt to be associated with the 327toolchain. Defaults to None. 328edition (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. 329dev_components (bool, optional): Whether to download the rustc-dev components. 330Requires version to be "nightly". Defaults to False. 331sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See 332[rust_repositories](#rust_repositories) for more details. 333urls (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'] 334auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. 335See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. 336register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain. 337""" 338 339rust_toolchain_repository ( 340name = name , 341exec_triple = exec_triple , 342include_rustc_srcs = include_rustc_srcs , 343extra_target_triples = extra_target_triples , 344iso_date = iso_date , 345toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX , 346version = version , 347rustfmt_version = rustfmt_version , 348edition = edition , 349dev_components = dev_components , 350sha256s = sha256s , 351urls = urls , 352auth = auth , 353) 354 355rust_toolchain_repository_proxy ( 356name = name + "_toolchains" , 357exec_triple = exec_triple , 358extra_target_triples = extra_target_triples , 359parent_workspace_name = name , 360toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX , 361) 362 363all_toolchain_names = [] 364for target_triple in [ exec_triple ] + extra_target_triples : 365all_toolchain_names . append ( "@{name}_toolchains//:{toolchain_name_prefix}_{triple}" . format ( 366name = name , 367toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX , 368triple = target_triple , 369)) 370 371# Register toolchains 372if register_toolchain : 373native . register_toolchains ( * all_toolchain_names ) 374native . register_toolchains ( str ( Label ( "//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain" ))) 375 376# Inform users that they should be using the canonical name if it's not detected 377if "rules_rust" not in native . existing_rules (): 378message = "\n " + (" = " * 79) + " \n " 379message += ( 380 "It appears that you are trying to import rules_rust without using its\n "+ 381 "canonical name, \"@rules_rust\" Please change your WORKSPACE file to\n "+ 382 "import this repo with `name = \"rules_rust\"` instead." 383) 384 385if "io_bazel_rules_rust" in native . existing_rules (): 386message += "\n \n "+ ( 387 "Note that the previous name of \"@io_bazel_rules_rust\" is deprecated.\n "+ 388"See https://github.com/bazelbuild/rules_rust/issues/499 for context." 389) 390 391message += "\n " + (" = " * 79 ) 392fail ( message )