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
use crate::errors::TargetRunnerError;
use camino::Utf8PathBuf;
use target_spec::Platform;
#[derive(Debug)]
pub struct TargetRunner {
runner_binary: Utf8PathBuf,
args: Vec<String>,
}
impl TargetRunner {
pub fn for_target(target_triple: Option<&str>) -> Result<Option<Self>, TargetRunnerError> {
Self::get_runner_by_precedence(target_triple, true, None)
}
pub fn with_root(
target_triple: Option<&str>,
use_cargo_home: bool,
root: Utf8PathBuf,
) -> Result<Option<Self>, TargetRunnerError> {
Self::get_runner_by_precedence(target_triple, use_cargo_home, Some(root))
}
fn get_runner_by_precedence(
target_triple: Option<&str>,
use_cargo_home: bool,
root: Option<Utf8PathBuf>,
) -> Result<Option<Self>, TargetRunnerError> {
let target = match target_triple {
Some(target) => target_spec::Platform::from_triple(
target_spec::Triple::new(target.to_owned()).map_err(|error| {
TargetRunnerError::FailedToParseTargetTriple {
triple: target.to_owned(),
error,
}
})?,
target_spec::TargetFeatures::Unknown,
),
None => {
target_spec::Platform::current().map_err(TargetRunnerError::UnknownHostPlatform)?
}
};
if let Some(tr) = Self::from_env(Self::runner_env_var(&target))? {
return Ok(Some(tr));
}
let root = match root {
Some(rp) => rp,
None => {
std::env::current_dir()
.map_err(TargetRunnerError::UnableToReadDir)
.and_then(|cwd| {
Utf8PathBuf::from_path_buf(cwd).map_err(TargetRunnerError::NonUtf8Path)
})?
}
};
Self::find_config(target, use_cargo_home, root)
}
fn from_env(env_key: String) -> Result<Option<Self>, TargetRunnerError> {
if let Some(runner_var) = std::env::var_os(&env_key) {
let runner = runner_var
.into_string()
.map_err(|_osstr| TargetRunnerError::InvalidEnvironmentVar(env_key.clone()))?;
Self::parse_runner(&env_key, runner).map(Some)
} else {
Ok(None)
}
}
#[doc(hidden)]
pub fn runner_env_var(target: &Platform) -> String {
let triple_str = target.triple_str().to_ascii_uppercase().replace('-', "_");
format!("CARGO_TARGET_{}_RUNNER", triple_str)
}
pub fn find_config(
target: target_spec::Platform,
use_cargo_home: bool,
root: Utf8PathBuf,
) -> Result<Option<Self>, TargetRunnerError> {
let mut configs = Vec::new();
fn read_config_dir(dir: &mut Utf8PathBuf) -> Option<Utf8PathBuf> {
dir.push("config");
if !dir.exists() {
dir.set_extension("toml");
}
let ret = if dir.exists() {
Some(dir.clone())
} else {
None
};
dir.pop();
ret
}
let mut dir = root
.canonicalize()
.map_err(|error| TargetRunnerError::FailedPathCanonicalization {
path: root.clone(),
error,
})
.and_then(|canon| {
Utf8PathBuf::from_path_buf(canon).map_err(TargetRunnerError::NonUtf8Path)
})?;
for _ in 0..dir.ancestors().count() {
dir.push(".cargo");
if !dir.exists() {
dir.pop();
dir.pop();
continue;
}
if let Some(config) = read_config_dir(&mut dir) {
configs.push(config);
}
dir.pop();
dir.pop();
}
if use_cargo_home {
let mut cargo_home_path = home::cargo_home_with_cwd(root.as_std_path())
.map_err(TargetRunnerError::UnableToReadDir)
.and_then(|home| {
Utf8PathBuf::from_path_buf(home).map_err(TargetRunnerError::NonUtf8Path)
})?;
if let Some(home_config) = read_config_dir(&mut cargo_home_path) {
if !configs.iter().any(|path| path == &home_config) {
configs.push(home_config);
}
}
}
#[derive(serde::Deserialize, Debug)]
struct CargoConfigRunner {
runner: Option<String>,
}
#[derive(serde::Deserialize, Debug)]
struct CargoConfig {
target: Option<std::collections::BTreeMap<String, CargoConfigRunner>>,
}
let mut target_runner = None;
let triple_runner_key = format!("target.{}.runner", target.triple_str());
'config: for config_path in configs.into_iter().rev() {
let config_contents = std::fs::read_to_string(&config_path).map_err(|error| {
TargetRunnerError::FailedToReadConfig {
path: config_path.clone(),
error,
}
})?;
let config: CargoConfig = toml::from_str(&config_contents).map_err(|error| {
TargetRunnerError::FailedToParseConfig {
path: config_path.clone(),
error,
}
})?;
if let Some(mut targets) = config.target {
if let Some(target) = targets.remove(target.triple_str()) {
if let Some(runner) = target.runner {
target_runner = Some(Self::parse_runner(&triple_runner_key, runner)?);
continue;
}
}
for (cfg, runner) in targets.into_iter().filter_map(|(k, v)| match v.runner {
Some(runner) if k.starts_with("cfg(") => Some((k, runner)),
_ => None,
}) {
let expr = match target_spec::TargetExpression::new(&cfg) {
Ok(expr) => expr,
Err(_err) => continue,
};
if expr.eval(&target) == Some(true) {
target_runner = Some(Self::parse_runner(&triple_runner_key, runner)?);
continue 'config;
}
}
}
}
Ok(target_runner)
}
fn parse_runner(key: &str, value: String) -> Result<Self, TargetRunnerError> {
let mut runner_iter = value.split_whitespace();
let runner_binary =
runner_iter
.next()
.ok_or_else(|| TargetRunnerError::BinaryNotSpecified {
key: key.to_owned(),
value: value.clone(),
})?;
let args = runner_iter.map(String::from).collect();
Ok(Self {
runner_binary: runner_binary.into(),
args,
})
}
#[inline]
pub fn binary(&self) -> &str {
self.runner_binary.as_str()
}
#[inline]
pub fn args(&self) -> impl Iterator<Item = &str> {
self.args.iter().map(AsRef::as_ref)
}
}