fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
42f78f25
0.5.0
1"""`crates_repository` rule implementation""" 2 3load ( "//crate_universe/private:common_utils.bzl" , "get_rust_tools" ) 4load ( 5"//crate_universe/private:generate_utils.bzl" , 6"CRATES_REPOSITORY_ENVIRON" , 7"determine_repin" , 8"execute_generator" , 9"generate_config" , 10"get_generator" , 11"get_lockfile" , 12) 13load ( 14"//crate_universe/private:splicing_utils.bzl" , 15"create_splicing_manifest" , 16"splice_workspace_manifest" , 17) 18load ( "//crate_universe/private:urls.bzl" , "CARGO_BAZEL_SHA256S" , "CARGO_BAZEL_URLS" ) 19load ( "//rust:defs.bzl" , "rust_common" ) 20load ( "//rust/platform:triple.bzl" , "get_host_triple" ) 21load ( "//rust/platform:triple_mappings.bzl" , "SUPPORTED_PLATFORM_TRIPLES" ) 22 23def _crates_repository_impl ( repository_ctx ): 24# Determine the current host's platform triple 25host_triple = get_host_triple ( repository_ctx ) 26 27# Locate the generator to use 28generator , generator_sha256 = get_generator ( repository_ctx , host_triple . str ) 29 30# Generate a config file for all settings 31config_path = generate_config ( repository_ctx ) 32 33# Locate the lockfile 34lockfile = get_lockfile ( repository_ctx ) 35 36# Locate Rust tools (cargo, rustc) 37tools = get_rust_tools ( repository_ctx , host_triple ) 38cargo_path = repository_ctx . path ( tools . cargo ) 39rustc_path = repository_ctx . path ( tools . rustc ) 40 41# Create a manifest of all dependency inputs 42splicing_manifest = create_splicing_manifest ( repository_ctx ) 43 44# Determine whether or not to repin depednencies 45repin = determine_repin ( 46repository_ctx = repository_ctx , 47generator = generator , 48lockfile_path = lockfile . path , 49lockfile_kind = lockfile . kind , 50config = config_path , 51splicing_manifest = splicing_manifest , 52cargo = cargo_path , 53rustc = rustc_path , 54) 55 56# If re-pinning is enabled, gather additional inputs for the generator 57kwargs = dict () 58if repin or lockfile . kind == "cargo" : 59# Generate a top level Cargo workspace and manifest for use in generation 60metadata_path = splice_workspace_manifest ( 61repository_ctx = repository_ctx , 62generator = generator , 63lockfile = lockfile , 64splicing_manifest = splicing_manifest , 65cargo = cargo_path , 66rustc = rustc_path , 67) 68 69kwargs . update ({ 70"metadata" : metadata_path , 71"repin" : True , 72}) 73 74# Run the generator 75execute_generator ( 76repository_ctx = repository_ctx , 77generator = generator , 78config = config_path , 79splicing_manifest = splicing_manifest , 80lockfile_path = lockfile . path , 81lockfile_kind = lockfile . kind , 82repository_dir = repository_ctx . path ( "." ), 83cargo = cargo_path , 84rustc = rustc_path , 85# sysroot = tools.sysroot, 86** kwargs 87) 88 89# Determine the set of reproducible values 90attrs = { attr : getattr ( repository_ctx . attr , attr ) for attr in dir ( repository_ctx . attr )} 91exclude = [ "to_json" , "to_proto" ] 92for attr in exclude : 93attrs . pop ( attr , None ) 94 95# Note that this is only scoped to the current host platform. Users should 96# ensure they provide all the values necessary for the host environments 97# they support 98if generator_sha256 : 99attrs . update ({ "generator_sha256s" : generator_sha256 }) 100 101return attrs 102 103crates_repository = repository_rule ( 104doc = """\ 105A rule for defining and downloading Rust dependencies (crates). This rule 106handles all the same [workflows](#workflows) `crate_universe` rules do. 107 108Environment Variables: 109 110| variable | usage | 111| --- | --- | 112| `CARGO_BAZEL_GENERATOR_SHA256` | The sha256 checksum of the file located at `CARGO_BAZEL_GENERATOR_URL` | 113| `CARGO_BAZEL_GENERATOR_URL` | The URL of a cargo-bazel binary. This variable takes precedence over attributes and can use `file://` for local paths | 114| `CARGO_BAZEL_ISOLATED` | An authorative flag as to whether or not the `CARGO_HOME` environment variable should be isolated from the host configuration | 115| `CARGO_BAZEL_REPIN` | An indicator that the dependencies represented by the rule should be regenerated. `REPIN` may also be used. | 116 117Example: 118 119Given the following workspace structure: 120``` 121[workspace]/ 122WORKSPACE 123BUILD 124Cargo.toml 125Cargo.Bazel.lock 126src/ 127main.rs 128``` 129 130The following is something that'd be found in the `WORKSPACE` file: 131 132```python 133load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate") 134 135crates_repository( 136name = "crate_index", 137annotations = annotations = { 138"rand": [crate.annotation( 139default_features = False, 140features = ["small_rng"], 141)], 142}, 143lockfile = "//:Cargo.Bazel.lock", 144manifests = ["//:Cargo.toml"], 145# Should match the version represented by the currently registered `rust_toolchain`. 146rust_version = "1.60.0", 147) 148``` 149 150The above will create an external repository which contains aliases and macros for accessing 151Rust targets found in the dependency graph defined by the given manifests. 152 153**NOTE**: The `lockfile` must be manually created. The rule unfortunately does not yet create 154it on its own. When initially setting up this rule, an empty file should be created and then 155populated by repinning dependencies. 156 157### Repinning / Updating Dependencies 158 159Dependency syncing and updating is done in the repository rule which means it's done during the 160analysis phase of builds. As mentioned in the environments variable table above, the `CARGO_BAZEL_REPIN` 161(or `REPIN`) environment variables can be used to force the rule to update dependencies and potentially 162render a new lockfile. Given an instance of this repository rule named `crate_index`, the easiest way to 163repin dependencies is to run: 164 165```shell 166CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index 167``` 168 169""" , 170implementation = _crates_repository_impl , 171attrs = { 172"annotations" : attr . string_list_dict ( 173doc = "Extra settings to apply to crates. See [crate.annotation](#crateannotation)." , 174), 175"cargo_config" : attr . label ( 176doc = "A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file" , 177), 178"extra_workspace_member_url_template" : attr . string ( 179doc = "The registry url to use when fetching extra workspace members" , 180default = "https://crates.io/api/v1/crates/{name}/{version}/download" , 181), 182"extra_workspace_members" : attr . string_dict ( 183doc = ( 184"Additional crates to download and include as a workspace member. This is unfortunately required in " + 185 "order to add information about \"binary-only\" crates so that a `rust_binary` may be generated for " + 186"it. [rust-lang/cargo#9096](https://github.com/rust-lang/cargo/issues/9096) tracks an RFC which may " + 187"solve for this." 188), 189), 190"generate_build_scripts" : attr . bool ( 191doc = ( 192"Whether or not to generate " + 193"[cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default." 194), 195default = True , 196), 197"generator" : attr . string ( 198doc = ( 199"The absolute label of a generator. Eg. `@cargo_bazel_bootstrap//:cargo-bazel`. " + 200"This is typically used when bootstrapping" 201), 202), 203"generator_sha256s" : attr . string_dict ( 204doc = "Dictionary of `host_triple` -> `sha256` for a `cargo-bazel` binary." , 205default = CARGO_BAZEL_SHA256S , 206), 207"generator_urls" : attr . string_dict ( 208doc = ( 209"URL template from which to download the `cargo-bazel` binary. `{host_triple}` and will be " + 210"filled in according to the host platform." 211), 212default = CARGO_BAZEL_URLS , 213), 214"isolated" : attr . bool ( 215doc = ( 216"If true, `CARGO_HOME` will be overwritten to a directory within the generated repository in " + 217"order to prevent other uses of Cargo from impacting having any effect on the generated targets " + 218"produced by this rule. For users who either have multiple `crate_repository` definitions in a " + 219"WORKSPACE or rapidly re-pin dependencies, setting this to false may improve build times. This " + 220"variable is also controled by `CARGO_BAZEL_ISOLATED` environment variable." 221), 222default = True , 223), 224"lockfile" : attr . label ( 225doc = ( 226"The path to a file to use for reproducible renderings. Two kinds of lock files are supported, " + 227"Cargo (`Cargo.lock` files) and Bazel (custom files generated by this rule, naming is irrelevant). " + 228"Bazel lockfiles should be the prefered kind as they're desigend with Bazel's notions of " + 229"reporducibility in mind. Cargo lockfiles can be used in cases where it's intended to be the " + 230"source of truth, but more work will need to be done to generate BUILD files which are not " + 231"guaranteed to be determinsitic." 232), 233mandatory = True , 234), 235"lockfile_kind" : attr . string ( 236doc = ( 237 "Two different kinds of lockfiles are supported, the custom \"Bazel\" lockfile, which is generated " + 238"by this rule, and Cargo lockfiles (`Cargo.lock`). This attribute allows for explicitly defining " + 239"the type in cases where it may not be auto-detectable." 240), 241values = [ 242"auto" , 243"bazel" , 244"cargo" , 245], 246default = "auto" , 247), 248"manifests" : attr . label_list ( 249doc = "A list of Cargo manifests (`Cargo.toml` files)." , 250), 251"packages" : attr . string_dict ( 252doc = "A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec)." , 253), 254"quiet" : attr . bool ( 255doc = "If stdout and stderr should not be printed to the terminal." , 256default = True , 257), 258"render_config" : attr . string ( 259doc = ( 260 "The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\\% render_config `to "+ 261"generate the value for this field. If unset, the defaults defined there will be used." 262), 263), 264"rust_toolchain_cargo_template" : attr . string ( 265doc = ( 266"The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), " + 267"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " + 268"`{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), and `{tool}` (eg. 'rustc.exe') will be replaced in " + 269"the string if present." 270), 271default = "@rust_{system}_{arch}//:bin/{tool}" , 272), 273"rust_toolchain_rustc_template" : attr . string ( 274doc = ( 275"The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), " + 276"`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " + 277"`{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), and `{tool}` (eg. 'cargo.exe') will be replaced in " + 278"the string if present." 279), 280default = "@rust_{system}_{arch}//:bin/{tool}" , 281), 282"rust_version" : attr . string ( 283doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly-2021-09-08`" , 284default = rust_common . default_version , 285), 286"splicing_config" : attr . string ( 287doc = ( 288 "The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\\% rsplicing_config `to "+ 289"generate the value for this field. If unset, the defaults defined there will be used." 290), 291), 292"supported_platform_triples" : attr . string_list ( 293doc = "A set of all platform triples to consider when generating dependencies." , 294default = SUPPORTED_PLATFORM_TRIPLES , 295), 296}, 297environ = CRATES_REPOSITORY_ENVIRON , 298)