]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_interface/src/util.rs
Auto merge of #106474 - erikdesjardins:noalias, r=bjorn3
[rust.git] / compiler / rustc_interface / src / util.rs
1 use info;
2 use libloading::Library;
3 use rustc_ast as ast;
4 use rustc_codegen_ssa::traits::CodegenBackend;
5 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6 use rustc_errors::registry::Registry;
7 use rustc_parse::validate_attr;
8 use rustc_session as session;
9 use rustc_session::config::CheckCfg;
10 use rustc_session::config::{self, CrateType};
11 use rustc_session::config::{ErrorOutputType, Input, OutputFilenames};
12 use rustc_session::filesearch::sysroot_candidates;
13 use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
14 use rustc_session::parse::CrateConfig;
15 use rustc_session::{early_error, filesearch, output, Session};
16 use rustc_span::edition::Edition;
17 use rustc_span::lev_distance::find_best_match_for_name;
18 use rustc_span::source_map::FileLoader;
19 use rustc_span::symbol::{sym, Symbol};
20 use std::env;
21 use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
22 use std::mem;
23 use std::path::{Path, PathBuf};
24 use std::sync::atomic::{AtomicBool, Ordering};
25 use std::sync::OnceLock;
26 use std::thread;
27
28 /// Function pointer type that constructs a new CodegenBackend.
29 pub type 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 LLVM.
36 pub fn add_configuration(
37     cfg: &mut CrateConfig,
38     sess: &mut Session,
39     codegen_backend: &dyn CodegenBackend,
40 ) {
41     let tf = sym::target_feature;
42
43     let unstable_target_features = codegen_backend.target_features(sess, true);
44     sess.unstable_target_features.extend(unstable_target_features.iter().cloned());
45
46     let target_features = codegen_backend.target_features(sess, false);
47     sess.target_features.extend(target_features.iter().cloned());
48
49     cfg.extend(target_features.into_iter().map(|feat| (tf, Some(feat))));
50
51     if sess.crt_static(None) {
52         cfg.insert((tf, Some(sym::crt_dash_static)));
53     }
54 }
55
56 pub fn create_session(
57     sopts: config::Options,
58     cfg: FxHashSet<(String, Option<String>)>,
59     check_cfg: CheckCfg,
60     file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
61     input_path: Option<PathBuf>,
62     lint_caps: FxHashMap<lint::LintId, lint::Level>,
63     make_codegen_backend: Option<
64         Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
65     >,
66     descriptions: Registry,
67 ) -> (Session, Box<dyn CodegenBackend>) {
68     let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
69         make_codegen_backend(&sopts)
70     } else {
71         get_codegen_backend(&sopts.maybe_sysroot, sopts.unstable_opts.codegen_backend.as_deref())
72     };
73
74     // target_override is documented to be called before init(), so this is okay
75     let target_override = codegen_backend.target_override(&sopts);
76
77     let bundle = match rustc_errors::fluent_bundle(
78         sopts.maybe_sysroot.clone(),
79         sysroot_candidates().to_vec(),
80         sopts.unstable_opts.translate_lang.clone(),
81         sopts.unstable_opts.translate_additional_ftl.as_deref(),
82         sopts.unstable_opts.translate_directionality_markers,
83     ) {
84         Ok(bundle) => bundle,
85         Err(e) => {
86             early_error(sopts.error_format, &format!("failed to load fluent bundle: {e}"));
87         }
88     };
89
90     let mut sess = session::build_session(
91         sopts,
92         input_path,
93         bundle,
94         descriptions,
95         lint_caps,
96         file_loader,
97         target_override,
98     );
99
100     codegen_backend.init(&sess);
101
102     let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg));
103     add_configuration(&mut cfg, &mut sess, &*codegen_backend);
104
105     let mut check_cfg = config::to_crate_check_config(check_cfg);
106     check_cfg.fill_well_known();
107
108     sess.parse_sess.config = cfg;
109     sess.parse_sess.check_config = check_cfg;
110
111     (sess, codegen_backend)
112 }
113
114 const STACK_SIZE: usize = 8 * 1024 * 1024;
115
116 fn get_stack_size() -> Option<usize> {
117     // FIXME: Hacks on hacks. If the env is trying to override the stack size
118     // then *don't* set it explicitly.
119     env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
120 }
121
122 #[cfg(not(parallel_compiler))]
123 pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
124     edition: Edition,
125     _threads: usize,
126     f: F,
127 ) -> R {
128     // The "thread pool" is a single spawned thread in the non-parallel
129     // compiler. We run on a spawned thread instead of the main thread (a) to
130     // provide control over the stack size, and (b) to increase similarity with
131     // the parallel compiler, in particular to ensure there is no accidental
132     // sharing of data between the main thread and the compilation thread
133     // (which might cause problems for the parallel compiler).
134     let mut builder = thread::Builder::new().name("rustc".to_string());
135     if let Some(size) = get_stack_size() {
136         builder = builder.stack_size(size);
137     }
138
139     // We build the session globals and run `f` on the spawned thread, because
140     // `SessionGlobals` does not impl `Send` in the non-parallel compiler.
141     thread::scope(|s| {
142         // `unwrap` is ok here because `spawn_scoped` only panics if the thread
143         // name contains null bytes.
144         let r = builder
145             .spawn_scoped(s, move || rustc_span::create_session_globals_then(edition, f))
146             .unwrap()
147             .join();
148
149         match r {
150             Ok(v) => v,
151             Err(e) => std::panic::resume_unwind(e),
152         }
153     })
154 }
155
156 #[cfg(parallel_compiler)]
157 pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
158     edition: Edition,
159     threads: usize,
160     f: F,
161 ) -> R {
162     use rustc_data_structures::jobserver;
163     use rustc_middle::ty::tls;
164     use rustc_query_impl::{deadlock, QueryContext, QueryCtxt};
165
166     let mut builder = rayon::ThreadPoolBuilder::new()
167         .thread_name(|_| "rustc".to_string())
168         .acquire_thread_handler(jobserver::acquire_thread)
169         .release_thread_handler(jobserver::release_thread)
170         .num_threads(threads)
171         .deadlock_handler(|| {
172             // On deadlock, creates a new thread and forwards information in thread
173             // locals to it. The new thread runs the deadlock handler.
174             let query_map = tls::with(|tcx| {
175                 QueryCtxt::from_tcx(tcx)
176                     .try_collect_active_jobs()
177                     .expect("active jobs shouldn't be locked in deadlock handler")
178             });
179             let registry = rustc_rayon_core::Registry::current();
180             thread::spawn(move || deadlock(query_map, &registry));
181         });
182     if let Some(size) = get_stack_size() {
183         builder = builder.stack_size(size);
184     }
185
186     // We create the session globals on the main thread, then create the thread
187     // pool. Upon creation, each worker thread created gets a copy of the
188     // session globals in TLS. This is possible because `SessionGlobals` impls
189     // `Send` in the parallel compiler.
190     rustc_span::create_session_globals_then(edition, || {
191         rustc_span::with_session_globals(|session_globals| {
192             builder
193                 .build_scoped(
194                     // Initialize each new worker thread when created.
195                     move |thread: rayon::ThreadBuilder| {
196                         rustc_span::set_session_globals_then(session_globals, || thread.run())
197                     },
198                     // Run `f` on the first thread in the thread pool.
199                     move |pool: &rayon::ThreadPool| pool.install(f),
200                 )
201                 .unwrap()
202         })
203     })
204 }
205
206 fn load_backend_from_dylib(path: &Path) -> MakeBackendFn {
207     let lib = unsafe { Library::new(path) }.unwrap_or_else(|err| {
208         let err = format!("couldn't load codegen backend {path:?}: {err}");
209         early_error(ErrorOutputType::default(), &err);
210     });
211
212     let backend_sym = unsafe { lib.get::<MakeBackendFn>(b"__rustc_codegen_backend") }
213         .unwrap_or_else(|e| {
214             let err = format!("couldn't load codegen backend: {e}");
215             early_error(ErrorOutputType::default(), &err);
216         });
217
218     // Intentionally leak the dynamic library. We can't ever unload it
219     // since the library can make things that will live arbitrarily long.
220     let backend_sym = unsafe { backend_sym.into_raw() };
221     mem::forget(lib);
222
223     *backend_sym
224 }
225
226 /// Get the codegen backend based on the name and specified sysroot.
227 ///
228 /// A name of `None` indicates that the default backend should be used.
229 pub fn get_codegen_backend(
230     maybe_sysroot: &Option<PathBuf>,
231     backend_name: Option<&str>,
232 ) -> Box<dyn CodegenBackend> {
233     static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();
234
235     let load = LOAD.get_or_init(|| {
236         let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm");
237
238         match backend_name.unwrap_or(default_codegen_backend) {
239             filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()),
240             #[cfg(feature = "llvm")]
241             "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
242             backend_name => get_codegen_sysroot(maybe_sysroot, backend_name),
243         }
244     });
245
246     // SAFETY: In case of a builtin codegen backend this is safe. In case of an external codegen
247     // backend we hope that the backend links against the same rustc_driver version. If this is not
248     // the case, we get UB.
249     unsafe { load() }
250 }
251
252 // This is used for rustdoc, but it uses similar machinery to codegen backend
253 // loading, so we leave the code here. It is potentially useful for other tools
254 // that want to invoke the rustc binary while linking to rustc as well.
255 pub fn rustc_path<'a>() -> Option<&'a Path> {
256     static RUSTC_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
257
258     const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
259
260     RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_deref()
261 }
262
263 fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
264     sysroot_candidates().iter().find_map(|sysroot| {
265         let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") {
266             "rustc.exe"
267         } else {
268             "rustc"
269         });
270         candidate.exists().then_some(candidate)
271     })
272 }
273
274 fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> MakeBackendFn {
275     // For now we only allow this function to be called once as it'll dlopen a
276     // few things, which seems to work best if we only do that once. In
277     // general this assertion never trips due to the once guard in `get_codegen_backend`,
278     // but there's a few manual calls to this function in this file we protect
279     // against.
280     static LOADED: AtomicBool = AtomicBool::new(false);
281     assert!(
282         !LOADED.fetch_or(true, Ordering::SeqCst),
283         "cannot load the default codegen backend twice"
284     );
285
286     let target = session::config::host_triple();
287     let sysroot_candidates = sysroot_candidates();
288
289     let sysroot = maybe_sysroot
290         .iter()
291         .chain(sysroot_candidates.iter())
292         .map(|sysroot| {
293             filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
294         })
295         .find(|f| {
296             info!("codegen backend candidate: {}", f.display());
297             f.exists()
298         });
299     let sysroot = sysroot.unwrap_or_else(|| {
300         let candidates = sysroot_candidates
301             .iter()
302             .map(|p| p.display().to_string())
303             .collect::<Vec<_>>()
304             .join("\n* ");
305         let err = format!(
306             "failed to find a `codegen-backends` folder \
307                            in the sysroot candidates:\n* {candidates}"
308         );
309         early_error(ErrorOutputType::default(), &err);
310     });
311     info!("probing {} for a codegen backend", sysroot.display());
312
313     let d = sysroot.read_dir().unwrap_or_else(|e| {
314         let err = format!(
315             "failed to load default codegen backend, couldn't \
316                            read `{}`: {}",
317             sysroot.display(),
318             e
319         );
320         early_error(ErrorOutputType::default(), &err);
321     });
322
323     let mut file: Option<PathBuf> = None;
324
325     let expected_names = &[
326         format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
327         format!("rustc_codegen_{backend_name}"),
328     ];
329     for entry in d.filter_map(|e| e.ok()) {
330         let path = entry.path();
331         let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { continue };
332         if !(filename.starts_with(DLL_PREFIX) && filename.ends_with(DLL_SUFFIX)) {
333             continue;
334         }
335         let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
336         if !expected_names.iter().any(|expected| expected == name) {
337             continue;
338         }
339         if let Some(ref prev) = file {
340             let err = format!(
341                 "duplicate codegen backends found\n\
342                                first:  {}\n\
343                                second: {}\n\
344             ",
345                 prev.display(),
346                 path.display()
347             );
348             early_error(ErrorOutputType::default(), &err);
349         }
350         file = Some(path.clone());
351     }
352
353     match file {
354         Some(ref s) => load_backend_from_dylib(s),
355         None => {
356             let err = format!("unsupported builtin codegen backend `{backend_name}`");
357             early_error(ErrorOutputType::default(), &err);
358         }
359     }
360 }
361
362 pub(crate) fn check_attr_crate_type(
363     sess: &Session,
364     attrs: &[ast::Attribute],
365     lint_buffer: &mut LintBuffer,
366 ) {
367     // Unconditionally collect crate types from attributes to make them used
368     for a in attrs.iter() {
369         if a.has_name(sym::crate_type) {
370             if let Some(n) = a.value_str() {
371                 if categorize_crate_type(n).is_some() {
372                     return;
373                 }
374
375                 if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
376                     let span = spanned.span;
377                     let lev_candidate = find_best_match_for_name(
378                         &CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
379                         n,
380                         None,
381                     );
382                     if let Some(candidate) = lev_candidate {
383                         lint_buffer.buffer_lint_with_diagnostic(
384                             lint::builtin::UNKNOWN_CRATE_TYPES,
385                             ast::CRATE_NODE_ID,
386                             span,
387                             "invalid `crate_type` value",
388                             BuiltinLintDiagnostics::UnknownCrateTypes(
389                                 span,
390                                 "did you mean".to_string(),
391                                 format!("\"{candidate}\""),
392                             ),
393                         );
394                     } else {
395                         lint_buffer.buffer_lint(
396                             lint::builtin::UNKNOWN_CRATE_TYPES,
397                             ast::CRATE_NODE_ID,
398                             span,
399                             "invalid `crate_type` value",
400                         );
401                     }
402                 }
403             } else {
404                 // This is here mainly to check for using a macro, such as
405                 // #![crate_type = foo!()]. That is not supported since the
406                 // crate type needs to be known very early in compilation long
407                 // before expansion. Otherwise, validation would normally be
408                 // caught in AstValidator (via `check_builtin_attribute`), but
409                 // by the time that runs the macro is expanded, and it doesn't
410                 // give an error.
411                 validate_attr::emit_fatal_malformed_builtin_attribute(
412                     &sess.parse_sess,
413                     a,
414                     sym::crate_type,
415                 );
416             }
417         }
418     }
419 }
420
421 const CRATE_TYPES: &[(Symbol, CrateType)] = &[
422     (sym::rlib, CrateType::Rlib),
423     (sym::dylib, CrateType::Dylib),
424     (sym::cdylib, CrateType::Cdylib),
425     (sym::lib, config::default_lib_output()),
426     (sym::staticlib, CrateType::Staticlib),
427     (sym::proc_dash_macro, CrateType::ProcMacro),
428     (sym::bin, CrateType::Executable),
429 ];
430
431 fn categorize_crate_type(s: Symbol) -> Option<CrateType> {
432     Some(CRATE_TYPES.iter().find(|(key, _)| *key == s)?.1)
433 }
434
435 pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<CrateType> {
436     // Unconditionally collect crate types from attributes to make them used
437     let attr_types: Vec<CrateType> = attrs
438         .iter()
439         .filter_map(|a| {
440             if a.has_name(sym::crate_type) {
441                 match a.value_str() {
442                     Some(s) => categorize_crate_type(s),
443                     _ => None,
444                 }
445             } else {
446                 None
447             }
448         })
449         .collect();
450
451     // If we're generating a test executable, then ignore all other output
452     // styles at all other locations
453     if session.opts.test {
454         return vec![CrateType::Executable];
455     }
456
457     // Only check command line flags if present. If no types are specified by
458     // command line, then reuse the empty `base` Vec to hold the types that
459     // will be found in crate attributes.
460     // JUSTIFICATION: before wrapper fn is available
461     #[allow(rustc::bad_opt_access)]
462     let mut base = session.opts.crate_types.clone();
463     if base.is_empty() {
464         base.extend(attr_types);
465         if base.is_empty() {
466             base.push(output::default_output_for_target(session));
467         } else {
468             base.sort();
469             base.dedup();
470         }
471     }
472
473     base.retain(|crate_type| {
474         let res = !output::invalid_output_for_target(session, *crate_type);
475
476         if !res {
477             session.warn(&format!(
478                 "dropping unsupported crate type `{}` for target `{}`",
479                 *crate_type, session.opts.target_triple
480             ));
481         }
482
483         res
484     });
485
486     base
487 }
488
489 pub fn build_output_filenames(
490     input: &Input,
491     odir: &Option<PathBuf>,
492     ofile: &Option<PathBuf>,
493     temps_dir: &Option<PathBuf>,
494     attrs: &[ast::Attribute],
495     sess: &Session,
496 ) -> OutputFilenames {
497     match *ofile {
498         None => {
499             // "-" as input file will cause the parser to read from stdin so we
500             // have to make up a name
501             // We want to toss everything after the final '.'
502             let dirpath = (*odir).as_ref().cloned().unwrap_or_default();
503
504             // If a crate name is present, we use it as the link name
505             let stem = sess
506                 .opts
507                 .crate_name
508                 .clone()
509                 .or_else(|| rustc_attr::find_crate_name(sess, attrs).map(|n| n.to_string()))
510                 .unwrap_or_else(|| input.filestem().to_owned());
511
512             OutputFilenames::new(
513                 dirpath,
514                 stem,
515                 None,
516                 temps_dir.clone(),
517                 sess.opts.cg.extra_filename.clone(),
518                 sess.opts.output_types.clone(),
519             )
520         }
521
522         Some(ref out_file) => {
523             let unnamed_output_types =
524                 sess.opts.output_types.values().filter(|a| a.is_none()).count();
525             let ofile = if unnamed_output_types > 1 {
526                 sess.warn(
527                     "due to multiple output types requested, the explicitly specified \
528                      output file name will be adapted for each output type",
529                 );
530                 None
531             } else {
532                 if !sess.opts.cg.extra_filename.is_empty() {
533                     sess.warn("ignoring -C extra-filename flag due to -o flag");
534                 }
535                 Some(out_file.clone())
536             };
537             if *odir != None {
538                 sess.warn("ignoring --out-dir flag due to -o flag");
539             }
540
541             OutputFilenames::new(
542                 out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
543                 out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(),
544                 ofile,
545                 temps_dir.clone(),
546                 sess.opts.cg.extra_filename.clone(),
547                 sess.opts.output_types.clone(),
548             )
549         }
550     }
551 }
552
553 /// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)" when invoked by an in-tree tool.
554 pub macro version_str() {
555     option_env!("CFG_VERSION")
556 }
557
558 /// Returns the version string for `rustc` itself (which may be different from a tool version).
559 pub fn rustc_version_str() -> Option<&'static str> {
560     version_str!()
561 }