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