Skip to content

compiletest: Extract auxiliary-crate properties to their own module/struct #131541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 14 additions & 57 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use std::process::Command;
use tracing::*;

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::needs::CachedNeedsConditions;
use crate::util::static_regex;
use crate::{extract_cdb_version, extract_gdb_version};

pub(crate) mod auxiliary;
mod cfg;
mod needs;
#[cfg(test)]
Expand All @@ -33,9 +35,10 @@ impl HeadersCache {
/// the test.
#[derive(Default)]
pub struct EarlyProps {
pub aux: Vec<String>,
pub aux_bin: Vec<String>,
pub aux_crate: Vec<(String, String)>,
/// Auxiliary crates that should be built and made available to this test.
/// Included in [`EarlyProps`] so that the indicated files can participate
/// in up-to-date checking. Building happens via [`TestProps::aux`] instead.
pub(crate) aux: AuxProps,
pub revisions: Vec<String>,
}

Expand All @@ -55,21 +58,7 @@ impl EarlyProps {
testfile,
rdr,
&mut |HeaderLine { directive: ln, .. }| {
config.push_name_value_directive(ln, directives::AUX_BUILD, &mut props.aux, |r| {
r.trim().to_string()
});
config.push_name_value_directive(
ln,
directives::AUX_BIN,
&mut props.aux_bin,
|r| r.trim().to_string(),
);
config.push_name_value_directive(
ln,
directives::AUX_CRATE,
&mut props.aux_crate,
Config::parse_aux_crate,
);
parse_and_update_aux(config, ln, &mut props.aux);
config.parse_and_update_revisions(ln, &mut props.revisions);
},
);
Expand Down Expand Up @@ -98,18 +87,8 @@ pub struct TestProps {
// If present, the name of a file that this test should match when
// pretty-printed
pub pp_exact: Option<PathBuf>,
// Other crates that should be compiled (typically from the same
// directory as the test, but for backwards compatibility reasons
// 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>,
/// Auxiliary crates that should be built and made available to this test.
pub(crate) aux: AuxProps,
// Environment settings to use for compiling
pub rustc_env: Vec<(String, String)>,
// Environment variables to unset prior to compiling.
Expand Down Expand Up @@ -276,10 +255,7 @@ impl TestProps {
run_flags: vec![],
doc_flags: vec![],
pp_exact: None,
aux_builds: vec![],
aux_bins: vec![],
aux_crates: vec![],
aux_codegen_backend: None,
aux: Default::default(),
revisions: vec![],
rustc_env: vec![
("RUSTC_ICE".to_string(), "0".to_string()),
Expand Down Expand Up @@ -454,21 +430,10 @@ impl TestProps {
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()
});
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());
}

// 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,
EXEC_ENV,
Expand Down Expand Up @@ -942,14 +907,6 @@ fn iter_header(
}

impl Config {
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(),
)
}

fn parse_and_update_revisions(&self, line: &str, existing: &mut Vec<String>) {
if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
Expand Down
60 changes: 60 additions & 0 deletions src/tools/compiletest/src/header/auxiliary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Code for dealing with test directives that request an "auxiliary" crate to
//! be built and made available to the test in some way.

use std::iter;

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>,
}

impl AuxProps {
/// Yields all of the paths (relative to `./auxiliary/`) that have been
/// specified in `aux-*` directives for this test.
pub(crate) fn all_aux_path_strings(&self) -> impl Iterator<Item = &str> {
let Self { builds, bins, crates, codegen_backend } = self;

iter::empty()
.chain(builds.iter().map(String::as_str))
.chain(bins.iter().map(String::as_str))
.chain(crates.iter().map(|(_, path)| path.as_str()))
.chain(codegen_backend.iter().map(String::as_str))
}
}

/// 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(),
)
}
3 changes: 2 additions & 1 deletion src/tools/compiletest/src/header/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ fn aux_build() {
//@ aux-build: b.rs
"
)
.aux,
.aux
.builds,
vec!["a.rs", "b.rs"],
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/tools/compiletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,8 @@ fn files_related_to_test(
related.push(testpaths.file.clone());
}

for aux in &props.aux {
for aux in props.aux.all_aux_path_strings() {
// FIXME(Zalathar): Perform all `auxiliary` path resolution in one place.
let path = testpaths.file.parent().unwrap().join("auxiliary").join(aux);
related.push(path);
}
Expand Down
18 changes: 9 additions & 9 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,13 +841,13 @@ impl<'test> TestCx<'test> {
/// 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 {
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_props =
let props_for_aux =
self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
let aux_cx = TestCx {
config: self.config,
props: &aux_props,
props: &props_for_aux,
testpaths: &aux_testpaths,
revision: self.revision,
};
Expand Down Expand Up @@ -1059,11 +1059,11 @@ impl<'test> TestCx<'test> {
fn aux_output_dir(&self) -> PathBuf {
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);
}

if !self.props.aux_bins.is_empty() {
if !self.props.aux.bins.is_empty() {
let aux_bin_dir = self.aux_bin_output_dir_name();
remove_and_create_dir_all(&aux_dir);
remove_and_create_dir_all(&aux_bin_dir);
Expand All @@ -1073,15 +1073,15 @@ impl<'test> TestCx<'test> {
}

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 */);
}

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 */);
}

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 lib_name =
get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), aux_type);
Expand All @@ -1097,7 +1097,7 @@ impl<'test> TestCx<'test> {

// Build any `//@ aux-codegen-backend`, and pass the resulting library
// 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);
if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
let lib_path = aux_dir.join(&lib_name);
Expand Down
Loading