-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbuild.rs
More file actions
217 lines (184 loc) · 7.03 KB
/
build.rs
File metadata and controls
217 lines (184 loc) · 7.03 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
use crate::{
app::{Filters, PackageFilters},
helpers::{clear_dir, get_codspeed_target_dir},
measurement_mode::MeasurementMode,
prelude::*,
};
use cargo_metadata::{camino::Utf8PathBuf, Message, Metadata, TargetKind};
use std::process::{exit, Command, Stdio};
struct BuildOptions<'a> {
filters: Filters,
features: &'a Option<Vec<String>>,
profile: &'a str,
passthrough_flags: &'a Vec<String>,
}
struct BuiltBench {
package: String,
bench: String,
executable_path: Utf8PathBuf,
}
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();
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_name = artifact.target.name;
let add_bench_to_codspeed_dir = match &self.filters.bench {
Some(allowed_bench_names) => allowed_bench_names
.iter()
.any(|allowed_bench_name| bench_name.contains(allowed_bench_name)),
None => true,
};
if add_bench_to_codspeed_dir {
built_benches.push(BuiltBench {
package: package.name.clone(),
bench: bench_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"));
}
for built_bench in &built_benches {
eprintln!(
"Built benchmark `{}` in package `{}`",
built_bench.bench, built_bench.package
);
}
Ok(built_benches)
}
/// 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.args(["build", "--benches"]);
let mut rust_flags = std::env::var("RUSTFLAGS").unwrap_or_else(|_| "".into());
// Add debug info (equivalent to -g)
rust_flags.push_str(" -C debuginfo=2");
// 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.
rust_flags.push_str(" -C strip=none");
// Add the codspeed cfg flag if instrumentation mode is enabled
if measurement_mode == MeasurementMode::Instrumentation {
rust_flags.push_str(" --cfg codspeed");
}
cargo.env("RUSTFLAGS", rust_flags);
if let Some(features) = self.features {
cargo.arg("--features").arg(features.join(","));
}
cargo.args(self.passthrough_flags);
cargo.arg("--profile").arg(self.profile);
self.filters.package.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,
filters: Filters,
features: Option<Vec<String>>,
profile: String,
quiet: bool,
measurement_mode: MeasurementMode,
passthrough_flags: Vec<String>,
) -> Result<()> {
let built_benches = BuildOptions {
filters,
features: &features,
profile: &profile,
passthrough_flags: &passthrough_flags,
}
.build(metadata, quiet, 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, 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(())
}