From 50d117ee7d54d1f74a69ae067a8304f6db61ffb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 17 Jul 2023 21:24:43 +0200 Subject: [PATCH] Resolve correct archive version name in `opt-dist` --- src/tools/opt-dist/src/tests.rs | 25 +++++++++++++++++++------ src/tools/opt-dist/src/utils/io.rs | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/tools/opt-dist/src/tests.rs b/src/tools/opt-dist/src/tests.rs index 6b35b13e58624..c85418baecd1a 100644 --- a/src/tools/opt-dist/src/tests.rs +++ b/src/tools/opt-dist/src/tests.rs @@ -1,8 +1,8 @@ use crate::environment::Environment; use crate::exec::cmd; -use crate::utils::io::{copy_directory, unpack_archive}; +use crate::utils::io::{copy_directory, find_file_in_dir, unpack_archive}; use anyhow::Context; -use camino::Utf8PathBuf; +use camino::{Utf8Path, Utf8PathBuf}; /// Run tests on optimized dist artifacts. pub fn run_tests(env: &dyn Environment) -> anyhow::Result<()> { @@ -22,13 +22,14 @@ pub fn run_tests(env: &dyn Environment) -> anyhow::Result<()> { Ok(extracted_path) }; let host_triple = env.host_triple(); + let version = find_dist_version(&dist_dir)?; // Extract rustc, libstd, cargo and src archives to create the optimized sysroot - let rustc_dir = extract_dist_dir(&format!("rustc-nightly-{host_triple}"))?.join("rustc"); - let libstd_dir = extract_dist_dir(&format!("rust-std-nightly-{host_triple}"))? + let rustc_dir = extract_dist_dir(&format!("rustc-{version}-{host_triple}"))?.join("rustc"); + let libstd_dir = extract_dist_dir(&format!("rust-std-{version}-{host_triple}"))? .join(format!("rust-std-{host_triple}")); - let cargo_dir = extract_dist_dir(&format!("cargo-nightly-{host_triple}"))?.join("cargo"); - let extracted_src_dir = extract_dist_dir("rust-src-nightly")?.join("rust-src"); + let cargo_dir = extract_dist_dir(&format!("cargo-{version}-{host_triple}"))?.join("cargo"); + let extracted_src_dir = extract_dist_dir(&format!("rust-src-{version}"))?.join("rust-src"); // We need to manually copy libstd to the extracted rustc sysroot copy_directory( @@ -99,3 +100,15 @@ llvm-config = "{llvm_config}" } cmd(&args).env("COMPILETEST_FORCE_STAGE0", "1").run().context("Cannot execute tests") } + +/// Tries to find the version of the dist artifacts (either nightly, beta, or 1.XY.Z). +fn find_dist_version(directory: &Utf8Path) -> anyhow::Result { + // Lookup a known file with a unique prefix and extract the version from its filename + let archive = find_file_in_dir(directory, "reproducible-artifacts-", ".tar.xz")? + .file_name() + .unwrap() + .to_string(); + let (version, _) = + archive.strip_prefix("reproducible-artifacts-").unwrap().split_once("-").unwrap(); + Ok(version.to_string()) +} diff --git a/src/tools/opt-dist/src/utils/io.rs b/src/tools/opt-dist/src/utils/io.rs index aab078067afca..211f867ac003b 100644 --- a/src/tools/opt-dist/src/utils/io.rs +++ b/src/tools/opt-dist/src/utils/io.rs @@ -60,3 +60,22 @@ pub fn get_files_from_dir( .map(|p| p.map(|p| Utf8PathBuf::from_path_buf(p).unwrap())) .collect::, _>>()?) } + +/// Finds a single file in the specified `directory` with the given `prefix` and `suffix`. +pub fn find_file_in_dir( + directory: &Utf8Path, + prefix: &str, + suffix: &str, +) -> anyhow::Result { + let files = glob::glob(&format!("{directory}/{prefix}*{suffix}"))? + .into_iter() + .collect::, _>>()?; + match files.len() { + 0 => Err(anyhow::anyhow!("No file with prefix {prefix} found in {directory}")), + 1 => Ok(Utf8PathBuf::from_path_buf(files[0].clone()).unwrap()), + _ => Err(anyhow::anyhow!( + "More than one file with prefix {prefix} found in {directory}: {:?}", + files + )), + } +}