Skip to main content

compiletest/
cli.rs

1//! Isolates the APIs used by `bin/main.rs`, to help minimize the surface area
2//! of public exports from the compiletest library crate.
3
4use std::env;
5use std::io::IsTerminal;
6use std::sync::{Arc, OnceLock};
7
8use camino::{Utf8Path, Utf8PathBuf};
9use clap::Parser;
10
11use crate::common::{CodegenBackend, CompareMode, Config, ForcePassMode, TestMode, TestSuite};
12use crate::edition::Edition;
13use crate::{debuggers, directives, early_config_check, run_tests};
14
15pub fn main() {
16    tracing_subscriber::fmt::init();
17
18    // colored checks stdout by default, but for some reason only stderr is a terminal.
19    // compiletest *does* print many things to stdout, but it doesn't really matter.
20    if std::io::stderr().is_terminal()
21        && matches!(std::env::var("NO_COLOR").as_deref(), Err(_) | Ok("0"))
22    {
23        colored::control::set_override(true);
24    }
25
26    let config = Arc::new(parse_config(env::args().collect()));
27
28    early_config_check(&config);
29
30    run_tests(config);
31}
32
33/// Compiletest command-line arguments.
34#[derive(clap::Parser)]
35struct Args {
36    // Required options
37    /// Path to host shared libraries.
38    #[arg(long)]
39    compile_lib_path: Utf8PathBuf,
40    /// Path to target shared libraries.
41    #[arg(long)]
42    run_lib_path: Utf8PathBuf,
43    /// Path to rustc to use for compiling.
44    #[arg(long)]
45    rustc_path: Utf8PathBuf,
46    /// Path to python to use for doc tests.
47    #[arg(long)]
48    python: String,
49    /// Directory containing sources.
50    #[arg(long)]
51    src_root: Utf8PathBuf,
52    /// Directory containing test suite sources.
53    #[arg(long)]
54    src_test_suite_root: Utf8PathBuf,
55    /// Path to root build directory.
56    #[arg(long)]
57    build_root: Utf8PathBuf,
58    /// Path to test suite specific build directory.
59    #[arg(long)]
60    build_test_suite_root: Utf8PathBuf,
61    /// Directory containing the compiler sysroot.
62    #[arg(long)]
63    sysroot_base: Utf8PathBuf,
64    /// Stage number under test.
65    #[arg(long)]
66    stage: u32,
67    /// The target-stage identifier.
68    #[arg(long)]
69    stage_id: String,
70    /// Which sort of compile tests to run.
71    #[arg(long)]
72    mode: TestMode,
73    /// Which suite of compile tests to run.
74    #[arg(long)]
75    suite: TestSuite,
76    /// Path to a C compiler.
77    #[arg(long)]
78    cc: String,
79    /// Path to a C++ compiler.
80    #[arg(long)]
81    cxx: String,
82    /// Flags for the C compiler.
83    #[arg(long, allow_hyphen_values = true)]
84    cflags: String,
85    /// Flags for the CXX compiler.
86    #[arg(long, allow_hyphen_values = true)]
87    cxxflags: String,
88    /// List of LLVM components built in.
89    #[arg(long)]
90    llvm_components: String,
91    /// Current Rust channel.
92    #[arg(long)]
93    channel: String,
94    /// Name of the git branch for nightly.
95    #[arg(long)]
96    nightly_branch: String,
97    /// Email address used for finding merge commits.
98    #[arg(long)]
99    git_merge_commit_email: String,
100    /// Path to minicore aux library.
101    #[arg(long)]
102    minicore_path: Utf8PathBuf,
103    /// Number of parallel jobs bootstrap was configured with.
104    #[arg(long)]
105    jobs: u32,
106    /// The host to build for.
107    #[arg(long)]
108    host: String,
109    /// The target to build for.
110    #[arg(long)]
111    target: String,
112
113    // Optional options
114    /// Path to cargo to use for compiling.
115    #[arg(long)]
116    cargo_path: Option<Utf8PathBuf>,
117    /// Path to rustc to use for compiling run-make recipes.
118    #[arg(long)]
119    stage0_rustc_path: Option<Utf8PathBuf>,
120    /// Path to rustc to use for querying target information.
121    #[arg(long)]
122    query_rustc_path: Option<Utf8PathBuf>,
123    /// Path to rustdoc to use for compiling.
124    #[arg(long)]
125    rustdoc_path: Option<Utf8PathBuf>,
126    /// Path to coverage-dump to use in tests.
127    #[arg(long)]
128    coverage_dump_path: Option<Utf8PathBuf>,
129    /// Path to jsondocck to use for doc tests.
130    #[arg(long)]
131    jsondocck_path: Option<Utf8PathBuf>,
132    /// Path to jsondoclint to use for doc tests.
133    #[arg(long)]
134    jsondoclint_path: Option<Utf8PathBuf>,
135    /// Path to Clang executable.
136    #[arg(long)]
137    run_clang_based_tests_with: Option<Utf8PathBuf>,
138    /// Path to LLVM's FileCheck binary.
139    #[arg(long)]
140    llvm_filecheck: Option<Utf8PathBuf>,
141    /// Path to LLVM's bin directory.
142    #[arg(long)]
143    llvm_bin_dir: Option<Utf8PathBuf>,
144    /// The name of nodejs.
145    #[arg(long)]
146    nodejs: Option<Utf8PathBuf>,
147    /// The name of npm.
148    #[arg(long)]
149    npm: Option<Utf8PathBuf>,
150    /// Path to the remote test client.
151    #[arg(long)]
152    remote_test_client: Option<Utf8PathBuf>,
153    /// Path to CDB to use for CDB debuginfo tests.
154    #[arg(long)]
155    cdb: Option<Utf8PathBuf>,
156    /// Path to GDB to use for GDB debuginfo tests.
157    #[arg(long)]
158    gdb: Option<Utf8PathBuf>,
159    /// Path to LLDB to use for LLDB debuginfo tests.
160    #[arg(long)]
161    lldb: Option<Utf8PathBuf>,
162    /// The version of LLDB used.
163    #[arg(long)]
164    lldb_version: Option<String>,
165    /// The version of LLVM used.
166    #[arg(long)]
167    llvm_version: Option<String>,
168    /// Android NDK standalone path.
169    #[arg(long)]
170    android_cross_path: Option<Utf8PathBuf>,
171    /// Path to the android debugger.
172    #[arg(long)]
173    adb_path: Option<Utf8PathBuf>,
174    /// Path to tests for the android debugger.
175    #[arg(long)]
176    adb_test_dir: Option<Utf8PathBuf>,
177    /// Path to an archiver.
178    #[arg(long, default_value = "ar")]
179    ar: String,
180    /// Path to a linker for the target.
181    #[arg(long)]
182    target_linker: Option<String>,
183    /// Path to a linker for the host.
184    #[arg(long)]
185    host_linker: Option<String>,
186    /// Force {check,build,run}-pass tests to this mode.
187    #[arg(long)]
188    pass: Option<ForcePassMode>,
189    /// Whether to execute run-* tests.
190    #[arg(long)]
191    run: Option<String>,
192    /// Supervisor program to run tests under (eg. emulator, valgrind).
193    #[arg(long)]
194    runner: Option<String>,
195    /// Mode describing what file the actual ui output will be compared to.
196    #[arg(long)]
197    compare_mode: Option<CompareMode>,
198    /// Default Rust edition.
199    #[arg(long)]
200    edition: Option<Edition>,
201    /// The codegen backend currently used.
202    #[arg(long)]
203    default_codegen_backend: Option<CodegenBackend>,
204    /// The codegen backend to use instead of the default one.
205    #[arg(long)]
206    override_codegen_backend: Option<String>,
207    /// Custom diff tool to use for displaying compiletest tests.
208    #[arg(long)]
209    compiletest_diff_tool: Option<String>,
210    /// Number of parallel threads to use for the frontend when building test artifacts
211    #[arg(long)]
212    parallel_frontend_threads: Option<u32>,
213    /// Number of times to execute each test.
214    #[arg(long)]
215    iteration_count: Option<u32>,
216
217    // Flags
218    /// Overwrite stderr/stdout files instead of complaining about a mismatch.
219    #[arg(long)]
220    bless: bool,
221    /// Stop as soon as possible after any test fails.
222    #[arg(long)]
223    fail_fast: bool,
224    /// Run tests marked as ignored.
225    #[arg(long)]
226    ignored: bool,
227    /// Run tests that require enzyme.
228    #[arg(long)]
229    has_enzyme: bool,
230    /// Run tests that require offload.
231    #[arg(long)]
232    has_offload: bool,
233    /// Whether rustc was built with debug assertions.
234    #[arg(long)]
235    with_rustc_debug_assertions: bool,
236    /// Whether std was built with debug assertions.
237    #[arg(long)]
238    with_std_debug_assertions: bool,
239    /// Whether std was built with remapping.
240    #[arg(long)]
241    with_std_remap_debuginfo: bool,
242    /// Filters match exactly.
243    #[arg(long)]
244    exact: bool,
245    /// Set this when rustc/stdlib were compiled with randomized layouts.
246    #[arg(long)]
247    rust_randomized_layout: bool,
248    /// Run tests with optimizations enabled.
249    #[arg(long)]
250    optimize_tests: bool,
251    /// Run tests verbosely, showing all output.
252    #[arg(long)]
253    verbose: bool,
254    /// Show verbose subprocess output for successful run-make tests.
255    #[arg(long)]
256    verbose_run_make_subprocess_output: bool,
257    /// Is LLVM the system LLVM.
258    #[arg(long)]
259    system_llvm: bool,
260    /// Rerun tests even if the inputs are unchanged.
261    #[arg(long)]
262    force_rerun: bool,
263    /// Only run tests that result been modified.
264    #[arg(long)]
265    only_modified: bool,
266    // Backcompat option
267    #[arg(long, hide = true)]
268    nocapture: bool,
269    /// Don't capture stdout/stderr of tests.
270    #[arg(long)]
271    no_capture: bool,
272    /// Is the profiler runtime enabled for this target.
273    #[arg(long)]
274    profiler_runtime: bool,
275    /// Run tests which rely on commit version being compiled into the binaries.
276    #[arg(long)]
277    git_hash: bool,
278    /// Enable this to generate a Rustfix coverage file.
279    #[arg(long)]
280    rustfix_coverage: bool,
281    /// Ignore `//@ ignore-backends` directives.
282    #[arg(long)]
283    bypass_ignore_backends: bool,
284
285    // These values can be entered multiple times, for example:
286    // --skip foo --skip bar
287    /// Skip tests matching SUBSTRING.
288    #[arg(long)]
289    skip: Vec<String>,
290    /// Flags to pass to rustc for host.
291    #[arg(long, allow_hyphen_values = true)]
292    host_rustcflags: Vec<String>,
293    /// Flags to pass to rustc for target.
294    #[arg(long, allow_hyphen_values = true)]
295    target_rustcflags: Vec<String>,
296
297    // Positional arguments
298    /// Test name filters.
299    /// All leftover arguments will be stored in this list.
300    filters: Vec<String>,
301}
302
303pub(crate) fn parse_config(args: Vec<String>) -> Config {
304    let args = Args::parse_from(args);
305
306    fn make_absolute(path: Utf8PathBuf) -> Utf8PathBuf {
307        if path.is_relative() {
308            Utf8PathBuf::try_from(env::current_dir().unwrap()).unwrap().join(path)
309        } else {
310            path
311        }
312    }
313
314    if args.nocapture {
315        panic!("`--nocapture` is deprecated; please use `--no-capture`");
316    }
317
318    let adb_device_status = args.target.contains("android") && args.adb_test_dir.is_some();
319
320    // FIXME: `cdb_version` is *derived* from cdb, but it's *not* technically a config!
321    let cdb_version = args.cdb.as_deref().and_then(debuggers::query_cdb_version);
322    // FIXME: `gdb_version` is *derived* from gdb, but it's *not* technically a config!
323    let gdb_version = args.gdb.as_deref().and_then(debuggers::query_gdb_version);
324    // FIXME: `lldb_version` is *derived* from lldb, but it's *not* technically a config!
325    let lldb_version = args.lldb_version.as_deref().and_then(debuggers::extract_lldb_version);
326    // FIXME: this is very questionable, we really should be obtaining LLVM version info from
327    // `bootstrap`, and not trying to be figuring out that in `compiletest` by running the
328    // `FileCheck` binary.
329    let llvm_version =
330        args.llvm_version.as_deref().map(directives::extract_llvm_version).or_else(|| {
331            directives::extract_llvm_version_from_binary(args.llvm_filecheck.as_ref()?.as_str())
332        });
333
334    let default_codegen_backend = args.default_codegen_backend.unwrap_or(CodegenBackend::Llvm);
335
336    let mode = args.mode;
337    let filters = if mode == TestMode::RunMake {
338        args.filters
339            .iter()
340            .map(|f| {
341                // Here `f` is relative to `./tests/run-make`. So if you run
342                //
343                //   ./x test tests/run-make/crate-loading
344                //
345                //  then `f` is "crate-loading".
346                let path = Utf8Path::new(f);
347                let mut iter = path.iter().skip(1);
348
349                if iter.next().is_some_and(|s| s == "rmake.rs") && iter.next().is_none() {
350                    // Strip the "rmake.rs" suffix. For example, if `f` is
351                    // "crate-loading/rmake.rs" then this gives us "crate-loading".
352                    path.parent().unwrap().to_string()
353                } else {
354                    f.to_string()
355                }
356            })
357            .collect::<Vec<_>>()
358    } else {
359        // Note that the filters are relative to the root dir of the different test
360        // suites. For example, with:
361        //
362        //   ./x test tests/ui/lint/unused
363        //
364        // the filter is "lint/unused".
365        args.filters.clone()
366    };
367
368    let compare_mode = args.compare_mode;
369
370    let src_root = args.src_root;
371    let src_test_suite_root = args.src_test_suite_root;
372    assert!(
373        src_test_suite_root.starts_with(&src_root),
374        "`src-root` must be a parent of `src-test-suite-root`: `src-root`=`{}`, `src-test-suite-root` = `{}`",
375        src_root,
376        src_test_suite_root
377    );
378
379    let build_root = args.build_root;
380    let build_test_suite_root = args.build_test_suite_root;
381    assert!(build_test_suite_root.starts_with(&build_root));
382
383    let parallel_frontend_threads =
384        args.parallel_frontend_threads.unwrap_or(Config::DEFAULT_PARALLEL_FRONTEND_THREADS);
385    let iteration_count = args.iteration_count.unwrap_or(Config::DEFAULT_ITERATION_COUNT);
386    assert!(iteration_count > 0, "`--iteration-count` must be a positive integer");
387
388    Config {
389        bless: args.bless,
390        fail_fast: args.fail_fast || env::var_os("RUSTC_TEST_FAIL_FAST").is_some(),
391
392        host_compile_lib_path: make_absolute(args.compile_lib_path),
393        target_run_lib_path: make_absolute(args.run_lib_path),
394        rustc_path: args.rustc_path,
395        cargo_path: args.cargo_path,
396        stage0_rustc_path: args.stage0_rustc_path,
397        query_rustc_path: args.query_rustc_path,
398        rustdoc_path: args.rustdoc_path,
399        coverage_dump_path: args.coverage_dump_path,
400        python: args.python,
401        jsondocck_path: args.jsondocck_path,
402        jsondoclint_path: args.jsondoclint_path,
403        run_clang_based_tests_with: args.run_clang_based_tests_with,
404        llvm_filecheck: args.llvm_filecheck,
405        llvm_bin_dir: args.llvm_bin_dir,
406
407        src_root,
408        src_test_suite_root,
409
410        build_root,
411        build_test_suite_root,
412
413        sysroot_base: args.sysroot_base,
414
415        stage: args.stage,
416        stage_id: args.stage_id,
417
418        mode,
419        suite: args.suite,
420        run_ignored: args.ignored,
421        with_rustc_debug_assertions: args.with_rustc_debug_assertions,
422        with_std_debug_assertions: args.with_std_debug_assertions,
423        with_std_remap_debuginfo: args.with_std_remap_debuginfo,
424        filters,
425        skip: args.skip,
426        filter_exact: args.exact,
427        force_pass_mode: args.pass,
428        // FIXME: this run scheme is... confusing.
429        run: args.run.and_then(|mode| match mode.as_str() {
430            "auto" => None,
431            "always" => Some(true),
432            "never" => Some(false),
433            _ => panic!("unknown `--run` option `{}` given", mode),
434        }),
435        runner: args.runner,
436        host_rustcflags: args.host_rustcflags,
437        target_rustcflags: args.target_rustcflags,
438        optimize_tests: args.optimize_tests,
439        rust_randomized_layout: args.rust_randomized_layout,
440        target: args.target,
441        host: args.host,
442        cdb: args.cdb,
443        cdb_version,
444        gdb: args.gdb,
445        gdb_version,
446        lldb: args.lldb,
447        lldb_version,
448        llvm_version,
449        system_llvm: args.system_llvm,
450        android_cross_path: args.android_cross_path,
451        adb_path: args.adb_path,
452        adb_test_dir: args.adb_test_dir,
453        adb_device_status,
454        verbose: args.verbose,
455        verbose_run_make_subprocess_output: args.verbose_run_make_subprocess_output,
456        only_modified: args.only_modified,
457        remote_test_client: args.remote_test_client,
458        compare_mode,
459        rustfix_coverage: args.rustfix_coverage,
460        has_enzyme: args.has_enzyme,
461        has_offload: args.has_offload,
462        channel: args.channel,
463        git_hash: args.git_hash,
464        edition: args.edition,
465
466        cc: args.cc,
467        cxx: args.cxx,
468        cflags: args.cflags,
469        cxxflags: args.cxxflags,
470        ar: args.ar,
471        target_linker: args.target_linker,
472        host_linker: args.host_linker,
473        llvm_components: args.llvm_components,
474        nodejs: args.nodejs,
475
476        force_rerun: args.force_rerun,
477
478        target_cfgs: OnceLock::new(),
479        builtin_cfg_names: OnceLock::new(),
480        supported_crate_types: OnceLock::new(),
481
482        capture: !args.no_capture,
483
484        nightly_branch: args.nightly_branch,
485        git_merge_commit_email: args.git_merge_commit_email,
486
487        profiler_runtime: args.profiler_runtime,
488
489        diff_command: args.compiletest_diff_tool,
490
491        minicore_path: args.minicore_path,
492
493        default_codegen_backend,
494        override_codegen_backend: args.override_codegen_backend,
495        bypass_ignore_backends: args.bypass_ignore_backends,
496
497        jobs: args.jobs,
498
499        parallel_frontend_threads,
500        iteration_count,
501    }
502}