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