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
use std::error::Error;
use std::fmt::Display;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::OnceLock;
use cargo_metadata::diagnostic::Diagnostic;
use memo_map::MemoMap;
use tokio::sync::OnceCell;
use crate::HostTargetType;
use crate::progress::ProgressTracker;
/// Build parameters for [`build_crate_memoized`].
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct BuildParams {
/// The working directory for the build, where the `cargo build` command will be run. Crate root.
/// [`Self::new`] canonicalizes this path.
src: PathBuf,
/// The workspace root encompassing the build, which may be a parent of `src` in a multi-crate
/// workspace.
workspace_root: PathBuf,
/// `--bin` binary name parameter.
bin: Option<String>,
/// `--example` parameter.
example: Option<String>,
/// `--profile` parameter.
profile: Option<String>,
rustflags: Option<String>,
target_dir: Option<PathBuf>,
// Environment variables available during build
build_env: Vec<(String, String)>,
no_default_features: bool,
/// `--target <linux>` if cross-compiling for linux ([`HostTargetType::Linux`]).
target_type: HostTargetType,
/// True is the build should use dynamic linking.
is_dylib: bool,
/// `--features` flags, will be comma-delimited.
features: Option<Vec<String>>,
/// `--config` flag
config: Vec<String>,
}
impl BuildParams {
/// Creates a new `BuildParams` and canonicalizes the `src` path.
#[expect(clippy::too_many_arguments, reason = "internal code")]
pub fn new(
src: impl AsRef<Path>,
workspace_root: impl AsRef<Path>,
bin: Option<String>,
example: Option<String>,
profile: Option<String>,
rustflags: Option<String>,
target_dir: Option<PathBuf>,
build_env: Vec<(String, String)>,
no_default_features: bool,
target_type: HostTargetType,
is_dylib: bool,
features: Option<Vec<String>>,
config: Vec<String>,
) -> Self {
// `fs::canonicalize` prepends windows paths with the `r"\\?\"`
// https://stackoverflow.com/questions/21194530/what-does-mean-when-prepended-to-a-file-path
// However, this breaks the `include!(concat!(env!("OUT_DIR"), "/my/forward/slash/path.rs"))`
// Rust codegen pattern on windows. To help mitigate this happening in third party crates, we
// instead use `dunce::canonicalize` which is the same as `fs::canonicalize` but avoids the
// `\\?\` prefix when possible.
let src = dunce::canonicalize(src.as_ref()).unwrap_or_else(|e| {
panic!(
"Failed to canonicalize path `{}` for build: {e}.",
src.as_ref().display(),
)
});
let workspace_root = dunce::canonicalize(workspace_root.as_ref()).unwrap_or_else(|e| {
panic!(
"Failed to canonicalize path `{}` for build: {e}.",
workspace_root.as_ref().display(),
)
});
BuildParams {
src,
workspace_root,
bin,
example,
profile,
rustflags,
target_dir,
build_env,
no_default_features,
target_type,
is_dylib,
features,
config,
}
}
}
/// Information about a built crate. See [`build_crate_memoized`].
pub struct BuildOutput {
/// The binary contents as a byte array.
pub bin_data: Vec<u8>,
/// The path to the binary file. [`Self::bin_data`] has a copy of the content.
pub bin_path: PathBuf,
/// Shared library path, containing any necessary dylibs.
pub shared_library_path: Option<PathBuf>,
}
impl BuildOutput {
/// A unique ID for the binary, based its contents.
pub fn unique_id(&self) -> impl use<> + Display {
blake3::hash(&self.bin_data).to_hex()
}
}
/// Build memoization cache.
static BUILDS: OnceLock<MemoMap<BuildParams, OnceCell<BuildOutput>>> = OnceLock::new();
pub async fn build_crate_memoized(params: BuildParams) -> Result<&'static BuildOutput, BuildError> {
BUILDS
.get_or_init(MemoMap::new)
.get_or_insert(¶ms, Default::default)
.get_or_try_init(move || {
ProgressTracker::rich_leaf("build", move |set_msg| async move {
tokio::task::spawn_blocking(move || {
let mut command = Command::new("cargo");
command.args(["build", "--locked"]);
if let Some(profile) = params.profile.as_ref() {
command.args(["--profile", profile]);
}
if let Some(bin) = params.bin.as_ref() {
command.args(["--bin", bin]);
}
if let Some(example) = params.example.as_ref() {
command.args(["--example", example]);
}
match params.target_type {
HostTargetType::Local => {}
HostTargetType::Linux(crate::LinuxCompileType::Glibc) => {
command.args(["--target", "x86_64-unknown-linux-gnu"]);
}
HostTargetType::Linux(crate::LinuxCompileType::Musl) => {
command.args(["--target", "x86_64-unknown-linux-musl"]);
}
}
if params.no_default_features {
command.arg("--no-default-features");
}
if let Some(features) = params.features {
command.args(["--features", &features.join(",")]);
}
for config in ¶ms.config {
command.args(["--config", config]);
}
command.arg("--message-format=json-diagnostic-rendered-ansi");
if let Some(target_dir) = params.target_dir.as_ref() {
command.args(["--target-dir", target_dir.to_str().unwrap()]);
}
if let Some(rustflags) = params.rustflags.as_ref() {
command.env("RUSTFLAGS", rustflags);
}
for (k, v) in params.build_env {
command.env(k, v);
}
let mut spawned = command
.current_dir(¶ms.src)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::null())
.spawn()
.unwrap();
let reader = std::io::BufReader::new(spawned.stdout.take().unwrap());
let stderr_reader = std::io::BufReader::new(spawned.stderr.take().unwrap());
let stderr_worker = std::thread::spawn(move || {
let mut stderr_lines = Vec::new();
for line in stderr_reader.lines() {
let Ok(line) = line else {
break;
};
set_msg(line.clone());
stderr_lines.push(line);
}
stderr_lines
});
let mut diagnostics = Vec::new();
let mut text_lines = Vec::new();
for message in cargo_metadata::Message::parse_stream(reader) {
match message.unwrap() {
cargo_metadata::Message::CompilerArtifact(artifact) => {
let is_output = if params.example.is_some() {
artifact.target.kind.iter().any(|k| "example" == k)
} else {
artifact.target.kind.iter().any(|k| "bin" == k)
};
if is_output {
let path = artifact.executable.unwrap();
let path_buf: PathBuf = path.clone().into();
let path = path.into_string();
let data = std::fs::read(path).unwrap();
assert!(spawned.wait().unwrap().success());
return Ok(BuildOutput {
bin_data: data,
bin_path: path_buf,
shared_library_path: if params.is_dylib {
Some(
params
.target_dir
.as_ref()
.unwrap_or(¶ms.src.join("target"))
.join("debug")
.join("deps"),
)
} else {
None
},
});
}
}
cargo_metadata::Message::CompilerMessage(mut msg) => {
// Update the path displayed to enable clicking in IDE.
// TODO(mingwei): deduplicate code with hydro_lang sim/graph.rs
if let Some(rendered) = msg.message.rendered.as_mut() {
let file_names = msg
.message
.spans
.iter()
.map(|s| &s.file_name)
.collect::<std::collections::BTreeSet<_>>();
for file_name in file_names {
if Path::new(file_name).is_relative() {
*rendered = rendered.replace(
file_name,
&format!(
"(full path) {}/{file_name}",
params.workspace_root.display(),
),
)
}
}
}
ProgressTracker::println(msg.message.to_string());
diagnostics.push(msg.message);
}
cargo_metadata::Message::TextLine(line) => {
ProgressTracker::println(&line);
text_lines.push(line);
}
cargo_metadata::Message::BuildFinished(_) => {}
cargo_metadata::Message::BuildScriptExecuted(_) => {}
msg => panic!("Unexpected message type: {:?}", msg),
}
}
let exit_status = spawned.wait().unwrap();
if exit_status.success() {
Err(BuildError::NoBinaryEmitted)
} else {
let stderr_lines = stderr_worker
.join()
.expect("Stderr worker unexpectedly panicked.");
Err(BuildError::FailedToBuildCrate {
exit_status,
diagnostics,
text_lines,
stderr_lines,
})
}
})
.await
.map_err(|_| BuildError::TokioJoinError)?
})
})
.await
}
#[derive(Clone, Debug)]
pub enum BuildError {
FailedToBuildCrate {
exit_status: ExitStatus,
diagnostics: Vec<Diagnostic>,
text_lines: Vec<String>,
stderr_lines: Vec<String>,
},
TokioJoinError,
NoBinaryEmitted,
}
impl Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FailedToBuildCrate {
exit_status,
diagnostics,
text_lines,
stderr_lines,
} => {
writeln!(f, "Failed to build crate ({})", exit_status)?;
writeln!(f, "Diagnostics ({}):", diagnostics.len())?;
for diagnostic in diagnostics {
write!(f, "{}", diagnostic)?;
}
writeln!(f, "Text output ({} lines):", text_lines.len())?;
for line in text_lines {
writeln!(f, "{}", line)?;
}
writeln!(f, "Stderr output ({} lines):", stderr_lines.len())?;
for line in stderr_lines {
writeln!(f, "{}", line)?;
}
}
Self::TokioJoinError => {
write!(f, "Failed to spawn tokio blocking task.")?;
}
Self::NoBinaryEmitted => {
write!(f, "`cargo build` succeeded but no binary was emitted.")?;
}
}
Ok(())
}
}
impl Error for BuildError {}