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    /// Build proc-macros for wasm. Assumes environment is configured to support this; e.g., std is
285    /// already built appropriately.
286    #[arg(long)]
287    wasm_proc_macros: bool,
288
289    // These values can be entered multiple times, for example:
290    // --skip foo --skip bar
291    /// Skip tests matching SUBSTRING.
292    #[arg(long)]
293    skip: Vec<String>,
294    /// Flags to pass to rustc for host.
295    #[arg(long, allow_hyphen_values = true)]
296    host_rustcflags: Vec<String>,
297    /// Flags to pass to rustc for target.
298    #[arg(long, allow_hyphen_values = true)]
299    target_rustcflags: Vec<String>,
300
301    // Positional arguments
302    /// Test name filters.
303    /// All leftover arguments will be stored in this list.
304    filters: Vec<String>,
305}
306
307pub(crate) fn parse_config(args: Vec<String>) -> Config {
308    let args = Args::parse_from(args);
309
310    fn make_absolute(path: Utf8PathBuf) -> Utf8PathBuf {
311        if path.is_relative() {
312            Utf8PathBuf::try_from(env::current_dir().unwrap()).unwrap().join(path)
313        } else {
314            path
315        }
316    }
317
318    if args.nocapture {
319        panic!("`--nocapture` is deprecated; please use `--no-capture`");
320    }
321
322    let adb_device_status = args.target.contains("android") && args.adb_test_dir.is_some();
323
324    // FIXME: `cdb_version` is *derived* from cdb, but it's *not* technically a config!
325    let cdb_version = args.cdb.as_deref().and_then(debuggers::query_cdb_version);
326    // FIXME: `gdb_version` is *derived* from gdb, but it's *not* technically a config!
327    let gdb_version = args.gdb.as_deref().and_then(debuggers::query_gdb_version);
328    // FIXME: `lldb_version` is *derived* from lldb, but it's *not* technically a config!
329    let lldb_version = args.lldb_version.as_deref().and_then(debuggers::extract_lldb_version);
330    // FIXME: this is very questionable, we really should be obtaining LLVM version info from
331    // `bootstrap`, and not trying to be figuring out that in `compiletest` by running the
332    // `FileCheck` binary.
333    let llvm_version =
334        args.llvm_version.as_deref().map(directives::extract_llvm_version).or_else(|| {
335            directives::extract_llvm_version_from_binary(args.llvm_filecheck.as_ref()?.as_str())
336        });
337
338    let default_codegen_backend = args.default_codegen_backend.unwrap_or(CodegenBackend::Llvm);
339
340    let mode = args.mode;
341    let filters = if mode == TestMode::RunMake {
342        args.filters
343            .iter()
344            .map(|f| {
345                // Here `f` is relative to `./tests/run-make`. So if you run
346                //
347                //   ./x test tests/run-make/crate-loading
348                //
349                //  then `f` is "crate-loading".
350                let path = Utf8Path::new(f);
351                let mut iter = path.iter().skip(1);
352
353                if iter.next().is_some_and(|s| s == "rmake.rs") && iter.next().is_none() {
354                    // Strip the "rmake.rs" suffix. For example, if `f` is
355                    // "crate-loading/rmake.rs" then this gives us "crate-loading".
356                    path.parent().unwrap().to_string()
357                } else {
358                    f.to_string()
359                }
360            })
361            .collect::<Vec<_>>()
362    } else {
363        // Note that the filters are relative to the root dir of the different test
364        // suites. For example, with:
365        //
366        //   ./x test tests/ui/lint/unused
367        //
368        // the filter is "lint/unused".
369        args.filters.clone()
370    };
371
372    let compare_mode = args.compare_mode;
373
374    let src_root = args.src_root;
375    let src_test_suite_root = args.src_test_suite_root;
376    assert!(
377        src_test_suite_root.starts_with(&src_root),
378        "`src-root` must be a parent of `src-test-suite-root`: `src-root`=`{}`, `src-test-suite-root` = `{}`",
379        src_root,
380        src_test_suite_root
381    );
382
383    let build_root = args.build_root;
384    let build_test_suite_root = args.build_test_suite_root;
385    assert!(build_test_suite_root.starts_with(&build_root));
386
387    let parallel_frontend_threads =
388        args.parallel_frontend_threads.unwrap_or(Config::DEFAULT_PARALLEL_FRONTEND_THREADS);
389    let iteration_count = args.iteration_count.unwrap_or(Config::DEFAULT_ITERATION_COUNT);
390    assert!(iteration_count > 0, "`--iteration-count` must be a positive integer");
391
392    let gcc_supported_target_tuples = match default_codegen_backend {
393        CodegenBackend::Gcc => {
394            directives::find_gcc_supported_targets(&args.sysroot_base, &args.host)
395        }
396        CodegenBackend::Llvm | CodegenBackend::Cranelift => vec![],
397    };
398
399    Config {
400        bless: args.bless,
401        fail_fast: args.fail_fast || env::var_os("RUSTC_TEST_FAIL_FAST").is_some(),
402
403        host_compile_lib_path: make_absolute(args.compile_lib_path),
404        target_run_lib_path: make_absolute(args.run_lib_path),
405        rustc_path: args.rustc_path,
406        cargo_path: args.cargo_path,
407        stage0_rustc_path: args.stage0_rustc_path,
408        query_rustc_path: args.query_rustc_path,
409        rustdoc_path: args.rustdoc_path,
410        coverage_dump_path: args.coverage_dump_path,
411        python: args.python,
412        jsondocck_path: args.jsondocck_path,
413        jsondoclint_path: args.jsondoclint_path,
414        run_clang_based_tests_with: args.run_clang_based_tests_with,
415        llvm_filecheck: args.llvm_filecheck,
416        llvm_bin_dir: args.llvm_bin_dir,
417
418        src_root,
419        src_test_suite_root,
420
421        build_root,
422        build_test_suite_root,
423
424        sysroot_base: args.sysroot_base,
425
426        stage: args.stage,
427        stage_id: args.stage_id,
428
429        mode,
430        suite: args.suite,
431        run_ignored: args.ignored,
432        with_rustc_debug_assertions: args.with_rustc_debug_assertions,
433        with_std_debug_assertions: args.with_std_debug_assertions,
434        with_std_remap_debuginfo: args.with_std_remap_debuginfo,
435        filters,
436        skip: args.skip,
437        filter_exact: args.exact,
438        force_pass_mode: args.pass,
439        // FIXME: this run scheme is... confusing.
440        run: args.run.and_then(|mode| match mode.as_str() {
441            "auto" => None,
442            "always" => Some(true),
443            "never" => Some(false),
444            _ => panic!("unknown `--run` option `{}` given", mode),
445        }),
446        runner: args.runner,
447        host_rustcflags: args.host_rustcflags,
448        target_rustcflags: args.target_rustcflags,
449        optimize_tests: args.optimize_tests,
450        rust_randomized_layout: args.rust_randomized_layout,
451        target: args.target,
452        host: args.host,
453        cdb: args.cdb,
454        cdb_version,
455        gdb: args.gdb,
456        gdb_version,
457        lldb: args.lldb,
458        lldb_version,
459        llvm_version,
460        system_llvm: args.system_llvm,
461        android_cross_path: args.android_cross_path,
462        adb_path: args.adb_path,
463        adb_test_dir: args.adb_test_dir,
464        adb_device_status,
465        verbose: args.verbose,
466        verbose_run_make_subprocess_output: args.verbose_run_make_subprocess_output,
467        only_modified: args.only_modified,
468        remote_test_client: args.remote_test_client,
469        compare_mode,
470        rustfix_coverage: args.rustfix_coverage,
471        has_enzyme: args.has_enzyme,
472        has_offload: args.has_offload,
473        channel: args.channel,
474        git_hash: args.git_hash,
475        edition: args.edition,
476
477        cc: args.cc,
478        cxx: args.cxx,
479        cflags: args.cflags,
480        cxxflags: args.cxxflags,
481        ar: args.ar,
482        target_linker: args.target_linker,
483        host_linker: args.host_linker,
484        llvm_components: args.llvm_components,
485        nodejs: args.nodejs,
486
487        force_rerun: args.force_rerun,
488
489        target_cfgs: OnceLock::new(),
490        builtin_cfg_names: OnceLock::new(),
491        supported_crate_types: OnceLock::new(),
492
493        capture: !args.no_capture,
494
495        nightly_branch: args.nightly_branch,
496        git_merge_commit_email: args.git_merge_commit_email,
497
498        profiler_runtime: args.profiler_runtime,
499
500        diff_command: args.compiletest_diff_tool,
501
502        minicore_path: args.minicore_path,
503
504        default_codegen_backend,
505        override_codegen_backend: args.override_codegen_backend,
506        bypass_ignore_backends: args.bypass_ignore_backends,
507
508        gcc_supported_target_tuples,
509
510        wasm_proc_macros: args.wasm_proc_macros,
511
512        jobs: args.jobs,
513
514        parallel_frontend_threads,
515        iteration_count,
516    }
517}