Skip to content

Commit 072b7db

Browse files
committed
Make debug_triple depend on target json file content rather than file path
This ensures that changes to target json files will force a recompilation. And more importantly that moving the files doesn't force a recompilation.
1 parent ff86b27 commit 072b7db

File tree

1 file changed

+28
-22
lines changed
  • compiler/rustc_target/src/spec

1 file changed

+28
-22
lines changed

compiler/rustc_target/src/spec/mod.rs

+28-22
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ impl Target {
21832183
TargetTriple::TargetTriple(ref target_triple) => {
21842184
load_builtin(target_triple).expect("built-in target")
21852185
}
2186-
TargetTriple::TargetPath(..) => {
2186+
TargetTriple::TargetJson { .. } => {
21872187
panic!("built-in targets doens't support target-paths")
21882188
}
21892189
}
@@ -2248,11 +2248,9 @@ impl Target {
22482248

22492249
Err(format!("Could not find specification for target {:?}", target_triple))
22502250
}
2251-
TargetTriple::TargetPath(ref target_path) => {
2252-
if target_path.is_file() {
2253-
return load_file(&target_path);
2254-
}
2255-
Err(format!("Target path {:?} is not a valid file", target_path))
2251+
TargetTriple::TargetJson { triple: _, ref contents } => {
2252+
let obj = serde_json::from_str(contents).map_err(|e| e.to_string())?;
2253+
Target::from_json(obj)
22562254
}
22572255
}
22582256
}
@@ -2424,7 +2422,7 @@ impl ToJson for Target {
24242422
#[derive(PartialEq, Clone, Debug, Hash, Encodable, Decodable)]
24252423
pub enum TargetTriple {
24262424
TargetTriple(String),
2427-
TargetPath(PathBuf),
2425+
TargetJson { triple: String, contents: String },
24282426
}
24292427

24302428
impl TargetTriple {
@@ -2436,20 +2434,28 @@ impl TargetTriple {
24362434
/// Creates a target triple from the passed target path.
24372435
pub fn from_path(path: &Path) -> Result<Self, io::Error> {
24382436
let canonicalized_path = path.canonicalize()?;
2439-
Ok(TargetTriple::TargetPath(canonicalized_path))
2437+
let contents = std::fs::read_to_string(&canonicalized_path).map_err(|err| {
2438+
io::Error::new(
2439+
io::ErrorKind::InvalidInput,
2440+
format!("Target path {:?} is not a valid file: {}", canonicalized_path, err),
2441+
)
2442+
})?;
2443+
let triple = canonicalized_path
2444+
.file_stem()
2445+
.expect("target path must not be empty")
2446+
.to_str()
2447+
.expect("target path must be valid unicode")
2448+
.to_owned();
2449+
Ok(TargetTriple::TargetJson { triple, contents })
24402450
}
24412451

24422452
/// Returns a string triple for this target.
24432453
///
24442454
/// If this target is a path, the file name (without extension) is returned.
24452455
pub fn triple(&self) -> &str {
24462456
match *self {
2447-
TargetTriple::TargetTriple(ref triple) => triple,
2448-
TargetTriple::TargetPath(ref path) => path
2449-
.file_stem()
2450-
.expect("target path must not be empty")
2451-
.to_str()
2452-
.expect("target path must be valid unicode"),
2457+
TargetTriple::TargetTriple(ref triple)
2458+
| TargetTriple::TargetJson { ref triple, contents: _ } => triple,
24532459
}
24542460
}
24552461

@@ -2461,14 +2467,14 @@ impl TargetTriple {
24612467
use std::collections::hash_map::DefaultHasher;
24622468
use std::hash::{Hash, Hasher};
24632469

2464-
let triple = self.triple();
2465-
if let TargetTriple::TargetPath(ref path) = *self {
2466-
let mut hasher = DefaultHasher::new();
2467-
path.hash(&mut hasher);
2468-
let hash = hasher.finish();
2469-
format!("{}-{}", triple, hash)
2470-
} else {
2471-
triple.into()
2470+
match self {
2471+
TargetTriple::TargetTriple(triple) => triple.to_owned(),
2472+
TargetTriple::TargetJson { triple, contents: content } => {
2473+
let mut hasher = DefaultHasher::new();
2474+
content.hash(&mut hasher);
2475+
let hash = hasher.finish();
2476+
format!("{}-{}", triple, hash)
2477+
}
24722478
}
24732479
}
24742480
}

0 commit comments

Comments
 (0)