fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
33f93ace
patching
1"""Macros used for representing crates or annotations for existing crates""" 2 3load ( ":common_utils.bzl" , "parse_alias_rule" ) 4 5def _workspace_member ( version , sha256 = None ): 6"""Define information for extra workspace members 7 8Args: 9version (str): The semver of the crate to download. Must be an exact version. 10sha256 (str, optional): The sha256 checksum of the `.crate` file. 11 12Returns: 13string: A json encoded string of all inputs 14""" 15return json . encode ( struct ( 16version = version , 17sha256 = sha256 , 18)) 19 20def _spec ( 21package = None , 22version = None , 23artifact = None , 24lib = None , 25default_features = True , 26features = [], 27git = None , 28branch = None , 29tag = None , 30rev = None ): 31"""A constructor for a crate dependency. 32 33See [specifying dependencies][sd] in the Cargo book for more details. 34 35[sd]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html 36 37Args: 38package (str, optional): The explicit name of the package (used when attempting to alias a crate). 39version (str, optional): The exact version of the crate. Cannot be used with `git`. 40artifact (str, optional): Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo. 41lib (bool, optional): If using `artifact = "bin"`, additionally setting `lib = True` declares a dependency on both the package's library and binary, as opposed to just the binary. 42default_features (bool, optional): Maps to the `default-features` flag. 43features (list, optional): A list of features to use for the crate 44git (str, optional): The Git url to use for the crate. Cannot be used with `version`. 45branch (str, optional): The git branch of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds. 46tag (str, optional): The git tag of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds. 47rev (str, optional): The git revision of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. 48 49Returns: 50string: A json encoded string of all inputs 51""" 52return json . encode ({ 53k : v 54for k , v in { 55"artifact" : artifact , 56"branch" : branch , 57"default_features" : default_features , 58"features" : features , 59"git" : git , 60"lib" : lib , 61"package" : package , 62"rev" : rev , 63"tag" : tag , 64"version" : version , 65}. items () 66# The `cargo_toml` crate parses unstable fields to a flattened 67# BTreeMap<String, toml::Value> and toml::Value does not support null, 68# so we must omit null values. 69if v != None 70}) 71 72def _assert_absolute ( label ): 73"""Ensure a given label is an absolute label 74 75Args: 76label (Label): The label to check 77""" 78label_str = str ( label ) 79if not label_str . startswith ( "@" ): 80fail ( "The labels must be absolute. Please update '{}'" . format ( 81label_str , 82)) 83 84# This should be kept in sync crate_universe/extension.bzl. 85def _annotation ( 86version = "*" , 87additive_build_file = None , 88additive_build_file_content = None , 89alias_rule = None , 90build_script_data = None , 91build_script_tools = None , 92build_script_data_glob = None , 93build_script_deps = None , 94build_script_env = None , 95build_script_proc_macro_deps = None , 96build_script_rundir = None , 97build_script_rustc_env = None , 98build_script_toolchains = None , 99compile_data = None , 100compile_data_glob = None , 101crate_features = None , 102data = None , 103data_glob = None , 104deps = None , 105extra_aliased_targets = None , 106gen_binaries = None , 107disable_pipelining = False , 108gen_build_script = None , 109patch_args = None , 110patch_tool = None , 111patches = None , 112proc_macro_deps = None , 113rustc_env = None , 114rustc_env_files = None , 115rustc_flags = None , 116shallow_since = None , 117override_targets = None ): 118"""A collection of extra attributes and settings for a particular crate 119 120Args: 121version (str, optional): The version or semver-conditions to match with a crate. The wildcard `*` 122matches any version, including prerelease versions. 123additive_build_file_content (str, optional): Extra contents to write to the bottom of generated BUILD files. 124additive_build_file (str, optional): A file containing extra contents to write to the bottom of 125generated BUILD files. 126alias_rule (str, optional): Alias rule to use instead of `native.alias()`. Overrides [render_config](#render_config)'s 127'default_alias_rule'. 128build_script_data (list, optional): A list of labels to add to a crate's `cargo_build_script::data` attribute. 129build_script_tools (list, optional): A list of labels to add to a crate's `cargo_build_script::tools` attribute. 130build_script_data_glob (list, optional): A list of glob patterns to add to a crate's `cargo_build_script::data` 131attribute. 132build_script_deps (list, optional): A list of labels to add to a crate's `cargo_build_script::deps` attribute. 133build_script_env (dict, optional): Additional environment variables to set on a crate's 134`cargo_build_script::env` attribute. 135build_script_proc_macro_deps (list, optional): A list of labels to add to a crate's 136`cargo_build_script::proc_macro_deps` attribute. 137build_script_rundir (str, optional): An override for the build script's rundir attribute. 138build_script_rustc_env (dict, optional): Additional environment variables to set on a crate's 139`cargo_build_script::env` attribute. 140build_script_toolchains (list, optional): A list of labels to set on a crates's `cargo_build_script::toolchains` attribute. 141compile_data (list, optional): A list of labels to add to a crate's `rust_library::compile_data` attribute. 142compile_data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::compile_data` 143attribute. 144crate_features (optional): A list of strings to add to a crate's `rust_library::crate_features` 145attribute. 146data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute. 147data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute. 148deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute. 149extra_aliased_targets (dict, optional): A list of targets to add to the generated aliases in the root 150crate_universe repository. 151gen_binaries (list or bool, optional): As a list, the subset of the crate's bins that should get `rust_binary` 152targets produced. Or `True` to generate all, `False` to generate none. 153disable_pipelining (bool, optional): If True, disables pipelining for library targets for this crate. 154gen_build_script (bool, optional): An authorative flag to determine whether or not to produce 155`cargo_build_script` targets for the current crate. 156patch_args (list, optional): The `patch_args` attribute of a Bazel repository rule. See 157[http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args) 158patch_tool (string, optional): The `patch_tool` attribute of a Bazel repository rule. See 159[http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool) 160patches (list, optional): The `patches` attribute of a Bazel repository rule. See 161[http_archive.patches](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patches) 162proc_macro_deps (list, optional): A list of labels to add to a crate's `rust_library::proc_macro_deps` 163attribute. 164rustc_env (dict, optional): Additional variables to set on a crate's `rust_library::rustc_env` attribute. 165rustc_env_files (list, optional): A list of labels to set on a crate's `rust_library::rustc_env_files` 166attribute. 167rustc_flags (list, optional): A list of strings to set on a crate's `rust_library::rustc_flags` attribute. 168shallow_since (str, optional): An optional timestamp used for crates originating from a git repository 169instead of a crate registry. This flag optimizes fetching the source code. 170override_targets (dict, optional): A dictionary of alternate tagets to use when something depends on this crate to allow 171the parent repo to provide its own version of this dependency. Keys can be `proc_marco`, `build_script`, `lib`, `bin`. 172 173Returns: 174string: A json encoded string containing the specified version and separately all other inputs. 175""" 176if additive_build_file : 177_assert_absolute ( additive_build_file ) 178if patches : 179for patch in patches : 180_assert_absolute ( patch ) 181 182return json . encode (( 183version , 184struct ( 185additive_build_file = _stringify_label ( additive_build_file ), 186additive_build_file_content = additive_build_file_content , 187alias_rule = parse_alias_rule ( alias_rule ), 188build_script_data = _stringify_list ( build_script_data ), 189build_script_tools = _stringify_list ( build_script_tools ), 190build_script_data_glob = build_script_data_glob , 191build_script_deps = _stringify_list ( build_script_deps ), 192build_script_env = build_script_env , 193build_script_proc_macro_deps = _stringify_list ( build_script_proc_macro_deps ), 194build_script_rundir = build_script_rundir , 195build_script_rustc_env = build_script_rustc_env , 196build_script_toolchains = _stringify_list ( build_script_toolchains ), 197compile_data = _stringify_list ( compile_data ), 198compile_data_glob = compile_data_glob , 199crate_features = crate_features , 200data = _stringify_list ( data ), 201data_glob = data_glob , 202deps = _stringify_list ( deps ), 203extra_aliased_targets = extra_aliased_targets , 204gen_binaries = gen_binaries , 205disable_pipelining = disable_pipelining , 206gen_build_script = gen_build_script , 207patch_args = patch_args , 208patch_tool = patch_tool , 209patches = _stringify_list ( patches ), 210proc_macro_deps = _stringify_list ( proc_macro_deps ), 211rustc_env = rustc_env , 212rustc_env_files = _stringify_list ( rustc_env_files ), 213rustc_flags = rustc_flags , 214shallow_since = shallow_since , 215override_targets = override_targets , 216), 217)) 218 219def _stringify_label ( value ): 220if not value : 221return value 222return str ( value ) 223 224# In bzlmod, attributes of type `attr.label_list` end up as `Label`s not `str`, 225# and the `json` module doesn't know how to serialize `Label`s, 226# so we proactively convert them to strings before serializing. 227def _stringify_list ( values ): 228if not values : 229return values 230return [ str ( x ) for x in values ] 231 232def _select ( common , selects ): 233"""A Starlark Select for `crate.annotation()`. 234 235Args: 236common: A value that applies to all configurations. 237selects (dict): A dict of `target_triple` to values. 238 239Returns: 240struct: A struct representing the Starlark Select. 241""" 242return struct ( 243common = common , 244selects = selects , 245) 246 247crate = struct ( 248spec = _spec , 249annotation = _annotation , 250workspace_member = _workspace_member , 251select = _select , 252)