Skip to content

Commit bb234d1

Browse files
committed
target: add -Zunstable-options when querying cfg
Since Rust 1.95,invoking rustc with `--target` requires `-Zunstable-options` when used with custom JSON targets. Without this flag, the command fails. Reference: rust-lang/rust@b4e645f rust-lang/rust@8563312 Signed-off-by: Deepesh Varatharajan <Deepesh.Varatharajan@windriver.com>
1 parent 67b9d25 commit bb234d1

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/target.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ impl Target {
3333

3434
cmd.arg("--print").arg("cfg");
3535
if let Some(target) = target {
36+
if Self::is_custom_target(target.as_ref()) {
37+
cmd.arg("-Zunstable-options");
38+
}
3639
cmd.arg("--target").arg(target);
3740
}
3841

@@ -77,6 +80,21 @@ impl Target {
7780
}
7881
}
7982

83+
/// Detect whether a target refers to a custom JSON target
84+
fn is_custom_target(target: &str) -> bool {
85+
if target.ends_with(".json") {
86+
return true;
87+
}
88+
89+
std::env::var_os("RUST_TARGET_PATH").map_or(false, |paths| {
90+
std::env::split_paths(&paths).any(|p| {
91+
let mut candidate = p.join(target);
92+
candidate.set_extension("json");
93+
candidate.exists()
94+
})
95+
})
96+
}
97+
8098
/// Produce the target name, if known
8199
pub fn name(&self) -> Option<&str> {
82100
self.target.as_ref().map(|t| t.short_name())
@@ -226,3 +244,35 @@ impl Target {
226244
"include".into()
227245
}
228246
}
247+
248+
#[cfg(test)]
249+
mod tests {
250+
use super::*;
251+
use std::fs;
252+
use tempfile::tempdir;
253+
254+
#[test]
255+
fn custom_target_does_not_fail() {
256+
let dir = tempdir().unwrap();
257+
258+
let target_json = dir.path().join("custom.json");
259+
fs::write(
260+
&target_json,
261+
r#"
262+
{
263+
"llvm-target": "x86_64-unknown-linux-gnu",
264+
"arch": "x86_64",
265+
"os": "linux",
266+
"env": "gnu",
267+
"vendor": "unknown",
268+
"linker-flavor": "gcc"
269+
}
270+
"#,
271+
)
272+
.unwrap();
273+
274+
let result = Target::new(Some(target_json.to_str().unwrap()), true);
275+
276+
assert!(result.is_ok());
277+
}
278+
}

0 commit comments

Comments
 (0)