Skip to content

Commit 2c5647e

Browse files
committed
Auto merge of #126097 - Kobzol:runmake-change-cwd, r=
Change how runmake v2 tests are executed This PR makes execution of v2 runmake tests more sane, by executing each test in a temporary directory by default, rather than running it inside `tests/run-make`. This will have.. a lot of conflicts. Fixes: #126080 Closes #125726, because it removes `tmp_dir`, lol. r? `@jieyouxu` try-job: x86_64-msvc
2 parents 655600c + b10a404 commit 2c5647e

File tree

74 files changed

+275
-302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+275
-302
lines changed

src/tools/compiletest/src/runtest.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::errors::{self, Error, ErrorKind};
1515
use crate::header::TestProps;
1616
use crate::json;
1717
use crate::read2::{read2_abbreviated, Truncated};
18-
use crate::util::{add_dylib_path, dylib_env_var, logv, PathBufExt};
18+
use crate::util::{add_dylib_path, copy_dir_all, dylib_env_var, logv, PathBufExt};
1919
use crate::ColorConfig;
2020
use colored::Colorize;
2121
use miropt_test_tools::{files_for_miropt_test, MiroptTest, MiroptTestFile};
@@ -3471,6 +3471,21 @@ impl<'test> TestCx<'test> {
34713471
let rmake_out_dir = base_dir.join("rmake_out");
34723472
create_dir_all(&rmake_out_dir).unwrap();
34733473

3474+
// Copy all input files (apart from rmake.rs) to the temporary directory,
3475+
// so that the input directory structure from `tests/run-make/<test>` is mirrored
3476+
// to the `rmake_out` directory.
3477+
for path in walkdir::WalkDir::new(&self.testpaths.file).min_depth(1) {
3478+
let path = path.unwrap().path().to_path_buf();
3479+
if path.file_name().is_some_and(|s| s != "rmake.rs") {
3480+
let target = rmake_out_dir.join(path.strip_prefix(&self.testpaths.file).unwrap());
3481+
if path.is_dir() {
3482+
copy_dir_all(&path, target).unwrap();
3483+
} else {
3484+
fs::copy(&path, target).unwrap();
3485+
}
3486+
}
3487+
}
3488+
34743489
// HACK: assume stageN-target, we only want stageN.
34753490
let stage = self.config.stage_id.split('-').next().unwrap();
34763491

@@ -3530,18 +3545,11 @@ impl<'test> TestCx<'test> {
35303545
.env("PYTHON", &self.config.python)
35313546
.env("RUST_BUILD_STAGE", &self.config.stage_id)
35323547
.env("RUSTC", cwd.join(&self.config.rustc_path))
3533-
.env("TMPDIR", &rmake_out_dir)
35343548
.env("LD_LIB_PATH_ENVVAR", dylib_env_var())
35353549
.env(dylib_env_var(), &host_dylib_env_paths)
35363550
.env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
35373551
.env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
3538-
.env("LLVM_COMPONENTS", &self.config.llvm_components)
3539-
// We for sure don't want these tests to run in parallel, so make
3540-
// sure they don't have access to these vars if we run via `make`
3541-
// at the top level
3542-
.env_remove("MAKEFLAGS")
3543-
.env_remove("MFLAGS")
3544-
.env_remove("CARGO_MAKEFLAGS");
3552+
.env("LLVM_COMPONENTS", &self.config.llvm_components);
35453553

35463554
if std::env::var_os("COMPILETEST_FORCE_STAGE0").is_some() {
35473555
let mut stage0_sysroot = build_root.clone();
@@ -3571,7 +3579,7 @@ impl<'test> TestCx<'test> {
35713579
let target_rpath_env_path = env::join_paths(target_rpath_env_path).unwrap();
35723580

35733581
let mut cmd = Command::new(&recipe_bin);
3574-
cmd.current_dir(&self.testpaths.file)
3582+
cmd.current_dir(&rmake_out_dir)
35753583
.stdout(Stdio::piped())
35763584
.stderr(Stdio::piped())
35773585
.env("LD_LIB_PATH_ENVVAR", dylib_env_var())
@@ -3582,16 +3590,9 @@ impl<'test> TestCx<'test> {
35823590
.env("SOURCE_ROOT", &src_root)
35833591
.env("RUST_BUILD_STAGE", &self.config.stage_id)
35843592
.env("RUSTC", cwd.join(&self.config.rustc_path))
3585-
.env("TMPDIR", &rmake_out_dir)
35863593
.env("HOST_RPATH_DIR", cwd.join(&self.config.compile_lib_path))
35873594
.env("TARGET_RPATH_DIR", cwd.join(&self.config.run_lib_path))
3588-
.env("LLVM_COMPONENTS", &self.config.llvm_components)
3589-
// We for sure don't want these tests to run in parallel, so make
3590-
// sure they don't have access to these vars if we run via `make`
3591-
// at the top level
3592-
.env_remove("MAKEFLAGS")
3593-
.env_remove("MFLAGS")
3594-
.env_remove("CARGO_MAKEFLAGS");
3595+
.env("LLVM_COMPONENTS", &self.config.llvm_components);
35953596

35963597
if let Some(ref rustdoc) = self.config.rustdoc_path {
35973598
cmd.env("RUSTDOC", cwd.join(rustdoc));

src/tools/compiletest/src/util.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::common::Config;
22
use std::env;
33
use std::ffi::OsStr;
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55
use std::process::Command;
66

77
use tracing::*;
@@ -76,3 +76,17 @@ pub fn add_dylib_path(cmd: &mut Command, paths: impl Iterator<Item = impl Into<P
7676
let new_paths = paths.map(Into::into).chain(old_paths.into_iter().flatten());
7777
cmd.env(dylib_env_var(), env::join_paths(new_paths).unwrap());
7878
}
79+
80+
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
81+
std::fs::create_dir_all(&dst)?;
82+
for entry in std::fs::read_dir(src)? {
83+
let entry = entry?;
84+
let ty = entry.file_type()?;
85+
if ty.is_dir() {
86+
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
87+
} else {
88+
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
89+
}
90+
}
91+
Ok(())
92+
}

src/tools/run-make-support/src/cc.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::path::Path;
22
use std::process::Command;
33

4-
use crate::{
5-
bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, tmp_dir, uname,
6-
};
4+
use crate::{bin_name, cygpath_windows, env_var, handle_failed_output, is_msvc, is_windows, uname};
75

86
/// Construct a new platform-specific C compiler invocation.
97
///
@@ -54,8 +52,7 @@ impl Cc {
5452
self
5553
}
5654

57-
/// Specify `-o` or `-Fe`/`-Fo` depending on platform/compiler. This assumes that the executable
58-
/// is under `$TMPDIR`.
55+
/// Specify `-o` or `-Fe`/`-Fo` depending on platform/compiler.
5956
pub fn out_exe(&mut self, name: &str) -> &mut Self {
6057
// Ref: tools.mk (irrelevant lines omitted):
6158
//
@@ -69,13 +66,13 @@ impl Cc {
6966
// ```
7067

7168
if is_msvc() {
72-
let fe_path = cygpath_windows(tmp_dir().join(bin_name(name)));
73-
let fo_path = cygpath_windows(tmp_dir().join(format!("{name}.obj")));
69+
let fe_path = cygpath_windows(bin_name(name));
70+
let fo_path = cygpath_windows(format!("{name}.obj"));
7471
self.cmd.arg(format!("-Fe:{fe_path}"));
7572
self.cmd.arg(format!("-Fo:{fo_path}"));
7673
} else {
7774
self.cmd.arg("-o");
78-
self.cmd.arg(tmp_dir().join(name));
75+
self.cmd.arg(name);
7976
}
8077

8178
self

src/tools/run-make-support/src/clang.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22
use std::process::Command;
33

4-
use crate::{bin_name, env_var, handle_failed_output, tmp_dir};
4+
use crate::{bin_name, env_var, handle_failed_output};
55

66
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
77
pub fn clang() -> Clang {
@@ -30,11 +30,11 @@ impl Clang {
3030
self
3131
}
3232

33-
/// Specify the name of the executable. The executable will be placed under `$TMPDIR`, and the
34-
/// extension will be determined by [`bin_name`].
33+
/// Specify the name of the executable. The executable will be placed under the current directory
34+
/// and the extension will be determined by [`bin_name`].
3535
pub fn out_exe(&mut self, name: &str) -> &mut Self {
3636
self.cmd.arg("-o");
37-
self.cmd.arg(tmp_dir().join(bin_name(name)));
37+
self.cmd.arg(bin_name(name));
3838
self
3939
}
4040

src/tools/run-make-support/src/lib.rs

+28-26
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ pub fn env_var_os(name: &str) -> OsString {
4545
}
4646
}
4747

48-
/// Path of `TMPDIR` (a temporary build directory, not under `/tmp`).
49-
pub fn tmp_dir() -> PathBuf {
50-
env_var_os("TMPDIR").into()
51-
}
52-
5348
/// `TARGET`
5449
pub fn target() -> String {
5550
env_var("TARGET")
@@ -70,12 +65,6 @@ pub fn is_darwin() -> bool {
7065
target().contains("darwin")
7166
}
7267

73-
/// Construct a path to a static library under `$TMPDIR` given the library name. This will return a
74-
/// path with `$TMPDIR` joined with platform-and-compiler-specific library name.
75-
pub fn static_lib(name: &str) -> PathBuf {
76-
tmp_dir().join(static_lib_name(name))
77-
}
78-
7968
pub fn python_command() -> Command {
8069
let python_path = env_var("PYTHON");
8170
Command::new(python_path)
@@ -116,12 +105,6 @@ pub fn static_lib_name(name: &str) -> String {
116105
if is_msvc() { format!("{name}.lib") } else { format!("lib{name}.a") }
117106
}
118107

119-
/// Construct a path to a dynamic library under `$TMPDIR` given the library name. This will return a
120-
/// path with `$TMPDIR` joined with platform-and-compiler-specific library name.
121-
pub fn dynamic_lib(name: &str) -> PathBuf {
122-
tmp_dir().join(dynamic_lib_name(name))
123-
}
124-
125108
/// Construct the dynamic library name based on the platform.
126109
pub fn dynamic_lib_name(name: &str) -> String {
127110
// See tools.mk (irrelevant lines omitted):
@@ -159,14 +142,7 @@ pub fn dynamic_lib_extension() -> &'static str {
159142
}
160143
}
161144

162-
/// Construct a path to a rust library (rlib) under `$TMPDIR` given the library name. This will return a
163-
/// path with `$TMPDIR` joined with the library name.
164-
pub fn rust_lib(name: &str) -> PathBuf {
165-
tmp_dir().join(rust_lib_name(name))
166-
}
167-
168-
/// Generate the name a rust library (rlib) would have. If you want the complete path, use
169-
/// [`rust_lib`] instead.
145+
/// Construct a rust library (rlib) name.
170146
pub fn rust_lib_name(name: &str) -> String {
171147
format!("lib{name}.rlib")
172148
}
@@ -176,6 +152,11 @@ pub fn bin_name(name: &str) -> String {
176152
if is_windows() { format!("{name}.exe") } else { name.to_string() }
177153
}
178154

155+
/// Return the current working directory.
156+
pub fn cwd() -> PathBuf {
157+
env::current_dir().unwrap()
158+
}
159+
179160
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
180161
/// available on the platform!
181162
#[track_caller]
@@ -227,7 +208,7 @@ pub fn set_host_rpath(cmd: &mut Command) {
227208
let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
228209
cmd.env(&ld_lib_path_envvar, {
229210
let mut paths = vec![];
230-
paths.push(PathBuf::from(env_var("TMPDIR")));
211+
paths.push(cwd());
231212
paths.push(PathBuf::from(env_var("HOST_RPATH_DIR")));
232213
for p in env::split_paths(&env_var(&ld_lib_path_envvar)) {
233214
paths.push(p.to_path_buf());
@@ -315,6 +296,27 @@ pub fn assert_not_contains(haystack: &str, needle: &str) {
315296
}
316297
}
317298

299+
/// This function is designed for running commands in a temporary directory
300+
/// that is cleared after the function ends.
301+
///
302+
/// What this function does:
303+
/// 1) Creates a temporary directory (`tmpdir`)
304+
/// 2) Copies all files from the current directory to `tmpdir`
305+
/// 3) Changes the current working directory to `tmpdir`
306+
/// 4) Calls `callback`
307+
/// 5) Switches working directory back to the original one
308+
/// 6) Removes `tmpdir`
309+
pub fn run_in_tmpdir<F: FnOnce()>(callback: F) {
310+
let original_dir = cwd();
311+
let tmpdir = original_dir.join("../temporary-directory");
312+
copy_dir_all(".", &tmpdir);
313+
314+
env::set_current_dir(&tmpdir).unwrap();
315+
callback();
316+
env::set_current_dir(original_dir).unwrap();
317+
fs::remove_dir_all(tmpdir).unwrap();
318+
}
319+
318320
/// Implement common helpers for command wrappers. This assumes that the command wrapper is a struct
319321
/// containing a `cmd: Command` field and a `output` function. The provided helpers are:
320322
///

src/tools/run-make-support/src/run.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ use std::env;
22
use std::path::{Path, PathBuf};
33
use std::process::{Command, Output};
44

5-
use crate::{env_var, is_windows};
5+
use crate::{cwd, env_var, is_windows};
66

77
use super::handle_failed_output;
88

99
fn run_common(name: &str) -> (Command, Output) {
1010
let mut bin_path = PathBuf::new();
11-
bin_path.push(env_var("TMPDIR"));
11+
bin_path.push(cwd());
1212
bin_path.push(name);
1313
let ld_lib_path_envvar = env_var("LD_LIB_PATH_ENVVAR");
1414
let mut cmd = Command::new(bin_path);
1515
cmd.env(&ld_lib_path_envvar, {
1616
let mut paths = vec![];
17-
paths.push(PathBuf::from(env_var("TMPDIR")));
17+
paths.push(cwd());
1818
for p in env::split_paths(&env_var("TARGET_RPATH_ENV")) {
1919
paths.push(p.to_path_buf());
2020
}

src/tools/run-make-support/src/rustc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use std::path::Path;
44
use std::process::{Command, Output, Stdio};
55

6-
use crate::{env_var, handle_failed_output, set_host_rpath, tmp_dir};
6+
use crate::{cwd, env_var, handle_failed_output, set_host_rpath};
77

88
/// Construct a new `rustc` invocation.
99
pub fn rustc() -> Rustc {
@@ -28,7 +28,7 @@ fn setup_common() -> Command {
2828
let rustc = env_var("RUSTC");
2929
let mut cmd = Command::new(rustc);
3030
set_host_rpath(&mut cmd);
31-
cmd.arg("--out-dir").arg(tmp_dir()).arg("-L").arg(tmp_dir());
31+
cmd.arg("-L").arg(cwd());
3232
cmd
3333
}
3434

tests/run-make/CURRENT_RUSTC_VERSION/rmake.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use run_make_support::{aux_build, rustc, source_root};
1010
fn main() {
1111
aux_build().input("stable.rs").emit("metadata").run();
1212

13-
let mut stable_path = PathBuf::from(env!("TMPDIR"));
14-
stable_path.push("libstable.rmeta");
15-
16-
let output =
17-
rustc().input("main.rs").emit("metadata").extern_("stable", &stable_path).command_output();
13+
let output = rustc()
14+
.input("main.rs")
15+
.emit("metadata")
16+
.extern_("stable", "libstable.rmeta")
17+
.command_output();
1818

1919
let stderr = String::from_utf8_lossy(&output.stderr);
2020
let version = std::fs::read_to_string(source_root().join("src/version")).unwrap();

tests/run-make/arguments-non-c-like-enum/rmake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Check that non-trivial `repr(C)` enum in Rust has valid C layout.
22
//@ ignore-cross-compile
33

4-
use run_make_support::{cc, extra_c_flags, extra_cxx_flags, run, rustc, static_lib};
4+
use run_make_support::{cc, extra_c_flags, extra_cxx_flags, run, rustc, static_lib_name};
55

66
pub fn main() {
77
use std::path::Path;
88

99
rustc().input("nonclike.rs").crate_type("staticlib").run();
1010
cc().input("test.c")
11-
.input(static_lib("nonclike"))
11+
.input(static_lib_name("nonclike"))
1212
.out_exe("test")
1313
.args(&extra_c_flags())
1414
.args(&extra_cxx_flags())

tests/run-make/artifact-incr-cache-no-obj/rmake.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
//
66
// Fixes: rust-lang/rust#123234
77

8-
use run_make_support::{rustc, tmp_dir};
8+
use run_make_support::rustc;
99

1010
fn main() {
11-
let inc_dir = tmp_dir();
12-
1311
for _ in 0..=1 {
1412
rustc()
1513
.input("lib.rs")
1614
.crate_type("lib")
1715
.emit("asm,dep-info,link,mir,llvm-ir,llvm-bc")
18-
.incremental(&inc_dir)
16+
.incremental("incremental")
1917
.run();
2018
}
2119
}

tests/run-make/artifact-incr-cache/rmake.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
// Also see discussion at
88
// <https://internals.rust-lang.org/t/interaction-between-incremental-compilation-and-emit/20551>
99

10-
use run_make_support::{rustc, tmp_dir};
10+
use run_make_support::rustc;
1111

1212
fn main() {
13-
let inc_dir = tmp_dir();
14-
1513
for _ in 0..=1 {
1614
rustc()
1715
.input("lib.rs")
1816
.crate_type("lib")
1917
.emit("obj,asm,dep-info,link,mir,llvm-ir,llvm-bc")
20-
.incremental(&inc_dir)
18+
.incremental("incremental")
2119
.run();
2220
}
2321
}

tests/run-make/bare-outfile/rmake.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33

44
//@ ignore-cross-compile
55

6-
use run_make_support::{run, rustc, tmp_dir};
7-
use std::env;
8-
use std::fs;
6+
use run_make_support::{run, rustc};
97

108
fn main() {
11-
fs::copy("foo.rs", tmp_dir().join("foo.rs")).unwrap();
12-
env::set_current_dir(tmp_dir());
139
rustc().output("foo").input("foo.rs").run();
1410
run("foo");
1511
}

0 commit comments

Comments
 (0)