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

Commit dbc8e42

Browse files
committed
Auto merge of rust-lang#127389 - boozook:target-spec-json-ext-case-insensitive, r=<try>
Support target-spec json file extension in various cases Currently user's target json file can have only `json` ext, but cannot have `JSON' or any other strange form. This is a little fix with added support of case-insensitive file-ext for user specified target only. That means that not supported in following cases: - any directory traversal, such as in `$RUST_TARGET_PATH` - same for `rustc_target::spec::Target::search`, so it doesn't work with `JSON` on case-sensitive FS, but __works fine on case-insensitive FS__. - `$RUST_TARGET_PATH` dir traversal in the bootstrap is not touched too. ## Performance cost: Almost without changes. Comparison using `eq_ignore_ascii_case` for __only latest ext_len chars__ of the `OsStr` (as bytes). ## Testing How to test: 1. `rustc --print target-spec-json -Zunstable-options > ./some-target.JSON` 1. remove `is-builtin` field from the `some-target.JSON` or set to `false` 1. `rustc --print target-spec-json -Zunstable-options --target=some-target.JSON` should work 1. `rustc --print target-spec-json -Zunstable-options --target=some-target` should work on case-insensitive FS 1. `rustc --print cfg --target=some-target` should work too, on case-insensitive FS Tests added. ## Implementation note I've separated the comparison into a method for potential future use instead of just `str.ends_with(".json")` and to maintain compatibility. The method is public for the same reasons. Fixes rust-lang#127387 issue. try-job: test-various try-job: armhf-gnu try-job: x86_64-msvc try-job: aarch64-apple
2 parents 3bec617 + 4ab215b commit dbc8e42

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
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 target.ends_with(".json") => {
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 & 0 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};
@@ -3716,3 +3717,11 @@ impl fmt::Display for TargetTriple {
37163717
write!(f, "{}", self.debug_triple())
37173718
}
37183719
}
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
use run_make_support::rustc;
7+
8+
fn main() {
9+
// This is a matrix [files x args], files for target, args for print.
10+
for file in [
11+
"normal-target-triple",
12+
"normal-target-triple.json",
13+
"ext-in-caps.JSON",
14+
"ALL-IN-CAPS.JSON",
15+
] {
16+
for args in [["cfg"].as_slice(), &["target-spec-json", "-Zunstable-options"]] {
17+
let output = rustc().arg("--print").args(args).args(["--target", file]).run();
18+
// check that the target triple is read correctly
19+
output.assert_stdout_contains("x86_64");
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)