Skip to content

Commit ef3c339

Browse files
committed
compiletest: introduce and use --src-root and --src-test-suite-root
Instead of only having `--src-base` and `src_base` which *actually* refers to the directory containing the test suite and not the sources root. More importantly, kill off `find_rust_src_root` when we can simply pass that info from bootstrap.
1 parent 9f48ded commit ef3c339

File tree

9 files changed

+74
-97
lines changed

9 files changed

+74
-97
lines changed

src/tools/compiletest/src/common.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ pub struct Config {
215215
/// `None` then these tests will be ignored.
216216
pub run_clang_based_tests_with: Option<String>,
217217

218-
/// The directory containing the tests to run
219-
pub src_base: PathBuf,
218+
/// The directory containing the sources.
219+
pub src_root: PathBuf,
220+
/// The directory containing the test suite sources. Must be a subdirectory of `src_root`.
221+
pub src_test_suite_root: PathBuf,
220222

221223
/// The directory where programs should be built
222224
pub build_base: PathBuf,

src/tools/compiletest/src/header.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1026,19 +1026,6 @@ impl Config {
10261026
}
10271027
}
10281028

1029-
pub fn find_rust_src_root(&self) -> Option<PathBuf> {
1030-
let mut path = self.src_base.clone();
1031-
let path_postfix = Path::new("src/etc/lldb_batchmode.py");
1032-
1033-
while path.pop() {
1034-
if path.join(&path_postfix).is_file() {
1035-
return Some(path);
1036-
}
1037-
}
1038-
1039-
None
1040-
}
1041-
10421029
fn parse_edition(&self, line: &str) -> Option<String> {
10431030
self.parse_name_value_directive(line, "edition")
10441031
}
@@ -1098,7 +1085,7 @@ fn expand_variables(mut value: String, config: &Config) -> String {
10981085
}
10991086

11001087
if value.contains(SRC_BASE) {
1101-
value = value.replace(SRC_BASE, &config.src_base.to_string_lossy());
1088+
value = value.replace(SRC_BASE, &config.src_test_suite_root.to_str().unwrap());
11021089
}
11031090

