]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_target/src/lib.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / compiler / rustc_target / src / lib.rs
CommitLineData
83c7162d
XL
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
9fa01778 7//! more 'stuff' here in the future. It does not have a dependency on
b7449926 8//! LLVM.
83c7162d 9
1b1a35ee 10#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
60c5eb7d 11#![feature(bool_to_option)]
0bf4aa26 12#![feature(nll)]
ba9703b0
XL
13#![feature(never_type)]
14#![feature(associated_type_bounds)]
15#![feature(exhaustive_patterns)]
17df50a5 16#![feature(min_specialization)]
94222f64
XL
17#![feature(step_trait)]
18#![feature(unchecked_math)]
17df50a5
XL
19
20use std::path::{Path, PathBuf};
9fa01778 21
3dfed10e 22#[macro_use]
f9f354fc
XL
23extern crate rustc_macros;
24
dfeec247 25#[macro_use]
3dfed10e 26extern crate tracing;
83c7162d 27
83c7162d 28pub mod abi;
f9f354fc 29pub mod asm;
83c7162d 30pub mod spec;
60c5eb7d 31
136023e0
XL
32#[cfg(test)]
33mod tests;
34
60c5eb7d
XL
35/// Requirements for a `StableHashingContext` to be used in this crate.
36/// This is a hack to allow using the `HashStable_Generic` derive macro
cdc7bbd5 37/// instead of implementing everything in `rustc_middle`.
60c5eb7d 38pub trait HashStableContext {}
17df50a5
XL
39
40/// The name of rustc's own place to organize libraries.
41///
42/// Used to be `rustc`, now the default is `rustlib`.
43const RUST_LIB_DIR: &str = "rustlib";
44
45/// Returns a `rustlib` path for this particular target, relative to the provided sysroot.
46///
47/// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
48/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
49pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
50 let libdir = find_libdir(sysroot);
51 std::array::IntoIter::new([
52 Path::new(libdir.as_ref()),
53 Path::new(RUST_LIB_DIR),
54 Path::new(target_triple),
55 ])
56 .collect::<PathBuf>()
57}
58
59/// The name of the directory rustc expects libraries to be located.
60fn 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}