-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcargo_cmd.rs
More file actions
282 lines (242 loc) · 9.26 KB
/
cargo_cmd.rs
File metadata and controls
282 lines (242 loc) · 9.26 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
use std::collections::HashMap;
use std::env;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use anyhow::{Result, bail};
pub trait CargoCmd {
fn manifest_path(&mut self, path: &Option<impl AsRef<Path>>) -> &mut Self;
fn target_dir(&mut self, path: impl AsRef<Path>) -> &mut Self;
fn target(&mut self, triplet: impl AsRef<str>) -> &mut Self;
fn cc_env(&mut self, triplet: impl AsRef<str>, cc: impl AsRef<Path>) -> &mut Self;
fn ar_env(&mut self, triplet: impl AsRef<str>, ar: impl AsRef<Path>) -> &mut Self;
fn sysroot(&mut self, path: impl AsRef<Path>) -> &mut Self;
fn entrypoint(&mut self, entry: impl AsRef<str>) -> &mut Self;
fn append_rustflags(&mut self, flags: impl AsRef<OsStr>) -> &mut Self;
fn append_cflags(&mut self, triplet: impl AsRef<str>, flags: impl AsRef<OsStr>) -> &mut Self;
fn append_bindgen_cflags(
&mut self,
triplet: impl AsRef<str>,
flags: impl AsRef<OsStr>,
) -> &mut Self;
fn allow_unstable(&mut self) -> &mut Self;
fn resolve_env(
&self,
base: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
) -> HashMap<OsString, OsString>;
fn checked_output(&mut self) -> Result<CheckedOutput>;
fn checked_status(&mut self) -> Result<()>;
}
#[derive(Clone, Hash)]
pub struct CargoBinary {
pub path: PathBuf,
pub rustup_toolchain: Option<OsString>,
}
impl CargoBinary {
pub fn command(&self) -> Command {
let mut cmd = Command::new(&self.path);
if let Some(rustup_toolchain) = &self.rustup_toolchain {
cmd.env("RUSTUP_TOOLCHAIN", rustup_toolchain);
}
cmd
}
}
pub fn find_cargo() -> Result<CargoBinary> {
let cargo = match env::var_os("CARGO") {
Some(cargo) => PathBuf::from(cargo),
None => which::which("cargo")?,
};
let rustup_toolchain = env::var_os("RUSTUP_TOOLCHAIN");
Ok(CargoBinary {
path: cargo,
rustup_toolchain,
})
}
pub fn cargo_cmd() -> Result<Command> {
Ok(find_cargo()?.command())
}
pub struct CheckedOutput {
pub stdout: Vec<u8>,
#[allow(dead_code)]
pub stderr: Vec<u8>,
}
impl CargoCmd for Command {
fn manifest_path(&mut self, path: &Option<impl AsRef<Path>>) -> &mut Self {
if let Some(path) = path {
self.arg("--manifest-path").arg(path.as_ref());
}
self
}
fn target_dir(&mut self, path: impl AsRef<Path>) -> &mut Self {
self.env("CARGO_BUILD_TARGET_DIR", path.as_ref());
self.env("CARGO_TARGET_DIR", path.as_ref());
self
}
fn target(&mut self, triplet: impl AsRef<str>) -> &mut Self {
self.env("CARGO_BUILD_TARGET", triplet.as_ref());
self
}
fn cc_env(&mut self, triplet: impl AsRef<str>, cc: impl AsRef<Path>) -> &mut Self {
// set both CC_<triplet> and CLANG_PATH so that cc-rs and bindgen can pick it up
// use CC_<triplet> as this is the highest priority for cc-rs
// see https://docs.rs/cc/latest/cc/#external-configuration-via-environment-variables
self.env(format!("CC_{}", triplet.as_ref()), cc.as_ref());
// bindgen uses clang-sys, which looks for clang in the CLANG_PATH env var
// see https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#environment-variables
// TODO(jprendes): can we make this env var target-specific as well?
self.env("CLANG_PATH", cc.as_ref());
// In windows, set LIBCLANG_PATH
if cfg!(windows)
&& let Some(parent) = cc.as_ref().parent()
{
// TODO(jprendes): is this considering all possible situations?
// * why is CLANG_PATH not enough on windows?
// see https://rust-lang.github.io/rust-bindgen/requirements.html#windows
// see https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#environment-variables
// TODO(jprendes): can we make this env var target-specific as well?
if parent.join("libclang.dll").exists() {
self.env("LIBCLANG_PATH", parent.join("libclang.dll"));
} else if parent.join("clang.dll").exists() {
self.env("LIBCLANG_PATH", parent.join("clang.dll"));
}
}
self
}
fn ar_env(&mut self, triplet: impl AsRef<str>, ar: impl AsRef<Path>) -> &mut Self {
// set AR_<triplet> so that cc-rs can pick it up
self.env(format!("AR_{}", triplet.as_ref()), ar.as_ref());
self
}
fn sysroot(&mut self, path: impl AsRef<Path>) -> &mut Self {
self.append_rustflags("--sysroot")
.append_rustflags(path.as_ref())
}
fn entrypoint(&mut self, entry: impl AsRef<str>) -> &mut Self {
let entry = entry.as_ref();
self.append_rustflags(format!("-Clink-args=-e{entry}"))
}
fn append_rustflags(&mut self, flags: impl AsRef<OsStr>) -> &mut Self {
if flags.as_ref().is_empty() {
return self;
}
let mut new_flags = get_env(self, "RUSTFLAGS").unwrap_or_default();
if !new_flags.is_empty() {
new_flags.push(" ");
}
new_flags.push(flags.as_ref());
self.env("RUSTFLAGS", new_flags);
self
}
fn append_cflags(&mut self, triplet: impl AsRef<str>, flags: impl AsRef<OsStr>) -> &mut Self {
if flags.as_ref().is_empty() {
return self;
}
let triplet = triplet.as_ref();
let mut new_flags = find_cflags(self, triplet);
if !new_flags.is_empty() {
new_flags.push(" ");
}
new_flags.push(flags.as_ref());
self.env(format!("CFLAGS_{triplet}"), new_flags);
self.append_bindgen_cflags(triplet, flags);
self
}
fn append_bindgen_cflags(
&mut self,
triplet: impl AsRef<str>,
flags: impl AsRef<OsStr>,
) -> &mut Self {
if flags.as_ref().is_empty() {
return self;
}
// For some reason we need to escape backslashes on Windows
// TODO(jprendes): check if we need to do any better escaping for other special characters
let flags = flags.as_ref().to_string_lossy().replace("\\", "\\\\");
// TODO(jprendes): account and use the target specific variants of BINDGEN_EXTRA_CLANG_ARGS
// see https://github.com/rust-lang/rust-bindgen/tree/main?tab=readme-ov-file#environment-variables
let mut new_flags =
get_env(self, "BINDGEN_EXTRA_CLANG_ARGS").unwrap_or_else(|| find_cflags(self, triplet));
if !new_flags.is_empty() {
new_flags.push(" ");
}
new_flags.push(flags);
self.env("BINDGEN_EXTRA_CLANG_ARGS", new_flags);
self
}
fn allow_unstable(&mut self) -> &mut Self {
self.env("RUSTC_BOOTSTRAP", "1")
}
fn resolve_env(
&self,
base: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
) -> HashMap<OsString, OsString> {
merge_env(base, self.get_envs())
}
fn checked_output(&mut self) -> Result<CheckedOutput> {
let output = self.output();
let Ok(output) = output else {
bail!("Failed to execute command:\n{self:?}");
};
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
if let Some(code) = output.status.code() {
bail!("Command exited with code {code}:\n{self:?}\n{stderr}");
} else {
bail!("Command terminated by signal:\n{self:?}\n{stderr}");
}
}
Ok(CheckedOutput {
stdout: output.stdout,
stderr: output.stderr,
})
}
fn checked_status(&mut self) -> Result<()> {
self.stderr(Stdio::inherit());
self.stdout(Stdio::inherit());
let _ = self.checked_output()?;
Ok(())
}
}
fn get_env(cmd: &Command, key: &str) -> Option<OsString> {
let mut envs = cmd.get_envs();
match envs.find(|(k, _)| *k == key) {
Some((_, v)) => v.map(ToOwned::to_owned),
None => std::env::var_os(key),
}
}
fn find_cflags(cmd: &Command, triplet: impl AsRef<str>) -> OsString {
let triplet = triplet.as_ref();
let triplet_snake_case = triplet.replace('-', "_");
let triplet_snake_case_upper = triplet_snake_case.to_uppercase();
let search_keys = [
format!("CFLAGS_{triplet}"),
format!("CFLAGS_{triplet_snake_case}"),
format!("CFLAGS_{triplet_snake_case_upper}"),
"CFLAGS_hyperlight".to_string(),
"CFLAGS_HYPERLIGHT".to_string(),
"HYPERLIGHT_CFLAGS".to_string(),
"TARGET_CFLAGS".to_string(),
"CFLAGS".to_string(),
];
search_keys
.iter()
.find_map(|key| get_env(cmd, key))
.unwrap_or_default()
}
pub fn merge_env(
base: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
envs: impl IntoIterator<Item = (impl AsRef<OsStr>, Option<impl AsRef<OsStr>>)>,
) -> HashMap<OsString, OsString> {
let mut base = base
.into_iter()
.map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned()))
.collect::<HashMap<_, _>>();
for (k, v) in envs {
if let Some(v) = v {
base.insert(k.as_ref().to_owned(), v.as_ref().to_owned());
} else {
base.remove(k.as_ref());
}
}
base
}