]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_target/src/lib.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / compiler / rustc_target / src / lib.rs
1 //! Some stuff used by rustc that doesn't have many dependencies
2 //!
3 //! Originally extracted from rustc::back, which was nominally the
4 //! compiler 'backend', though LLVM is rustc's backend, so rustc_target
5 //! is really just odds-and-ends relating to code gen and linking.
6 //! This crate mostly exists to make rustc smaller, so we might put
7 //! more 'stuff' here in the future. It does not have a dependency on
8 //! LLVM.
9
10 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
11 #![cfg_attr(not(bootstrap), doc(rust_logo))]
12 #![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
13 #![feature(assert_matches)]
14 #![feature(associated_type_bounds)]
15 #![feature(exhaustive_patterns)]
16 #![feature(iter_intersperse)]
17 #![feature(let_chains)]
18 #![feature(min_specialization)]
19 #![feature(never_type)]
20 #![feature(rustc_attrs)]
21 #![feature(step_trait)]
22 #![deny(rustc::untranslatable_diagnostic)]
23 #![deny(rustc::diagnostic_outside_of_impl)]
24 #![allow(internal_features)]
25
26 use std::path::{Path, PathBuf};
27
28 #[macro_use]
29 extern crate rustc_macros;
30
31 #[macro_use]
32 extern crate tracing;
33
34 pub mod abi;
35 pub mod asm;
36 pub mod json;
37 pub mod spec;
38
39 #[cfg(test)]
40 mod tests;
41
42 pub use rustc_abi::HashStableContext;
43
44 /// The name of rustc's own place to organize libraries.
45 ///
46 /// Used to be `rustc`, now the default is `rustlib`.
47 const RUST_LIB_DIR: &str = "rustlib";
48
49 /// Returns a `rustlib` path for this particular target, relative to the provided sysroot.
50 ///
51 /// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
52 /// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
53 pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
54 let libdir = find_libdir(sysroot);
55 PathBuf::from_iter([
56 Path::new(libdir.as_ref()),
57 Path::new(RUST_LIB_DIR),
58 Path::new(target_triple),
59 ])
60 }
61
62 /// The name of the directory rustc expects libraries to be located.
63 fn find_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
64 // FIXME: This is a quick hack to make the rustc binary able to locate
65 // Rust libraries in Linux environments where libraries might be installed
66 // to lib64/lib32. This would be more foolproof by basing the sysroot off
67 // of the directory where `librustc_driver` is located, rather than
68 // where the rustc binary is.
69 // If --libdir is set during configuration to the value other than
70 // "lib" (i.e., non-default), this value is used (see issue #16552).
71
72 #[cfg(target_pointer_width = "64")]
73 const PRIMARY_LIB_DIR: &str = "lib64";
74
75 #[cfg(target_pointer_width = "32")]
76 const PRIMARY_LIB_DIR: &str = "lib32";
77
78 const SECONDARY_LIB_DIR: &str = "lib";
79
80 match option_env!("CFG_LIBDIR_RELATIVE") {
81 None | Some("lib") => {
82 if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
83 PRIMARY_LIB_DIR.into()
84 } else {
85 SECONDARY_LIB_DIR.into()
86 }
87 }
88 Some(libdir) => libdir.into(),
89 }
90 }