rustc_interface/
util.rs

1use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
2use std::path::{Path, PathBuf};
3use std::sync::atomic::{AtomicBool, Ordering};
4use std::sync::{Arc, OnceLock};
5use std::{env, iter, thread};
6
7use rustc_ast as ast;
8use rustc_codegen_ssa::traits::CodegenBackend;
9use rustc_data_structures::jobserver::Proxy;
10use rustc_data_structures::sync;
11use rustc_metadata::{DylibError, load_symbol_from_dylib};
12use rustc_middle::ty::CurrentGcx;
13use rustc_parse::validate_attr;
14use rustc_session::config::{Cfg, OutFileName, OutputFilenames, OutputTypes, host_tuple};
15use rustc_session::filesearch::sysroot_candidates;
16use rustc_session::lint::{self, BuiltinLintDiag, LintBuffer};
17use rustc_session::output::{CRATE_TYPES, categorize_crate_type};
18use rustc_session::{EarlyDiagCtxt, Session, filesearch};
19use rustc_span::edit_distance::find_best_match_for_name;
20use rustc_span::edition::Edition;
21use rustc_span::source_map::SourceMapInputs;
22use rustc_span::{SessionGlobals, Symbol, sym};
23use rustc_target::spec::Target;
24use tracing::info;
25
26use crate::errors;
27
28/// Function pointer type that constructs a new CodegenBackend.
29type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
30
31/// Adds `target_feature = "..."` cfgs for a variety of platform
32/// specific features (SSE, NEON etc.).
33///
34/// This is performed by checking whether a set of permitted features
35/// is available on the target machine, by querying the codegen backend.
36pub(crate) fn add_configuration(
37    cfg: &mut Cfg,
38    sess: &mut Session,
39    codegen_backend: &dyn CodegenBackend,
40) {
41    let tf = sym::target_feature;
42    let tf_cfg = codegen_backend.target_config(sess);
43
44    sess.unstable_target_features.extend(tf_cfg.unstable_target_features.iter().copied());
45    sess.target_features.extend(tf_cfg.target_features.iter().copied());
46
47    cfg.extend(tf_cfg.target_features.into_iter().map(|feat| (tf, Some(feat))));
48
49    if tf_cfg.has_reliable_f16 {
50        cfg.insert((sym::target_has_reliable_f16, None));
51    }
52    if tf_cfg.has_reliable_f16_math {
53        cfg.insert((sym::target_has_reliable_f16_math, None));
54    }
55    if tf_cfg.has_reliable_f128 {
56        cfg.insert((sym::target_has_reliable_f128, None));
57    }
58    if tf_cfg.has_reliable_f128_math {
59        cfg.insert((sym::target_has_reliable_f128_math, None));
60    }
61
62    if sess.crt_static(None) {
63        cfg.insert((tf, Some(sym::crt_dash_static)));
64    }
65}
66
67/// Ensures that all target features required by the ABI are present.
68/// Must be called after `unstable_target_features` has been populated!
69pub(crate) fn check_abi_required_features(sess: &Session) {
70    let abi_feature_constraints = sess.target.abi_required_features();
71    // We check this against `unstable_target_features` as that is conveniently already
72    // back-translated to rustc feature names, taking into account `-Ctarget-cpu` and `-Ctarget-feature`.
73    // Just double-check that the features we care about are actually on our list.
74    for feature in
75        abi_feature_constraints.required.iter().chain(abi_feature_constraints.incompatible.iter())
76    {
77        assert!(
78            sess.target.rust_target_features().iter().any(|(name, ..)| feature == name),
79            "target feature {feature} is required/incompatible for the current ABI but not a recognized feature for this target"
80        );
81    }
82
83    for feature in abi_feature_constraints.required {
84        if !sess.unstable_target_features.contains(&Symbol::intern(feature)) {
85            sess.dcx().emit_warn(errors::AbiRequiredTargetFeature { feature, enabled: "enabled" });
86        }
87    }
88    for feature in abi_feature_constraints.incompatible {
89        if sess.unstable_target_features.contains(&Symbol::intern(feature)) {
90            sess.dcx().emit_warn(errors::AbiRequiredTargetFeature { feature, enabled: "disabled" });
91        }
92    }
93}
94
95pub static STACK_SIZE: OnceLock<usize> = OnceLock::new();
96pub const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;
97
98fn init_stack_size(early_dcx: &EarlyDiagCtxt) -> usize {
99    // Obey the environment setting or default
100    *STACK_SIZE.get_or_init(|| {
101        env::var_os("RUST_MIN_STACK")
102            .as_ref()
103            .map(|os_str| os_str.to_string_lossy())
104            // if someone finds out `export RUST_MIN_STACK=640000` isn't enough stack
105            // they might try to "unset" it by running `RUST_MIN_STACK=  rustc code.rs`
106            // this is wrong, but std would nonetheless "do what they mean", so let's do likewise
107            .filter(|s| !s.trim().is_empty())
108            // rustc is a batch program, so error early on inputs which are unlikely to be intended
109            // so no one thinks we parsed them setting `RUST_MIN_STACK="64 megabytes"`
110            // FIXME: we could accept `RUST_MIN_STACK=64MB`, perhaps?
111            .map(|s| {
112                let s = s.trim();
113                // FIXME(workingjubilee): add proper diagnostics when we factor out "pre-run" setup
114                #[allow(rustc::untranslatable_diagnostic, rustc::diagnostic_outside_of_impl)]
115                s.parse::<usize>().unwrap_or_else(|_| {
116                    let mut err = early_dcx.early_struct_fatal(format!(
117                        r#"`RUST_MIN_STACK` should be a number of bytes, but was "{s}""#,
118                    ));
119                    err.note("you can also unset `RUST_MIN_STACK` to use the default stack size");
120                    err.emit()
121                })
122            })
123            // otherwise pick a consistent default
124            .unwrap_or(DEFAULT_STACK_SIZE)
125    })
126}
127
128fn run_in_thread_with_globals<F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send, R: Send>(
129    thread_stack_size: usize,
130    edition: Edition,
131    sm_inputs: SourceMapInputs,
132    extra_symbols: &[&'static str],
133    f: F,
134) -> R {
135    // The "thread pool" is a single spawned thread in the non-parallel
136    // compiler. We run on a spawned thread instead of the main thread (a) to
137    // provide control over the stack size, and (b) to increase similarity with
138    // the parallel compiler, in particular to ensure there is no accidental
139    // sharing of data between the main thread and the compilation thread
140    // (which might cause problems for the parallel compiler).
141    let builder = thread::Builder::new().name("rustc".to_string()).stack_size(thread_stack_size);
142
143    // We build the session globals and run `f` on the spawned thread, because
144    // `SessionGlobals` does not impl `Send` in the non-parallel compiler.
145    thread::scope(|s| {
146        // `unwrap` is ok here because `spawn_scoped` only panics if the thread
147        // name contains null bytes.
148        let r = builder
149            .spawn_scoped(s, move || {
150                rustc_span::create_session_globals_then(
151                    edition,
152                    extra_symbols,
153                    Some(sm_inputs),
154                    || f(CurrentGcx::new(), Proxy::new()),
155                )
156            })
157            .unwrap()
158            .join();
159
160        match r {
161            Ok(v) => v,
162            Err(e) => std::panic::resume_unwind(e),
163        }
164    })
165}
166
167pub(crate) fn run_in_thread_pool_with_globals<
168    F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send,
169    R: Send,
170>(
171    thread_builder_diag: &EarlyDiagCtxt,
172    edition: Edition,
173    threads: usize,
174    extra_symbols: &[&'static str],
175    sm_inputs: SourceMapInputs,
176    f: F,
177) -> R {
178    use std::process;
179
180    use rustc_data_structures::defer;
181    use rustc_data_structures::sync::FromDyn;
182    use rustc_middle::ty::tls;
183    use rustc_query_impl::QueryCtxt;
184    use rustc_query_system::query::{QueryContext, break_query_cycles};
185
186    let thread_stack_size = init_stack_size(thread_builder_diag);
187
188    let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());
189
190    if !sync::is_dyn_thread_safe() {
191        return run_in_thread_with_globals(
192            thread_stack_size,
193            edition,
194            sm_inputs,
195            extra_symbols,
196            |current_gcx, jobserver_proxy| {
197                // Register the thread for use with the `WorkerLocal` type.
198                registry.register();
199
200                f(current_gcx, jobserver_proxy)
201            },
202        );
203    }
204
205    let current_gcx = FromDyn::from(CurrentGcx::new());
206    let current_gcx2 = current_gcx.clone();
207
208    let proxy = Proxy::new();
209
210    let proxy_ = Arc::clone(&proxy);
211    let proxy__ = Arc::clone(&proxy);
212    let builder = rayon_core::ThreadPoolBuilder::new()
213        .thread_name(|_| "rustc".to_string())
214        .acquire_thread_handler(move || proxy_.acquire_thread())
215        .release_thread_handler(move || proxy__.release_thread())
216        .num_threads(threads)
217        .deadlock_handler(move || {
218            // On deadlock, creates a new thread and forwards information in thread
219            // locals to it. The new thread runs the deadlock handler.
220
221            let current_gcx2 = current_gcx2.clone();
222            let registry = rayon_core::Registry::current();
223            let session_globals = rustc_span::with_session_globals(|session_globals| {
224                session_globals as *const SessionGlobals as usize
225            });
226            thread::Builder::new()
227                .name("rustc query cycle handler".to_string())
228                .spawn(move || {
229                    let on_panic = defer(|| {
230                        eprintln!("internal compiler error: query cycle handler thread panicked, aborting process");
231                        // We need to abort here as we failed to resolve the deadlock,
232                        // otherwise the compiler could just hang,
233                        process::abort();
234                    });
235
236                    // Get a `GlobalCtxt` reference from `CurrentGcx` as we cannot rely on having a
237                    // `TyCtxt` TLS reference here.
238                    current_gcx2.access(|gcx| {
239                        tls::enter_context(&tls::ImplicitCtxt::new(gcx), || {
240                            tls::with(|tcx| {
241                                // Accessing session globals is sound as they outlive `GlobalCtxt`.
242                                // They are needed to hash query keys containing spans or symbols.
243                                let query_map = rustc_span::set_session_globals_then(unsafe { &*(session_globals as *const SessionGlobals) }, || {
244                                    // Ensure there was no errors collecting all active jobs.
245                                    // We need the complete map to ensure we find a cycle to break.
246                                    QueryCtxt::new(tcx).collect_active_jobs().ok().expect("failed to collect active queries in deadlock handler")
247                                });
248                                break_query_cycles(query_map, &registry);
249                            })
250                        })
251                    });
252
253                    on_panic.disable();
254                })
255                .unwrap();
256        })
257        .stack_size(thread_stack_size);
258
259    // We create the session globals on the main thread, then create the thread
260    // pool. Upon creation, each worker thread created gets a copy of the
261    // session globals in TLS. This is possible because `SessionGlobals` impls
262    // `Send` in the parallel compiler.
263    rustc_span::create_session_globals_then(edition, extra_symbols, Some(sm_inputs), || {
264        rustc_span::with_session_globals(|session_globals| {
265            let session_globals = FromDyn::from(session_globals);
266            builder
267                .build_scoped(
268                    // Initialize each new worker thread when created.
269                    move |thread: rayon_core::ThreadBuilder| {
270                        // Register the thread for use with the `WorkerLocal` type.
271                        registry.register();
272
273                        rustc_span::set_session_globals_then(session_globals.into_inner(), || {
274                            thread.run()
275                        })
276                    },
277                    // Run `f` on the first thread in the thread pool.
278                    move |pool: &rayon_core::ThreadPool| {
279                        pool.install(|| f(current_gcx.into_inner(), proxy))
280                    },
281                )
282                .unwrap()
283        })
284    })
285}
286
287#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
288fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBackendFn {
289    match unsafe { load_symbol_from_dylib::<MakeBackendFn>(path, "__rustc_codegen_backend") } {
290        Ok(backend_sym) => backend_sym,
291        Err(DylibError::DlOpen(path, err)) => {
292            let err = format!("couldn't load codegen backend {path}{err}");
293            early_dcx.early_fatal(err);
294        }
295        Err(DylibError::DlSym(_path, err)) => {
296            let e = format!(
297                "`__rustc_codegen_backend` symbol lookup in the codegen backend failed{err}",
298            );
299            early_dcx.early_fatal(e);
300        }
301    }
302}
303
304/// Get the codegen backend based on the name and specified sysroot.
305///
306/// A name of `None` indicates that the default backend should be used.
307pub fn get_codegen_backend(
308    early_dcx: &EarlyDiagCtxt,
309    sysroot: &Path,
310    backend_name: Option<&str>,
311    target: &Target,
312) -> Box<dyn CodegenBackend> {
313    static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();
314
315    let load = LOAD.get_or_init(|| {
316        let backend = backend_name
317            .or(target.default_codegen_backend.as_deref())
318            .or(option_env!("CFG_DEFAULT_CODEGEN_BACKEND"))
319            .unwrap_or("llvm");
320
321        match backend {
322            filename if filename.contains('.') => {
323                load_backend_from_dylib(early_dcx, filename.as_ref())
324            }
325            #[cfg(feature = "llvm")]
326            "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
327            backend_name => get_codegen_sysroot(early_dcx, sysroot, backend_name),
328        }
329    });
330
331    // SAFETY: In case of a builtin codegen backend this is safe. In case of an external codegen
332    // backend we hope that the backend links against the same rustc_driver version. If this is not
333    // the case, we get UB.
334    unsafe { load() }
335}
336
337// This is used for rustdoc, but it uses similar machinery to codegen backend
338// loading, so we leave the code here. It is potentially useful for other tools
339// that want to invoke the rustc binary while linking to rustc as well.
340pub fn rustc_path<'a>() -> Option<&'a Path> {
341    static RUSTC_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
342
343    const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
344
345    RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_deref()
346}
347
348fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
349    sysroot_candidates().iter().find_map(|sysroot| {
350        let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") {
351            "rustc.exe"
352        } else {
353            "rustc"
354        });
355        candidate.exists().then_some(candidate)
356    })
357}
358
359#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
360fn get_codegen_sysroot(
361    early_dcx: &EarlyDiagCtxt,
362    sysroot: &Path,
363    backend_name: &str,
364) -> MakeBackendFn {
365    // For now we only allow this function to be called once as it'll dlopen a
366    // few things, which seems to work best if we only do that once. In
367    // general this assertion never trips due to the once guard in `get_codegen_backend`,
368    // but there's a few manual calls to this function in this file we protect
369    // against.
370    static LOADED: AtomicBool = AtomicBool::new(false);
371    assert!(
372        !LOADED.fetch_or(true, Ordering::SeqCst),
373        "cannot load the default codegen backend twice"
374    );
375
376    let target = host_tuple();
377    let sysroot_candidates = sysroot_candidates();
378
379    let sysroot = iter::once(sysroot)
380        .chain(sysroot_candidates.iter().map(<_>::as_ref))
381        .map(|sysroot| {
382            filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
383        })
384        .find(|f| {
385            info!("codegen backend candidate: {}", f.display());
386            f.exists()
387        })
388        .unwrap_or_else(|| {
389            let candidates = sysroot_candidates
390                .iter()
391                .map(|p| p.display().to_string())
392                .collect::<Vec<_>>()
393                .join("\n* ");
394            let err = format!(
395                "failed to find a `codegen-backends` folder \
396                           in the sysroot candidates:\n* {candidates}"
397            );
398            early_dcx.early_fatal(err);
399        });
400
401    info!("probing {} for a codegen backend", sysroot.display());
402
403    let d = sysroot.read_dir().unwrap_or_else(|e| {
404        let err = format!(
405            "failed to load default codegen backend, couldn't \
406                           read `{}`: {}",
407            sysroot.display(),
408            e
409        );
410        early_dcx.early_fatal(err);
411    });
412
413    let mut file: Option<PathBuf> = None;
414
415    let expected_names = &[
416        format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
417        format!("rustc_codegen_{backend_name}"),
418    ];
419    for entry in d.filter_map(|e| e.ok()) {
420        let path = entry.path();
421        let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { continue };
422        if !(filename.starts_with(DLL_PREFIX) && filename.ends_with(DLL_SUFFIX)) {
423            continue;
424        }
425        let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
426        if !expected_names.iter().any(|expected| expected == name) {
427            continue;
428        }
429        if let Some(ref prev) = file {
430            let err = format!(
431                "duplicate codegen backends found\n\
432                               first:  {}\n\
433                               second: {}\n\
434            ",
435                prev.display(),
436                path.display()
437            );
438            early_dcx.early_fatal(err);
439        }
440        file = Some(path.clone());
441    }
442
443    match file {
444        Some(ref s) => load_backend_from_dylib(early_dcx, s),
445        None => {
446            let err = format!("unsupported builtin codegen backend `{backend_name}`");
447            early_dcx.early_fatal(err);
448        }
449    }
450}
451
452pub(crate) fn check_attr_crate_type(
453    sess: &Session,
454    attrs: &[ast::Attribute],
455    lint_buffer: &mut LintBuffer,
456) {
457    // Unconditionally collect crate types from attributes to make them used
458    for a in attrs.iter() {
459        if a.has_name(sym::crate_type) {
460            if let Some(n) = a.value_str() {
461                if categorize_crate_type(n).is_some() {
462                    return;
463                }
464
465                if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
466                    let span = spanned.span;
467                    let candidate = find_best_match_for_name(
468                        &CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
469                        n,
470                        None,
471                    );
472                    lint_buffer.buffer_lint(
473                        lint::builtin::UNKNOWN_CRATE_TYPES,
474                        ast::CRATE_NODE_ID,
475                        span,
476                        BuiltinLintDiag::UnknownCrateTypes { span, candidate },
477                    );
478                }
479            } else {
480                // This is here mainly to check for using a macro, such as
481                // `#![crate_type = foo!()]`. That is not supported since the
482                // crate type needs to be known very early in compilation long
483                // before expansion. Otherwise, validation would normally be
484                // caught during semantic analysis via `TyCtxt::check_mod_attrs`,
485                // but by the time that runs the macro is expanded, and it doesn't
486                // give an error.
487                validate_attr::emit_fatal_malformed_builtin_attribute(
488                    &sess.psess,
489                    a,
490                    sym::crate_type,
491                );
492            }
493        }
494    }
495}
496
497fn multiple_output_types_to_stdout(
498    output_types: &OutputTypes,
499    single_output_file_is_stdout: bool,
500) -> bool {
501    use std::io::IsTerminal;
502    if std::io::stdout().is_terminal() {
503        // If stdout is a tty, check if multiple text output types are
504        // specified by `--emit foo=- --emit bar=-` or `-o - --emit foo,bar`
505        let named_text_types = output_types
506            .iter()
507            .filter(|(f, o)| f.is_text_output() && *o == &Some(OutFileName::Stdout))
508            .count();
509        let unnamed_text_types =
510            output_types.iter().filter(|(f, o)| f.is_text_output() && o.is_none()).count();
511        named_text_types > 1 || unnamed_text_types > 1 && single_output_file_is_stdout
512    } else {
513        // Otherwise, all the output types should be checked
514        let named_types =
515            output_types.values().filter(|o| *o == &Some(OutFileName::Stdout)).count();
516        let unnamed_types = output_types.values().filter(|o| o.is_none()).count();
517        named_types > 1 || unnamed_types > 1 && single_output_file_is_stdout
518    }
519}
520
521pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames {
522    if multiple_output_types_to_stdout(
523        &sess.opts.output_types,
524        sess.io.output_file == Some(OutFileName::Stdout),
525    ) {
526        sess.dcx().emit_fatal(errors::MultipleOutputTypesToStdout);
527    }
528
529    let crate_name = sess
530        .opts
531        .crate_name
532        .clone()
533        .or_else(|| rustc_attr_parsing::find_crate_name(attrs).map(|n| n.to_string()));
534
535    match sess.io.output_file {
536        None => {
537            // "-" as input file will cause the parser to read from stdin so we
538            // have to make up a name
539            // We want to toss everything after the final '.'
540            let dirpath = sess.io.output_dir.clone().unwrap_or_default();
541
542            // If a crate name is present, we use it as the link name
543            let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
544
545            OutputFilenames::new(
546                dirpath,
547                crate_name.unwrap_or_else(|| stem.replace('-', "_")),
548                stem,
549                None,
550                sess.io.temps_dir.clone(),
551                sess.opts.cg.extra_filename.clone(),
552                sess.opts.output_types.clone(),
553            )
554        }
555
556        Some(ref out_file) => {
557            let unnamed_output_types =
558                sess.opts.output_types.values().filter(|a| a.is_none()).count();
559            let ofile = if unnamed_output_types > 1 {
560                sess.dcx().emit_warn(errors::MultipleOutputTypesAdaption);
561                None
562            } else {
563                if !sess.opts.cg.extra_filename.is_empty() {
564                    sess.dcx().emit_warn(errors::IgnoringExtraFilename);
565                }
566                Some(out_file.clone())
567            };
568            if sess.io.output_dir != None {
569                sess.dcx().emit_warn(errors::IgnoringOutDir);
570            }
571
572            let out_filestem =
573                out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
574            OutputFilenames::new(
575                out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
576                crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")),
577                out_filestem,
578                ofile,
579                sess.io.temps_dir.clone(),
580                sess.opts.cg.extra_filename.clone(),
581                sess.opts.output_types.clone(),
582            )
583        }
584    }
585}
586
587/// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)" when invoked by an in-tree tool.
588pub macro version_str() {
589    option_env!("CFG_VERSION")
590}
591
592/// Returns the version string for `rustc` itself (which may be different from a tool version).
593pub fn rustc_version_str() -> Option<&'static str> {
594    version_str!()
595}