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