Skip to content

Make target-spec json file extensions case-insensitive #127389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
use rustc_span::edition::{Edition, DEFAULT_EDITION, EDITION_NAME_LIST, LATEST_STABLE_EDITION};
use rustc_span::source_map::FilePathMapping;
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm};
use rustc_target::spec::{has_ext_ignore_case, SplitDebuginfo, Target, TargetTriple};
use rustc_target::spec::{FramePointer, LinkSelfContainedComponents, LinkerFeatures};
use rustc_target::spec::{SplitDebuginfo, Target, TargetTriple};
use std::collections::btree_map::{
Iter as BTreeMapIter, Keys as BTreeMapKeysIter, Values as BTreeMapValuesIter,
};
Expand Down Expand Up @@ -1991,7 +1991,7 @@ fn collect_print_requests(

pub fn parse_target_triple(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> TargetTriple {
match matches.opt_str("target") {
Some(target) if target.ends_with(".json") => {
Some(target) if has_ext_ignore_case(&target, "json") => {
let path = Path::new(&target);
TargetTriple::from_path(path).unwrap_or_else(|_| {
early_dcx.early_fatal(format!("target file {path:?} does not exist"))
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use rustc_span::symbol::{kw, sym, Symbol};
use serde_json::Value;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -3716,3 +3717,11 @@ impl fmt::Display for TargetTriple {
write!(f, "{}", self.debug_triple())
}
}

/// Helper function to check if the given `s` ends with `ext` __case-insensitive__.
/// The `ext` should not starts with a dot.
///
/// Does not check if the path exists.
pub fn has_ext_ignore_case<S: AsRef<OsStr>, Ext: AsRef<OsStr>>(s: S, ext: Ext) -> bool {
Path::new(&s).extension().is_some_and(|s| s.eq_ignore_ascii_case(ext))
}
6 changes: 6 additions & 0 deletions tests/run-make/target-specs-filename/ALL-IN-CAPS.JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arch": "x86_64",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-unknown-gnu",
"target-pointer-width": "64"
}
6 changes: 6 additions & 0 deletions tests/run-make/target-specs-filename/ext-in-caps.JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arch": "x86_64",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-unknown-gnu",
"target-pointer-width": "64"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arch": "x86_64",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-unknown-gnu",
"target-pointer-width": "64"
}
22 changes: 22 additions & 0 deletions tests/run-make/target-specs-filename/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This test checks the rustc can accept target-spec-json with cases
// including file extension - it could be in upper and lower case.
// Used to test: print target-spec-json and cfg
// Issue: https://github.com/rust-lang/rust/issues/127387

use run_make_support::rustc;

fn main() {
// This is a matrix [files x args], files for target, args for print.
for file in [
"normal-target-triple",
"normal-target-triple.json",
"ext-in-caps.JSON",
"ALL-IN-CAPS.JSON",
] {
for args in [["cfg"].as_slice(), &["target-spec-json", "-Zunstable-options"]] {
let output = rustc().arg("--print").args(args).args(["--target", file]).run();
// check that the target triple is read correctly
output.assert_stdout_contains("x86_64");
}
}
}
Loading