Skip to content

Commit 50d117e

Browse files
committed
Resolve correct archive version name in opt-dist
1 parent c4e6fe9 commit 50d117e

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/tools/opt-dist/src/tests.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::environment::Environment;
22
use crate::exec::cmd;
3-
use crate::utils::io::{copy_directory, unpack_archive};
3+
use crate::utils::io::{copy_directory, find_file_in_dir, unpack_archive};
44
use anyhow::Context;
5-
use camino::Utf8PathBuf;
5+
use camino::{Utf8Path, Utf8PathBuf};
66

77
/// Run tests on optimized dist artifacts.
88
pub fn run_tests(env: &dyn Environment) -> anyhow::Result<()> {
@@ -22,13 +22,14 @@ pub fn run_tests(env: &dyn Environment) -> anyhow::Result<()> {
2222
Ok(extracted_path)
2323
};
2424
let host_triple = env.host_triple();
25+
let version = find_dist_version(&dist_dir)?;
2526

2627
// Extract rustc, libstd, cargo and src archives to create the optimized sysroot
27-
let rustc_dir = extract_dist_dir(&format!("rustc-nightly-{host_triple}"))?.join("rustc");
28-
let libstd_dir = extract_dist_dir(&format!("rust-std-nightly-{host_triple}"))?
28+
let rustc_dir = extract_dist_dir(&format!("rustc-{version}-{host_triple}"))?.join("rustc");
29+
let libstd_dir = extract_dist_dir(&format!("rust-std-{version}-{host_triple}"))?
2930
.join(format!("rust-std-{host_triple}"));
30-
let cargo_dir = extract_dist_dir(&format!("cargo-nightly-{host_triple}"))?.join("cargo");
31-
let extracted_src_dir = extract_dist_dir("rust-src-nightly")?.join("rust-src");
31+
let cargo_dir = extract_dist_dir(&format!("cargo-{version}-{host_triple}"))?.join("cargo");
32+
let extracted_src_dir = extract_dist_dir(&format!("rust-src-{version}"))?.join("rust-src");
3233

3334
// We need to manually copy libstd to the extracted rustc sysroot
3435
copy_directory(
@@ -99,3 +100,15 @@ llvm-config = "{llvm_config}"
99100
}
100101
cmd(&args).env("COMPILETEST_FORCE_STAGE0", "1").run().context("Cannot execute tests")
101102
}
103+
104+
/// Tries to find the version of the dist artifacts (either nightly, beta, or 1.XY.Z).
105+
fn find_dist_version(directory: &Utf8Path) -> anyhow::Result<String> {
106+
// Lookup a known file with a unique prefix and extract the version from its filename
107+
let archive = find_file_in_dir(directory, "reproducible-artifacts-", ".tar.xz")?
108+
.file_name()
109+
.unwrap()
110+
.to_string();
111+
let (version, _) =
112+
archive.strip_prefix("reproducible-artifacts-").unwrap().split_once("-").unwrap();
113+
Ok(version.to_string())
114+
}

src/tools/opt-dist/src/utils/io.rs

+19
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,22 @@ pub fn get_files_from_dir(
6060
.map(|p| p.map(|p| Utf8PathBuf::from_path_buf(p).unwrap()))
6161
.collect::<Result<Vec<_>, _>>()?)
6262
}
63+
64+
/// Finds a single file in the specified `directory` with the given `prefix` and `suffix`.
65+
pub fn find_file_in_dir(
66+
directory: &Utf8Path,
67+
prefix: &str,
68+
suffix: &str,
69+
) -> anyhow::Result<Utf8PathBuf> {
70+
let files = glob::glob(&format!("{directory}/{prefix}*{suffix}"))?
71+
.into_iter()
72+
.collect::<Result<Vec<_>, _>>()?;
73+
match files.len() {
74+
0 => Err(anyhow::anyhow!("No file with prefix {prefix} found in {directory}")),
75+
1 => Ok(Utf8PathBuf::from_path_buf(files[0].clone()).unwrap()),
76+
_ => Err(anyhow::anyhow!(
77+
"More than one file with prefix {prefix} found in {directory}: {:?}",
78+
files
79+
)),
80+
}
81+
}

0 commit comments

Comments
 (0)