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, 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::lint::{self, BuiltinLintDiag, LintBuffer};
16use rustc_session::output::{CRATE_TYPES, categorize_crate_type};
17use rustc_session::{EarlyDiagCtxt, Session, filesearch};
18use rustc_span::edit_distance::find_best_match_for_name;
19use rustc_span::edition::Edition;
20use rustc_span::source_map::SourceMapInputs;
21use rustc_span::{SessionGlobals, Symbol, sym};
22use rustc_target::spec::Target;
23use tracing::info;
24
25use crate::errors;
26
27type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
29
30pub(crate) fn add_configuration(
36 cfg: &mut Cfg,
37 sess: &mut Session,
38 codegen_backend: &dyn CodegenBackend,
39) {
40 let tf = sym::target_feature;
41 let tf_cfg = codegen_backend.target_config(sess);
42
43 sess.unstable_target_features.extend(tf_cfg.unstable_target_features.iter().copied());
44 sess.target_features.extend(tf_cfg.target_features.iter().copied());
45
46 cfg.extend(tf_cfg.target_features.into_iter().map(|feat| (tf, Some(feat))));
47
48 if tf_cfg.has_reliable_f16 {
49 cfg.insert((sym::target_has_reliable_f16, None));
50 }
51 if tf_cfg.has_reliable_f16_math {
52 cfg.insert((sym::target_has_reliable_f16_math, None));
53 }
54 if tf_cfg.has_reliable_f128 {
55 cfg.insert((sym::target_has_reliable_f128, None));
56 }
57 if tf_cfg.has_reliable_f128_math {
58 cfg.insert((sym::target_has_reliable_f128_math, None));
59 }
60
61 if sess.crt_static(None) {
62 cfg.insert((tf, Some(sym::crt_dash_static)));
63 }
64}
65
66pub(crate) fn check_abi_required_features(sess: &Session) {
69 let abi_feature_constraints = sess.target.abi_required_features();
70 for feature in
74 abi_feature_constraints.required.iter().chain(abi_feature_constraints.incompatible.iter())
75 {
76 assert!(
77 sess.target.rust_target_features().iter().any(|(name, ..)| feature == name),
78 "target feature {feature} is required/incompatible for the current ABI but not a recognized feature for this target"
79 );
80 }
81
82 for feature in abi_feature_constraints.required {
83 if !sess.unstable_target_features.contains(&Symbol::intern(feature)) {
84 sess.dcx().emit_warn(errors::AbiRequiredTargetFeature { feature, enabled: "enabled" });
85 }
86 }
87 for feature in abi_feature_constraints.incompatible {
88 if sess.unstable_target_features.contains(&Symbol::intern(feature)) {
89 sess.dcx().emit_warn(errors::AbiRequiredTargetFeature { feature, enabled: "disabled" });
90 }
91 }
92}
93
94pub static STACK_SIZE: OnceLock<usize> = OnceLock::new();
95pub const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;
96
97fn init_stack_size(early_dcx: &EarlyDiagCtxt) -> usize {
98 *STACK_SIZE.get_or_init(|| {
100 env::var_os("RUST_MIN_STACK")
101 .as_ref()
102 .map(|os_str| os_str.to_string_lossy())
103 .filter(|s| !s.trim().is_empty())
107 .map(|s| {
111 let s = s.trim();
112 #[allow(rustc::untranslatable_diagnostic, rustc::diagnostic_outside_of_impl)]
114 s.parse::<usize>().unwrap_or_else(|_| {
115 let mut err = early_dcx.early_struct_fatal(format!(
116 r#"`RUST_MIN_STACK` should be a number of bytes, but was "{s}""#,
117 ));
118 err.note("you can also unset `RUST_MIN_STACK` to use the default stack size");
119 err.emit()
120 })
121 })
122 .unwrap_or(DEFAULT_STACK_SIZE)
124 })
125}
126
127fn run_in_thread_with_globals<F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send, R: Send>(
128 thread_stack_size: usize,
129 edition: Edition,
130 sm_inputs: SourceMapInputs,
131 extra_symbols: &[&'static str],
132 f: F,
133) -> R {
134 let builder = thread::Builder::new().name("rustc".to_string()).stack_size(thread_stack_size);
141
142 thread::scope(|s| {
145 let r = builder
148 .spawn_scoped(s, move || {
149 rustc_span::create_session_globals_then(
150 edition,
151 extra_symbols,
152 Some(sm_inputs),
153 || f(CurrentGcx::new(), Proxy::new()),
154 )
155 })
156 .unwrap()
157 .join();
158
159 match r {
160 Ok(v) => v,
161 Err(e) => std::panic::resume_unwind(e),
162 }
163 })
164}
165
166pub(crate) fn run_in_thread_pool_with_globals<
167 F: FnOnce(CurrentGcx, Arc<Proxy>) -> R + Send,
168 R: Send,
169>(
170 thread_builder_diag: &EarlyDiagCtxt,
171 edition: Edition,
172 threads: usize,
173 extra_symbols: &[&'static str],
174 sm_inputs: SourceMapInputs,
175 f: F,
176) -> R {
177 use std::process;
178
179 use rustc_data_structures::defer;
180 use rustc_data_structures::sync::FromDyn;
181 use rustc_middle::ty::tls;
182 use rustc_query_impl::QueryCtxt;
183 use rustc_query_system::query::{QueryContext, break_query_cycles};
184
185 let thread_stack_size = init_stack_size(thread_builder_diag);
186
187 let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());
188
189 if !sync::is_dyn_thread_safe() {
190 return run_in_thread_with_globals(
191 thread_stack_size,
192 edition,
193 sm_inputs,
194 extra_symbols,
195 |current_gcx, jobserver_proxy| {
196 registry.register();
198
199 f(current_gcx, jobserver_proxy)
200 },
201 );
202 }
203
204 let current_gcx = FromDyn::from(CurrentGcx::new());
205 let current_gcx2 = current_gcx.clone();
206
207 let proxy = Proxy::new();
208
209 let proxy_ = Arc::clone(&proxy);
210 let proxy__ = Arc::clone(&proxy);
211 let builder = rayon_core::ThreadPoolBuilder::new()
212 .thread_name(|_| "rustc".to_string())
213 .acquire_thread_handler(move || proxy_.acquire_thread())
214 .release_thread_handler(move || proxy__.release_thread())
215 .num_threads(threads)
216 .deadlock_handler(move || {
217 let current_gcx2 = current_gcx2.clone();
221 let registry = rayon_core::Registry::current();
222 let session_globals = rustc_span::with_session_globals(|session_globals| {
223 session_globals as *const SessionGlobals as usize
224 });
225 thread::Builder::new()
226 .name("rustc query cycle handler".to_string())
227 .spawn(move || {
228 let on_panic = defer(|| {
229 eprintln!("internal compiler error: query cycle handler thread panicked, aborting process");
230 process::abort();
233 });
234
235 current_gcx2.access(|gcx| {
238 tls::enter_context(&tls::ImplicitCtxt::new(gcx), || {
239 tls::with(|tcx| {
240 let query_map = rustc_span::set_session_globals_then(unsafe { &*(session_globals as *const SessionGlobals) }, || {
243 QueryCtxt::new(tcx).collect_active_jobs().ok().expect("failed to collect active queries in deadlock handler")
246 });
247 break_query_cycles(query_map, ®istry);
248 })
249 })
250 });
251
252 on_panic.disable();
253 })
254 .unwrap();
255 })
256 .stack_size(thread_stack_size);
257
258 rustc_span::create_session_globals_then(edition, extra_symbols, Some(sm_inputs), || {
263 rustc_span::with_session_globals(|session_globals| {
264 let session_globals = FromDyn::from(session_globals);
265 builder
266 .build_scoped(
267 move |thread: rayon_core::ThreadBuilder| {
269 registry.register();
271
272 rustc_span::set_session_globals_then(session_globals.into_inner(), || {
273 thread.run()
274 })
275 },
276 move |pool: &rayon_core::ThreadPool| {
278 pool.install(|| f(current_gcx.into_inner(), proxy))
279 },
280 )
281 .unwrap()
282 })
283 })
284}
285
286#[allow(rustc::untranslatable_diagnostic)] fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBackendFn {
288 match unsafe { load_symbol_from_dylib::<MakeBackendFn>(path, "__rustc_codegen_backend") } {
289 Ok(backend_sym) => backend_sym,
290 Err(DylibError::DlOpen(path, err)) => {
291 let err = format!("couldn't load codegen backend {path}{err}");
292 early_dcx.early_fatal(err);
293 }
294 Err(DylibError::DlSym(_path, err)) => {
295 let e = format!(
296 "`__rustc_codegen_backend` symbol lookup in the codegen backend failed{err}",
297 );
298 early_dcx.early_fatal(e);
299 }
300 }
301}
302
303pub fn get_codegen_backend(
307 early_dcx: &EarlyDiagCtxt,
308 sysroot: &Path,
309 backend_name: Option<&str>,
310 target: &Target,
311) -> Box<dyn CodegenBackend> {
312 static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();
313
314 let load = LOAD.get_or_init(|| {
315 let backend = backend_name
316 .or(target.default_codegen_backend.as_deref())
317 .or(option_env!("CFG_DEFAULT_CODEGEN_BACKEND"))
318 .unwrap_or("llvm");
319
320 match backend {
321 filename if filename.contains('.') => {
322 load_backend_from_dylib(early_dcx, filename.as_ref())
323 }
324 #[cfg(feature = "llvm")]
325 "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
326 backend_name => get_codegen_sysroot(early_dcx, sysroot, backend_name),
327 }
328 });
329
330 unsafe { load() }
334}
335
336pub fn rustc_path<'a>() -> Option<&'a Path> {
340 static RUSTC_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
341
342 const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
343
344 RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_deref()
345}
346
347fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
348 let candidate = filesearch::get_or_default_sysroot()
349 .join(bin_path)
350 .join(if cfg!(target_os = "windows") { "rustc.exe" } else { "rustc" });
351 candidate.exists().then_some(candidate)
352}
353
354#[allow(rustc::untranslatable_diagnostic)] fn get_codegen_sysroot(
356 early_dcx: &EarlyDiagCtxt,
357 sysroot: &Path,
358 backend_name: &str,
359) -> MakeBackendFn {
360 static LOADED: AtomicBool = AtomicBool::new(false);
366 assert!(
367 !LOADED.fetch_or(true, Ordering::SeqCst),
368 "cannot load the default codegen backend twice"
369 );
370
371 let target = host_tuple();
372 let sysroot_candidates = filesearch::sysroot_with_fallback(&sysroot);
373
374 let sysroot = sysroot_candidates
375 .iter()
376 .map(|sysroot| {
377 filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
378 })
379 .find(|f| {
380 info!("codegen backend candidate: {}", f.display());
381 f.exists()
382 })
383 .unwrap_or_else(|| {
384 let candidates = sysroot_candidates
385 .iter()
386 .map(|p| p.display().to_string())
387 .collect::<Vec<_>>()
388 .join("\n* ");
389 let err = format!(
390 "failed to find a `codegen-backends` folder \
391 in the sysroot candidates:\n* {candidates}"
392 );
393 early_dcx.early_fatal(err);
394 });
395
396 info!("probing {} for a codegen backend", sysroot.display());
397
398 let d = sysroot.read_dir().unwrap_or_else(|e| {
399 let err = format!(
400 "failed to load default codegen backend, couldn't \
401 read `{}`: {}",
402 sysroot.display(),
403 e
404 );
405 early_dcx.early_fatal(err);
406 });
407
408 let mut file: Option<PathBuf> = None;
409
410 let expected_names = &[
411 format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
412 format!("rustc_codegen_{backend_name}"),
413 ];
414 for entry in d.filter_map(|e| e.ok()) {
415 let path = entry.path();
416 let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { continue };
417 if !(filename.starts_with(DLL_PREFIX) && filename.ends_with(DLL_SUFFIX)) {
418 continue;
419 }
420 let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
421 if !expected_names.iter().any(|expected| expected == name) {
422 continue;
423 }
424 if let Some(ref prev) = file {
425 let err = format!(
426 "duplicate codegen backends found\n\
427 first: {}\n\
428 second: {}\n\
429 ",
430 prev.display(),
431 path.display()
432 );
433 early_dcx.early_fatal(err);
434 }
435 file = Some(path.clone());
436 }
437
438 match file {
439 Some(ref s) => load_backend_from_dylib(early_dcx, s),
440 None => {
441 let err = format!("unsupported builtin codegen backend `{backend_name}`");
442 early_dcx.early_fatal(err);
443 }
444 }
445}
446
447pub(crate) fn check_attr_crate_type(
448 sess: &Session,
449 attrs: &[ast::Attribute],
450 lint_buffer: &mut LintBuffer,
451) {
452 for a in attrs.iter() {
454 if a.has_name(sym::crate_type) {
455 if let Some(n) = a.value_str() {
456 if categorize_crate_type(n).is_some() {
457 return;
458 }
459
460 if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
461 let span = spanned.span;
462 let candidate = find_best_match_for_name(
463 &CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
464 n,
465 None,
466 );
467 lint_buffer.buffer_lint(
468 lint::builtin::UNKNOWN_CRATE_TYPES,
469 ast::CRATE_NODE_ID,
470 span,
471 BuiltinLintDiag::UnknownCrateTypes { span, candidate },
472 );
473 }
474 } else {
475 validate_attr::emit_fatal_malformed_builtin_attribute(
483 &sess.psess,
484 a,
485 sym::crate_type,
486 );
487 }
488 }
489 }
490}
491
492fn multiple_output_types_to_stdout(
493 output_types: &OutputTypes,
494 single_output_file_is_stdout: bool,
495) -> bool {
496 use std::io::IsTerminal;
497 if std::io::stdout().is_terminal() {
498 let named_text_types = output_types
501 .iter()
502 .filter(|(f, o)| f.is_text_output() && *o == &Some(OutFileName::Stdout))
503 .count();
504 let unnamed_text_types =
505 output_types.iter().filter(|(f, o)| f.is_text_output() && o.is_none()).count();
506 named_text_types > 1 || unnamed_text_types > 1 && single_output_file_is_stdout
507 } else {
508 let named_types =
510 output_types.values().filter(|o| *o == &Some(OutFileName::Stdout)).count();
511 let unnamed_types = output_types.values().filter(|o| o.is_none()).count();
512 named_types > 1 || unnamed_types > 1 && single_output_file_is_stdout
513 }
514}
515
516pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames {
517 if multiple_output_types_to_stdout(
518 &sess.opts.output_types,
519 sess.io.output_file == Some(OutFileName::Stdout),
520 ) {
521 sess.dcx().emit_fatal(errors::MultipleOutputTypesToStdout);
522 }
523
524 let crate_name = sess
525 .opts
526 .crate_name
527 .clone()
528 .or_else(|| rustc_attr_parsing::find_crate_name(attrs).map(|n| n.to_string()));
529
530 match sess.io.output_file {
531 None => {
532 let dirpath = sess.io.output_dir.clone().unwrap_or_default();
536
537 let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
539
540 OutputFilenames::new(
541 dirpath,
542 crate_name.unwrap_or_else(|| stem.replace('-', "_")),
543 stem,
544 None,
545 sess.io.temps_dir.clone(),
546 sess.opts.cg.extra_filename.clone(),
547 sess.opts.output_types.clone(),
548 )
549 }
550
551 Some(ref out_file) => {
552 let unnamed_output_types =
553 sess.opts.output_types.values().filter(|a| a.is_none()).count();
554 let ofile = if unnamed_output_types > 1 {
555 sess.dcx().emit_warn(errors::MultipleOutputTypesAdaption);
556 None
557 } else {
558 if !sess.opts.cg.extra_filename.is_empty() {
559 sess.dcx().emit_warn(errors::IgnoringExtraFilename);
560 }
561 Some(out_file.clone())
562 };
563 if sess.io.output_dir != None {
564 sess.dcx().emit_warn(errors::IgnoringOutDir);
565 }
566
567 let out_filestem =
568 out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
569 OutputFilenames::new(
570 out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
571 crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")),
572 out_filestem,
573 ofile,
574 sess.io.temps_dir.clone(),
575 sess.opts.cg.extra_filename.clone(),
576 sess.opts.output_types.clone(),
577 )
578 }
579 }
580}
581
582pub macro version_str() {
584 option_env!("CFG_VERSION")
585}
586
587pub fn rustc_version_str() -> Option<&'static str> {
589 version_str!()
590}