diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-10-31 12:35:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-31 12:35:55 +0100 |
| commit | 2da55600bc3d7830ee7d3e0567d60bd6c680eb11 (patch) | |
| tree | b996b2fd00fb02462648e39c368db0c0994eaaed /src | |
| parent | 4d296eabe4c5cfbce9bb68e6221bca2165aae97b (diff) | |
| parent | adb6d4752fd00093d44e0285cfff28406dfb7229 (diff) | |
| download | rust-2da55600bc3d7830ee7d3e0567d60bd6c680eb11.tar.gz rust-2da55600bc3d7830ee7d3e0567d60bd6c680eb11.zip | |
Rollup merge of #130693 - jieyouxu:minicore, r=bjorn3
Add `minicore` test auxiliary and support `//@ add-core-stubs` directive in ui/assembly/codegen tests
Context: [Real cross-compiling tests instead of `#![no_core]` silliness #130375](https://github.com/rust-lang/rust/issues/130375)
MCP: https://github.com/rust-lang/compiler-team/issues/786
Tracking issue: https://github.com/rust-lang/rust/issues/131485
This prototype PR is subject to further changes based on feedback.
### New `minicore` test auxiliary and `//@ add-core-stubs` compiletest directive
This PR introduces a prototype implementation of a `minicore` auxiliary test helper that provides `core` stubs for `#![no_core]` ui/assembly/codegen tests that need to build but not run on both the host platform and the cross-compiled target platform.
Key summary:
- `tests/auxiliary/minicore.rs` contains stub definitions of `core` items intended for consumption by `check-pass`/`build-pass` tests that want the typical prelude items like `Copy` to be stubbed out under `#![no_core]` scenarios, so that the test can be built (not run) for cross-compiled target platforms. Such tests don't want nor need full `-Z build-std` (e.g. `tests/ui/abi/compatibility.rs`).
- `minicore` is intended for `core` items **only**, not `std`- or `alloc`-exclusive items. If stubs for `alloc` or `std` are wanted, they should be provided by an additional directive and test auxiliary, and not be conflated with `minicore` or `core` stubs. This is because a wider range of tests can benefit from `core`-only stubs.
### Implementation
- The `minicore` auxiliary is a single source file `tests/auxiliary/minicore.rs`.
- The path to `minicore` is made avaiable from bootstrap to compiletest via the `--minicore-path` compiletest flag.
- `minicore` is then built on-demand via the `//@ add-core-stubs` compiletest directive, for each test revision for the given target platform (this distinction is important for when host platform != target platform in cross-compilation scenario).
- `minicore` is then made available to the test as an [extern prelude].
[extern prelude]: https://doc.rust-lang.org/reference/names/preludes.html#extern-prelude
### Example usage
```rs
// tests/ui/abi/my-abi-test.rs
//@ check-pass
//@ add-core-stubs
//@ compile-flags: --target i686-unknown-linux-gnu
//@ needs-llvm-components: x86
#![feature(no_core, lang_items)]
#![no_std]
#![no_core]
#![allow(unused, internal_features)]
extern crate minicore;
use minicore::*;
#[lang = "clone"]
pub trait Clone: Sized { // `Sized` is provided by `minicore`
fn clone(&self) -> Self;
}
```
### Implementation steps
- [x] 1. Add an initial `minicore` test auxiliary.
- [x] 2. Build `minicore` in bootstrap.
- [x] 3. Setup a `--minicore-path` compiletest cli flag and pass `minicore` build artifact path from bootstrap to compiletest.
- [x] 4. Assert `add-core-stubs` is mutually incompatible with tests that require to be `run`, as the stubs are only good for tests that only need to be built (i.e. no `run-{pass,fail}`).
- [x] 5. Add some self-tests to sanity check the behavior.
- [x] 6. Ensure that `tests/auxiliary/minicore.rs` is input stamped, i.e. modifying `tests/auxiliary/minicore.rs` should invalidate test cache and force the test to be rerun.
### Known limitations
- The current `minicore` is very minimal, because this PR is intended to focus on supporting the test infrastructure first. Further stubs could be added in follow-up PRs and/or on a as-needed basis.
try-job: aarch64-apple
try-job: armhf-gnu
try-job: x86_64-msvc
try-job: test-various
try-job: dist-various-1
Diffstat (limited to 'src')
| -rw-r--r-- | src/bootstrap/src/core/build_steps/test.rs | 5 | ||||
| -rw-r--r-- | src/tools/compiletest/src/common.rs | 5 | ||||
| -rw-r--r-- | src/tools/compiletest/src/directive-list.rs | 1 | ||||
| -rw-r--r-- | src/tools/compiletest/src/header.rs | 28 | ||||
| -rw-r--r-- | src/tools/compiletest/src/header/tests.rs | 1 | ||||
| -rw-r--r-- | src/tools/compiletest/src/lib.rs | 13 | ||||
| -rw-r--r-- | src/tools/compiletest/src/runtest.rs | 78 | ||||
| -rw-r--r-- | src/tools/compiletest/src/runtest/assembly.rs | 32 |
8 files changed, 131 insertions, 32 deletions
diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 2ad1b39a87c..0a290a697e6 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1725,6 +1725,11 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cmd.arg("--run-lib-path").arg(builder.sysroot_libdir(compiler, target)); cmd.arg("--rustc-path").arg(builder.rustc(compiler)); + // Minicore auxiliary lib for `no_core` tests that need `core` stubs in cross-compilation + // scenarios. + cmd.arg("--minicore-path") + .arg(builder.src.join("tests").join("auxiliary").join("minicore.rs")); + let is_rustdoc = suite.ends_with("rustdoc-ui") || suite.ends_with("rustdoc-js"); if mode == "run-make" { diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index e82b88eef79..e4f2f95a91b 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -392,6 +392,11 @@ pub struct Config { /// Command for visual diff display, e.g. `diff-tool --color=always`. pub diff_command: Option<String>, + + /// Path to minicore aux library, used for `no_core` tests that need `core` stubs in + /// cross-compilation scenarios that do not otherwise want/need to `-Zbuild-std`. Used in e.g. + /// ABI tests. + pub minicore_path: PathBuf, } impl Config { diff --git a/src/tools/compiletest/src/directive-list.rs b/src/tools/compiletest/src/directive-list.rs index 4d57907f26f..980b3f6829a 100644 --- a/src/tools/compiletest/src/directive-list.rs +++ b/src/tools/compiletest/src/directive-list.rs @@ -3,6 +3,7 @@ /// a best-effort approximation for diagnostics. Add new headers to this list when needed. const KNOWN_DIRECTIVE_NAMES: &[&str] = &[ // tidy-alphabetical-start + "add-core-stubs", "assembly-output", "aux-bin", "aux-build", diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index bfcdd747eb4..300a03e5f33 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -198,6 +198,9 @@ pub struct TestProps { pub no_auto_check_cfg: bool, /// Run tests which require enzyme being build pub has_enzyme: bool, + /// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios + /// that don't otherwise want/need `-Z build-std`. + pub add_core_stubs: bool, } mod directives { @@ -243,6 +246,7 @@ mod directives { pub const LLVM_COV_FLAGS: &'static str = "llvm-cov-flags"; pub const FILECHECK_FLAGS: &'static str = "filecheck-flags"; pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg"; + pub const ADD_CORE_STUBS: &'static str = "add-core-stubs"; // This isn't a real directive, just one that is probably mistyped often pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags"; } @@ -300,6 +304,7 @@ impl TestProps { filecheck_flags: vec![], no_auto_check_cfg: false, has_enzyme: false, + add_core_stubs: false, } } @@ -564,6 +569,8 @@ impl TestProps { } config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut self.no_auto_check_cfg); + + self.update_add_core_stubs(ln, config); }, ); @@ -677,6 +684,27 @@ impl TestProps { pub fn local_pass_mode(&self) -> Option<PassMode> { self.pass_mode } + + pub fn update_add_core_stubs(&mut self, ln: &str, config: &Config) { + let add_core_stubs = config.parse_name_directive(ln, directives::ADD_CORE_STUBS); + if add_core_stubs { + if !matches!(config.mode, Mode::Ui | Mode::Codegen | Mode::Assembly) { + panic!( + "`add-core-stubs` is currently only supported for ui, codegen and assembly test modes" + ); + } + + // FIXME(jieyouxu): this check is currently order-dependent, but we should probably + // collect all directives in one go then perform a validation pass after that. + if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) { + // `minicore` can only be used with non-run modes, because it's `core` prelude stubs + // and can't run. + panic!("`add-core-stubs` cannot be used to run the test binary"); + } + + self.add_core_stubs = add_core_stubs; + } + } } /// If the given line begins with the appropriate comment prefix for a directive, diff --git a/src/tools/compiletest/src/header/tests.rs b/src/tools/compiletest/src/header/tests.rs index 2e6effcab98..0e735dc77c4 100644 --- a/src/tools/compiletest/src/header/tests.rs +++ b/src/tools/compiletest/src/header/tests.rs @@ -152,6 +152,7 @@ impl ConfigBuilder { "--git-repository=", "--nightly-branch=", "--git-merge-commit-email=", + "--minicore-path=", ]; let mut args: Vec<String> = args.iter().map(ToString::to_string).collect(); diff --git a/src/tools/compiletest/src/lib.rs b/src/tools/compiletest/src/lib.rs index ccf8057bf5c..5c06a39c477 100644 --- a/src/tools/compiletest/src/lib.rs +++ b/src/tools/compiletest/src/lib.rs @@ -181,7 +181,8 @@ pub fn parse_config(args: Vec<String>) -> Config { "compiletest-diff-tool", "What custom diff tool to use for displaying compiletest tests.", "COMMAND", - ); + ) + .reqopt("", "minicore-path", "path to minicore aux library", "PATH"); let (argv0, args_) = args.split_first().unwrap(); if args.len() == 1 || args[1] == "-h" || args[1] == "--help" { @@ -371,7 +372,10 @@ pub fn parse_config(args: Vec<String>) -> Config { git_merge_commit_email: matches.opt_str("git-merge-commit-email").unwrap(), profiler_runtime: matches.opt_present("profiler-runtime"), + diff_command: matches.opt_str("compiletest-diff-tool"), + + minicore_path: opt_path(matches, "minicore-path"), } } @@ -409,6 +413,7 @@ pub fn log_config(config: &Config) { logv(c, format!("host-linker: {:?}", config.host_linker)); logv(c, format!("verbose: {}", config.verbose)); logv(c, format!("format: {:?}", config.format)); + logv(c, format!("minicore_path: {:?}", config.minicore_path.display())); logv(c, "\n".to_string()); } @@ -885,6 +890,12 @@ fn files_related_to_test( related.push(path); } + // `minicore.rs` test auxiliary: we need to make sure tests get rerun if this changes. + // + // FIXME(jieyouxu): untangle these paths, we should provide both a path to root `tests/` or + // `tests/auxiliary/` and the test suite in question. `src_base` is also a terrible name. + related.push(config.src_base.parent().unwrap().join("auxiliary").join("minicore.rs")); + related } diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index a8a71c196fc..b337458f943 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1150,14 +1150,20 @@ impl<'test> TestCx<'test> { } } - /// `root_testpaths` refers to the path of the original test. - /// the auxiliary and the test with an aux-build have the same `root_testpaths`. + /// `root_testpaths` refers to the path of the original test. the auxiliary and the test with an + /// aux-build have the same `root_testpaths`. fn compose_and_run_compiler( &self, mut rustc: Command, input: Option<String>, root_testpaths: &TestPaths, ) -> ProcRes { + if self.props.add_core_stubs { + let minicore_path = self.build_minicore(); + rustc.arg("--extern"); + rustc.arg(&format!("minicore={}", minicore_path.to_str().unwrap())); + } + let aux_dir = self.aux_output_dir(); self.build_all_auxiliary(root_testpaths, &aux_dir, &mut rustc); @@ -1171,6 +1177,37 @@ impl<'test> TestCx<'test> { ) } + /// Builds `minicore`. Returns the path to the minicore rlib within the base test output + /// directory. + fn build_minicore(&self) -> PathBuf { + let output_file_path = self.output_base_dir().join("libminicore.rlib"); + let mut rustc = self.make_compile_args( + &self.config.minicore_path, + TargetLocation::ThisFile(output_file_path.clone()), + Emit::None, + AllowUnused::Yes, + LinkToAux::No, + vec![], + ); + + rustc.args(&["--crate-type", "rlib"]); + rustc.arg("-Cpanic=abort"); + + let res = + self.compose_and_run(rustc, self.config.compile_lib_path.to_str().unwrap(), None, None); + if !res.status.success() { + self.fatal_proc_rec( + &format!( + "auxiliary build of {:?} failed to compile: ", + self.config.minicore_path.display() + ), + &res, + ); + } + + output_file_path + } + /// Builds an aux dependency. fn build_auxiliary( &self, @@ -1662,6 +1699,15 @@ impl<'test> TestCx<'test> { rustc.args(&self.props.compile_flags); + // FIXME(jieyouxu): we should report a fatal error or warning if user wrote `-Cpanic=` with + // something that's not `abort`, however, by moving this last we should override previous + // `-Cpanic=`s + // + // `minicore` requires `#![no_std]` and `#![no_core]`, which means no unwinding panics. + if self.props.add_core_stubs { + rustc.arg("-Cpanic=abort"); + } + rustc } @@ -1848,34 +1894,6 @@ impl<'test> TestCx<'test> { (proc_res, output_path) } - fn compile_test_and_save_assembly(&self) -> (ProcRes, PathBuf) { - // This works with both `--emit asm` (as default output name for the assembly) - // and `ptx-linker` because the latter can write output at requested location. - let output_path = self.output_base_name().with_extension("s"); - let input_file = &self.testpaths.file; - - // Use the `//@ assembly-output:` directive to determine how to emit assembly. - let emit = match self.props.assembly_output.as_deref() { - Some("emit-asm") => Emit::Asm, - Some("bpf-linker") => Emit::LinkArgsAsm, - Some("ptx-linker") => Emit::None, // No extra flags needed. - Some(other) => self.fatal(&format!("unknown 'assembly-output' directive: {other}")), - None => self.fatal("missing 'assembly-output' directive"), - }; - - let rustc = self.make_compile_args( - input_file, - TargetLocation::ThisFile(output_path.clone()), - emit, - AllowUnused::No, - LinkToAux::Yes, - Vec::new(), - ); - - let proc_res = self.compose_and_run_compiler(rustc, None, self.testpaths); - (proc_res, output_path) - } - fn verify_with_filecheck(&self, output: &Path) -> ProcRes { let mut filecheck = Command::new(self.config.llvm_filecheck.as_ref().unwrap()); filecheck.arg("--input-file").arg(output).arg(&self.testpaths.file); diff --git a/src/tools/compiletest/src/runtest/assembly.rs b/src/tools/compiletest/src/runtest/assembly.rs index 430a5534da1..89d7de58c20 100644 --- a/src/tools/compiletest/src/runtest/assembly.rs +++ b/src/tools/compiletest/src/runtest/assembly.rs @@ -1,4 +1,6 @@ -use super::TestCx; +use std::path::PathBuf; + +use super::{AllowUnused, Emit, LinkToAux, ProcRes, TargetLocation, TestCx}; impl TestCx<'_> { pub(super) fn run_assembly_test(&self) { @@ -16,4 +18,32 @@ impl TestCx<'_> { self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res); } } + + fn compile_test_and_save_assembly(&self) -> (ProcRes, PathBuf) { + // This works with both `--emit asm` (as default output name for the assembly) + // and `ptx-linker` because the latter can write output at requested location. + let output_path = self.output_base_name().with_extension("s"); + let input_file = &self.testpaths.file; + + // Use the `//@ assembly-output:` directive to determine how to emit assembly. + let emit = match self.props.assembly_output.as_deref() { + Some("emit-asm") => Emit::Asm, + Some("bpf-linker") => Emit::LinkArgsAsm, + Some("ptx-linker") => Emit::None, // No extra flags needed. + Some(other) => self.fatal(&format!("unknown 'assembly-output' directive: {other}")), + None => self.fatal("missing 'assembly-output' directive"), + }; + + let rustc = self.make_compile_args( + input_file, + TargetLocation::ThisFile(output_path.clone()), + emit, + AllowUnused::No, + LinkToAux::Yes, + Vec::new(), + ); + + let proc_res = self.compose_and_run_compiler(rustc, None, self.testpaths); + (proc_res, output_path) + } } |
