Skip to content

Commit

Permalink
feat: support a wildcard in exclude crate paths
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-sk committed Oct 7, 2024
1 parent 28c7760 commit edd3b7f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
/target
/vendor
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,43 @@ for example.

More information: https://github.com/rust-lang/cargo/issues/7058

# Generating a vendor/ directory with filtering
## Generating a vendor/ directory with filtering

Here's a basic example which filters out all crates that don't target Linux;
for example this will drop out crates like `winapi-x86_64-pc-windows-gnu` and
`core-foundation` that are Windows or MacOS only.

```
```sh
$ cargo vendor-filterer --platform=x86_64-unknown-linux-gnu
```

You may instead want to filter by tiers:

```
```sh
$ cargo vendor-filterer --tier=2
```

Currently this will drop out crates such as `redox_syscall`.

You can also declaratively specify the desired vendor configuration via the [Cargo metadata](https://doc.rust-lang.org/cargo/reference/manifest.html#the-metadata-table)
key `package.metadata.vendor-filter`. In this example, we include only tier 1 and 2 Linux platforms, and additionally remove some vendored C sources:
key `package.metadata.vendor-filter`. In this example, we include only tier 1 and 2 Linux platforms, and additionally remove some vendored C sources and `tests` folders from all crates:

```
```toml
[package.metadata.vendor-filter]
platforms = ["*-unknown-linux-gnu"]
tier = "2"
all-features = true
exclude-crate-paths = [ { name = "curl-sys", exclude = "curl" },
{ name = "libz-sys", exclude = "src/zlib" },
{ name = "libz-sys", exclude = "src/zlib-ng" },
{ name = "*", exclude = "tests" },
]
```

For workspaces, use the corresponding [workspace metadata](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-metadata-table)
key `workspace.metadata.vendor-filter`.

## Available options for for `package.metadata.vendor-filter` in Cargo.toml
### Available options for for `package.metadata.vendor-filter` in Cargo.toml

- `platforms`: List of rustc target triples; this is the same values accepted by
e.g. `cargo metadata --filter-platform`. You can specify multiple values,
Expand All @@ -52,10 +53,11 @@ key `workspace.metadata.vendor-filter`.
- `exclude-crate-paths`: Remove files and directories from target crates. A key
use case for this is removing the vendored copy of C libraries embedded in
crates like `libz-sys`, when you only want to support dynamically linking.
`*` wildcard removes the folder from all creates (typical use case for `tests` folder).

All of these options have corresponding CLI flags; see `cargo vendor-filterer --help`.

# Generating reproducible vendor tarballs
## Generating reproducible vendor tarballs

You can also provide `--format=tar.zstd` to output a reproducible tar archive
compressed via zstd; the default filename will be `vendor.tar.zstd`. Similarly
Expand Down
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,14 @@ struct Args {

/// Remove files/subdirectories in crates that match an exact path.
///
/// The format is "CRATENAME#PATH". CRATENAME is the name of a crate (without
/// a version included). PATH must be a relative path, and can name a regular
/// file, symbolic link or a directory.
/// The format is "CRATENAME#PATH". CRATENAME is the name of a crate (without
/// a version included) or "*" as a wildcard for all crates. PATH must be a
/// relative path, and can name a regular file, symbolic link or a directory.
///
/// If the filename matches a directory, it and all its contents will be removed.
/// For example, `curl-sys#curl` will remove the vendored libcurl C sources
/// from the `curl-sys` crate.
/// For example, `*#tests` will remove tests folder from all crates.
///
/// Nonexistent paths will emit a warning, but are not currently an error.
#[arg(long)]
Expand Down Expand Up @@ -712,8 +713,11 @@ fn delete_unreferenced_packages(
assert!(unreferenced.insert(name.to_string()));
}

if let Some(excludes) = excludes.get(name) {
process_excludes(&pbuf, name, excludes)?;
if let Some(crate_excludes) = excludes.get(name) {
process_excludes(&pbuf, name, crate_excludes)?;
}
if let Some(generic_excludes) = excludes.get("*") {
process_excludes(&pbuf, name, generic_excludes)?;
}

let r = pbuf.pop();
Expand Down
4 changes: 3 additions & 1 deletion tests/vendor_filterer/exclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ fn linux_multiple_platforms() {
let output = vendor(VendorOptions {
output: Some(&test_folder),
platforms: Some(&["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu"]),
exclude_crate_paths: Some(&["hex#benches"]),
exclude_crate_paths: Some(&["hex#benches", "*#tests"]),
..Default::default()
})
.unwrap();
assert!(output.status.success());
verify_no_windows(&test_folder);
test_folder.push("hex/benches");
assert!(!test_folder.exists());
test_folder.push("../tests");
assert!(!test_folder.exists());
}

0 comments on commit edd3b7f

Please sign in to comment.