fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
d1f397be
cleanup
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_compile_data = None , 91build_script_data = None , 92build_script_tools = None , 93build_script_data_glob = None , 94build_script_deps = None , 95build_script_env = None , 96build_script_proc_macro_deps = None , 97build_script_rundir = None , 98build_script_rustc_env = None , 99build_script_toolchains = None , 100compile_data = None , 101compile_data_glob = None , 102crate_features = None , 103data = None , 104data_glob = None , 105deps = None , 106extra_aliased_targets = None , 107gen_binaries = None , 108disable_pipelining = False , 109gen_build_script = None , 110patch_args = None , 111patch_tool = None , 112patches = None , 113proc_macro_deps = None , 114rustc_env = None , 115rustc_env_files = None , 116rustc_flags = None , 117shallow_since = None , 118override_targets = None ): 119"""A collection of extra attributes and settings for a particular crate 120 121Args: 122version (str, optional): The version or semver-conditions to match with a crate. The wildcard `*` 123matches any version, including prerelease versions. 124additive_build_file_content (str, optional): Extra contents to write to the bottom of generated BUILD files. 125additive_build_file (str, optional): A file containing extra contents to write to the bottom of 126generated BUILD files. 127alias_rule (str, optional): Alias rule to use instead of `native.alias()`. Overrides [render_config](#render_config)'s 128'default_alias_rule'. 129build_script_compile_data (list, optional): A list of labels to add to a crate's `cargo_build_script::compile_data` attribute. 130build_script_data (list, optional): A list of labels to add to a crate's `cargo_build_script::data` attribute. 131build_script_tools (list, optional): A list of labels to add to a crate's `cargo_build_script::tools` attribute. 132build_script_data_glob (list, optional): A list of glob patterns to add to a crate's `cargo_build_script::data` 133attribute. 134build_script_deps (list, optional): A list of labels to add to a crate's `cargo_build_script::deps` attribute. 135build_script_env (dict, optional): Additional environment variables to set on a crate's 136`cargo_build_script::env` attribute. 137build_script_proc_macro_deps (list, optional): A list of labels to add to a crate's 138`cargo_build_script::proc_macro_deps` attribute. 139build_script_rundir (str, optional): An override for the build script's rundir attribute. 140build_script_rustc_env (dict, optional): Additional environment variables to set on a crate's 141`cargo_build_script::env` attribute. 142build_script_toolchains (list, optional): A list of labels to set on a crates's `cargo_build_script::toolchains` attribute. 143compile_data (list, optional): A list of labels to add to a crate's `rust_library::compile_data` attribute. 144compile_data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::compile_data` 145attribute. 146crate_features (optional): A list of strings to add to a crate's `rust_library::crate_features` 147attribute. 148data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute. 149data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute. 150deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute. 151extra_aliased_targets (dict, optional): A list of targets to add to the generated aliases in the root 152crate_universe repository. 153gen_binaries (list or bool, optional): As a list, the subset of the crate's bins that should get `rust_binary` 154targets produced. Or `True` to generate all, `False` to generate none. 155disable_pipelining (bool, optional): If True, disables pipelining for library targets for this crate. 156gen_build_script (bool, optional): An authorative flag to determine whether or not to produce 157`cargo_build_script` targets for the current crate. 158patch_args (list, optional): The `patch_args` attribute of a Bazel repository rule. See 159[http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args) 160patch_tool (string, optional): The `patch_tool` attribute of a Bazel repository rule. See 161[http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool) 162patches (list, optional): The `patches` attribute of a Bazel repository rule. See 163[http_archive.patches](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patches) 164proc_macro_deps (list, optional): A list of labels to add to a crate's `rust_library::proc_macro_deps` 165attribute. 166rustc_env (dict, optional): Additional variables to set on a crate's `rust_library::rustc_env` attribute. 167rustc_env_files (list, optional): A list of labels to set on a crate's `rust_library::rustc_env_files` 168attribute. 169rustc_flags (list, optional): A list of strings to set on a crate's `rust_library::rustc_flags` attribute. 170shallow_since (str, optional): An optional timestamp used for crates originating from a git repository 171instead of a crate registry. This flag optimizes fetching the source code. 172override_targets (dict, optional): A dictionary of alternate targets to use when something depends on this crate to allow 173the parent repo to provide its own version of this dependency. Keys can be `proc-marco`, `custom-build`, `lib`, `bin`. 174 175Returns: 176string: A json encoded string containing the specified version and separately all other inputs. 177""" 178if additive_build_file : 179_assert_absolute ( additive_build_file ) 180if patches : 181for patch in patches : 182_assert_absolute ( patch ) 183 184return json . encode (( 185version , 186struct ( 187additive_build_file = _stringify_label ( additive_build_file ), 188additive_build_file_content = additive_build_file_content , 189alias_rule = parse_alias_rule ( alias_rule ), 190build_script_compile_data = _stringify_list ( build_script_compile_data ), 191build_script_data = _stringify_list ( build_script_data ), 192build_script_tools = _stringify_list ( build_script_tools ), 193build_script_data_glob = build_script_data_glob , 194build_script_deps = _stringify_list ( build_script_deps ), 195build_script_env = build_script_env , 196build_script_proc_macro_deps = _stringify_list ( build_script_proc_macro_deps ), 197build_script_rundir = build_script_rundir , 198build_script_rustc_env = build_script_rustc_env , 199build_script_toolchains = _stringify_list ( build_script_toolchains ), 200compile_data = _stringify_list ( compile_data ), 201compile_data_glob = compile_data_glob , 202crate_features = crate_features , 203data = _stringify_list ( data ), 204data_glob = data_glob , 205deps = _stringify_list ( deps ), 206extra_aliased_targets = extra_aliased_targets , 207gen_binaries = gen_binaries , 208disable_pipelining = disable_pipelining , 209gen_build_script = gen_build_script , 210patch_args = patch_args , 211patch_tool = patch_tool , 212patches = _stringify_list ( patches ), 213proc_macro_deps = _stringify_list ( proc_macro_deps ), 214rustc_env = rustc_env , 215rustc_env_files = _stringify_list ( rustc_env_files ), 216rustc_flags = rustc_flags , 217shallow_since = shallow_since , 218override_targets = override_targets , 219), 220)) 221 222def _stringify_label ( value ): 223if not value : 224return value 225return str ( value ) 226 227# In bzlmod, attributes of type `attr.label_list` end up as `Label`s not `str`, 228# and the `json` module doesn't know how to serialize `Label`s, 229# so we proactively convert them to strings before serializing. 230def _stringify_list ( values ): 231if not values : 232return values 233 234if type ( values ) == "list" : 235return [ str ( x ) for x in values ] 236 237# if values is a struct with a "selects" attribute, assume it was created with 238# crate.select() and map values for all platforms 239if type ( values ) == "struct" and type ( values . selects ) != "NoneType" : 240new_selects = {} 241 242for k , v in values . selects . items (): 243new_selects [ k ] = [ str ( x ) for x in v ] 244 245return struct ( common = [ str ( x ) for x in values . common ], selects = new_selects ) 246 247fail ( "Cannot stringify unknown type for list '{}'" . format ( values )) 248 249def _select ( common , selects ): 250"""A Starlark Select for `crate.annotation()`. 251 252Args: 253common: A value that applies to all configurations. 254selects (dict): A dict of `target_triple` to values. 255 256Returns: 257struct: A struct representing the Starlark Select. 258""" 259return struct ( 260common = common , 261selects = selects , 262) 263 264crate = struct ( 265spec = _spec , 266annotation = _annotation , 267workspace_member = _workspace_member , 268select = _select , 269)