Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2e27b60

Browse files
committed
add test, improvements following review
1 parent 6f2248d commit 2e27b60

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1919
use rustc_span::edition::{Edition, DEFAULT_EDITION, EDITION_NAME_LIST, LATEST_STABLE_EDITION};
2020
use rustc_span::source_map::FilePathMapping;
2121
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm};
22+
use rustc_target::spec::{has_ext_ignore_case, SplitDebuginfo, Target, TargetTriple};
2223
use rustc_target::spec::{FramePointer, LinkSelfContainedComponents, LinkerFeatures};
23-
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple};
2424
use std::collections::btree_map::{
2525
Iter as BTreeMapIter, Keys as BTreeMapKeysIter, Values as BTreeMapValuesIter,
2626
};
@@ -1991,7 +1991,7 @@ fn collect_print_requests(
19911991

19921992
pub fn parse_target_triple(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> TargetTriple {
19931993
match matches.opt_str("target") {
1994-
Some(target) if TargetTriple::has_json_ext(&target) => {
1994+
Some(target) if has_ext_ignore_case(&target, "json") => {
19951995
let path = Path::new(&target);
19961996
TargetTriple::from_path(path).unwrap_or_else(|_| {
19971997
early_dcx.early_fatal(format!("target file {path:?} does not exist"))

compiler/rustc_target/src/spec/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use rustc_span::symbol::{kw, sym, Symbol};
4646
use serde_json::Value;
4747
use std::borrow::Cow;
4848
use std::collections::BTreeMap;
49+
use std::ffi::OsStr;
4950
use std::hash::{Hash, Hasher};
5051
use std::ops::{Deref, DerefMut};
5152
use std::path::{Path, PathBuf};
@@ -3709,22 +3710,18 @@ impl TargetTriple {
37093710
}
37103711
}
37113712
}
3712-
3713-
/// Helper function to check if the given `triple` ends with `".json"` __case-insensitive__.
3714-
/// Does not check if the path exists.
3715-
pub fn has_json_ext<S: AsRef<str>>(triple: S) -> bool {
3716-
const EXT: &'static str = ".json";
3717-
triple.as_ref().ends_with(EXT) || {
3718-
let s = triple.as_ref();
3719-
s.get(s.len().wrapping_sub(EXT.len())..)
3720-
.map(|ext| ext.eq_ignore_ascii_case(EXT))
3721-
.unwrap_or_default()
3722-
}
3723-
}
37243713
}
37253714

37263715
impl fmt::Display for TargetTriple {
37273716
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37283717
write!(f, "{}", self.debug_triple())
37293718
}
37303719
}
3720+
3721+
/// Helper function to check if the given `s` ends with `ext` __case-insensitive__.
3722+
/// The `ext` should not starts with a dot.
3723+
///
3724+
/// Does not check if the path exists.
3725+
pub fn has_ext_ignore_case<S: AsRef<OsStr>, Ext: AsRef<OsStr>>(s: S, ext: Ext) -> bool {
3726+
Path::new(&s).extension().is_some_and(|s| s.eq_ignore_ascii_case(ext))
3727+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arch": "x86_64",
3+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
4+
"llvm-target": "x86_64-unknown-unknown-gnu",
5+
"target-pointer-width": "64"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arch": "x86_64",
3+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
4+
"llvm-target": "x86_64-unknown-unknown-gnu",
5+
"target-pointer-width": "64"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arch": "x86_64",
3+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
4+
"llvm-target": "x86_64-unknown-unknown-gnu",
5+
"target-pointer-width": "64"
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This test checks the rustc can accept target-spec-json with cases
2+
// including file extension - it could be in upper and lower case.
3+
// Used to test: print target-spec-json and cfg
4+
// Issue: https://github.com/rust-lang/rust/issues/127387
5+
6+
///@ ignore-cross-compile
7+
use run_make_support::rustc;
8+
9+
fn main() {
10+
// This is a matrix [files x args], files for target, args for print.
11+
for file in [
12+
"normal-target-triple",
13+
"normal-target-triple.json",
14+
"ext-in-caps.JSON",
15+
"ALL-IN-CAPS.JSON",
16+
] {
17+
for args in [["cfg"].as_slice(), &["target-spec-json", "-Zunstable-options"]] {
18+
let output = rustc().arg("--print").args(args).args(["--target", file]).run();
19+
// check that the target triple is accepted without failures
20+
assert!(output.status().success(), "target triple doesn't accepted");
21+
// check that the target triple is read correctly
22+
output.assert_stdout_contains("x86_64");
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)