Skip to content

Commit 8e4a1ac

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

File tree

11 files changed

+489
-3097
lines changed

11 files changed

+489
-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: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
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+
/// Package selection flags
33+
#[derive(Args)]
34+
pub(crate) struct Filters {
35+
/// Optional list of benchmarks to build (builds all benchmarks by default)
36+
pub(crate) bench: Option<Vec<String>>,
37+
#[command(flatten)]
38+
pub(crate) package: PackageFilters,
3139
}
3240

3341
#[derive(Subcommand)]
3442
enum Commands {
3543
/// Build the benchmarks
3644
Build {
37-
/// Optional list of benchmarks to build (builds all benchmarks by default)
38-
benches: Option<Vec<String>>,
39-
4045
#[command(flatten)]
41-
package_selection: PackageSelection,
46+
filters: Filters,
4247

4348
/// Space or comma separated list of features to activate
4449
#[arg(short = 'F', long)]
@@ -50,60 +55,40 @@ enum Commands {
5055
},
5156
/// Run the previously built benchmarks
5257
Run {
53-
/// Optional list of benchmarks to run (run all found benchmarks by default)
54-
benches: Option<Vec<String>>,
55-
5658
#[command(flatten)]
57-
package_selection: PackageSelection,
59+
filters: Filters,
5860
},
5961
}
6062

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

6870
pub fn run(args: impl Iterator<Item = OsString>) -> Result<()> {
71+
set_codspeed_rustflags();
72+
73+
let metadata = MetadataCommand::new().exec()?;
74+
6975
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)?;
7376

7477
let res = match cli.command {
7578
Commands::Build {
76-
benches,
77-
package_selection,
79+
filters,
7880
features,
7981
profile,
8082
} => {
8183
let features =
8284
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)
85+
build_benches(&metadata, filters, features, profile, cli.quiet)
10086
}
87+
Commands::Run { filters } => run_benches(&metadata, filters),
10188
};
10289

10390
if let Err(e) = res {
104-
ws.gctx()
105-
.shell()
106-
.status_with_color("Error", e.to_string(), &style::ERROR)?;
91+
eprintln!("Error: {e}");
10792
exit(1);
10893
}
10994

0 commit comments

Comments
 (0)