Skip to content

Commit f9b2e07

Browse files
feat(cargo-codspeed): use cargo from cli
1 parent 9474c1a commit f9b2e07

File tree

8 files changed

+472
-3081
lines changed

8 files changed

+472
-3081
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,18 +18,15 @@ categories = [
1818
keywords = ["codspeed", "benchmark", "cargo"]
1919

2020
[dependencies]
21-
cargo = "0.81.0"
2221
clap = { version = "=4.5.17", features = ["derive"] }
2322
termcolor = "1.4"
2423
anyhow = "1.0.86"
2524
itertools = "0.13.0"
2625
anstyle = "1.0.8"
26+
cargo_metadata = "0.19.1"
2727

2828
[dev-dependencies]
2929
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`(recommanded 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: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
11
use std::{ffi::OsString, process::exit};
22

3-
use crate::helpers::style;
43
use crate::{prelude::*, run::run_benches};
54

6-
use cargo::GlobalContext;
7-
use cargo::{ops::Packages, util::important_paths::find_root_manifest_for_wd};
5+
use cargo_metadata::MetadataCommand;
86
use clap::{Args, Parser, Subcommand};
97

108
use crate::build::build_benches;
119

1210
#[derive(Parser)]
1311
#[command(author, version, about, long_about = None)]
1412
struct Cli {
13+
/// Do not print cargo log messages
14+
#[arg(short, long, global = true)]
15+
quiet: bool,
16+
1517
#[command(subcommand)]
1618
command: Commands,
1719
}
1820

19-
/// Package selection flags
2021
#[derive(Args)]
21-
struct PackageSelection {
22+
pub(crate) struct PackageFilters {
2223
/// Select all packages in the workspace
2324
#[arg(long)]
24-
workspace: bool,
25+
pub(crate) workspace: bool,
2526
/// Exclude packages
2627
#[arg(long)]
27-
exclude: Vec<String>,
28-
/// Package to select
28+
pub(crate) exclude: Vec<String>,
29+
/// Package to select (builds all workspace package by default)
2930
#[arg(short, long)]
30-
package: Vec<String>,
31+
pub(crate) package: Vec<String>,
32+
}
33+
34+
/// Package selection flags
35+
#[derive(Args)]
36+
pub(crate) struct Filters {
37+
/// Optional list of benchmarks to build (builds all benchmarks by default)
38+
pub(crate) bench: Option<Vec<String>>,
39+
#[command(flatten)]
40+
pub(crate) package: PackageFilters,
3141
}
3242

3343
#[derive(Subcommand)]
3444
enum Commands {
3545
/// Build the benchmarks
3646
Build {
37-
/// Optional list of benchmarks to build (builds all benchmarks by default)
38-
benches: Option<Vec<String>>,
39-
4047
#[command(flatten)]
41-
package_selection: PackageSelection,
48+
filters: Filters,
4249

4350
/// Space or comma separated list of features to activate
4451
#[arg(short = 'F', long)]
@@ -50,60 +57,40 @@ enum Commands {
5057
},
5158
/// Run the previously built benchmarks
5259
Run {
53-
/// Optional list of benchmarks to run (run all found benchmarks by default)
54-
benches: Option<Vec<String>>,
55-
5660
#[command(flatten)]
57-
package_selection: PackageSelection,
61+
filters: Filters,
5862
},
5963
}
6064

61-
pub fn get_cargo_config() -> Result<GlobalContext> {
65+
/// Forward the `codspeed` cfg flag to the compiler through env variable for all managed here operations
66+
fn set_codspeed_rustflags() {
6267
let mut rustflags = std::env::var("RUSTFLAGS").unwrap_or_else(|_| "".into());
6368
rustflags.push_str(" -g --cfg codspeed");
6469
std::env::set_var("RUSTFLAGS", &rustflags);
65-
GlobalContext::default()
6670
}
6771

6872
pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> {
73+
set_codspeed_rustflags();
74+
75+
let metadata = MetadataCommand::new().exec()?;
76+
6977
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)?;
7378

7479
let res = match cli.command {
7580
Commands::Build {
76-
benches,
77-
package_selection,
81+
filters,
7882
features,
7983
profile,
8084
} => {
8185
let features =
8286
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)
87+
build_benches(&metadata, filters, features, profile, cli.quiet)
10088
}
89+
Commands::Run { filters } => run_benches(&metadata, filters),
10190
};
10291

10392
if let Err(e) = res {
104-
ws.gctx()
105-
.shell()
106-
.status_with_color("Error", e.to_string(), &style::ERROR)?;
93+
println!("Error: {}", e.to_string());
10794
exit(1);
10895
}
10996

0 commit comments

Comments
 (0)