Skip to content

Commit e965f80

Browse files
feat(cargo-codspeed): use cargo from cli
1 parent 1399846 commit e965f80

File tree

11 files changed

+494
-3097
lines changed

11 files changed

+494
-3097
lines changed

Cargo.lock

Lines changed: 211 additions & 2783 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cargo-codspeed/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ categories = [
1818
keywords = ["codspeed", "benchmark", "cargo"]
1919

2020
[dependencies]
21-
cargo = "0.81.0"
21+
cargo_metadata = "0.19.1"
2222
clap = { version = "=4.5.17", features = ["derive"] }
2323
termcolor = "1.4"
2424
anyhow = "1.0.86"
@@ -30,6 +30,3 @@ assert_cmd = "2.0.15"
3030
fs_extra = "1.3.0"
3131
predicates = "3.1.2"
3232
uuid = { version = "1.10.0", features = ["v4"] }
33-
34-
[features]
35-
vendored-openssl = ["cargo/vendored-openssl"]

crates/cargo-codspeed/README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ A cargo subcommand for running CodSpeed on your project
1212

1313
## Installation
1414

15-
### With `cargo-binstall`(recommended)
15+
### With `cargo`
16+
17+
```bash
18+
cargo install cargo-codspeed --locked
19+
```
20+
21+
### With `cargo-binstall`(recommended in CI)
1622

1723
[`cargo-binstall`](https://github.com/cargo-bins/cargo-binstall) enables you to install binaries directly without having to build from the source(with `cargo install`) every time.
1824

@@ -28,12 +34,6 @@ You can then install `cargo-codspeed` with:
2834
cargo binstall cargo-codspeed
2935
```
3036

31-
### With `cargo`
32-
33-
```bash
34-
cargo install cargo-codspeed --locked
35-
```
36-
3737
## Usage
3838

3939
```
@@ -48,10 +48,6 @@ Options:
4848
-V, --version Print version information
4949
```
5050

51-
## Advanced Usage
52-
53-
The `vendored-openssl` feature can be used to statically link with openssl with `cargo install cargo-codspeed --features vendored-openssl`.
54-
5551
## Development
5652

5753
### Troubleshooting

crates/cargo-codspeed/src/app.rs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
1-
use std::{ffi::OsString, process::exit};
2-
3-
use crate::helpers::style;
41
use crate::{prelude::*, run::run_benches};
5-
6-
use cargo::GlobalContext;
7-
use cargo::{ops::Packages, util::important_paths::find_root_manifest_for_wd};
2+
use cargo_metadata::MetadataCommand;
83
use clap::{Args, Parser, Subcommand};
4+
use std::{ffi::OsString, process::exit};
95

106
use crate::build::build_benches;
117

128
#[derive(Parser)]
139
#[command(author, version, about, long_about = None)]
1410
struct Cli {
11+
/// Do not print cargo log messages
12+
#[arg(short, long, global = true)]
13+
quiet: bool,
14+
1515
#[command(subcommand)]
1616
command: Commands,
1717
}
1818

19-
/// Package selection flags
2019
#[derive(Args)]
21-
struct PackageSelection {
20+
pub(crate) struct PackageFilters {
2221
/// Select all packages in the workspace
2322
#[arg(long)]
24-
workspace: bool,
23+
pub(crate) workspace: bool,
2524
/// Exclude packages
2625
#[arg(long)]
27-
exclude: Vec<String>,
28-
/// Package to select
26+
pub(crate) exclude: Vec<String>,
27+
/// Package to select (builds all workspace package by default)
2928
#[arg(short, long)]
30-
package: Vec<String>,
29+
pub(crate) package: Vec<String>,
30+
}
31+
32+
#[derive(Args)]
33+
pub(crate) struct Filters {
34+
/// Optional list of benchmarks to build (builds all benchmarks by default)
35+
pub(crate) bench: Option<Vec<String>>,
36+
#[command(flatten)]
37+
pub(crate) package: PackageFilters,
3138
}
3239

3340
#[derive(Subcommand)]
3441
enum Commands {
3542
/// Build the benchmarks
3643
Build {
37-
/// Optional list of benchmarks to build (builds all benchmarks by default)
38-
benches: Option<Vec<String>>,
39-
4044
#[command(flatten)]
41-
package_selection: PackageSelection,
45+
filters: Filters,
4246

4347
/// Space or comma separated list of features to activate
4448
#[arg(short = 'F', long)]
@@ -50,60 +54,38 @@ enum Commands {
5054
},
5155
/// Run the previously built benchmarks
5256
Run {
53-
/// Optional list of benchmarks to run (run all found benchmarks by default)
54-
benches: Option<Vec<String>>,
55-
5657
#[command(flatten)]
57-
package_selection: PackageSelection,
58+
filters: Filters,
5859
},
5960
}
6061

61-
pub fn get_cargo_config() -> Result<GlobalContext> {
62+
/// Forwards the `codspeed` cfg flag to the compiler through env variable for all future
63+
fn set_codspeed_rustflags() {
6264
let mut rustflags = std::env::var("RUSTFLAGS").unwrap_or_else(|_| "".into());
6365
rustflags.push_str(" -g --cfg codspeed");
6466
std::env::set_var("RUSTFLAGS", &rustflags);
65-
GlobalContext::default()
6667
}
6768

6869
pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> {
70+
set_codspeed_rustflags();
71+
let metadata = MetadataCommand::new().exec()?;
6972
let cli = Cli::try_parse_from(args)?;
70-
let cargo_config = get_cargo_config()?;
71-
let manifest_path = find_root_manifest_for_wd(cargo_config.cwd())?;
72-
let ws = Workspace::new(&manifest_path, &cargo_config)?;
7373

7474
let res = match cli.command {
7575
Commands::Build {
76-
benches,
77-
package_selection,
76+
filters,
7877
features,
7978
profile,
8079
} => {
8180
let features =
8281
features.map(|f| f.split([' ', ',']).map(|s| s.to_string()).collect_vec());
83-
let packages = Packages::from_flags(
84-
package_selection.workspace,
85-
package_selection.exclude,
86-
package_selection.package,
87-
)?;
88-
build_benches(&ws, benches, packages, features, profile)
89-
}
90-
Commands::Run {
91-
benches,
92-
package_selection,
93-
} => {
94-
let packages = Packages::from_flags(
95-
package_selection.workspace,
96-
package_selection.exclude,
97-
package_selection.package,
98-
)?;
99-
run_benches(&ws, benches, packages)
82+
build_benches(&metadata, filters, features, profile, cli.quiet)
10083
}
84+
Commands::Run { filters } => run_benches(&metadata, filters),
10185
};
10286

10387
if let Err(e) = res {
104-
ws.gctx()
105-
.shell()
106-
.status_with_color("Error", e.to_string(), &style::ERROR)?;
88+
eprintln!("Error: {e}");
10789
exit(1);
10890
}
10991

0 commit comments

Comments
 (0)