From baad94fd2568b3058086bdc9752e3ca4615dd564 Mon Sep 17 00:00:00 2001 From: Julia Ryan Date: Wed, 4 Dec 2024 11:19:00 -0800 Subject: [PATCH] Add patching example --- examples/crate_universe/DEVELOPMENT.md | 1 + .../vendor_local_patching/.gitignore | 3 + .../vendor_local_patching/BUILD.bazel | 31 ++ .../vendor_local_patching/Cargo.lock | 70 ++++ .../vendor_local_patching/Cargo.toml | 15 + .../vendor_local_patching/README.md | 19 + .../empty_wasi/Cargo.toml | 3 + .../empty_wasi/src/lib.rs | 0 .../forked_getrandom/BUILD.bazel | 23 ++ .../forked_getrandom/Cargo.toml | 35 ++ .../forked_getrandom/build.rs | 4 + .../forked_getrandom/src/lib.rs | 23 ++ .../vendor_local_patching/src/main.rs | 11 + .../vendor_local_patching/vendor/BUILD.bazel | 37 ++ .../vendor/alias_rules.bzl | 47 +++ .../vendor/cfg-if-1.0.0/BUILD.bazel | 53 +++ .../vendor_local_patching/vendor/defs.bzl | 365 ++++++++++++++++++ .../vendor/getrandom-0.2.15/BUILD.bazel | 90 +++++ .../vendor/libc-0.2.141/BUILD.bazel | 87 +++++ .../vendor/ppv-lite86-0.2.17/BUILD.bazel | 57 +++ .../vendor/rand-0.8.5/BUILD.bazel | 83 ++++ .../vendor/rand_chacha-0.3.1/BUILD.bazel | 60 +++ .../vendor/rand_core-0.6.4/BUILD.bazel | 61 +++ util/fetch_shas/fetch_shas_TARGETS.txt | 2 - 24 files changed, 1178 insertions(+), 2 deletions(-) create mode 100644 examples/crate_universe/vendor_local_patching/.gitignore create mode 100644 examples/crate_universe/vendor_local_patching/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/Cargo.lock create mode 100644 examples/crate_universe/vendor_local_patching/Cargo.toml create mode 100644 examples/crate_universe/vendor_local_patching/README.md create mode 100644 examples/crate_universe/vendor_local_patching/empty_wasi/Cargo.toml create mode 100644 examples/crate_universe/vendor_local_patching/empty_wasi/src/lib.rs create mode 100644 examples/crate_universe/vendor_local_patching/forked_getrandom/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/forked_getrandom/Cargo.toml create mode 100644 examples/crate_universe/vendor_local_patching/forked_getrandom/build.rs create mode 100644 examples/crate_universe/vendor_local_patching/forked_getrandom/src/lib.rs create mode 100644 examples/crate_universe/vendor_local_patching/src/main.rs create mode 100644 examples/crate_universe/vendor_local_patching/vendor/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/alias_rules.bzl create mode 100644 examples/crate_universe/vendor_local_patching/vendor/cfg-if-1.0.0/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/defs.bzl create mode 100644 examples/crate_universe/vendor_local_patching/vendor/getrandom-0.2.15/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/libc-0.2.141/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/ppv-lite86-0.2.17/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/rand-0.8.5/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/rand_chacha-0.3.1/BUILD.bazel create mode 100644 examples/crate_universe/vendor_local_patching/vendor/rand_core-0.6.4/BUILD.bazel diff --git a/examples/crate_universe/DEVELOPMENT.md b/examples/crate_universe/DEVELOPMENT.md index f5e24b1746..42e828547b 100644 --- a/examples/crate_universe/DEVELOPMENT.md +++ b/examples/crate_universe/DEVELOPMENT.md @@ -10,6 +10,7 @@ bazel commands: bazel run //vendor_external:crates_vendor bazel run //vendor_local_manifests:crates_vendor bazel run //vendor_local_pkgs:crates_vendor +bazel run //vendor_local_patching:vendor bazel run //vendor_remote_manifests:crates_vendor bazel run //vendor_remote_pkgs:crates_vendor ``` diff --git a/examples/crate_universe/vendor_local_patching/.gitignore b/examples/crate_universe/vendor_local_patching/.gitignore new file mode 100644 index 0000000000..190f3d14e2 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/.gitignore @@ -0,0 +1,3 @@ +# Ignore everything but the `BUILD` files within the vendored directories +vendor/*/* +!vendor/*/BUILD.bazel diff --git a/examples/crate_universe/vendor_local_patching/BUILD.bazel b/examples/crate_universe/vendor_local_patching/BUILD.bazel new file mode 100644 index 0000000000..336972400f --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/BUILD.bazel @@ -0,0 +1,31 @@ +load("@rules_rust//crate_universe:defs.bzl", "crates_vendor") +load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test") + +crates_vendor( + name = "vendor", + cargo_lockfile = ":Cargo.lock", + manifests = [":Cargo.toml"], + mode = "local", + vendor_path = "vendor", + # No wasi support + supported_platform_triples = [ + "x86_64-unknown-linux-gnu", + "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", + "x86_64-pc-windows-msvc", + "x86_64-unknown-fuchsia", + "aarch64-unknown-fuchsia", + ], +) + +rust_binary( + name = "bin", + srcs = ["src/main.rs"], + edition = "2021", + deps = ["//vendor_local_patching/vendor:rand"], +) + +rust_test( + name = "test", + crate = ":bin", +) diff --git a/examples/crate_universe/vendor_local_patching/Cargo.lock b/examples/crate_universe/vendor_local_patching/Cargo.lock new file mode 100644 index 0000000000..e107144607 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/Cargo.lock @@ -0,0 +1,70 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.15" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "libc" +version = "0.2.141" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" + +[[package]] +name = "my_third_party" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[patch.unused]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" + diff --git a/examples/crate_universe/vendor_local_patching/Cargo.toml b/examples/crate_universe/vendor_local_patching/Cargo.toml new file mode 100644 index 0000000000..50bc4940e3 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "my_third_party" +version = "0.1.0" + +[dependencies] +rand = "0.8.5" + +[patch.crates-io] +# We need to make sure that forked_getrandom/BUILD.bazel exposes the correct +# filegroups so that the generated BUILD file in the vendor dir can point to them. +getrandom = { path = "forked_getrandom" } + +# Since we know this crate is never used, we don't have to bother creating a BUILD +# file for our empty stub crate we're using to patch it out +wasi = { path = "empty_wasi" } diff --git a/examples/crate_universe/vendor_local_patching/README.md b/examples/crate_universe/vendor_local_patching/README.md new file mode 100644 index 0000000000..69dd7ba784 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/README.md @@ -0,0 +1,19 @@ +# Local vendoring with patches + +This demonstrates patching out crates when using the "local" vendor mode. The +example crate just depends on `rand`, and we patch out two of the transitive deps: + +- `getrandom` is forked. In `forked_getrandom/BUILD.bazel` the necessary +filegroups are exposed so that the generated BUILD file at +`vendor/getrandom-0.2.8/BUILD.bazel` can use them. +- `wasi` is "empty-patched" because it's not actually used in the build. When +using local vendoring, `crate_universe` runs `cargo vendor` which isn't aware +of which target-triples you're using. The `wasi` crate is only used by `rand` +when you're actually targeting `wasi` (which we're not), so we +patch it out with a stub crate to avoid downloading and vendoring the source +of the that crate. This can be helpful to avoid vendoring large crates or +ones with conflicting licenses that aren't used in your build graph. + +The result of our `getrandom` patch is that the binary is always given the +same value for seeding its RNG. Therefore we can test that `rand::random` +returns the same value to check that the patching behavior is working correctly. diff --git a/examples/crate_universe/vendor_local_patching/empty_wasi/Cargo.toml b/examples/crate_universe/vendor_local_patching/empty_wasi/Cargo.toml new file mode 100644 index 0000000000..f77ae02a6c --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/empty_wasi/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" diff --git a/examples/crate_universe/vendor_local_patching/empty_wasi/src/lib.rs b/examples/crate_universe/vendor_local_patching/empty_wasi/src/lib.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/crate_universe/vendor_local_patching/forked_getrandom/BUILD.bazel b/examples/crate_universe/vendor_local_patching/forked_getrandom/BUILD.bazel new file mode 100644 index 0000000000..71d850b293 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/forked_getrandom/BUILD.bazel @@ -0,0 +1,23 @@ +filegroup( + name = "crate_root", + srcs = ["src/lib.rs"], + visibility = ["//vendor_local_patching/vendor:__subpackages__"], +) + +filegroup( + name = "srcs", + srcs = glob(["**/*.rs"]), + visibility = ["//vendor_local_patching/vendor:__subpackages__"], +) + +filegroup( + name = "compile_data", + srcs = glob(["**/*"]), + visibility = ["//vendor_local_patching/vendor:__subpackages__"], +) + +filegroup( + name = "build_script_crate_root", + srcs = ["build.rs"], + visibility = ["//vendor_local_patching/vendor:__subpackages__"], +) diff --git a/examples/crate_universe/vendor_local_patching/forked_getrandom/Cargo.toml b/examples/crate_universe/vendor_local_patching/forked_getrandom/Cargo.toml new file mode 100644 index 0000000000..fd91b640eb --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/forked_getrandom/Cargo.toml @@ -0,0 +1,35 @@ +[package] +edition = "2018" +name = "getrandom" +version = "0.2.15" +authors = ["The Rand Project Developers"] +exclude = [".*"] +autobins = false +autoexamples = false +autotests = false +autobenches = false +description = "A small cross-platform library for retrieving random data from system source" +documentation = "https://docs.rs/getrandom" +readme = "README.md" +categories = ["os", "no-std"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/rust-random/getrandom" + +[lib] +name = "getrandom" +path = "src/lib.rs" + +[dependencies.cfg-if] +version = "1" + +[dependencies.compiler_builtins] +version = "0.1" +optional = true + +[dependencies.core] +version = "1.0" +optional = true +package = "rustc-std-workspace-core" + +[features] +std = [] diff --git a/examples/crate_universe/vendor_local_patching/forked_getrandom/build.rs b/examples/crate_universe/vendor_local_patching/forked_getrandom/build.rs new file mode 100644 index 0000000000..71d7ac7976 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/forked_getrandom/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rustc-env=RANDOM_NUMBER=4"); +} diff --git a/examples/crate_universe/vendor_local_patching/forked_getrandom/src/lib.rs b/examples/crate_universe/vendor_local_patching/forked_getrandom/src/lib.rs new file mode 100644 index 0000000000..1a6816069c --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/forked_getrandom/src/lib.rs @@ -0,0 +1,23 @@ +use std::{fmt, num::NonZeroU32}; + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub struct Error(NonZeroU32); +impl std::error::Error for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Error({})", self.0.get()) + } +} + +pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { + if dest.is_empty() { + return Ok(()); + } + let num = env!("RANDOM_NUMBER").parse().unwrap(); + for n in dest { + *n = num; // chosen by fair dice roll. + // guaranteed to be random. + } + Ok(()) +} diff --git a/examples/crate_universe/vendor_local_patching/src/main.rs b/examples/crate_universe/vendor_local_patching/src/main.rs new file mode 100644 index 0000000000..464196c200 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + println!("\"random\" number: {}", rand::random::()); +} + +#[cfg(test)] +mod tests { + #[test] + fn not_actually_random() { + assert_eq!(rand::random::(), 34253218); + } +} diff --git a/examples/crate_universe/vendor_local_patching/vendor/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/BUILD.bazel new file mode 100644 index 0000000000..283e95158c --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/BUILD.bazel @@ -0,0 +1,37 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +package(default_visibility = ["//visibility:public"]) + +exports_files( + [ + "cargo-bazel.json", + "defs.bzl", + ] + glob( + include = ["*.bazel"], + allow_empty = True, + ), +) + +filegroup( + name = "srcs", + srcs = glob( + include = [ + "*.bazel", + "*.bzl", + ], + allow_empty = True, + ), +) + +# Workspace Member Dependencies +alias( + name = "rand", + actual = "//vendor_local_patching/vendor/rand-0.8.5:rand", + tags = ["manual"], +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/alias_rules.bzl b/examples/crate_universe/vendor_local_patching/vendor/alias_rules.bzl new file mode 100644 index 0000000000..14b04c1272 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/alias_rules.bzl @@ -0,0 +1,47 @@ +"""Alias that transitions its target to `compilation_mode=opt`. Use `transition_alias="opt"` to enable.""" + +load("@rules_cc//cc:defs.bzl", "CcInfo") +load("@rules_rust//rust:rust_common.bzl", "COMMON_PROVIDERS") + +def _transition_alias_impl(ctx): + # `ctx.attr.actual` is a list of 1 item due to the transition + providers = [ctx.attr.actual[0][provider] for provider in COMMON_PROVIDERS] + if CcInfo in ctx.attr.actual[0]: + providers.append(ctx.attr.actual[0][CcInfo]) + return providers + +def _change_compilation_mode(compilation_mode): + def _change_compilation_mode_impl(_settings, _attr): + return { + "//command_line_option:compilation_mode": compilation_mode, + } + + return transition( + implementation = _change_compilation_mode_impl, + inputs = [], + outputs = [ + "//command_line_option:compilation_mode", + ], + ) + +def _transition_alias_rule(compilation_mode): + return rule( + implementation = _transition_alias_impl, + provides = COMMON_PROVIDERS, + attrs = { + "actual": attr.label( + mandatory = True, + doc = "`rust_library()` target to transition to `compilation_mode=opt`.", + providers = COMMON_PROVIDERS, + cfg = _change_compilation_mode(compilation_mode), + ), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), + }, + doc = "Transitions a Rust library crate to the `compilation_mode=opt`.", + ) + +transition_alias_dbg = _transition_alias_rule("dbg") +transition_alias_fastbuild = _transition_alias_rule("fastbuild") +transition_alias_opt = _transition_alias_rule("opt") diff --git a/examples/crate_universe/vendor_local_patching/vendor/cfg-if-1.0.0/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/cfg-if-1.0.0/BUILD.bazel new file mode 100644 index 0000000000..04f7dc24ff --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/cfg-if-1.0.0/BUILD.bazel @@ -0,0 +1,53 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "cfg_if", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_root = "src/lib.rs", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=cfg-if", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "1.0.0", +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/defs.bzl b/examples/crate_universe/vendor_local_patching/vendor/defs.bzl new file mode 100644 index 0000000000..fe96c5a200 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/defs.bzl @@ -0,0 +1,365 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### +""" +# `crates_repository` API + +- [aliases](#aliases) +- [crate_deps](#crate_deps) +- [all_crate_deps](#all_crate_deps) +- [crate_repositories](#crate_repositories) + +""" + +load("@bazel_skylib//lib:selects.bzl", "selects") + +############################################################################### +# MACROS API +############################################################################### + +# An identifier that represent common dependencies (unconditional). +_COMMON_CONDITION = "" + +def _flatten_dependency_maps(all_dependency_maps): + """Flatten a list of dependency maps into one dictionary. + + Dependency maps have the following structure: + + ```python + DEPENDENCIES_MAP = { + # The first key in the map is a Bazel package + # name of the workspace this file is defined in. + "workspace_member_package": { + + # Not all dependencies are supported for all platforms. + # the condition key is the condition required to be true + # on the host platform. + "condition": { + + # An alias to a crate target. # The label of the crate target the + # Aliases are only crate names. # package name refers to. + "package_name": "@full//:label", + } + } + } + ``` + + Args: + all_dependency_maps (list): A list of dicts as described above + + Returns: + dict: A dictionary as described above + """ + dependencies = {} + + for workspace_deps_map in all_dependency_maps: + for pkg_name, conditional_deps_map in workspace_deps_map.items(): + if pkg_name not in dependencies: + non_frozen_map = dict() + for key, values in conditional_deps_map.items(): + non_frozen_map.update({key: dict(values.items())}) + dependencies.setdefault(pkg_name, non_frozen_map) + continue + + for condition, deps_map in conditional_deps_map.items(): + # If the condition has not been recorded, do so and continue + if condition not in dependencies[pkg_name]: + dependencies[pkg_name].setdefault(condition, dict(deps_map.items())) + continue + + # Alert on any miss-matched dependencies + inconsistent_entries = [] + for crate_name, crate_label in deps_map.items(): + existing = dependencies[pkg_name][condition].get(crate_name) + if existing and existing != crate_label: + inconsistent_entries.append((crate_name, existing, crate_label)) + dependencies[pkg_name][condition].update({crate_name: crate_label}) + + return dependencies + +def crate_deps(deps, package_name = None): + """Finds the fully qualified label of the requested crates for the package where this macro is called. + + Args: + deps (list): The desired list of crate targets. + package_name (str, optional): The package name of the set of dependencies to look up. + Defaults to `native.package_name()`. + + Returns: + list: A list of labels to generated rust targets (str) + """ + + if not deps: + return [] + + if package_name == None: + package_name = native.package_name() + + # Join both sets of dependencies + dependencies = _flatten_dependency_maps([ + _NORMAL_DEPENDENCIES, + _NORMAL_DEV_DEPENDENCIES, + _PROC_MACRO_DEPENDENCIES, + _PROC_MACRO_DEV_DEPENDENCIES, + _BUILD_DEPENDENCIES, + _BUILD_PROC_MACRO_DEPENDENCIES, + ]).pop(package_name, {}) + + # Combine all conditional packages so we can easily index over a flat list + # TODO: Perhaps this should actually return select statements and maintain + # the conditionals of the dependencies + flat_deps = {} + for deps_set in dependencies.values(): + for crate_name, crate_label in deps_set.items(): + flat_deps.update({crate_name: crate_label}) + + missing_crates = [] + crate_targets = [] + for crate_target in deps: + if crate_target not in flat_deps: + missing_crates.append(crate_target) + else: + crate_targets.append(flat_deps[crate_target]) + + if missing_crates: + fail("Could not find crates `{}` among dependencies of `{}`. Available dependencies were `{}`".format( + missing_crates, + package_name, + dependencies, + )) + + return crate_targets + +def all_crate_deps( + normal = False, + normal_dev = False, + proc_macro = False, + proc_macro_dev = False, + build = False, + build_proc_macro = False, + package_name = None): + """Finds the fully qualified label of all requested direct crate dependencies \ + for the package where this macro is called. + + If no parameters are set, all normal dependencies are returned. Setting any one flag will + otherwise impact the contents of the returned list. + + Args: + normal (bool, optional): If True, normal dependencies are included in the + output list. + normal_dev (bool, optional): If True, normal dev dependencies will be + included in the output list.. + proc_macro (bool, optional): If True, proc_macro dependencies are included + in the output list. + proc_macro_dev (bool, optional): If True, dev proc_macro dependencies are + included in the output list. + build (bool, optional): If True, build dependencies are included + in the output list. + build_proc_macro (bool, optional): If True, build proc_macro dependencies are + included in the output list. + package_name (str, optional): The package name of the set of dependencies to look up. + Defaults to `native.package_name()` when unset. + + Returns: + list: A list of labels to generated rust targets (str) + """ + + if package_name == None: + package_name = native.package_name() + + # Determine the relevant maps to use + all_dependency_maps = [] + if normal: + all_dependency_maps.append(_NORMAL_DEPENDENCIES) + if normal_dev: + all_dependency_maps.append(_NORMAL_DEV_DEPENDENCIES) + if proc_macro: + all_dependency_maps.append(_PROC_MACRO_DEPENDENCIES) + if proc_macro_dev: + all_dependency_maps.append(_PROC_MACRO_DEV_DEPENDENCIES) + if build: + all_dependency_maps.append(_BUILD_DEPENDENCIES) + if build_proc_macro: + all_dependency_maps.append(_BUILD_PROC_MACRO_DEPENDENCIES) + + # Default to always using normal dependencies + if not all_dependency_maps: + all_dependency_maps.append(_NORMAL_DEPENDENCIES) + + dependencies = _flatten_dependency_maps(all_dependency_maps).pop(package_name, None) + + if not dependencies: + if dependencies == None: + fail("Tried to get all_crate_deps for package " + package_name + " but that package had no Cargo.toml file") + else: + return [] + + crate_deps = list(dependencies.pop(_COMMON_CONDITION, {}).values()) + for condition, deps in dependencies.items(): + crate_deps += selects.with_or({ + tuple(_CONDITIONS[condition]): deps.values(), + "//conditions:default": [], + }) + + return crate_deps + +def aliases( + normal = False, + normal_dev = False, + proc_macro = False, + proc_macro_dev = False, + build = False, + build_proc_macro = False, + package_name = None): + """Produces a map of Crate alias names to their original label + + If no dependency kinds are specified, `normal` and `proc_macro` are used by default. + Setting any one flag will otherwise determine the contents of the returned dict. + + Args: + normal (bool, optional): If True, normal dependencies are included in the + output list. + normal_dev (bool, optional): If True, normal dev dependencies will be + included in the output list.. + proc_macro (bool, optional): If True, proc_macro dependencies are included + in the output list. + proc_macro_dev (bool, optional): If True, dev proc_macro dependencies are + included in the output list. + build (bool, optional): If True, build dependencies are included + in the output list. + build_proc_macro (bool, optional): If True, build proc_macro dependencies are + included in the output list. + package_name (str, optional): The package name of the set of dependencies to look up. + Defaults to `native.package_name()` when unset. + + Returns: + dict: The aliases of all associated packages + """ + if package_name == None: + package_name = native.package_name() + + # Determine the relevant maps to use + all_aliases_maps = [] + if normal: + all_aliases_maps.append(_NORMAL_ALIASES) + if normal_dev: + all_aliases_maps.append(_NORMAL_DEV_ALIASES) + if proc_macro: + all_aliases_maps.append(_PROC_MACRO_ALIASES) + if proc_macro_dev: + all_aliases_maps.append(_PROC_MACRO_DEV_ALIASES) + if build: + all_aliases_maps.append(_BUILD_ALIASES) + if build_proc_macro: + all_aliases_maps.append(_BUILD_PROC_MACRO_ALIASES) + + # Default to always using normal aliases + if not all_aliases_maps: + all_aliases_maps.append(_NORMAL_ALIASES) + all_aliases_maps.append(_PROC_MACRO_ALIASES) + + aliases = _flatten_dependency_maps(all_aliases_maps).pop(package_name, None) + + if not aliases: + return dict() + + common_items = aliases.pop(_COMMON_CONDITION, {}).items() + + # If there are only common items in the dictionary, immediately return them + if not len(aliases.keys()) == 1: + return dict(common_items) + + # Build a single select statement where each conditional has accounted for the + # common set of aliases. + crate_aliases = {"//conditions:default": dict(common_items)} + for condition, deps in aliases.items(): + condition_triples = _CONDITIONS[condition] + for triple in condition_triples: + if triple in crate_aliases: + crate_aliases[triple].update(deps) + else: + crate_aliases.update({triple: dict(deps.items() + common_items)}) + + return select(crate_aliases) + +############################################################################### +# WORKSPACE MEMBER DEPS AND ALIASES +############################################################################### + +_NORMAL_DEPENDENCIES = { + "vendor_local_patching": { + _COMMON_CONDITION: { + "rand": Label("//vendor_local_patching/vendor/rand-0.8.5:rand"), + }, + }, +} + +_NORMAL_ALIASES = { + "vendor_local_patching": { + _COMMON_CONDITION: { + }, + }, +} + +_NORMAL_DEV_DEPENDENCIES = { + "vendor_local_patching": { + }, +} + +_NORMAL_DEV_ALIASES = { + "vendor_local_patching": { + }, +} + +_PROC_MACRO_DEPENDENCIES = { + "vendor_local_patching": { + }, +} + +_PROC_MACRO_ALIASES = { + "vendor_local_patching": { + }, +} + +_PROC_MACRO_DEV_DEPENDENCIES = { + "vendor_local_patching": { + }, +} + +_PROC_MACRO_DEV_ALIASES = { + "vendor_local_patching": { + }, +} + +_BUILD_DEPENDENCIES = { + "vendor_local_patching": { + }, +} + +_BUILD_ALIASES = { + "vendor_local_patching": { + }, +} + +_BUILD_PROC_MACRO_DEPENDENCIES = { + "vendor_local_patching": { + }, +} + +_BUILD_PROC_MACRO_ALIASES = { + "vendor_local_patching": { + }, +} + +_CONDITIONS = { + "aarch64-unknown-fuchsia": ["@rules_rust//rust/platform:aarch64-unknown-fuchsia"], + "aarch64-unknown-linux-gnu": ["@rules_rust//rust/platform:aarch64-unknown-linux-gnu"], + "x86_64-apple-darwin": ["@rules_rust//rust/platform:x86_64-apple-darwin"], + "x86_64-pc-windows-msvc": ["@rules_rust//rust/platform:x86_64-pc-windows-msvc"], + "x86_64-unknown-fuchsia": ["@rules_rust//rust/platform:x86_64-unknown-fuchsia"], + "x86_64-unknown-linux-gnu": ["@rules_rust//rust/platform:x86_64-unknown-linux-gnu"], +} diff --git a/examples/crate_universe/vendor_local_patching/vendor/getrandom-0.2.15/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/getrandom-0.2.15/BUILD.bazel new file mode 100644 index 0000000000..c6bb8e6a60 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/getrandom-0.2.15/BUILD.bazel @@ -0,0 +1,90 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//cargo:defs.bzl", "cargo_build_script") +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "getrandom", + srcs = ["//vendor_local_patching/forked_getrandom:srcs"], + compile_data = ["//vendor_local_patching/forked_getrandom:compile_data"], + crate_features = [ + "std", + ], + crate_root = "//vendor_local_patching/forked_getrandom:crate_root", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=getrandom", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "0.2.15", + deps = [ + "//vendor_local_patching/vendor/cfg-if-1.0.0:cfg_if", + "//vendor_local_patching/vendor/getrandom-0.2.15:build_script_build", + ], +) + +cargo_build_script( + name = "_bs", + srcs = ["//vendor_local_patching/forked_getrandom:srcs"], + compile_data = ["//vendor_local_patching/forked_getrandom:compile_data"], + crate_features = [ + "std", + ], + crate_name = "build_script_build", + crate_root = "//vendor_local_patching/forked_getrandom:build_script_crate_root", + edition = "2018", + pkg_name = "getrandom", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=getrandom", + "manual", + "noclippy", + "norustfmt", + ], + version = "0.2.15", + visibility = ["//visibility:private"], +) + +alias( + name = "build_script_build", + actual = ":_bs", + tags = ["manual"], +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/libc-0.2.141/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/libc-0.2.141/BUILD.bazel new file mode 100644 index 0000000000..a0e40fdf88 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/libc-0.2.141/BUILD.bazel @@ -0,0 +1,87 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//cargo:defs.bzl", "cargo_build_script") +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "libc", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_root = "src/lib.rs", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2015", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=libc", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "0.2.141", + deps = [ + "//vendor_local_patching/vendor/libc-0.2.141:build_script_build", + ], +) + +cargo_build_script( + name = "_bs", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_name = "build_script_build", + crate_root = "build.rs", + edition = "2015", + pkg_name = "libc", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=libc", + "manual", + "noclippy", + "norustfmt", + ], + version = "0.2.141", + visibility = ["//visibility:private"], +) + +alias( + name = "build_script_build", + actual = ":_bs", + tags = ["manual"], +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/ppv-lite86-0.2.17/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/ppv-lite86-0.2.17/BUILD.bazel new file mode 100644 index 0000000000..c3d8ac3777 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/ppv-lite86-0.2.17/BUILD.bazel @@ -0,0 +1,57 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "ppv_lite86", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_features = [ + "simd", + "std", + ], + crate_root = "src/lib.rs", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=ppv-lite86", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "0.2.17", +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/rand-0.8.5/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/rand-0.8.5/BUILD.bazel new file mode 100644 index 0000000000..6582c304bf --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/rand-0.8.5/BUILD.bazel @@ -0,0 +1,83 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "rand", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_features = [ + "alloc", + "default", + "getrandom", + "libc", + "rand_chacha", + "std", + "std_rng", + ], + crate_root = "src/lib.rs", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=rand", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "0.8.5", + deps = [ + "//vendor_local_patching/vendor/rand_chacha-0.3.1:rand_chacha", + "//vendor_local_patching/vendor/rand_core-0.6.4:rand_core", + ] + select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [ + "//vendor_local_patching/vendor/libc-0.2.141:libc", # aarch64-unknown-fuchsia + ], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [ + "//vendor_local_patching/vendor/libc-0.2.141:libc", # aarch64-unknown-linux-gnu + ], + "@rules_rust//rust/platform:x86_64-apple-darwin": [ + "//vendor_local_patching/vendor/libc-0.2.141:libc", # x86_64-apple-darwin + ], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [ + "//vendor_local_patching/vendor/libc-0.2.141:libc", # x86_64-unknown-fuchsia + ], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [ + "//vendor_local_patching/vendor/libc-0.2.141:libc", # x86_64-unknown-linux-gnu + ], + "//conditions:default": [], + }), +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/rand_chacha-0.3.1/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/rand_chacha-0.3.1/BUILD.bazel new file mode 100644 index 0000000000..35c09e5585 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/rand_chacha-0.3.1/BUILD.bazel @@ -0,0 +1,60 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "rand_chacha", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_features = [ + "std", + ], + crate_root = "src/lib.rs", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=rand_chacha", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "0.3.1", + deps = [ + "//vendor_local_patching/vendor/ppv-lite86-0.2.17:ppv_lite86", + "//vendor_local_patching/vendor/rand_core-0.6.4:rand_core", + ], +) diff --git a/examples/crate_universe/vendor_local_patching/vendor/rand_core-0.6.4/BUILD.bazel b/examples/crate_universe/vendor_local_patching/vendor/rand_core-0.6.4/BUILD.bazel new file mode 100644 index 0000000000..97e3134f07 --- /dev/null +++ b/examples/crate_universe/vendor_local_patching/vendor/rand_core-0.6.4/BUILD.bazel @@ -0,0 +1,61 @@ +############################################################################### +# @generated +# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To +# regenerate this file, run the following: +# +# bazel run @//vendor_local_patching:vendor +############################################################################### + +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +rust_library( + name = "rand_core", + srcs = glob( + include = ["**/*.rs"], + allow_empty = False, + ), + crate_features = [ + "alloc", + "getrandom", + "std", + ], + crate_root = "src/lib.rs", + data = glob( + include = [], + allow_empty = True, + exclude = [ + "**/* *", + ".tmp_git_root/**/*", + "BUILD", + "BUILD.bazel", + "WORKSPACE", + "WORKSPACE.bazel", + ], + ), + edition = "2018", + rustc_flags = [ + "--cap-lints=allow", + ], + tags = [ + "cargo-bazel", + "crate-name=rand_core", + "manual", + "noclippy", + "norustfmt", + ], + target_compatible_with = select({ + "@rules_rust//rust/platform:aarch64-unknown-fuchsia": [], + "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [], + "@rules_rust//rust/platform:x86_64-apple-darwin": [], + "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [], + "@rules_rust//rust/platform:x86_64-unknown-fuchsia": [], + "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + version = "0.6.4", + deps = [ + "//vendor_local_patching/vendor/getrandom-0.2.15:getrandom", + ], +) diff --git a/util/fetch_shas/fetch_shas_TARGETS.txt b/util/fetch_shas/fetch_shas_TARGETS.txt index a098fcee4e..eed3092fc4 100644 --- a/util/fetch_shas/fetch_shas_TARGETS.txt +++ b/util/fetch_shas/fetch_shas_TARGETS.txt @@ -1,7 +1,6 @@ aarch64-apple-darwin aarch64-apple-ios aarch64-apple-ios-sim -aarch64-fuchsia aarch64-linux-android aarch64-pc-windows-msvc aarch64-unknown-fuchsia @@ -76,7 +75,6 @@ wasm64-unknown-unknown x86_64-apple-darwin x86_64-apple-ios x86_64-fortanix-unknown-sgx -x86_64-fuchsia x86_64-linux-android x86_64-pc-solaris x86_64-pc-windows-gnu