Skip to content

Commit 1b0167d

Browse files
Prevent unwanted recompilations
1 parent d9b9ce7 commit 1b0167d

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

build_system/src/test.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,12 @@ fn remove_files_callback<'a>(
13351335
}
13361336

13371337
fn test_asm(env: &Env, args: &TestArg) -> Result<(), String> {
1338+
fn is_path_time_more_recent(ref_time: std::time::SystemTime, path: &str) -> bool {
1339+
std::fs::metadata(path)
1340+
.and_then(|metadata| metadata.modified())
1341+
.is_ok_and(|time| ref_time < time)
1342+
}
1343+
13381344
// FIXME: create a function "display_if_not_quiet" or something along the line.
13391345
println!("[TEST] cg_gcc assembly");
13401346
let llvm_filecheck = get_llvm_filecheck(env)?;
@@ -1343,12 +1349,35 @@ fn test_asm(env: &Env, args: &TestArg) -> Result<(), String> {
13431349
let mut config = ConfigInfo::default();
13441350
config.setup(&mut env, false)?;
13451351

1352+
let target_dir = std::env::current_dir().unwrap().join("build_system/asm-tester/target");
1353+
1354+
// All this code is because `cargo` keeps recompiling this file, and we can't figure out why.
1355+
let binary_file_path = "build_system/asm-tester/target/debug/asm-tester";
1356+
let mut need_recompilation = true;
1357+
if let Ok(metadata) = std::fs::metadata(binary_file_path)
1358+
&& let Ok(ref_time) = metadata.modified()
1359+
&& !is_path_time_more_recent(ref_time, "build_system/asm-tester/Cargo.toml")
1360+
&& !is_path_time_more_recent(ref_time, "build_system/asm-tester/Cargo.lock")
1361+
&& !is_path_time_more_recent(ref_time, "build_system/asm-tester/src/main.rs")
1362+
{
1363+
need_recompilation = false;
1364+
}
1365+
1366+
if need_recompilation {
1367+
let build_asm_args: Vec<&dyn AsRef<OsStr>> = vec![
1368+
&"cargo",
1369+
&"build",
1370+
&"--manifest-path",
1371+
&"build_system/asm-tester/Cargo.toml",
1372+
&"--target-dir",
1373+
&target_dir,
1374+
&"--",
1375+
];
1376+
run_command_with_output_and_env_no_err(&build_asm_args, Some(Path::new(".")), Some(&env))?;
1377+
}
1378+
13461379
let mut test_asm_args: Vec<&dyn AsRef<OsStr>> = vec![
1347-
&"cargo",
1348-
&"run",
1349-
&"--manifest-path",
1350-
&"build_system/asm-tester/Cargo.toml",
1351-
&"--",
1380+
&"build_system/asm-tester/target/debug/asm-tester",
13521381
&"--llvm-filecheck",
13531382
&llvm_filecheck,
13541383
];

0 commit comments

Comments
 (0)