Skip to content

Commit 7f79c1e

Browse files
authored
Rollup merge of rust-lang#131541 - Zalathar:aux-props, r=jieyouxu
compiletest: Extract auxiliary-crate properties to their own module/struct This moves the values of the 4 different `aux-*` directives into their own sub-struct. That struct, along with its directive-parsing code, can then be shared by both `TestProps` and `EarlyProps`. The final patch also fixes an oversight in up-to-date checking, by including *all* auxiliary crates in the timestamp, not just ordinary `aux-build` ones.
2 parents 33b1264 + ec662b9 commit 7f79c1e

File tree

5 files changed

+87
-68
lines changed

5 files changed

+87
-68
lines changed

src/tools/compiletest/src/header.rs

+14-57
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ use std::process::Command;
99
use tracing::*;
1010

1111
use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
12+
use crate::header::auxiliary::{AuxProps, parse_and_update_aux};
1213
use crate::header::cfg::{MatchOutcome, parse_cfg_name_directive};
1314
use crate::header::needs::CachedNeedsConditions;
1415
use crate::util::static_regex;
1516
use crate::{extract_cdb_version, extract_gdb_version};
1617

18+
pub(crate) mod auxiliary;
1719
mod cfg;
1820
mod needs;
1921
#[cfg(test)]
@@ -33,9 +35,10 @@ impl HeadersCache {
3335
/// the test.
3436
#[derive(Default)]
3537
pub struct EarlyProps {
36-
pub aux: Vec<String>,
37-
pub aux_bin: Vec<String>,
38-
pub aux_crate: Vec<(String, String)>,
38+
/// Auxiliary crates that should be built and made available to this test.
39+
/// Included in [`EarlyProps`] so that the indicated files can participate
40+
/// in up-to-date checking. Building happens via [`TestProps::aux`] instead.
41+
pub(crate) aux: AuxProps,
3942
pub revisions: Vec<String>,
4043
}
4144

@@ -55,21 +58,7 @@ impl EarlyProps {
5558
testfile,
5659
rdr,
5760
&mut |HeaderLine { directive: ln, .. }| {
58-
config.push_name_value_directive(ln, directives::AUX_BUILD, &mut props.aux, |r| {
59-
r.trim().to_string()
60-
});
61-
config.push_name_value_directive(
62-
ln,
63-
directives::AUX_BIN,
64-
&mut props.aux_bin,
65-
|r| r.trim().to_string(),
66-
);
67-
config.push_name_value_directive(
68-
ln,
69-
directives::AUX_CRATE,
70-
&mut props.aux_crate,
71-
Config::parse_aux_crate,
72-
);
61+
parse_and_update_aux(config, ln, &mut props.aux);
7362
config.parse_and_update_revisions(ln, &mut props.revisions);
7463
},
7564
);
@@ -98,18 +87,8 @@ pub struct TestProps {
9887
// If present, the name of a file that this test should match when
9988
// pretty-printed
10089
pub pp_exact: Option<PathBuf>,
101-
// Other crates that should be compiled (typically from the same
102-
// directory as the test, but for backwards compatibility reasons
103-
// we also check the auxiliary directory)
104-
pub aux_builds: Vec<String>,
105-
// Auxiliary crates that should be compiled as `#![crate_type = "bin"]`.
106-
pub aux_bins: Vec<String>,
107-
// Similar to `aux_builds`, but a list of NAME=somelib.rs of dependencies
108-
// to build and pass with the `--extern` flag.
109-
pub aux_crates: Vec<(String, String)>,
110-
/// Similar to `aux_builds`, but also passes the resulting dylib path to
111-
/// `-Zcodegen-backend`.
112-
pub aux_codegen_backend: Option<String>,
90+
/// Auxiliary crates that should be built and made available to this test.
91+
pub(crate) aux: AuxProps,
11392
// Environment settings to use for compiling
11493
pub rustc_env: Vec<(String, String)>,
11594
// Environment variables to unset prior to compiling.
@@ -276,10 +255,7 @@ impl TestProps {
276255
run_flags: vec![],
277256
doc_flags: vec![],
278257
pp_exact: None,
279-
aux_builds: vec![],
280-
aux_bins: vec![],
281-
aux_crates: vec![],
282-
aux_codegen_backend: None,
258+
aux: Default::default(),
283259
revisions: vec![],
284260
rustc_env: vec![
285261
("RUSTC_ICE".to_string(), "0".to_string()),
@@ -454,21 +430,10 @@ impl TestProps {
454430
PRETTY_COMPARE_ONLY,
455431
&mut self.pretty_compare_only,
456432
);
457-
config.push_name_value_directive(ln, AUX_BUILD, &mut self.aux_builds, |r| {
458-
r.trim().to_string()
459-
});
460-
config.push_name_value_directive(ln, AUX_BIN, &mut self.aux_bins, |r| {
461-
r.trim().to_string()
462-
});
463-
config.push_name_value_directive(
464-
ln,
465-
AUX_CRATE,
466-
&mut self.aux_crates,
467-
Config::parse_aux_crate,
468-
);
469-
if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND) {
470-
self.aux_codegen_backend = Some(r.trim().to_owned());
471-
}
433+
434+
// Call a helper method to deal with aux-related directives.
435+
parse_and_update_aux(config, ln, &mut self.aux);
436+
472437
config.push_name_value_directive(
473438
ln,
474439
EXEC_ENV,
@@ -942,14 +907,6 @@ fn iter_header(
942907
}
943908

944909
impl Config {
945-
fn parse_aux_crate(r: String) -> (String, String) {
946-
let mut parts = r.trim().splitn(2, '=');
947-
(
948-
parts.next().expect("missing aux-crate name (e.g. log=log.rs)").to_string(),
949-
parts.next().expect("missing aux-crate value (e.g. log=log.rs)").to_string(),
950-
)
951-
}
952-
953910
fn parse_and_update_revisions(&self, line: &str, existing: &mut Vec<String>) {
954911
if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
955912
let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Code for dealing with test directives that request an "auxiliary" crate to
2+
//! be built and made available to the test in some way.
3+
4+
use std::iter;
5+
6+
use crate::common::Config;
7+
use crate::header::directives::{AUX_BIN, AUX_BUILD, AUX_CODEGEN_BACKEND, AUX_CRATE};
8+
9+
/// Properties parsed from `aux-*` test directives.
10+
#[derive(Clone, Debug, Default)]
11+
pub(crate) struct AuxProps {
12+
/// Other crates that should be built and made available to this test.
13+
/// These are filenames relative to `./auxiliary/` in the test's directory.
14+
pub(crate) builds: Vec<String>,
15+
/// Auxiliary crates that should be compiled as `#![crate_type = "bin"]`.
16+
pub(crate) bins: Vec<String>,
17+
/// Similar to `builds`, but a list of NAME=somelib.rs of dependencies
18+
/// to build and pass with the `--extern` flag.
19+
pub(crate) crates: Vec<(String, String)>,
20+
/// Similar to `builds`, but also uses the resulting dylib as a
21+
/// `-Zcodegen-backend` when compiling the test file.
22+
pub(crate) codegen_backend: Option<String>,
23+
}
24+
25+
impl AuxProps {
26+
/// Yields all of the paths (relative to `./auxiliary/`) that have been
27+
/// specified in `aux-*` directives for this test.
28+
pub(crate) fn all_aux_path_strings(&self) -> impl Iterator<Item = &str> {
29+
let Self { builds, bins, crates, codegen_backend } = self;
30+
31+
iter::empty()
32+
.chain(builds.iter().map(String::as_str))
33+
.chain(bins.iter().map(String::as_str))
34+
.chain(crates.iter().map(|(_, path)| path.as_str()))
35+
.chain(codegen_backend.iter().map(String::as_str))
36+
}
37+
}
38+
39+
/// If the given test directive line contains an `aux-*` directive, parse it
40+
/// and update [`AuxProps`] accordingly.
41+
pub(super) fn parse_and_update_aux(config: &Config, ln: &str, aux: &mut AuxProps) {
42+
if !ln.starts_with("aux-") {
43+
return;
44+
}
45+
46+
config.push_name_value_directive(ln, AUX_BUILD, &mut aux.builds, |r| r.trim().to_string());
47+
config.push_name_value_directive(ln, AUX_BIN, &mut aux.bins, |r| r.trim().to_string());
48+
config.push_name_value_directive(ln, AUX_CRATE, &mut aux.crates, parse_aux_crate);
49+
if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND) {
50+
aux.codegen_backend = Some(r.trim().to_owned());
51+
}
52+
}
53+
54+
fn parse_aux_crate(r: String) -> (String, String) {
55+
let mut parts = r.trim().splitn(2, '=');
56+
(
57+
parts.next().expect("missing aux-crate name (e.g. log=log.rs)").to_string(),
58+
parts.next().expect("missing aux-crate value (e.g. log=log.rs)").to_string(),
59+
)
60+
}

src/tools/compiletest/src/header/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ fn aux_build() {
242242
//@ aux-build: b.rs
243243
"
244244
)
245-
.aux,
245+
.aux
246+
.builds,
246247
vec!["a.rs", "b.rs"],
247248
);
248249
}

src/tools/compiletest/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,8 @@ fn files_related_to_test(
862862
related.push(testpaths.file.clone());
863863
}
864864

865-
for aux in &props.aux {
865+
for aux in props.aux.all_aux_path_strings() {
866+
// FIXME(Zalathar): Perform all `auxiliary` path resolution in one place.
866867
let path = testpaths.file.parent().unwrap().join("auxiliary").join(aux);
867868
related.push(path);
868869
}

src/tools/compiletest/src/runtest.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -841,13 +841,13 @@ impl<'test> TestCx<'test> {
841841
/// Auxiliaries, no matter how deep, have the same root_out_dir and root_testpaths.
842842
fn document(&self, root_out_dir: &Path, root_testpaths: &TestPaths) -> ProcRes {
843843
if self.props.build_aux_docs {
844-
for rel_ab in &self.props.aux_builds {
844+
for rel_ab in &self.props.aux.builds {
845845
let aux_testpaths = self.compute_aux_test_paths(root_testpaths, rel_ab);
846-
let aux_props =
846+
let props_for_aux =
847847
self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
848848
let aux_cx = TestCx {
849849
config: self.config,
850-
props: &aux_props,
850+
props: &props_for_aux,
851851
testpaths: &aux_testpaths,
852852
revision: self.revision,
853853
};
@@ -1059,11 +1059,11 @@ impl<'test> TestCx<'test> {
10591059
fn aux_output_dir(&self) -> PathBuf {
10601060
let aux_dir = self.aux_output_dir_name();
10611061

1062-
if !self.props.aux_builds.is_empty() {
1062+
if !self.props.aux.builds.is_empty() {
10631063
remove_and_create_dir_all(&aux_dir);
10641064
}
10651065

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

10751075
fn build_all_auxiliary(&self, of: &TestPaths, aux_dir: &Path, rustc: &mut Command) {
1076-
for rel_ab in &self.props.aux_builds {
1076+
for rel_ab in &self.props.aux.builds {
10771077
self.build_auxiliary(of, rel_ab, &aux_dir, false /* is_bin */);
10781078
}
10791079

1080-
for rel_ab in &self.props.aux_bins {
1080+
for rel_ab in &self.props.aux.bins {
10811081
self.build_auxiliary(of, rel_ab, &aux_dir, true /* is_bin */);
10821082
}
10831083

1084-
for (aux_name, aux_path) in &self.props.aux_crates {
1084+
for (aux_name, aux_path) in &self.props.aux.crates {
10851085
let aux_type = self.build_auxiliary(of, &aux_path, &aux_dir, false /* is_bin */);
10861086
let lib_name =
10871087
get_lib_name(&aux_path.trim_end_matches(".rs").replace('-', "_"), aux_type);
@@ -1097,7 +1097,7 @@ impl<'test> TestCx<'test> {
10971097

10981098
// Build any `//@ aux-codegen-backend`, and pass the resulting library
10991099
// to `-Zcodegen-backend` when compiling the test file.
1100-
if let Some(aux_file) = &self.props.aux_codegen_backend {
1100+
if let Some(aux_file) = &self.props.aux.codegen_backend {
11011101
let aux_type = self.build_auxiliary(of, aux_file, aux_dir, false);
11021102
if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
11031103
let lib_path = aux_dir.join(&lib_name);

0 commit comments

Comments
 (0)