compiletest/runtest/run_make.rs
1use std::path::{Path, PathBuf};
2use std::process::{Command, Output, Stdio};
3use std::{env, fs};
4
5use build_helper::fs::{ignore_not_found, recursive_remove};
6use camino::{Utf8Path, Utf8PathBuf};
7
8use super::{ProcRes, TestCx, disable_error_reporting};
9use crate::common::TestSuite;
10use crate::util::{ArgFileCommand, copy_dir_all, dylib_env_var};
11
12impl TestCx<'_> {
13 pub(super) fn run_rmake_test(&self) {
14 // For `run-make`, we need to perform 2 steps to build and run a `run-make` recipe
15 // (`rmake.rs`) to run the actual tests. The support library is already built as a tool rust
16 // library and is available under
17 // `build/$HOST/bootstrap-tools/$TARGET/release/librun_make_support.rlib`.
18 //
19 // 1. We need to build the recipe `rmake.rs` as a binary and link in the `run_make_support`
20 // library.
21 // 2. We need to run the recipe binary.
22
23 let host_build_root = self.config.build_root.join(&self.config.host);
24
25 // We construct the following directory tree for each rmake.rs test:
26 // ```
27 // <base_dir>/
28 // rmake.exe
29 // rmake_out/
30 // ```
31 // having the recipe executable separate from the output artifacts directory allows the
32 // recipes to `remove_dir_all($TMPDIR)` without running into issues related trying to remove
33 // a currently running executable because the recipe executable is not under the
34 // `rmake_out/` directory.
35 let base_dir = self.output_base_dir();
36 ignore_not_found(|| recursive_remove(&base_dir)).unwrap();
37
38 let rmake_out_dir = base_dir.join("rmake_out");
39 fs::create_dir_all(&rmake_out_dir).unwrap();
40
41 // Copy all input files (apart from rmake.rs) to the temporary directory,
42 // so that the input directory structure from `tests/run-make/<test>` is mirrored
43 // to the `rmake_out` directory.
44 for entry in walkdir::WalkDir::new(&self.testpaths.file).min_depth(1) {
45 let entry = entry.unwrap();
46 let path = entry.path();
47 let path = <&Utf8Path>::try_from(path).unwrap();
48 if path.file_name().is_some_and(|s| s != "rmake.rs") {
49 let target = rmake_out_dir.join(path.strip_prefix(&self.testpaths.file).unwrap());
50 if path.is_dir() {
51 copy_dir_all(&path, &target).unwrap();
52 } else {
53 fs::copy(path.as_std_path(), target).unwrap();
54 }
55 }
56 }
57
58 // In order to link in the support library as a rlib when compiling recipes, we need three
59 // paths:
60 // 1. Path of the built support library rlib itself.
61 // 2. Path of the built support library's dependencies directory.
62 // 3. Path of the built support library's dependencies' dependencies directory.
63 //
64 // The paths look like
65 //
66 // ```
67 // build/<target_triple>/
68 // ├── bootstrap-tools/
69 // │ ├── <host_triple>/release/librun_make_support.rlib // <- support rlib itself
70 // │ ├── <host_triple>/release/build/<pkg>/<hash>/out // <- deps
71 // │ └── release/build/<pkg>/<hash>/out // <- deps of deps
72 // ```
73 //
74 // FIXME(jieyouxu): there almost certainly is a better way to do this (specifically how the
75 // support lib and its deps are organized), but this seems to work for now.
76
77 let tools_bin = host_build_root.join("bootstrap-tools");
78 let support_host_path = tools_bin.join(&self.config.host).join("release");
79 let support_lib_path = support_host_path.join("librun_make_support.rlib");
80
81 let support_lib_deps = discover_out_dirs(support_host_path.join("build"));
82 let support_lib_deps_deps = discover_out_dirs(tools_bin.join("release").join("build"));
83
84 // To compile the recipe with rustc, we need to provide suitable dynamic library search
85 // paths to rustc. This includes both:
86 // 1. The "base" dylib search paths that was provided to compiletest, e.g. `LD_LIBRARY_PATH`
87 // on some linux distros.
88 // 2. Specific library paths in `self.config.compile_lib_path` needed for running rustc.
89
90 let base_dylib_search_paths = Vec::from_iter(
91 env::split_paths(&env::var(dylib_env_var()).unwrap())
92 .map(|p| Utf8PathBuf::try_from(p).expect("dylib env var contains non-UTF8 paths")),
93 );
94
95 // Calculate the paths of the recipe binary. As previously discussed, this is placed at
96 // `<base_dir>/<bin_name>` with `bin_name` being `rmake` or `rmake.exe` depending on
97 // platform.
98 let recipe_bin = {
99 let mut p = base_dir.join("rmake");
100 p.set_extension(env::consts::EXE_EXTENSION);
101 p
102 };
103
104 let out_dirs_to_args = |paths: Vec<PathBuf>| {
105 paths.into_iter().map(|p| format!("-Ldependency={}", p.display())).collect::<Vec<_>>()
106 };
107
108 // run-make-support and run-make tests are compiled using the stage0 compiler
109 // If the stage is 0, then the compiler that we test (either bootstrap or an explicitly
110 // set compiler) is the one that actually compiled run-make-support.
111 let stage0_rustc = self
112 .config
113 .stage0_rustc_path
114 .as_ref()
115 .expect("stage0 rustc is required to run run-make tests");
116 let mut rustc = ArgFileCommand::new(&stage0_rustc);
117 rustc
118 // `rmake.rs` **must** be buildable by a stable compiler, it may not use *any* unstable
119 // library or compiler features. Here, we force the stage 0 rustc to consider itself as
120 // a stable-channel compiler via `RUSTC_BOOTSTRAP=-1` to prevent *any* unstable
121 // library/compiler usages, even if stage 0 rustc is *actually* a nightly rustc.
122 .env("RUSTC_BOOTSTRAP", "-1")
123 .arg("-o")
124 .arg(&recipe_bin)
125 // Specify library search paths for `run_make_support`.
126 .arg(format!("-Ldependency={}", &support_lib_path.parent().unwrap()))
127 .args(out_dirs_to_args(support_lib_deps))
128 .args(out_dirs_to_args(support_lib_deps_deps))
129 // Provide `run_make_support` as extern prelude, so test writers don't need to write
130 // `extern run_make_support;`.
131 .arg("--extern")
132 .arg(format!("run_make_support={}", &support_lib_path))
133 .arg("--edition=2024")
134 .arg(&self.testpaths.file.join("rmake.rs"))
135 .arg("-Cprefer-dynamic");
136
137 // In test code we want to be very pedantic about values being silently discarded that are
138 // annotated with `#[must_use]`.
139 rustc.arg("-Dunused_must_use");
140
141 // Now run rustc to build the recipe.
142 let res = self.run_command_to_procres(rustc);
143 if !res.status.success() {
144 self.fatal_proc_rec("run-make test failed: could not build `rmake.rs` recipe", &res);
145 }
146
147 // To actually run the recipe, we have to provide the recipe with a bunch of information
148 // provided through env vars.
149
150 // Compute dynamic library search paths for recipes.
151 // These dylib directories are needed to **execute the recipe**.
152 let recipe_dylib_search_paths = {
153 let mut paths = base_dylib_search_paths.clone();
154 paths.push(
155 stage0_rustc
156 .parent()
157 .unwrap()
158 .parent()
159 .unwrap()
160 .join("lib")
161 .join("rustlib")
162 .join(&self.config.host)
163 .join("lib"),
164 );
165 paths
166 };
167
168 let mut cmd = Command::new(&recipe_bin);
169 cmd.current_dir(&rmake_out_dir)
170 .stdout(Stdio::piped())
171 .stderr(Stdio::piped())
172 // Provide the target-specific env var that is used to record dylib search paths. For
173 // example, this could be `LD_LIBRARY_PATH` on some linux distros but `PATH` on Windows.
174 .env("LD_LIB_PATH_ENVVAR", dylib_env_var())
175 // Provide the dylib search paths.
176 // This is required to run the **recipe** itself.
177 .env(dylib_env_var(), &env::join_paths(recipe_dylib_search_paths).unwrap())
178 // Provide the directory to libraries that are needed to run the *compiler* invoked
179 // by the recipe.
180 .env("HOST_RUSTC_DYLIB_PATH", &self.config.host_compile_lib_path)
181 // Provide the directory to libraries that might be needed to run binaries created
182 // by a compiler invoked by the recipe.
183 .env("TARGET_EXE_DYLIB_PATH", &self.config.target_run_lib_path)
184 // Provide the target.
185 .env("TARGET", &self.config.target)
186 // Some tests unfortunately still need Python, so provide path to a Python interpreter.
187 .env("PYTHON", &self.config.python)
188 // Provide path to sources root.
189 .env("SOURCE_ROOT", &self.config.src_root)
190 // Path to the host build directory.
191 .env("BUILD_ROOT", &host_build_root)
192 // Provide path to stage-corresponding rustc.
193 .env("RUSTC", &self.config.rustc_path)
194 // Provide which LLVM components are available (e.g. which LLVM components are provided
195 // through a specific CI runner).
196 .env("LLVM_COMPONENTS", &self.config.llvm_components);
197
198 // The `run-make-cargo` and `build-std` suites need an in-tree `cargo`, `run-make` does not.
199 if matches!(self.config.suite, TestSuite::RunMakeCargo | TestSuite::BuildStd) {
200 cmd.env(
201 "CARGO",
202 self.config.cargo_path.as_ref().expect("cargo must be built and made available"),
203 );
204 }
205
206 if let Some(ref rustdoc) = self.config.rustdoc_path {
207 cmd.env("RUSTDOC", rustdoc);
208 }
209
210 if let Some(ref node) = self.config.nodejs {
211 cmd.env("NODE", node);
212 }
213
214 if let Some(ref linker) = self.config.target_linker {
215 cmd.env("RUSTC_LINKER", linker);
216 }
217
218 if let Some(ref clang) = self.config.run_clang_based_tests_with {
219 cmd.env("CLANG", clang);
220 }
221
222 if let Some(ref filecheck) = self.config.llvm_filecheck {
223 cmd.env("LLVM_FILECHECK", filecheck);
224 }
225
226 if let Some(ref llvm_bin_dir) = self.config.llvm_bin_dir {
227 cmd.env("LLVM_BIN_DIR", llvm_bin_dir);
228 }
229
230 if let Some(ref remote_test_client) = self.config.remote_test_client {
231 cmd.env("REMOTE_TEST_CLIENT", remote_test_client);
232 }
233
234 if let Some(runner) = &self.config.runner {
235 cmd.env("RUNNER", runner);
236 }
237
238 // Guard against externally-set env vars.
239 // Set env var to enable verbose output for successful commands.
240 // Only set when --verbose-run-make-subprocess-output is passed.
241 cmd.env_remove("__RMAKE_VERBOSE_SUBPROCESS_OUTPUT");
242 if self.config.verbose_run_make_subprocess_output {
243 cmd.env("__RMAKE_VERBOSE_SUBPROCESS_OUTPUT", "1");
244 }
245
246 cmd.env_remove("__RUSTC_DEBUG_ASSERTIONS_ENABLED");
247 if self.config.with_rustc_debug_assertions {
248 // Used for `run_make_support::env::rustc_debug_assertions_enabled`.
249 cmd.env("__RUSTC_DEBUG_ASSERTIONS_ENABLED", "1");
250 }
251
252 cmd.env_remove("__STD_DEBUG_ASSERTIONS_ENABLED");
253 if self.config.with_std_debug_assertions {
254 // Used for `run_make_support::env::std_debug_assertions_enabled`.
255 cmd.env("__STD_DEBUG_ASSERTIONS_ENABLED", "1");
256 }
257
258 cmd.env_remove("__STD_REMAP_DEBUGINFO_ENABLED");
259 if self.config.with_std_remap_debuginfo {
260 // Used for `run_make_support::env::std_remap_debuginfo_enabled`.
261 cmd.env("__STD_REMAP_DEBUGINFO_ENABLED", "1");
262 }
263
264 // Used for `run_make_support::env::jobs`.
265 cmd.env("__BOOTSTRAP_JOBS", self.config.jobs.to_string());
266
267 // We don't want RUSTFLAGS set from the outside to interfere with
268 // compiler flags set in the test cases:
269 cmd.env_remove("RUSTFLAGS");
270
271 // Use dynamic musl for tests because static doesn't allow creating dylibs
272 if self.config.host.contains("musl") {
273 cmd.env("RUSTFLAGS", "-Ctarget-feature=-crt-static").env("IS_MUSL_HOST", "1");
274 }
275
276 if self.config.bless {
277 // If we're running in `--bless` mode, set an environment variable to tell
278 // `run_make_support` to bless snapshot files instead of checking them.
279 //
280 // The value is this test's source directory, because the support code
281 // will need that path in order to bless the _original_ snapshot files,
282 // not the copies in `rmake_out`.
283 // (See <https://github.com/rust-lang/rust/issues/129038>.)
284 cmd.env("RUSTC_BLESS_TEST", &self.testpaths.file);
285 }
286
287 if self.config.target.contains("msvc") && !self.config.cc.is_empty() {
288 // We need to pass a path to `lib.exe`, so assume that `cc` is `cl.exe`
289 // and that `lib.exe` lives next to it.
290 let lib = Utf8Path::new(&self.config.cc).parent().unwrap().join("lib.exe");
291
292 // MSYS doesn't like passing flags of the form `/foo` as it thinks it's
293 // a path and instead passes `C:\msys64\foo`, so convert all
294 // `/`-arguments to MSVC here to `-` arguments.
295 let cflags = self
296 .config
297 .cflags
298 .split(' ')
299 .map(|s| s.replace("/", "-"))
300 .collect::<Vec<_>>()
301 .join(" ");
302 let cxxflags = self
303 .config
304 .cxxflags
305 .split(' ')
306 .map(|s| s.replace("/", "-"))
307 .collect::<Vec<_>>()
308 .join(" ");
309
310 cmd.env("IS_MSVC", "1")
311 .env("IS_WINDOWS", "1")
312 .env("MSVC_LIB", format!("'{}' -nologo", lib))
313 .env("MSVC_LIB_PATH", &lib)
314 // Note: we diverge from legacy run_make and don't lump `CC` the compiler and
315 // default flags together.
316 .env("CC_DEFAULT_FLAGS", &cflags)
317 .env("CC", &self.config.cc)
318 .env("CXX_DEFAULT_FLAGS", &cxxflags)
319 .env("CXX", &self.config.cxx);
320 } else {
321 cmd.env("CC_DEFAULT_FLAGS", &self.config.cflags)
322 .env("CC", &self.config.cc)
323 .env("CXX_DEFAULT_FLAGS", &self.config.cxxflags)
324 .env("CXX", &self.config.cxx)
325 .env("AR", &self.config.ar);
326
327 if self.config.target.contains("windows") {
328 cmd.env("IS_WINDOWS", "1");
329 }
330 }
331
332 let proc = disable_error_reporting(|| cmd.spawn().expect("failed to spawn `rmake`"));
333 let (Output { stdout, stderr, status }, truncated) = self.read2_abbreviated(proc);
334 let stdout = String::from_utf8_lossy(&stdout).into_owned();
335 let stderr = String::from_utf8_lossy(&stderr).into_owned();
336 // This conditions on `status.success()` so we don't print output twice on error.
337 // NOTE: this code is called from an executor thread, so it's hidden by default unless --no-capture is passed.
338 self.dump_output(status.success(), &cmd.get_program().to_string_lossy(), &stdout, &stderr);
339 if !status.success() {
340 let res = ProcRes { status, stdout, stderr, truncated, cmdline: format!("{:?}", cmd) };
341 self.fatal_proc_rec("rmake recipe failed to complete", &res);
342 }
343 }
344}
345
346/// Gets all of the `out` dirs in a given Cargo `build-dir/<profile>/build` dir.
347fn discover_out_dirs(dir: Utf8PathBuf) -> Vec<PathBuf> {
348 let read_dir = |path: &Path| path.read_dir().ok().into_iter().flatten().filter_map(Result::ok);
349 let contents = dir
350 .read_dir()
351 .unwrap_or_else(|e| panic!("Couldn't read {}: {}", dir, e))
352 .map(|e| e.unwrap())
353 .flat_map(|e| read_dir(&e.path()))
354 .flat_map(|e| read_dir(&e.path()))
355 .map(|e| e.path())
356 .filter(|path| path.ends_with("out"))
357 .collect::<Vec<_>>();
358
359 return contents;
360}