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