Skip to content

Commit 46cb2a0

Browse files
feat(cargo-codspeed): handle rustflags from .cargo/config.toml
1 parent 422c564 commit 46cb2a0

2 files changed

Lines changed: 52 additions & 22 deletions

File tree

crates/cargo-codspeed/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ repository = "https://github.com/CodSpeedHQ/codspeed-rust"
1010
homepage = "https://codspeed.io"
1111
license = "MIT OR Apache-2.0"
1212
categories = [
13-
"development-tools",
14-
"development-tools::cargo-plugins",
15-
"development-tools::profiling",
16-
"development-tools::testing",
13+
"development-tools",
14+
"development-tools::cargo-plugins",
15+
"development-tools::profiling",
16+
"development-tools::testing",
1717
]
1818
keywords = ["codspeed", "benchmark", "cargo"]
1919

crates/cargo-codspeed/src/build.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,60 @@ impl BuildOptions<'_> {
105105
Ok(built_benches)
106106
}
107107

108+
/// Adds debug flags and codspeed compilation
109+
///
110+
/// If the user has set `RUSTFLAGS`, it will append the flags to it.
111+
/// Else, and if the cargo version allows it, it will set the cargo config through
112+
/// `--config 'build.rustflags=[ ... ]'`
113+
///
114+
/// # Why we do this
115+
/// As tracked in [https://github.com/rust-lang/cargo/issues/5376], setting `RUSTFLAGS`
116+
/// completely overrides rustflags from rust config
117+
/// We use the cargo built-in config mechanism to set the flags if the user has not set
118+
/// `RUSTFLAGS`.
119+
fn add_rust_flags(&self, cargo: &mut Command, measurement_mode: MeasurementMode) {
120+
let mut flags = vec![
121+
// Add debug info (equivalent to -g)
122+
"-Cdebuginfo=2".to_owned(),
123+
// Prevent debug info stripping
124+
// https://doc.rust-lang.org/cargo/reference/profiles.html#release
125+
// According to cargo docs, for release profile which we default to:
126+
// `strip = "none"` and `debug = false`.
127+
// In practice, if we set debug info through RUSTFLAGS, cargo still strips them, most
128+
// likely because debug = false in the release profile.
129+
// We also need to disable stripping through rust flags.
130+
"-Cstrip=none".to_owned(),
131+
];
132+
133+
// Add the codspeed cfg flag if instrumentation mode is enabled
134+
if measurement_mode == MeasurementMode::Instrumentation {
135+
flags.push("--cfg".to_owned());
136+
flags.push("codspeed".to_owned());
137+
}
138+
139+
match std::env::var("RUSTFLAGS") {
140+
Result::Ok(existing_rustflags) => {
141+
let flags_str = flags.join(" ");
142+
cargo.env("RUSTFLAGS", format!("{existing_rustflags} {flags_str}"));
143+
}
144+
Err(_) => {
145+
// Use --config to set rustflags
146+
let config_value = format!(
147+
"build.rustflags=[{}]",
148+
flags.into_iter().map(|f| format!("\"{f}\"")).join(",")
149+
);
150+
cargo.arg("--config").arg(config_value);
151+
}
152+
}
153+
}
154+
108155
/// Generates a subcommand to build the benchmarks by invoking cargo and forwarding the filters
109156
/// This command explicitly ignores the `self.benches`: all benches are built
110157
fn build_command(&self, measurement_mode: MeasurementMode) -> Command {
111158
let mut cargo = Command::new("cargo");
112159
cargo.args(["build", "--benches"]);
113160

114-
let mut rust_flags = std::env::var("RUSTFLAGS").unwrap_or_else(|_| "".into());
115-
// Add debug info (equivalent to -g)
116-
rust_flags.push_str(" -C debuginfo=2");
117-
118-
// Prevent debug info stripping
119-
// https://doc.rust-lang.org/cargo/reference/profiles.html#release
120-
// According to cargo docs, for release profile which we default to:
121-
// `strip = "none"` and `debug = false`.
122-
// In practice, if we set debug info through RUSTFLAGS, cargo still strips them, most
123-
// likely because debug = false in the release profile.
124-
// We also need to disable stripping through rust flags.
125-
rust_flags.push_str(" -C strip=none");
126-
127-
// Add the codspeed cfg flag if instrumentation mode is enabled
128-
if measurement_mode == MeasurementMode::Instrumentation {
129-
rust_flags.push_str(" --cfg codspeed");
130-
}
131-
cargo.env("RUSTFLAGS", rust_flags);
161+
self.add_rust_flags(&mut cargo, measurement_mode);
132162

133163
if let Some(features) = self.features {
134164
cargo.arg("--features").arg(features.join(","));

0 commit comments

Comments
 (0)