2 use libloading::Library;
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};
21 use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
23 use std::path::{Path, PathBuf};
24 use std::sync::atomic::{AtomicBool, Ordering};
25 use std::sync::OnceLock;
28 /// Function pointer type that constructs a new CodegenBackend.
29 pub type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
31 /// Adds `target_feature = "..."` cfgs for a variety of platform
32 /// specific features (SSE, NEON etc.).
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,
39 codegen_backend: &dyn CodegenBackend,
41 let tf = sym::target_feature;
43 let unstable_target_features = codegen_backend.target_features(sess, true);
44 sess.unstable_target_features.extend(unstable_target_features.iter().cloned());
46 let target_features = codegen_backend.target_features(sess, false);
47 sess.target_features.extend(target_features.iter().cloned());
49 cfg.extend(target_features.into_iter().map(|feat| (tf, Some(feat))));
51 if sess.crt_static(None) {
52 cfg.insert((tf, Some(sym::crt_dash_static)));
56 pub fn create_session(
57 sopts: config::Options,
58 cfg: FxHashSet<(String, Option<String>)>,
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>,
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)
71 get_codegen_backend(&sopts.maybe_sysroot, sopts.unstable_opts.codegen_backend.as_deref())
74 // target_override is documented to be called before init(), so this is okay
75 let target_override = codegen_backend.target_override(&sopts);
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,
86 early_error(sopts.error_format, &format!("failed to load fluent bundle: {e}"));
90 let mut sess = session::build_session(
100 codegen_backend.init(&sess);
102 let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg));
103 add_configuration(&mut cfg, &mut sess, &*codegen_backend);
105 let mut check_cfg = config::to_crate_check_config(check_cfg);
106 check_cfg.fill_well_known();
108 sess.parse_sess.config = cfg;
109 sess.parse_sess.check_config = check_cfg;
111 (sess, codegen_backend)
114 const STACK_SIZE: usize = 8 * 1024 * 1024;
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)
122 #[cfg(not(parallel_compiler))]
123 pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
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);
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.
142 // `unwrap` is ok here because `spawn_scoped` only panics if the thread
143 // name contains null bytes.
145 .spawn_scoped(s, move || rustc_span::create_session_globals_then(edition, f))
151 Err(e) => std::panic::resume_unwind(e),
156 #[cfg(parallel_compiler)]
157 pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
162 use rustc_data_structures::jobserver;
163 use rustc_middle::ty::tls;
164 use rustc_query_impl::{deadlock, QueryContext, QueryCtxt};
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")
179 let registry = rustc_rayon_core::Registry::current();
180 thread::spawn(move || deadlock(query_map, ®istry));
182 if let Some(size) = get_stack_size() {
183 builder = builder.stack_size(size);
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| {
194 // Initialize each new worker thread when created.
195 move |thread: rayon::ThreadBuilder| {
196 rustc_span::set_session_globals_then(session_globals, || thread.run())
198 // Run `f` on the first thread in the thread pool.
199 move |pool: &rayon::ThreadPool| pool.install(f),
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);
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);
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() };
226 /// Get the codegen backend based on the name and specified sysroot.
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();
235 let load = LOAD.get_or_init(|| {
236 let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm");
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),
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.
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();
258 const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
260 RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_deref()
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") {
270 candidate.exists().then_some(candidate)
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
280 static LOADED: AtomicBool = AtomicBool::new(false);
282 !LOADED.fetch_or(true, Ordering::SeqCst),
283 "cannot load the default codegen backend twice"
286 let target = session::config::host_triple();
287 let sysroot_candidates = sysroot_candidates();
289 let sysroot = maybe_sysroot
291 .chain(sysroot_candidates.iter())
293 filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
296 info!("codegen backend candidate: {}", f.display());
299 let sysroot = sysroot.unwrap_or_else(|| {
300 let candidates = sysroot_candidates
302 .map(|p| p.display().to_string())
306 "failed to find a `codegen-backends` folder \
307 in the sysroot candidates:\n* {candidates}"
309 early_error(ErrorOutputType::default(), &err);
311 info!("probing {} for a codegen backend", sysroot.display());
313 let d = sysroot.read_dir().unwrap_or_else(|e| {
315 "failed to load default codegen backend, couldn't \
320 early_error(ErrorOutputType::default(), &err);
323 let mut file: Option<PathBuf> = None;
325 let expected_names = &[
326 format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
327 format!("rustc_codegen_{backend_name}"),
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)) {
335 let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
336 if !expected_names.iter().any(|expected| expected == name) {
339 if let Some(ref prev) = file {
341 "duplicate codegen backends found\n\
348 early_error(ErrorOutputType::default(), &err);
350 file = Some(path.clone());
354 Some(ref s) => load_backend_from_dylib(s),
356 let err = format!("unsupported builtin codegen backend `{backend_name}`");
357 early_error(ErrorOutputType::default(), &err);
362 pub(crate) fn check_attr_crate_type(
364 attrs: &[ast::Attribute],
365 lint_buffer: &mut LintBuffer,
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() {
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<_>>(),
382 if let Some(candidate) = lev_candidate {
383 lint_buffer.buffer_lint_with_diagnostic(
384 lint::builtin::UNKNOWN_CRATE_TYPES,
387 "invalid `crate_type` value",
388 BuiltinLintDiagnostics::UnknownCrateTypes(
390 "did you mean".to_string(),
391 format!("\"{candidate}\""),
395 lint_buffer.buffer_lint(
396 lint::builtin::UNKNOWN_CRATE_TYPES,
399 "invalid `crate_type` value",
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
411 validate_attr::emit_fatal_malformed_builtin_attribute(
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),
431 fn categorize_crate_type(s: Symbol) -> Option<CrateType> {
432 Some(CRATE_TYPES.iter().find(|(key, _)| *key == s)?.1)
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
440 if a.has_name(sym::crate_type) {
441 match a.value_str() {
442 Some(s) => categorize_crate_type(s),
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];
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();
464 base.extend(attr_types);
466 base.push(output::default_output_for_target(session));
473 base.retain(|crate_type| {
474 let res = !output::invalid_output_for_target(session, *crate_type);
477 session.warn(&format!(
478 "dropping unsupported crate type `{}` for target `{}`",
479 *crate_type, session.opts.target_triple
489 pub fn build_output_filenames(
491 odir: &Option<PathBuf>,
492 ofile: &Option<PathBuf>,
493 temps_dir: &Option<PathBuf>,
494 attrs: &[ast::Attribute],
496 ) -> OutputFilenames {
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();
504 // If a crate name is present, we use it as the link name
509 .or_else(|| rustc_attr::find_crate_name(sess, attrs).map(|n| n.to_string()))
510 .unwrap_or_else(|| input.filestem().to_owned());
512 OutputFilenames::new(
517 sess.opts.cg.extra_filename.clone(),
518 sess.opts.output_types.clone(),
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 {
527 "due to multiple output types requested, the explicitly specified \
528 output file name will be adapted for each output type",
532 if !sess.opts.cg.extra_filename.is_empty() {
533 sess.warn("ignoring -C extra-filename flag due to -o flag");
535 Some(out_file.clone())
538 sess.warn("ignoring --out-dir flag due to -o flag");
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(),
546 sess.opts.cg.extra_filename.clone(),
547 sess.opts.output_types.clone(),
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")
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> {