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