rustc_target/
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// tidy-alphabetical-start
11#![allow(internal_features)]
12#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
13#![doc(rust_logo)]
14#![feature(assert_matches)]
15#![feature(iter_intersperse)]
16#![feature(let_chains)]
17#![feature(rustc_attrs)]
18#![feature(rustdoc_internals)]
19// tidy-alphabetical-end
20
21use std::path::{Path, PathBuf};
22
23pub mod asm;
24pub mod callconv;
25pub mod json;
26pub mod spec;
27pub mod target_features;
28
29#[cfg(test)]
30mod tests;
31
32use rustc_abi::HashStableContext;
33
34/// The name of rustc's own place to organize libraries.
35///
36/// Used to be `rustc`, now the default is `rustlib`.
37const RUST_LIB_DIR: &str = "rustlib";
38
39/// Returns a `rustlib` path for this particular target, relative to the provided sysroot.
40///
41/// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
42/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
43pub fn relative_target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
44    let libdir = find_relative_libdir(sysroot);
45    Path::new(libdir.as_ref()).join(RUST_LIB_DIR).join(target_triple)
46}
47
48/// The name of the directory rustc expects libraries to be located.
49fn find_relative_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
50    // FIXME: This is a quick hack to make the rustc binary able to locate
51    // Rust libraries in Linux environments where libraries might be installed
52    // to lib64/lib32. This would be more foolproof by basing the sysroot off
53    // of the directory where `librustc_driver` is located, rather than
54    // where the rustc binary is.
55    // If --libdir is set during configuration to the value other than
56    // "lib" (i.e., non-default), this value is used (see issue #16552).
57
58    #[cfg(target_pointer_width = "64")]
59    const PRIMARY_LIB_DIR: &str = "lib64";
60
61    #[cfg(target_pointer_width = "32")]
62    const PRIMARY_LIB_DIR: &str = "lib32";
63
64    const SECONDARY_LIB_DIR: &str = "lib";
65
66    match option_env!("CFG_LIBDIR_RELATIVE") {
67        None | Some("lib") => {
68            if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
69                PRIMARY_LIB_DIR.into()
70            } else {
71                SECONDARY_LIB_DIR.into()
72            }
73        }
74        Some(libdir) => libdir.into(),
75    }
76}