11041091
if value.contains(BUILD_BASE) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ impl ConfigBuilder {
153153
"--run-lib-path=",
154154
"--python=",
155155
"--jsondocck-path=",
156-
"--src-base=",
156+
"--src-root=",
157+
"--src-test-suite-root=",
157158
"--build-base=",
158159
"--sysroot-base=",
159160
"--cc=c",

src/tools/compiletest/src/lib.rs

+44-25
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ pub fn parse_config(args: Vec<String>) -> Config {
6161
.optopt("", "jsondoclint-path", "path to jsondoclint to use for doc tests", "PATH")
6262
.optopt("", "run-clang-based-tests-with", "path to Clang executable", "PATH")
6363
.optopt("", "llvm-filecheck", "path to LLVM's FileCheck binary", "DIR")
64-
.reqopt("", "src-base", "directory to scan for test files", "PATH")
64+
.reqopt("", "src-root", "directory containing sources", "PATH")
65+
.reqopt("", "src-test-suite-root", "directory containing test suite sources", "PATH")
6566
.reqopt("", "build-base", "directory to deposit test outputs", "PATH")
6667
.reqopt("", "sysroot-base", "directory containing the compiler sysroot", "PATH")
6768
.reqopt("", "stage", "stage number under test", "N")
@@ -243,7 +244,6 @@ pub fn parse_config(args: Vec<String>) -> Config {
243244
|| header::extract_llvm_version_from_binary(&matches.opt_str("llvm-filecheck")?),
244245
);
245246

246-
let src_base = opt_path(matches, "src-base");
247247
let run_ignored = matches.opt_present("ignored");
248248
let with_rustc_debug_assertions = matches.opt_present("with-rustc-debug-assertions");
249249
let with_std_debug_assertions = matches.opt_present("with-std-debug-assertions");
@@ -300,6 +300,15 @@ pub fn parse_config(args: Vec<String>) -> Config {
300300
None => panic!("`--stage` is required"),
301301
};
302302

303+
let src_root = opt_path(matches, "src-root");
304+
let src_test_suite_root = opt_path(matches, "src-test-suite-root");
305+
assert!(
306+
src_test_suite_root.starts_with(&src_root),
307+
"`src-root` must be a parent of `src-test-suite-root`: `src-root`=`{}`, `src-test-suite-root` = `{}`",
308+
src_root.display(),
309+
src_test_suite_root.display()
310+
);
311+
303312
Config {
304313
bless: matches.opt_present("bless"),
305314
compile_lib_path: make_absolute(opt_path(matches, "compile-lib-path")),
@@ -314,7 +323,10 @@ pub fn parse_config(args: Vec<String>) -> Config {
314323
run_clang_based_tests_with: matches.opt_str("run-clang-based-tests-with"),
315324
llvm_filecheck: matches.opt_str("llvm-filecheck").map(PathBuf::from),
316325
llvm_bin_dir: matches.opt_str("llvm-bin-dir").map(PathBuf::from),
317-
src_base,
326+
327+
src_root,
328+
src_test_suite_root,
329+
318330
build_base: opt_path(matches, "build-base"),
319331
sysroot_base: opt_path(matches, "sysroot-base"),
320332

@@ -422,7 +434,10 @@ pub fn log_config(config: &Config) {
422434
logv(c, format!("rustc_path: {:?}", config.rustc_path.display()));
423435
logv(c, format!("cargo_path: {:?}", config.cargo_path));
424436
logv(c, format!("rustdoc_path: {:?}", config.rustdoc_path));
425-
logv(c, format!("src_base: {:?}", config.src_base.display()));
437+
438+
logv(c, format!("src_root: {}", config.src_root.display()));
439+
logv(c, format!("src_test_suite_root: {}", config.src_test_suite_root.display()));
440+
426441
logv(c, format!("build_base: {:?}", config.build_base.display()));
427442
logv(c, format!("stage: {}", config.stage));
428443
logv(c, format!("stage_id: {}", config.stage_id));
@@ -620,20 +635,29 @@ struct TestCollector {
620635
/// regardless of whether any filters/tests were specified on the command-line,
621636
/// because filtering is handled later by libtest.
622637
pub fn collect_and_make_tests(config: Arc<Config>) -> Vec<test::TestDescAndFn> {
623-
debug!("making tests from {:?}", config.src_base.display());
638+
debug!("making tests from {}", config.src_test_suite_root.display());
624639
let common_inputs_stamp = common_inputs_stamp(&config);
625-
let modified_tests = modified_tests(&config, &config.src_base).unwrap_or_else(|err| {
626-
panic!("modified_tests got error from dir: {}, error: {}", config.src_base.display(), err)
627-
});
640+
let modified_tests =
641+
modified_tests(&config, &config.src_test_suite_root).unwrap_or_else(|err| {
642+
panic!(
643+
"modified_tests got error from dir: {}, error: {}",
644+
config.src_test_suite_root.display(),
645+
err
646+
)
647+
});
628648
let cache = HeadersCache::load(&config);
629649

630650
let cx = TestCollectorCx { config, cache, common_inputs_stamp, modified_tests };
631651
let mut collector =
632652
TestCollector { tests: vec![], found_path_stems: HashSet::new(), poisoned: false };
633653

634-
collect_tests_from_dir(&cx, &mut collector, &cx.config.src_base, Path::new("")).unwrap_or_else(
635-
|reason| panic!("Could not read tests from {}: {reason}", cx.config.src_base.display()),
636-
);
654+
collect_tests_from_dir(&cx, &mut collector, &cx.config.src_test_suite_root, Path::new(""))
655+
.unwrap_or_else(|reason| {
656+
panic!(
657+
"Could not read tests from {}: {reason}",
658+
cx.config.src_test_suite_root.display()
659+
)
660+
});
637661

638662
let TestCollector { tests, found_path_stems, poisoned } = collector;
639663

@@ -655,7 +679,7 @@ pub fn collect_and_make_tests(config: Arc<Config>) -> Vec<test::TestDescAndFn> {
655679
/// common to some subset of tests, and are hopefully unlikely to be modified
656680
/// while working on other tests.)
657681
fn common_inputs_stamp(config: &Config) -> Stamp {
658-
let rust_src_dir = config.find_rust_src_root().expect("Could not find Rust source root");
682+
let src_root = &config.src_root;
659683

660684
let mut stamp = Stamp::from_path(&config.rustc_path);
661685

@@ -670,17 +694,17 @@ fn common_inputs_stamp(config: &Config) -> Stamp {
670694
"src/etc/lldb_providers.py",
671695
];
672696
for file in &pretty_printer_files {
673-
let path = rust_src_dir.join(file);
697+
let path = src_root.join(file);
674698
stamp.add_path(&path);
675699
}
676700

677-
stamp.add_dir(&rust_src_dir.join("src/etc/natvis"));
701+
stamp.add_dir(&src_root.join("src/etc/natvis"));
678702

679703
stamp.add_dir(&config.run_lib_path);
680704

681705
if let Some(ref rustdoc_path) = config.rustdoc_path {
682706
stamp.add_path(&rustdoc_path);
683-
stamp.add_path(&rust_src_dir.join("src/etc/htmldocck.py"));
707+
stamp.add_path(&src_root.join("src/etc/htmldocck.py"));
684708
}
685709

686710
// Re-run coverage tests if the `coverage-dump` tool was modified,
@@ -689,10 +713,10 @@ fn common_inputs_stamp(config: &Config) -> Stamp {
689713
stamp.add_path(coverage_dump_path)
690714
}
691715

692-
stamp.add_dir(&rust_src_dir.join("src/tools/run-make-support"));
716+
stamp.add_dir(&src_root.join("src/tools/run-make-support"));
693717

694718
// Compiletest itself.
695-
stamp.add_dir(&rust_src_dir.join("src/tools/compiletest/"));
719+
stamp.add_dir(&src_root.join("src/tools/compiletest"));
696720

697721
stamp
698722
}
@@ -933,10 +957,7 @@ fn files_related_to_test(
933957
}
934958

935959
// `minicore.rs` test auxiliary: we need to make sure tests get rerun if this changes.
936-
//
937-
// FIXME(jieyouxu): untangle these paths, we should provide both a path to root `tests/` or
938-
// `tests/auxiliary/` and the test suite in question. `src_base` is also a terrible name.
939-
related.push(config.src_base.parent().unwrap().join("auxiliary").join("minicore.rs"));
960+
related.push(config.src_root.join("tests").join("auxiliary").join("minicore.rs"));
940961

941962
related
942963
}
@@ -1026,10 +1047,8 @@ fn make_test_name(
10261047
testpaths: &TestPaths,
10271048
revision: Option<&str>,
10281049
) -> test::TestName {
1029-
// Print the name of the file, relative to the repository root.
1030-
// `src_base` looks like `/path/to/rust/tests/ui`
1031-
let root_directory = config.src_base.parent().unwrap().parent().unwrap();
1032-
let path = testpaths.file.strip_prefix(root_directory).unwrap();
1050+
// Print the name of the file, relative to the sources root.
1051+
let path = testpaths.file.strip_prefix(&config.src_root).unwrap();
10331052
let debugger = match config.debugger {
10341053
Some(d) => format!("-{}", d),
10351054
None => String::new(),

src/tools/compiletest/src/runtest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ impl<'test> TestCx<'test> {
13651365
//
13661366
// Note: avoid adding a subdirectory of an already filtered directory here, otherwise the
13671367
// same slice of text will be double counted and the truncation might not happen.
1368-
add_path(&self.config.src_base);
1368+
add_path(&self.config.src_test_suite_root);
13691369
add_path(&self.config.build_base);
13701370

13711371
read2_abbreviated(child, &filter_paths_from_len).expect("failed to read output")
@@ -1471,7 +1471,7 @@ impl<'test> TestCx<'test> {
14711471
// Similarly, vendored sources shouldn't be shown when running from a dist tarball.
14721472
rustc.arg("-Z").arg(format!(
14731473
"ignore-directory-in-diagnostics-source-blocks={}",
1474-
self.config.find_rust_src_root().unwrap().join("vendor").display(),
1474+
self.config.src_root.join("vendor").to_str().unwrap(),
14751475
));
14761476

14771477
// Optionally prevent default --sysroot if specified in test compile-flags.
@@ -1632,7 +1632,7 @@ impl<'test> TestCx<'test> {
16321632
if self.props.remap_src_base {
16331633
rustc.arg(format!(
16341634
"--remap-path-prefix={}={}",
1635-
self.config.src_base.display(),
1635+
self.config.src_test_suite_root.to_str().unwrap(),
16361636
FAKE_SRC_BASE,
16371637
));
16381638
}

src/tools/compiletest/src/runtest/debuginfo.rs

+9-20
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,8 @@ impl TestCx<'_> {
257257
println!("Adb process is already finished.");
258258
}
259259
} else {
260-
let rust_src_root =
261-
self.config.find_rust_src_root().expect("Could not find Rust source root");
262-
let rust_pp_module_rel_path = Path::new("./src/etc");
263-
let rust_pp_module_abs_path =
264-
rust_src_root.join(rust_pp_module_rel_path).to_str().unwrap().to_owned();
260+
let rust_pp_module_abs_path = self.config.src_root.join("src").join("etc");
261+
let rust_pp_module_abs_path = rust_pp_module_abs_path.to_str().unwrap();
265262
// write debugger script
266263
let mut script_str = String::with_capacity(2048);
267264
script_str.push_str(&format!("set charset {}\n", Self::charset()));
@@ -338,7 +335,7 @@ impl TestCx<'_> {
338335
let pythonpath = if let Ok(pp) = std::env::var("PYTHONPATH") {
339336
format!("{pp}:{rust_pp_module_abs_path}")
340337
} else {
341-
rust_pp_module_abs_path
338+
rust_pp_module_abs_path.to_string()
342339
};
343340
gdb.args(debugger_opts).env("PYTHONPATH", pythonpath);
344341

@@ -407,11 +404,8 @@ impl TestCx<'_> {
407404
// Make LLDB emit its version, so we have it documented in the test output
408405
script_str.push_str("version\n");
409406

410-
// Switch LLDB into "Rust mode"
411-
let rust_src_root =
412-
self.config.find_rust_src_root().expect("Could not find Rust source root");
413-
let rust_pp_module_rel_path = Path::new("./src/etc");
414-
let rust_pp_module_abs_path = rust_src_root.join(rust_pp_module_rel_path);
407+
// Switch LLDB into "Rust mode".
408+
let rust_pp_module_abs_path = self.config.src_root.join("src/etc");
415409

416410
script_str.push_str(&format!(
417411
"command script import {}/lldb_lookup.py\n",
@@ -445,7 +439,7 @@ impl TestCx<'_> {
445439
let debugger_script = self.make_out_name("debugger.script");
446440

447441
// Let LLDB execute the script via lldb_batchmode.py
448-
let debugger_run_result = self.run_lldb(&exe_file, &debugger_script, &rust_src_root);
442+
let debugger_run_result = self.run_lldb(&exe_file, &debugger_script);
449443

450444
if !debugger_run_result.status.success() {
451445
self.fatal_proc_rec("Error while running LLDB", &debugger_run_result);
@@ -456,18 +450,13 @@ impl TestCx<'_> {
456450
}
457451
}
458452

459-
fn run_lldb(
460-
&self,
461-
test_executable: &Path,
462-
debugger_script: &Path,
463-
rust_src_root: &Path,
464-
) -> ProcRes {
453+
fn run_lldb(&self, test_executable: &Path, debugger_script: &Path) -> ProcRes {
465454
// Prepare the lldb_batchmode which executes the debugger script
466-
let lldb_script_path = rust_src_root.join("src/etc/lldb_batchmode.py");
455+
let lldb_script_path = self.config.src_root.join("src/etc/lldb_batchmode.py");
467456
let pythonpath = if let Ok(pp) = std::env::var("PYTHONPATH") {
468457
format!("{pp}:{}", self.config.lldb_python_dir.as_ref().unwrap())
469458
} else {
470-
self.config.lldb_python_dir.as_ref().unwrap().to_string()
459+
self.config.lldb_python_dir.clone().unwrap()
471460
};
472461
self.run_command_to_procres(
473462
Command::new(&self.config.python)

src/tools/compiletest/src/runtest/js_doc.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ impl TestCx<'_> {
99

1010
self.document(&out_dir, &self.testpaths);
1111

12-
let root = self.config.find_rust_src_root().unwrap();
1312
let file_stem =
1413
self.testpaths.file.file_stem().and_then(|f| f.to_str()).expect("no file stem");
1514
let res = self.run_command_to_procres(
1615
Command::new(&nodejs)
17-
.arg(root.join("src/tools/rustdoc-js/tester.js"))
16+
.arg(self.config.src_root.join("src/tools/rustdoc-js/tester.js"))
1817
.arg("--doc-folder")
1918
.arg(out_dir)
2019
.arg("--crate-name")

src/tools/compiletest/src/runtest/run_make.rs

+7-28
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ impl TestCx<'_> {
2121

2222
fn run_rmake_legacy_test(&self) {
2323
let cwd = env::current_dir().unwrap();
24-
let src_root = self.config.src_base.parent().unwrap().parent().unwrap();
25-
let src_root = cwd.join(&src_root);
2624

2725
// FIXME(Zalathar): This should probably be `output_base_dir` to avoid
2826
// an unnecessary extra subdirectory, but since legacy Makefile tests
@@ -51,7 +49,7 @@ impl TestCx<'_> {
5149
.stderr(Stdio::piped())
5250
.env("TARGET", &self.config.target)
5351
.env("PYTHON", &self.config.python)
54-
.env("S", src_root)
52+
.env("S", &self.config.src_root)
5553
.env("RUST_BUILD_STAGE", &self.config.stage_id)
5654
.env("RUSTC", cwd.join(&self.config.rustc_path))
5755
.env("TMPDIR", &tmpdir)
@@ -181,28 +179,10 @@ impl TestCx<'_> {
181179
// library.
182180
// 2. We need to run the recipe binary.
183181

184-
// So we assume the rust-lang/rust project setup looks like the following (our `.` is the
185-
// top-level directory, irrelevant entries to our purposes omitted):
186-
//
187-
// ```
188-
// . // <- `source_root`
189-
// ├── build/ // <- `build_root`
190-
// ├── compiler/
191-
// ├── library/
192-
// ├── src/
193-
// │ └── tools/
194-
// │ └── run_make_support/
195-
// └── tests
196-
// └── run-make/
197-
// ```
198-
199-
// `source_root` is the top-level directory containing the rust-lang/rust checkout.
200-
let source_root =
201-
self.config.find_rust_src_root().expect("could not determine rust source root");
202182
// `self.config.build_base` is actually the build base folder + "test" + test suite name, it
203183
// looks like `build/<host_triple>/test/run-make`. But we want `build/<host_triple>/`. Note
204184
// that the `build` directory does not need to be called `build`, nor does it need to be
205-
// under `source_root`, so we must compute it based off of `self.config.build_base`.
185+
// under `src_root`, so we must compute it based off of `self.config.build_base`.
206186
let build_root =
207187
self.config.build_base.parent().and_then(Path::parent).unwrap().to_path_buf();
208188

@@ -389,10 +369,9 @@ impl TestCx<'_> {
389369
.env("TARGET", &self.config.target)
390370
// Some tests unfortunately still need Python, so provide path to a Python interpreter.
391371
.env("PYTHON", &self.config.python)
392-
// Provide path to checkout root. This is the top-level directory containing
393-
// rust-lang/rust checkout.
394-
.env("SOURCE_ROOT", &source_root)
395-
// Path to the build directory. This is usually the same as `source_root.join("build").join("host")`.
372+
// Provide path to sources root.
373+
.env("SOURCE_ROOT", &self.config.src_root)
374+
// Path to the host build directory.
396375
.env("BUILD_ROOT", &build_root)
397376
// Provide path to stage-corresponding rustc.
398377
.env("RUSTC", &self.config.rustc_path)
@@ -408,11 +387,11 @@ impl TestCx<'_> {
408387
.env("LLVM_COMPONENTS", &self.config.llvm_components);
409388

410389
if let Some(ref cargo) = self.config.cargo_path {
411-
cmd.env("CARGO", source_root.join(cargo));
390+
cmd.env("CARGO", cargo);
412391
}
413392

414393
if let Some(ref rustdoc) = self.config.rustdoc_path {
415-
cmd.env("RUSTDOC", source_root.join(rustdoc));
394+
cmd.env("RUSTDOC", rustdoc);
416395
}
417396

418397
if let Some(ref node) = self.config.nodejs {

0 commit comments

Comments
 (0)