Extract auxiliary-crate properties to their own module/struct

This commit is contained in:
Zalathar 2024-10-11 13:13:24 +11:00
parent ce697f919d
commit 78fd3b5319
3 changed files with 62 additions and 40 deletions

View File

@ -9,11 +9,13 @@ use std::process::Command;
use tracing::*; use tracing::*;
use crate::common::{Config, Debugger, FailMode, Mode, PassMode}; use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
use crate::header::auxiliary::{AuxProps, parse_and_update_aux};
use crate::header::cfg::{MatchOutcome, parse_cfg_name_directive}; use crate::header::cfg::{MatchOutcome, parse_cfg_name_directive};
use crate::header::needs::CachedNeedsConditions; use crate::header::needs::CachedNeedsConditions;
use crate::util::static_regex; use crate::util::static_regex;
use crate::{extract_cdb_version, extract_gdb_version}; use crate::{extract_cdb_version, extract_gdb_version};
pub(crate) mod auxiliary;
mod cfg; mod cfg;
mod needs; mod needs;
#[cfg(test)] #[cfg(test)]
@ -98,18 +100,8 @@ pub struct TestProps {
// If present, the name of a file that this test should match when // If present, the name of a file that this test should match when
// pretty-printed // pretty-printed
pub pp_exact: Option<PathBuf>, pub pp_exact: Option<PathBuf>,
// Other crates that should be compiled (typically from the same /// Auxiliary crates that should be built and made available to this test.
// directory as the test, but for backwards compatibility reasons pub(crate) aux: AuxProps,
// we also check the auxiliary directory)
pub aux_builds: Vec<String>,
// Auxiliary crates that should be compiled as `#![crate_type = "bin"]`.
pub aux_bins: Vec<String>,
// Similar to `aux_builds`, but a list of NAME=somelib.rs of dependencies
// to build and pass with the `--extern` flag.
pub aux_crates: Vec<(String, String)>,
/// Similar to `aux_builds`, but also passes the resulting dylib path to
/// `-Zcodegen-backend`.
pub aux_codegen_backend: Option<String>,
// Environment settings to use for compiling // Environment settings to use for compiling
pub rustc_env: Vec<(String, String)>, pub rustc_env: Vec<(String, String)>,
// Environment variables to unset prior to compiling. // Environment variables to unset prior to compiling.
@ -276,10 +268,7 @@ impl TestProps {
run_flags: vec![], run_flags: vec![],
doc_flags: vec![], doc_flags: vec![],
pp_exact: None, pp_exact: None,
aux_builds: vec![], aux: Default::default(),
aux_bins: vec![],
aux_crates: vec![],
aux_codegen_backend: None,
revisions: vec![], revisions: vec![],
rustc_env: vec![ rustc_env: vec![
("RUSTC_ICE".to_string(), "0".to_string()), ("RUSTC_ICE".to_string(), "0".to_string()),
@ -454,21 +443,10 @@ impl TestProps {
PRETTY_COMPARE_ONLY, PRETTY_COMPARE_ONLY,
&mut self.pretty_compare_only, &mut self.pretty_compare_only,
); );
config.push_name_value_directive(ln, AUX_BUILD, &mut self.aux_builds, |r| {
r.trim().to_string() // Call a helper method to deal with aux-related directives.
}); parse_and_update_aux(config, ln, &mut self.aux);
config.push_name_value_directive(ln, AUX_BIN, &mut self.aux_bins, |r| {
r.trim().to_string()
});
config.push_name_value_directive(
ln,
AUX_CRATE,
&mut self.aux_crates,
Config::parse_aux_crate,
);
if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND) {
self.aux_codegen_backend = Some(r.trim().to_owned());
}
config.push_name_value_directive( config.push_name_value_directive(
ln, ln,
EXEC_ENV, EXEC_ENV,

View File

@ -0,0 +1,44 @@
//! Code for dealing with test directives that request an "auxiliary" crate to
//! be built and made available to the test in some way.
use crate::common::Config;
use crate::header::directives::{AUX_BIN, AUX_BUILD, AUX_CODEGEN_BACKEND, AUX_CRATE};
/// Properties parsed from `aux-*` test directives.
#[derive(Clone, Debug, Default)]
pub(crate) struct AuxProps {
/// Other crates that should be built and made available to this test.
/// These are filenames relative to `./auxiliary/` in the test's directory.
pub(crate) builds: Vec<String>,
/// Auxiliary crates that should be compiled as `#![crate_type = "bin"]`.
pub(crate) bins: Vec<String>,
/// Similar to `builds`, but a list of NAME=somelib.rs of dependencies
/// to build and pass with the `--extern` flag.
pub(crate) crates: Vec<(String, String)>,
/// Similar to `builds`, but also uses the resulting dylib as a
/// `-Zcodegen-backend` when compiling the test file.
pub(crate) codegen_backend: Option<String>,
}
/// If the given test directive line contains an `aux-*` directive, parse it
/// and update [`AuxProps`] accordingly.
pub(super) fn parse_and_update_aux(config: &Config, ln: &str, aux: &mut AuxProps) {
if !ln.starts_with("aux-") {
return;
}
config.push_name_value_directive(ln, AUX_BUILD, &mut aux.builds, |r| r.trim().to_string());
config.push_name_value_directive(ln, AUX_BIN, &mut aux.bins, |r| r.trim().to_string());
config.push_name_value_directive(ln, AUX_CRATE, &mut aux.crates, parse_aux_crate);
if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND) {
aux.codegen_backend = Some(r.trim().to_owned());
}
}
fn parse_aux_crate(r: String) -> (String, String) {
let mut parts = r.trim().splitn(2, '=');
(
parts.next().expect("missing aux-crate name (e.g. log=log.rs)").to_string(),
parts.next().expect("missing aux-crate value (e.g. log=log.rs)").to_string(),
)
}

View File

@ -841,13 +841,13 @@ impl<'test> TestCx<'test> {
/// Auxiliaries, no matter how deep, have the same root_out_dir and root_testpaths. /// Auxiliaries, no matter how deep, have the same root_out_dir and root_testpaths.
fn document(&self, root_out_dir: &Path, root_testpaths: &TestPaths) -> ProcRes { fn document(&self, root_out_dir: &Path, root_testpaths: &TestPaths) -> ProcRes {
if self.props.build_aux_docs { if self.props.build_aux_docs {
for rel_ab in &self.props.aux_builds { for rel_ab in &self.props.aux.builds {
let aux_testpaths = self.compute_aux_test_paths(root_testpaths, rel_ab); let aux_testpaths = self.compute_aux_test_paths(root_testpaths, rel_ab);
let aux_props = let props_for_aux =
self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config); self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
let aux_cx = TestCx { let aux_cx = TestCx {
config: self.config, config: self.config,
props: &aux_props, props: &props_for_aux,
testpaths: &aux_testpaths, testpaths: &aux_testpaths,
revision: self.revision, revision: self.revision,
}; };
@ -1059,11 +1059,11 @@ impl<'test> TestCx<'test> {
fn aux_output_dir(&self) -> PathBuf { fn aux_output_dir(&self) -> PathBuf {
let aux_dir = self.aux_output_dir_name(); let aux_dir = self.aux_output_dir_name();
if !self.props.aux_builds.is_empty() { if !self.props.aux.builds.is_empty() {
remove_and_create_dir_all(&aux_dir); remove_and_create_dir_all(&aux_dir);
} }
if !self.props.aux_bins.is_empty() { if !self.props.aux.bins.is_empty() {
let aux_bin_dir = self.aux_bin_output_dir_name(); let aux_bin_dir = self.aux_bin_output_dir_name();
remove_and_create_dir_all(&aux_dir); remove_and_create_dir_all(&aux_dir);
remove_and_create_dir_all(&aux_bin_dir); remove_and_create_dir_all(&aux_bin_dir);
@ -1073,15 +1073,15 @@ impl<'test> TestCx<'test> {
} }
fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Path, rustc: &mut Command) { fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Path, rustc: &mut Command) {
for rel_ab in &self.props.aux_builds { for rel_ab in &self.props.aux.builds {
self.build_auxiliary(of, rel_ab, &aux_dir, false /* is_bin */); self.build_auxiliary(of, rel_ab, &aux_dir, false /* is_bin */);
} }
for rel_ab in &self.props.aux_bins { for rel_ab in &self.props.aux.bins {
self.build_auxiliary(of, rel_ab, &aux_dir, true /* is_bin */); self.build_auxiliary(of, rel_ab, &aux_dir, true /* is_bin */);
} }
for (aux_name, aux_path) in &self.props.aux_crates { for (aux_name, aux_path) in &self.props.aux.crates {
let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, false /* is_bin */); let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, false /* is_bin */);
let lib_name = let lib_name =
get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), aux_type); get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), aux_type);
@ -1097,7 +1097,7 @@ impl<'test> TestCx<'test> {
// Build any `//@ aux-codegen-backend`, and pass the resulting library // Build any `//@ aux-codegen-backend`, and pass the resulting library
// to `-Zcodegen-backend` when compiling the test file. // to `-Zcodegen-backend` when compiling the test file.
if let Some(aux_file) = &self.props.aux_codegen_backend { if let Some(aux_file) = &self.props.aux.codegen_backend {
let aux_type = self.build_auxiliary(of, aux_file, aux_dir, false); let aux_type = self.build_auxiliary(of, aux_file, aux_dir, false);
if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) { if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
let lib_path = aux_dir.join(&lib_name); let lib_path = aux_dir.join(&lib_name);