-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbuild.rs
More file actions
348 lines (299 loc) · 12.3 KB
/
build.rs
File metadata and controls
348 lines (299 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::{
app::{BenchTargetFilters, PackageFilters},
helpers::{clear_dir, get_codspeed_target_dir},
measurement_mode::MeasurementMode,
prelude::*,
};
use anyhow::Context;
use cargo_metadata::{camino::Utf8PathBuf, Message, Metadata, TargetKind};
use std::collections::HashMap;
use std::process::{exit, Command, Stdio};
struct BuildOptions<'a> {
bench_target_filters: BenchTargetFilters,
package_filters: PackageFilters,
features: &'a Option<Vec<String>>,
profile: &'a str,
passthrough_flags: &'a Vec<String>,
}
struct BuiltBench {
package: String,
bench: String,
executable_path: Utf8PathBuf,
}
pub struct BuildConfig {
pub package_filters: PackageFilters,
pub bench_target_filters: BenchTargetFilters,
pub features: Option<Vec<String>>,
pub profile: String,
pub quiet: bool,
pub measurement_mode: MeasurementMode,
pub passthrough_flags: Vec<String>,
}
fn get_bench_harness_value(
manifest_path: &Utf8PathBuf,
bench_name: &str,
cache: &mut HashMap<Utf8PathBuf, toml::Table>,
) -> Result<bool> {
let manifest_table = if let Some(table) = cache.get(manifest_path) {
table
} else {
// Read and parse the Cargo.toml file
let manifest_content = std::fs::read_to_string(manifest_path)
.with_context(|| format!("Failed to read manifest at {manifest_path}"))?;
let table: toml::Table = toml::from_str(&manifest_content)
.with_context(|| format!("Failed to parse TOML in {manifest_path}"))?;
cache.insert(manifest_path.clone(), table);
cache.get(manifest_path).unwrap()
};
// Look for [[bench]] sections
let Some(benches) = manifest_table.get("bench").and_then(|v| v.as_array()) else {
// If no [[bench]] sections, it's not an error, benches present in <root>/benches/<name>.rs
// are still collected with harness = true
return Ok(true);
};
// Find the bench entry with matching name
let matching_bench = benches
.iter()
.filter_map(|bench| bench.as_table())
.find(|bench_table| {
bench_table
.get("name")
.and_then(|v| v.as_str())
.is_some_and(|name| name == bench_name)
});
// Check if harness is enabled (defaults to true)
let harness_enabled = matching_bench
.and_then(|bench_table| bench_table.get("harness"))
.and_then(|v| v.as_bool())
.unwrap_or(true);
Ok(harness_enabled)
}
impl BuildOptions<'_> {
/// Builds the benchmarks by invoking cargo
/// Returns a list of built benchmarks, with path to associated executables
fn build(
&self,
metadata: &Metadata,
quiet: bool,
measurement_mode: MeasurementMode,
) -> Result<Vec<BuiltBench>> {
let workspace_packages = metadata.workspace_packages();
let mut cargo = self.build_command(measurement_mode);
if quiet {
cargo.arg("--quiet");
}
cargo.args(["--message-format", "json"]);
let mut cargo = cargo
.stdout(Stdio::piped())
.spawn()
.expect("Failed to spawn cargo command");
let reader = std::io::BufReader::new(
cargo
.stdout
.take()
.expect("Unable to get stdout for child process"),
);
let mut built_benches = Vec::new();
let mut bench_targets_with_default_harness = Vec::new();
let mut manifest_cache = HashMap::new();
let package_names = self
.package_filters
.packages_from_flags(metadata)
.map_err(|e| {
// Avoid leaving an orphan cargo process, even if something went wrong
cargo.wait().expect("Could not get cargo's exist status");
e
})?
.into_iter()
.map(|t| t.name.clone())
.collect::<Vec<_>>();
for message in Message::parse_stream(reader) {
match message.expect("Failed to parse message") {
// Those messages will include build errors and warnings even if stderr also contain some of them
Message::CompilerMessage(msg) => {
println!("{}", &msg.message);
}
Message::TextLine(line) => {
println!("{line}");
}
Message::CompilerArtifact(artifact)
if artifact.target.is_kind(TargetKind::Bench) =>
{
let package = workspace_packages
.iter()
.find(|p| p.id == artifact.package_id)
.expect("Could not find package");
let bench_target_name = artifact.target.name;
let add_bench_to_codspeed_dir = package_names.iter().contains(&package.name);
if add_bench_to_codspeed_dir {
if get_bench_harness_value(
&package.manifest_path,
&bench_target_name,
&mut manifest_cache,
)? {
bench_targets_with_default_harness
.push((package.name.to_string(), bench_target_name.clone()));
}
built_benches.push(BuiltBench {
package: package.name.to_string(),
bench: bench_target_name,
executable_path: artifact
.executable
.expect("Unexpected missing executable path"),
});
}
}
_ => {}
}
}
let status = cargo.wait().expect("Could not get cargo's exist status");
if !status.success() {
exit(status.code().expect("Could not get exit code"));
}
if !bench_targets_with_default_harness.is_empty() {
let targets_list = bench_targets_with_default_harness
.into_iter()
.map(|(package, bench)| format!(" - `{bench}` in package `{package}`"))
.join("\n");
bail!("\
CodSpeed will not work with the following benchmark targets:
{targets_list}
CodSpeed requires benchmark targets to disable the default test harness because benchmark frameworks handle harnessing themselves.
Either disable the default harness by adding `harness = false` to the corresponding \
`[[bench]]` section in the Cargo.toml, or specify which targets to build by using \
`cargo codspeed build -p package_name --bench first_target --bench second_target`.
See `cargo codspeed build --help` for more information.");
}
for built_bench in &built_benches {
eprintln!(
"Built benchmark `{}` in package `{}`",
built_bench.bench, built_bench.package
);
}
Ok(built_benches)
}
/// Adds debug flags and codspeed compilation
///
/// If the user has set `RUSTFLAGS`, it will append the flags to it.
/// Else, and if the cargo version allows it, it will set the cargo config through
/// `--config 'build.rustflags=[ ... ]'`
///
/// # Why we do this
/// As tracked in [https://github.com/rust-lang/cargo/issues/5376], setting `RUSTFLAGS`
/// completely overrides rustflags from cargo config
/// We use the cargo built-in config mechanism to set the flags if the user has not set
/// `RUSTFLAGS`.
fn add_rust_flags(&self, cargo: &mut Command, measurement_mode: MeasurementMode) {
let mut flags = vec![
// Add debug info (equivalent to -g)
"-Cdebuginfo=2".to_owned(),
// Prevent debug info stripping
// https://doc.rust-lang.org/cargo/reference/profiles.html#release
// According to cargo docs, for release profile which we default to:
// `strip = "none"` and `debug = false`.
// In practice, if we set debug info through RUSTFLAGS, cargo still strips them, most
// likely because debug = false in the release profile.
// We also need to disable stripping through rust flags.
"-Cstrip=none".to_owned(),
];
// Add the codspeed cfg flag if the benchmark should only run once
if measurement_mode == MeasurementMode::Simulation
|| measurement_mode == MeasurementMode::Analysis
{
flags.push("--cfg=codspeed".to_owned());
}
match std::env::var("RUSTFLAGS") {
Result::Ok(existing_rustflags) => {
// Expand already existing RUSTFLAGS env var
let flags_str = flags.join(" ");
cargo.env("RUSTFLAGS", format!("{existing_rustflags} {flags_str}"));
}
Err(_) => {
// Use --config to set rustflags
// Our rust integration has an msrv of 1.74, --config is available since 1.63
// https://doc.rust-lang.org/nightly/cargo/CHANGELOG.html#cargo-163-2022-08-11
// Note: We have to use `target.cfg(all())` since `build` has a lower precedence.
let config_value = format!(
"target.'cfg(all())'.rustflags=[{}]",
flags.into_iter().map(|f| format!("\"{f}\"")).join(",")
);
cargo.arg("--config").arg(config_value);
}
}
}
/// Generates a subcommand to build the benchmarks by invoking cargo and forwarding the filters
/// This command explicitly ignores the `self.benches`: all benches are built
fn build_command(&self, measurement_mode: MeasurementMode) -> Command {
let mut cargo = Command::new("cargo");
cargo.arg("build");
if let Some(bench_target_filters) = &self.bench_target_filters.bench {
for bench_target_filter in bench_target_filters {
cargo.args(["--bench", bench_target_filter]);
}
} else {
cargo.args(["--benches"]);
}
self.add_rust_flags(&mut cargo, measurement_mode);
if let Some(features) = self.features {
cargo.arg("--features").arg(features.join(","));
}
cargo.args(self.passthrough_flags);
cargo.arg("--profile").arg(self.profile);
self.package_filters.add_cargo_args(&mut cargo);
cargo
}
}
impl PackageFilters {
fn add_cargo_args(&self, cargo: &mut Command) {
if self.workspace {
cargo.arg("--workspace");
}
if !self.package.is_empty() {
self.package.iter().for_each(|p| {
cargo.arg("--package").arg(p);
});
}
if !self.exclude.is_empty() {
self.exclude.iter().for_each(|p| {
cargo.arg("--exclude").arg(p);
});
}
}
}
pub fn build_benches(metadata: &Metadata, config: BuildConfig) -> Result<()> {
let built_benches = BuildOptions {
bench_target_filters: config.bench_target_filters,
package_filters: config.package_filters,
features: &config.features,
profile: &config.profile,
passthrough_flags: &config.passthrough_flags,
}
.build(metadata, config.quiet, config.measurement_mode)?;
if built_benches.is_empty() {
bail!(
"No benchmark target found. \
Please add a benchmark target to your Cargo.toml"
);
}
let codspeed_target_dir = get_codspeed_target_dir(metadata, config.measurement_mode);
let built_bench_count = built_benches.len();
// Create and clear packages codspeed target directories
let target_dir_to_clear = built_benches
.iter()
.unique_by(|bench| &bench.package)
.map(|bench| codspeed_target_dir.clone().join(&bench.package));
for target_dir in target_dir_to_clear {
std::fs::create_dir_all(&target_dir)?;
clear_dir(&target_dir)?;
}
// Copy built artifacts to codspeed target directory
for built_bench in built_benches {
let codspeed_target_package_dir = codspeed_target_dir.clone().join(&built_bench.package);
std::fs::copy(
built_bench.executable_path,
codspeed_target_package_dir.join(built_bench.bench),
)?;
}
eprintln!("Built {built_bench_count} benchmark suite(s)");
Ok(())
}