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