]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/lib.rs
Rollup merge of #97613 - jsha:implementation-is-on-local-type, r=GuillaumeGomez
[rust.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 #![feature(associated_type_bounds)]
12 #![feature(exhaustive_patterns)]
13 #![feature(let_else)]
14 #![feature(min_specialization)]
15 #![feature(never_type)]
16 #![feature(nll)]
17 #![feature(rustc_attrs)]
18 #![feature(step_trait)]
19
20 use std::iter::FromIterator;
21 use std::path::{Path, PathBuf};
22
23 #[macro_use]
24 extern crate rustc_macros;
25
26 #[macro_use]
27 extern crate tracing;
28
29 pub mod abi;
30 pub mod asm;
31 pub mod spec;
32
33 #[cfg(test)]
34 mod tests;
35
36 /// Requirements for a `StableHashingContext` to be used in this crate.
37 /// This is a hack to allow using the `HashStable_Generic` derive macro
38 /// instead of implementing everything in `rustc_middle`.
39 pub trait HashStableContext {}
40
41 /// The name of rustc's own place to organize libraries.
42 ///
43 /// Used to be `rustc`, now the default is `rustlib`.
44 const RUST_LIB_DIR: &str = "rustlib";
45
46 /// Returns a `rustlib` path for this particular target, relative to the provided sysroot.
47 ///
48 /// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
49 /// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
50 pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
51     let libdir = find_libdir(sysroot);
52     PathBuf::from_iter([
53         Path::new(libdir.as_ref()),
54         Path::new(RUST_LIB_DIR),
55         Path::new(target_triple),
56     ])
57 }
58
59 /// The name of the directory rustc expects libraries to be located.
60 fn find_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
61     // FIXME: This is a quick hack to make the rustc binary able to locate
62     // Rust libraries in Linux environments where libraries might be installed
63     // to lib64/lib32. This would be more foolproof by basing the sysroot off
64     // of the directory where `librustc_driver` is located, rather than
65     // where the rustc binary is.
66     // If --libdir is set during configuration to the value other than
67     // "lib" (i.e., non-default), this value is used (see issue #16552).
68
69     #[cfg(target_pointer_width = "64")]
70     const PRIMARY_LIB_DIR: &str = "lib64";
71
72     #[cfg(target_pointer_width = "32")]
73     const PRIMARY_LIB_DIR: &str = "lib32";
74
75     const SECONDARY_LIB_DIR: &str = "lib";
76
77     match option_env!("CFG_LIBDIR_RELATIVE") {
78         None | Some("lib") => {
79             if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
80                 PRIMARY_LIB_DIR.into()
81             } else {
82                 SECONDARY_LIB_DIR.into()
83             }
84         }
85         Some(libdir) => libdir.into(),
86     }
87 }