]>
Commit | Line | Data |
---|---|---|
a2a8927a | 1 | use libloading::Library; |
5e7ed085 | 2 | use rustc_ast as ast; |
ba9703b0 | 3 | use rustc_codegen_ssa::traits::CodegenBackend; |
dfeec247 | 4 | use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
532ac7d7 XL |
5 | #[cfg(parallel_compiler)] |
6 | use rustc_data_structures::jobserver; | |
3dfed10e | 7 | use rustc_data_structures::sync::Lrc; |
532ac7d7 | 8 | use rustc_errors::registry::Registry; |
6a06907d XL |
9 | #[cfg(parallel_compiler)] |
10 | use rustc_middle::ty::tls; | |
c295e0f8 | 11 | use rustc_parse::validate_attr; |
136023e0 XL |
12 | #[cfg(parallel_compiler)] |
13 | use rustc_query_impl::QueryCtxt; | |
dfeec247 | 14 | use rustc_session as session; |
5099ac24 | 15 | use rustc_session::config::CheckCfg; |
f9f354fc | 16 | use rustc_session::config::{self, CrateType}; |
dfeec247 | 17 | use rustc_session::config::{ErrorOutputType, Input, OutputFilenames}; |
ba9703b0 | 18 | use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer}; |
74b04a01 | 19 | use rustc_session::parse::CrateConfig; |
f9f354fc | 20 | use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session}; |
dfeec247 | 21 | use rustc_span::edition::Edition; |
fc512014 | 22 | use rustc_span::lev_distance::find_best_match_for_name; |
f9f354fc | 23 | use rustc_span::source_map::FileLoader; |
dfeec247 | 24 | use rustc_span::symbol::{sym, Symbol}; |
532ac7d7 | 25 | use std::env; |
29967ef6 | 26 | use std::env::consts::{DLL_PREFIX, DLL_SUFFIX}; |
532ac7d7 | 27 | use std::mem; |
6a06907d XL |
28 | #[cfg(not(parallel_compiler))] |
29 | use std::panic; | |
532ac7d7 | 30 | use std::path::{Path, PathBuf}; |
29967ef6 | 31 | use std::sync::atomic::{AtomicBool, Ordering}; |
923072b8 | 32 | use std::sync::OnceLock; |
6a06907d | 33 | use std::thread; |
3dfed10e | 34 | use tracing::info; |
532ac7d7 | 35 | |
a2a8927a XL |
36 | /// Function pointer type that constructs a new CodegenBackend. |
37 | pub type MakeBackendFn = fn() -> Box<dyn CodegenBackend>; | |
38 | ||
532ac7d7 XL |
39 | /// Adds `target_feature = "..."` cfgs for a variety of platform |
40 | /// specific features (SSE, NEON etc.). | |
41 | /// | |
f035d41b XL |
42 | /// This is performed by checking whether a set of permitted features |
43 | /// is available on the target machine, by querying LLVM. | |
532ac7d7 | 44 | pub fn add_configuration( |
74b04a01 | 45 | cfg: &mut CrateConfig, |
f9f354fc | 46 | sess: &mut Session, |
532ac7d7 XL |
47 | codegen_backend: &dyn CodegenBackend, |
48 | ) { | |
dc9dc135 | 49 | let tf = sym::target_feature; |
532ac7d7 | 50 | |
f9f354fc XL |
51 | let target_features = codegen_backend.target_features(sess); |
52 | sess.target_features.extend(target_features.iter().cloned()); | |
53 | ||
54 | cfg.extend(target_features.into_iter().map(|feat| (tf, Some(feat)))); | |
532ac7d7 | 55 | |
f9f354fc | 56 | if sess.crt_static(None) { |
3dfed10e | 57 | cfg.insert((tf, Some(sym::crt_dash_static))); |
532ac7d7 XL |
58 | } |
59 | } | |
60 | ||
61 | pub fn create_session( | |
62 | sopts: config::Options, | |
63 | cfg: FxHashSet<(String, Option<String>)>, | |
5099ac24 | 64 | check_cfg: CheckCfg, |
532ac7d7 XL |
65 | diagnostic_output: DiagnosticOutput, |
66 | file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>, | |
67 | input_path: Option<PathBuf>, | |
68 | lint_caps: FxHashMap<lint::LintId, lint::Level>, | |
1b1a35ee XL |
69 | make_codegen_backend: Option< |
70 | Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>, | |
71 | >, | |
60c5eb7d | 72 | descriptions: Registry, |
f9f354fc | 73 | ) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) { |
1b1a35ee XL |
74 | let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend { |
75 | make_codegen_backend(&sopts) | |
76 | } else { | |
17df50a5 XL |
77 | get_codegen_backend( |
78 | &sopts.maybe_sysroot, | |
79 | sopts.debugging_opts.codegen_backend.as_ref().map(|name| &name[..]), | |
80 | ) | |
1b1a35ee XL |
81 | }; |
82 | ||
83 | // target_override is documented to be called before init(), so this is okay | |
84 | let target_override = codegen_backend.target_override(&sopts); | |
85 | ||
04454e1e FG |
86 | let bundle = match rustc_errors::fluent_bundle( |
87 | sopts.maybe_sysroot.clone(), | |
88 | sysroot_candidates(), | |
89 | sopts.debugging_opts.translate_lang.clone(), | |
90 | sopts.debugging_opts.translate_additional_ftl.as_deref(), | |
91 | sopts.debugging_opts.translate_directionality_markers, | |
92 | ) { | |
93 | Ok(bundle) => bundle, | |
94 | Err(e) => { | |
95 | early_error(sopts.error_format, &format!("failed to load fluent bundle: {e}")); | |
96 | } | |
97 | }; | |
98 | ||
f9f354fc | 99 | let mut sess = session::build_session( |
532ac7d7 XL |
100 | sopts, |
101 | input_path, | |
04454e1e | 102 | bundle, |
532ac7d7 | 103 | descriptions, |
532ac7d7 XL |
104 | diagnostic_output, |
105 | lint_caps, | |
ba9703b0 | 106 | file_loader, |
1b1a35ee | 107 | target_override, |
532ac7d7 XL |
108 | ); |
109 | ||
1b1a35ee | 110 | codegen_backend.init(&sess); |
532ac7d7 | 111 | |
532ac7d7 | 112 | let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg)); |
f9f354fc | 113 | add_configuration(&mut cfg, &mut sess, &*codegen_backend); |
5099ac24 FG |
114 | |
115 | let mut check_cfg = config::to_crate_check_config(check_cfg); | |
116 | check_cfg.fill_well_known(); | |
117 | check_cfg.fill_actual(&cfg); | |
118 | ||
532ac7d7 | 119 | sess.parse_sess.config = cfg; |
5099ac24 | 120 | sess.parse_sess.check_config = check_cfg; |
532ac7d7 | 121 | |
f9f354fc | 122 | (Lrc::new(sess), Lrc::new(codegen_backend)) |
532ac7d7 XL |
123 | } |
124 | ||
f9f354fc | 125 | const STACK_SIZE: usize = 8 * 1024 * 1024; |
532ac7d7 XL |
126 | |
127 | fn get_stack_size() -> Option<usize> { | |
128 | // FIXME: Hacks on hacks. If the env is trying to override the stack size | |
129 | // then *don't* set it explicitly. | |
60c5eb7d | 130 | env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE) |
532ac7d7 XL |
131 | } |
132 | ||
f035d41b XL |
133 | /// Like a `thread::Builder::spawn` followed by a `join()`, but avoids the need |
134 | /// for `'static` bounds. | |
532ac7d7 | 135 | #[cfg(not(parallel_compiler))] |
5099ac24 | 136 | fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -> R { |
c295e0f8 XL |
137 | // SAFETY: join() is called immediately, so any closure captures are still |
138 | // alive. | |
139 | match unsafe { cfg.spawn_unchecked(f) }.unwrap().join() { | |
140 | Ok(v) => v, | |
141 | Err(e) => panic::resume_unwind(e), | |
532ac7d7 XL |
142 | } |
143 | } | |
144 | ||
145 | #[cfg(not(parallel_compiler))] | |
5099ac24 | 146 | pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>( |
dc9dc135 | 147 | edition: Edition, |
e74abb32 | 148 | _threads: usize, |
532ac7d7 XL |
149 | f: F, |
150 | ) -> R { | |
151 | let mut cfg = thread::Builder::new().name("rustc".to_string()); | |
152 | ||
153 | if let Some(size) = get_stack_size() { | |
154 | cfg = cfg.stack_size(size); | |
155 | } | |
156 | ||
5099ac24 | 157 | let main_handler = move || rustc_span::create_session_globals_then(edition, f); |
f035d41b XL |
158 | |
159 | scoped_thread(cfg, main_handler) | |
532ac7d7 XL |
160 | } |
161 | ||
6a06907d XL |
162 | /// Creates a new thread and forwards information in thread locals to it. |
163 | /// The new thread runs the deadlock handler. | |
164 | /// Must only be called when a deadlock is about to happen. | |
165 | #[cfg(parallel_compiler)] | |
166 | unsafe fn handle_deadlock() { | |
167 | let registry = rustc_rayon_core::Registry::current(); | |
168 | ||
169 | let context = tls::get_tlv(); | |
170 | assert!(context != 0); | |
171 | rustc_data_structures::sync::assert_sync::<tls::ImplicitCtxt<'_, '_>>(); | |
172 | let icx: &tls::ImplicitCtxt<'_, '_> = &*(context as *const tls::ImplicitCtxt<'_, '_>); | |
173 | ||
136023e0 | 174 | let session_globals = rustc_span::with_session_globals(|sg| sg as *const _); |
6a06907d XL |
175 | let session_globals = &*session_globals; |
176 | thread::spawn(move || { | |
177 | tls::enter_context(icx, |_| { | |
136023e0 XL |
178 | rustc_span::set_session_globals_then(session_globals, || { |
179 | tls::with(|tcx| QueryCtxt::from_tcx(tcx).deadlock(®istry)) | |
180 | }) | |
6a06907d XL |
181 | }); |
182 | }); | |
183 | } | |
184 | ||
532ac7d7 | 185 | #[cfg(parallel_compiler)] |
5099ac24 | 186 | pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>( |
dc9dc135 | 187 | edition: Edition, |
e74abb32 | 188 | threads: usize, |
532ac7d7 XL |
189 | f: F, |
190 | ) -> R { | |
f035d41b | 191 | let mut config = rayon::ThreadPoolBuilder::new() |
e74abb32 | 192 | .thread_name(|_| "rustc".to_string()) |
532ac7d7 XL |
193 | .acquire_thread_handler(jobserver::acquire_thread) |
194 | .release_thread_handler(jobserver::release_thread) | |
e74abb32 | 195 | .num_threads(threads) |
6a06907d | 196 | .deadlock_handler(|| unsafe { handle_deadlock() }); |
532ac7d7 XL |
197 | |
198 | if let Some(size) = get_stack_size() { | |
199 | config = config.stack_size(size); | |
200 | } | |
201 | ||
29967ef6 | 202 | let with_pool = move |pool: &rayon::ThreadPool| pool.install(f); |
f035d41b | 203 | |
136023e0 XL |
204 | rustc_span::create_session_globals_then(edition, || { |
205 | rustc_span::with_session_globals(|session_globals| { | |
3dfed10e XL |
206 | // The main handler runs for each Rayon worker thread and sets up |
207 | // the thread local rustc uses. `session_globals` is captured and set | |
208 | // on the new threads. | |
209 | let main_handler = move |thread: rayon::ThreadBuilder| { | |
5099ac24 | 210 | rustc_span::set_session_globals_then(session_globals, || thread.run()) |
3dfed10e XL |
211 | }; |
212 | ||
213 | config.build_scoped(main_handler, with_pool).unwrap() | |
532ac7d7 XL |
214 | }) |
215 | }) | |
216 | } | |
217 | ||
a2a8927a XL |
218 | fn load_backend_from_dylib(path: &Path) -> MakeBackendFn { |
219 | let lib = unsafe { Library::new(path) }.unwrap_or_else(|err| { | |
220 | let err = format!("couldn't load codegen backend {:?}: {}", path, err); | |
532ac7d7 XL |
221 | early_error(ErrorOutputType::default(), &err); |
222 | }); | |
a2a8927a XL |
223 | |
224 | let backend_sym = unsafe { lib.get::<MakeBackendFn>(b"__rustc_codegen_backend") } | |
225 | .unwrap_or_else(|e| { | |
226 | let err = format!("couldn't load codegen backend: {}", e); | |
227 | early_error(ErrorOutputType::default(), &err); | |
228 | }); | |
229 | ||
230 | // Intentionally leak the dynamic library. We can't ever unload it | |
231 | // since the library can make things that will live arbitrarily long. | |
232 | let backend_sym = unsafe { backend_sym.into_raw() }; | |
233 | mem::forget(lib); | |
234 | ||
235 | *backend_sym | |
532ac7d7 XL |
236 | } |
237 | ||
17df50a5 XL |
238 | /// Get the codegen backend based on the name and specified sysroot. |
239 | /// | |
240 | /// A name of `None` indicates that the default backend should be used. | |
241 | pub fn get_codegen_backend( | |
242 | maybe_sysroot: &Option<PathBuf>, | |
243 | backend_name: Option<&str>, | |
244 | ) -> Box<dyn CodegenBackend> { | |
923072b8 | 245 | static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new(); |
532ac7d7 | 246 | |
17df50a5 | 247 | let load = LOAD.get_or_init(|| { |
5e7ed085 | 248 | let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm"); |
29967ef6 | 249 | |
5e7ed085 | 250 | match backend_name.unwrap_or(default_codegen_backend) { |
74b04a01 | 251 | filename if filename.contains('.') => load_backend_from_dylib(filename.as_ref()), |
17df50a5 XL |
252 | #[cfg(feature = "llvm")] |
253 | "llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new, | |
254 | backend_name => get_codegen_sysroot(maybe_sysroot, backend_name), | |
532ac7d7 XL |
255 | } |
256 | }); | |
17df50a5 XL |
257 | |
258 | // SAFETY: In case of a builtin codegen backend this is safe. In case of an external codegen | |
259 | // backend we hope that the backend links against the same rustc_driver version. If this is not | |
260 | // the case, we get UB. | |
261 | unsafe { load() } | |
532ac7d7 XL |
262 | } |
263 | ||
e1599b0c XL |
264 | // This is used for rustdoc, but it uses similar machinery to codegen backend |
265 | // loading, so we leave the code here. It is potentially useful for other tools | |
266 | // that want to invoke the rustc binary while linking to rustc as well. | |
267 | pub fn rustc_path<'a>() -> Option<&'a Path> { | |
923072b8 | 268 | static RUSTC_PATH: OnceLock<Option<PathBuf>> = OnceLock::new(); |
e1599b0c XL |
269 | |
270 | const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR"); | |
532ac7d7 | 271 | |
e1599b0c XL |
272 | RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_ref().map(|v| &**v) |
273 | } | |
274 | ||
275 | fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> { | |
f9f354fc XL |
276 | sysroot_candidates().iter().find_map(|sysroot| { |
277 | let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") { | |
278 | "rustc.exe" | |
279 | } else { | |
280 | "rustc" | |
281 | }); | |
282 | candidate.exists().then_some(candidate) | |
283 | }) | |
e1599b0c XL |
284 | } |
285 | ||
286 | fn sysroot_candidates() -> Vec<PathBuf> { | |
532ac7d7 XL |
287 | let target = session::config::host_triple(); |
288 | let mut sysroot_candidates = vec![filesearch::get_or_default_sysroot()]; | |
e1599b0c | 289 | let path = current_dll_path().and_then(|s| s.canonicalize().ok()); |
532ac7d7 XL |
290 | if let Some(dll) = path { |
291 | // use `parent` twice to chop off the file name and then also the | |
292 | // directory containing the dll which should be either `lib` or `bin`. | |
293 | if let Some(path) = dll.parent().and_then(|p| p.parent()) { | |
294 | // The original `path` pointed at the `rustc_driver` crate's dll. | |
295 | // Now that dll should only be in one of two locations. The first is | |
296 | // in the compiler's libdir, for example `$sysroot/lib/*.dll`. The | |
297 | // other is the target's libdir, for example | |
298 | // `$sysroot/lib/rustlib/$target/lib/*.dll`. | |
299 | // | |
300 | // We don't know which, so let's assume that if our `path` above | |
301 | // ends in `$target` we *could* be in the target libdir, and always | |
302 | // assume that we may be in the main libdir. | |
303 | sysroot_candidates.push(path.to_owned()); | |
304 | ||
305 | if path.ends_with(target) { | |
dfeec247 XL |
306 | sysroot_candidates.extend( |
307 | path.parent() // chop off `$target` | |
308 | .and_then(|p| p.parent()) // chop off `rustlib` | |
309 | .and_then(|p| p.parent()) // chop off `lib` | |
310 | .map(|s| s.to_owned()), | |
311 | ); | |
532ac7d7 XL |
312 | } |
313 | } | |
314 | } | |
315 | ||
e1599b0c | 316 | return sysroot_candidates; |
532ac7d7 XL |
317 | |
318 | #[cfg(unix)] | |
319 | fn current_dll_path() -> Option<PathBuf> { | |
dfeec247 | 320 | use std::ffi::{CStr, OsStr}; |
532ac7d7 XL |
321 | use std::os::unix::prelude::*; |
322 | ||
323 | unsafe { | |
324 | let addr = current_dll_path as usize as *mut _; | |
325 | let mut info = mem::zeroed(); | |
326 | if libc::dladdr(addr, &mut info) == 0 { | |
327 | info!("dladdr failed"); | |
dfeec247 | 328 | return None; |
532ac7d7 XL |
329 | } |
330 | if info.dli_fname.is_null() { | |
331 | info!("dladdr returned null pointer"); | |
dfeec247 | 332 | return None; |
532ac7d7 XL |
333 | } |
334 | let bytes = CStr::from_ptr(info.dli_fname).to_bytes(); | |
335 | let os = OsStr::from_bytes(bytes); | |
336 | Some(PathBuf::from(os)) | |
337 | } | |
338 | } | |
339 | ||
340 | #[cfg(windows)] | |
341 | fn current_dll_path() -> Option<PathBuf> { | |
342 | use std::ffi::OsString; | |
5099ac24 | 343 | use std::io; |
532ac7d7 | 344 | use std::os::windows::prelude::*; |
dfeec247 | 345 | use std::ptr; |
532ac7d7 | 346 | |
dfeec247 XL |
347 | use winapi::um::libloaderapi::{ |
348 | GetModuleFileNameW, GetModuleHandleExW, GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
349 | }; | |
532ac7d7 XL |
350 | |
351 | unsafe { | |
dfeec247 XL |
352 | let mut module = ptr::null_mut(); |
353 | let r = GetModuleHandleExW( | |
354 | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
355 | current_dll_path as usize as *mut _, | |
356 | &mut module, | |
357 | ); | |
532ac7d7 XL |
358 | if r == 0 { |
359 | info!("GetModuleHandleExW failed: {}", io::Error::last_os_error()); | |
dfeec247 | 360 | return None; |
532ac7d7 XL |
361 | } |
362 | let mut space = Vec::with_capacity(1024); | |
dfeec247 | 363 | let r = GetModuleFileNameW(module, space.as_mut_ptr(), space.capacity() as u32); |
532ac7d7 XL |
364 | if r == 0 { |
365 | info!("GetModuleFileNameW failed: {}", io::Error::last_os_error()); | |
dfeec247 | 366 | return None; |
532ac7d7 XL |
367 | } |
368 | let r = r as usize; | |
369 | if r >= space.capacity() { | |
dfeec247 XL |
370 | info!("our buffer was too small? {}", io::Error::last_os_error()); |
371 | return None; | |
532ac7d7 XL |
372 | } |
373 | space.set_len(r); | |
374 | let os = OsString::from_wide(&space); | |
375 | Some(PathBuf::from(os)) | |
376 | } | |
377 | } | |
378 | } | |
379 | ||
5099ac24 | 380 | fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> MakeBackendFn { |
29967ef6 XL |
381 | // For now we only allow this function to be called once as it'll dlopen a |
382 | // few things, which seems to work best if we only do that once. In | |
383 | // general this assertion never trips due to the once guard in `get_codegen_backend`, | |
384 | // but there's a few manual calls to this function in this file we protect | |
385 | // against. | |
386 | static LOADED: AtomicBool = AtomicBool::new(false); | |
387 | assert!( | |
388 | !LOADED.fetch_or(true, Ordering::SeqCst), | |
389 | "cannot load the default codegen backend twice" | |
390 | ); | |
391 | ||
392 | let target = session::config::host_triple(); | |
393 | let sysroot_candidates = sysroot_candidates(); | |
394 | ||
cdc7bbd5 | 395 | let sysroot = maybe_sysroot |
29967ef6 | 396 | .iter() |
cdc7bbd5 | 397 | .chain(sysroot_candidates.iter()) |
29967ef6 | 398 | .map(|sysroot| { |
c295e0f8 | 399 | filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends") |
29967ef6 XL |
400 | }) |
401 | .find(|f| { | |
402 | info!("codegen backend candidate: {}", f.display()); | |
403 | f.exists() | |
404 | }); | |
405 | let sysroot = sysroot.unwrap_or_else(|| { | |
406 | let candidates = sysroot_candidates | |
407 | .iter() | |
408 | .map(|p| p.display().to_string()) | |
409 | .collect::<Vec<_>>() | |
410 | .join("\n* "); | |
411 | let err = format!( | |
412 | "failed to find a `codegen-backends` folder \ | |
413 | in the sysroot candidates:\n* {}", | |
414 | candidates | |
415 | ); | |
416 | early_error(ErrorOutputType::default(), &err); | |
417 | }); | |
418 | info!("probing {} for a codegen backend", sysroot.display()); | |
419 | ||
420 | let d = sysroot.read_dir().unwrap_or_else(|e| { | |
421 | let err = format!( | |
422 | "failed to load default codegen backend, couldn't \ | |
423 | read `{}`: {}", | |
424 | sysroot.display(), | |
425 | e | |
426 | ); | |
427 | early_error(ErrorOutputType::default(), &err); | |
428 | }); | |
429 | ||
430 | let mut file: Option<PathBuf> = None; | |
431 | ||
cdc7bbd5 XL |
432 | let expected_names = &[ |
433 | format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE")), | |
434 | format!("rustc_codegen_{}", backend_name), | |
435 | ]; | |
29967ef6 XL |
436 | for entry in d.filter_map(|e| e.ok()) { |
437 | let path = entry.path(); | |
5e7ed085 | 438 | let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { continue }; |
29967ef6 XL |
439 | if !(filename.starts_with(DLL_PREFIX) && filename.ends_with(DLL_SUFFIX)) { |
440 | continue; | |
441 | } | |
442 | let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()]; | |
cdc7bbd5 | 443 | if !expected_names.iter().any(|expected| expected == name) { |
29967ef6 | 444 | continue; |
e1599b0c | 445 | } |
29967ef6 XL |
446 | if let Some(ref prev) = file { |
447 | let err = format!( | |
448 | "duplicate codegen backends found\n\ | |
449 | first: {}\n\ | |
450 | second: {}\n\ | |
451 | ", | |
452 | prev.display(), | |
453 | path.display() | |
454 | ); | |
455 | early_error(ErrorOutputType::default(), &err); | |
456 | } | |
457 | file = Some(path.clone()); | |
e1599b0c XL |
458 | } |
459 | ||
29967ef6 XL |
460 | match file { |
461 | Some(ref s) => load_backend_from_dylib(s), | |
462 | None => { | |
463 | let err = format!("unsupported builtin codegen backend `{}`", backend_name); | |
464 | early_error(ErrorOutputType::default(), &err); | |
465 | } | |
466 | } | |
e1599b0c XL |
467 | } |
468 | ||
3dfed10e | 469 | pub(crate) fn check_attr_crate_type( |
c295e0f8 | 470 | sess: &Session, |
3dfed10e XL |
471 | attrs: &[ast::Attribute], |
472 | lint_buffer: &mut LintBuffer, | |
473 | ) { | |
e74abb32 XL |
474 | // Unconditionally collect crate types from attributes to make them used |
475 | for a in attrs.iter() { | |
94222f64 | 476 | if a.has_name(sym::crate_type) { |
e74abb32 | 477 | if let Some(n) = a.value_str() { |
74b04a01 | 478 | if categorize_crate_type(n).is_some() { |
e74abb32 XL |
479 | return; |
480 | } | |
481 | ||
a2a8927a | 482 | if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() { |
e74abb32 | 483 | let span = spanned.span; |
fc512014 XL |
484 | let lev_candidate = find_best_match_for_name( |
485 | &CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(), | |
486 | n, | |
487 | None, | |
488 | ); | |
e74abb32 XL |
489 | if let Some(candidate) = lev_candidate { |
490 | lint_buffer.buffer_lint_with_diagnostic( | |
491 | lint::builtin::UNKNOWN_CRATE_TYPES, | |
492 | ast::CRATE_NODE_ID, | |
493 | span, | |
494 | "invalid `crate_type` value", | |
dfeec247 XL |
495 | BuiltinLintDiagnostics::UnknownCrateTypes( |
496 | span, | |
497 | "did you mean".to_string(), | |
498 | format!("\"{}\"", candidate), | |
499 | ), | |
e74abb32 XL |
500 | ); |
501 | } else { | |
502 | lint_buffer.buffer_lint( | |
503 | lint::builtin::UNKNOWN_CRATE_TYPES, | |
504 | ast::CRATE_NODE_ID, | |
505 | span, | |
dfeec247 | 506 | "invalid `crate_type` value", |
e74abb32 XL |
507 | ); |
508 | } | |
509 | } | |
c295e0f8 XL |
510 | } else { |
511 | // This is here mainly to check for using a macro, such as | |
512 | // #![crate_type = foo!()]. That is not supported since the | |
513 | // crate type needs to be known very early in compilation long | |
514 | // before expansion. Otherwise, validation would normally be | |
515 | // caught in AstValidator (via `check_builtin_attribute`), but | |
516 | // by the time that runs the macro is expanded, and it doesn't | |
517 | // give an error. | |
518 | validate_attr::emit_fatal_malformed_builtin_attribute( | |
519 | &sess.parse_sess, | |
520 | a, | |
521 | sym::crate_type, | |
522 | ); | |
e74abb32 XL |
523 | } |
524 | } | |
525 | } | |
526 | } | |
527 | ||
f9f354fc XL |
528 | const CRATE_TYPES: &[(Symbol, CrateType)] = &[ |
529 | (sym::rlib, CrateType::Rlib), | |
530 | (sym::dylib, CrateType::Dylib), | |
531 | (sym::cdylib, CrateType::Cdylib), | |
e74abb32 | 532 | (sym::lib, config::default_lib_output()), |
f9f354fc XL |
533 | (sym::staticlib, CrateType::Staticlib), |
534 | (sym::proc_dash_macro, CrateType::ProcMacro), | |
535 | (sym::bin, CrateType::Executable), | |
e74abb32 XL |
536 | ]; |
537 | ||
f9f354fc | 538 | fn categorize_crate_type(s: Symbol) -> Option<CrateType> { |
e74abb32 | 539 | Some(CRATE_TYPES.iter().find(|(key, _)| *key == s)?.1) |
532ac7d7 XL |
540 | } |
541 | ||
f9f354fc | 542 | pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<CrateType> { |
532ac7d7 | 543 | // Unconditionally collect crate types from attributes to make them used |
f9f354fc | 544 | let attr_types: Vec<CrateType> = attrs |
532ac7d7 XL |
545 | .iter() |
546 | .filter_map(|a| { | |
94222f64 | 547 | if a.has_name(sym::crate_type) { |
532ac7d7 | 548 | match a.value_str() { |
e74abb32 XL |
549 | Some(s) => categorize_crate_type(s), |
550 | _ => None, | |
532ac7d7 XL |
551 | } |
552 | } else { | |
553 | None | |
554 | } | |
555 | }) | |
556 | .collect(); | |
557 | ||
558 | // If we're generating a test executable, then ignore all other output | |
559 | // styles at all other locations | |
560 | if session.opts.test { | |
f9f354fc | 561 | return vec![CrateType::Executable]; |
532ac7d7 XL |
562 | } |
563 | ||
564 | // Only check command line flags if present. If no types are specified by | |
565 | // command line, then reuse the empty `base` Vec to hold the types that | |
566 | // will be found in crate attributes. | |
567 | let mut base = session.opts.crate_types.clone(); | |
568 | if base.is_empty() { | |
569 | base.extend(attr_types); | |
570 | if base.is_empty() { | |
ba9703b0 | 571 | base.push(output::default_output_for_target(session)); |
532ac7d7 XL |
572 | } else { |
573 | base.sort(); | |
574 | base.dedup(); | |
575 | } | |
576 | } | |
577 | ||
578 | base.retain(|crate_type| { | |
ba9703b0 | 579 | let res = !output::invalid_output_for_target(session, *crate_type); |
532ac7d7 XL |
580 | |
581 | if !res { | |
582 | session.warn(&format!( | |
583 | "dropping unsupported crate type `{}` for target `{}`", | |
584 | *crate_type, session.opts.target_triple | |
585 | )); | |
586 | } | |
587 | ||
588 | res | |
589 | }); | |
590 | ||
591 | base | |
592 | } | |
593 | ||
594 | pub fn build_output_filenames( | |
595 | input: &Input, | |
596 | odir: &Option<PathBuf>, | |
597 | ofile: &Option<PathBuf>, | |
3c0e092e | 598 | temps_dir: &Option<PathBuf>, |
532ac7d7 XL |
599 | attrs: &[ast::Attribute], |
600 | sess: &Session, | |
601 | ) -> OutputFilenames { | |
602 | match *ofile { | |
603 | None => { | |
604 | // "-" as input file will cause the parser to read from stdin so we | |
605 | // have to make up a name | |
606 | // We want to toss everything after the final '.' | |
607 | let dirpath = (*odir).as_ref().cloned().unwrap_or_default(); | |
608 | ||
609 | // If a crate name is present, we use it as the link name | |
dfeec247 XL |
610 | let stem = sess |
611 | .opts | |
532ac7d7 XL |
612 | .crate_name |
613 | .clone() | |
c295e0f8 | 614 | .or_else(|| rustc_attr::find_crate_name(sess, attrs).map(|n| n.to_string())) |
532ac7d7 XL |
615 | .unwrap_or_else(|| input.filestem().to_owned()); |
616 | ||
dfeec247 XL |
617 | OutputFilenames::new( |
618 | dirpath, | |
619 | stem, | |
620 | None, | |
3c0e092e | 621 | temps_dir.clone(), |
dfeec247 XL |
622 | sess.opts.cg.extra_filename.clone(), |
623 | sess.opts.output_types.clone(), | |
624 | ) | |
532ac7d7 XL |
625 | } |
626 | ||
627 | Some(ref out_file) => { | |
dfeec247 XL |
628 | let unnamed_output_types = |
629 | sess.opts.output_types.values().filter(|a| a.is_none()).count(); | |
532ac7d7 XL |
630 | let ofile = if unnamed_output_types > 1 { |
631 | sess.warn( | |
632 | "due to multiple output types requested, the explicitly specified \ | |
633 | output file name will be adapted for each output type", | |
634 | ); | |
635 | None | |
636 | } else { | |
416331ca XL |
637 | if !sess.opts.cg.extra_filename.is_empty() { |
638 | sess.warn("ignoring -C extra-filename flag due to -o flag"); | |
639 | } | |
532ac7d7 XL |
640 | Some(out_file.clone()) |
641 | }; | |
642 | if *odir != None { | |
643 | sess.warn("ignoring --out-dir flag due to -o flag"); | |
644 | } | |
532ac7d7 | 645 | |
dfeec247 XL |
646 | OutputFilenames::new( |
647 | out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(), | |
648 | out_file.file_stem().unwrap_or_default().to_str().unwrap().to_string(), | |
649 | ofile, | |
3c0e092e | 650 | temps_dir.clone(), |
dfeec247 XL |
651 | sess.opts.cg.extra_filename.clone(), |
652 | sess.opts.output_types.clone(), | |
653 | ) | |
532ac7d7 XL |
654 | } |
655 | } | |
656 | } | |
657 | ||
6a06907d XL |
658 | #[cfg(not(target_os = "linux"))] |
659 | pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> { | |
660 | std::fs::rename(src, dst) | |
661 | } | |
662 | ||
663 | /// This function attempts to bypass the auto_da_alloc heuristic implemented by some filesystems | |
664 | /// such as btrfs and ext4. When renaming over a file that already exists then they will "helpfully" | |
665 | /// write back the source file before committing the rename in case a developer forgot some of | |
666 | /// the fsyncs in the open/write/fsync(file)/rename/fsync(dir) dance for atomic file updates. | |
667 | /// | |
668 | /// To avoid triggering this heuristic we delete the destination first, if it exists. | |
669 | /// The cost of an extra syscall is much lower than getting descheduled for the sync IO. | |
670 | #[cfg(target_os = "linux")] | |
671 | pub fn non_durable_rename(src: &Path, dst: &Path) -> std::io::Result<()> { | |
672 | let _ = std::fs::remove_file(dst); | |
673 | std::fs::rename(src, dst) | |
674 | } | |
675 | ||
17df50a5 | 676 | /// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)" |
29967ef6 XL |
677 | pub fn version_str() -> Option<&'static str> { |
678 | option_env!("CFG_VERSION") | |
679 | } | |
680 | ||
681 | /// Returns a version string such as "0.12.0-dev". | |
682 | pub fn release_str() -> Option<&'static str> { | |
683 | option_env!("CFG_RELEASE") | |
684 | } | |
685 | ||
686 | /// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built. | |
687 | pub fn commit_hash_str() -> Option<&'static str> { | |
688 | option_env!("CFG_VER_HASH") | |
689 | } | |
690 | ||
691 | /// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string. | |
692 | pub fn commit_date_str() -> Option<&'static str> { | |
693 | option_env!("CFG_VER_DATE") | |
532ac7d7 | 694 | } |