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#![warn(unreachable_pub)]
20// tidy-alphabetical-end
21
22use std::path::{Path, PathBuf};
23
24pub mod asm;
25pub mod callconv;
26pub mod json;
27pub mod spec;
28pub mod target_features;
29
30#[cfg(test)]
31mod tests;
32
33use rustc_abi::HashStableContext;
34
35/// The name of rustc's own place to organize libraries.
36///
37/// Used to be `rustc`, now the default is `rustlib`.
38const 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"`.
44pub fn relative_target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
45    let libdir = find_relative_libdir(sysroot);
46    Path::new(libdir.as_ref()).join(RUST_LIB_DIR).join(target_triple)
47}
48
49/// The name of the directory rustc expects libraries to be located.
50fn find_relative_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
51    // FIXME: This is a quick hack to make the rustc binary able to locate
52    // Rust libraries in Linux environments where libraries might be installed
53    // to lib64/lib32. This would be more foolproof by basing the sysroot off
54    // of the directory where `librustc_driver` is located, rather than
55    // where the rustc binary is.
56    // If --libdir is set during configuration to the value other than
57    // "lib" (i.e., non-default), this value is used (see issue #16552).
58
59    #[cfg(target_pointer_width = "64")]
60    const PRIMARY_LIB_DIR: &str = "lib64";
61
62    #[cfg(target_pointer_width = "32")]
63    const PRIMARY_LIB_DIR: &str = "lib32";
64
65    const SECONDARY_LIB_DIR: &str = "lib";
66
67    match option_env!("CFG_LIBDIR_RELATIVE") {
68        None | Some("lib") => {
69            if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
70                PRIMARY_LIB_DIR.into()
71            } else {
72                SECONDARY_LIB_DIR.into()
73            }
74        }
75        Some(libdir) => libdir.into(),
76    }
77}