2017-08-05 08:42:53 +00:00
|
|
|
//! the rustc crate store interface. This also includes types that
|
|
|
|
//! are *mostly* used as a part of that interface, but these should
|
|
|
|
//! probably get a better home if someone can find one.
|
2015-11-24 22:00:26 +00:00
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
use crate::search_paths::PathKind;
|
|
|
|
use crate::utils::NativeLibKind;
|
2021-10-02 15:35:27 +00:00
|
|
|
use crate::Session;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2019-10-20 11:06:48 +00:00
|
|
|
use rustc_data_structures::sync::{self, MetadataRef};
|
2021-07-10 21:34:41 +00:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, StableCrateId, LOCAL_CRATE};
|
2020-07-29 16:26:15 +00:00
|
|
|
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
|
2021-06-27 13:51:25 +00:00
|
|
|
use rustc_span::hygiene::{ExpnHash, ExpnId};
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2017-12-08 19:18:21 +00:00
|
|
|
use rustc_target::spec::Target;
|
2020-03-11 11:49:08 +00:00
|
|
|
|
2016-09-28 23:30:53 +00:00
|
|
|
use std::any::Any;
|
2017-04-26 21:22:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-11-24 22:00:26 +00:00
|
|
|
|
|
|
|
// lonely orphan structs and enums looking for a better home
|
|
|
|
|
2017-08-05 08:42:53 +00:00
|
|
|
/// Where a crate came from on the local filesystem. One of these three options
|
|
|
|
/// must be non-None.
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, HashStable_Generic, Encodable, Decodable)]
|
2015-11-24 22:00:26 +00:00
|
|
|
pub struct CrateSource {
|
|
|
|
pub dylib: Option<(PathBuf, PathKind)>,
|
|
|
|
pub rlib: Option<(PathBuf, PathKind)>,
|
2016-11-10 02:57:30 +00:00
|
|
|
pub rmeta: Option<(PathBuf, PathKind)>,
|
2015-11-24 22:00:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 13:42:14 +00:00
|
|
|
impl CrateSource {
|
2021-10-03 14:07:26 +00:00
|
|
|
#[inline]
|
2019-10-06 13:42:14 +00:00
|
|
|
pub fn paths(&self) -> impl Iterator<Item = &PathBuf> {
|
|
|
|
self.dylib.iter().chain(self.rlib.iter()).chain(self.rmeta.iter()).map(|p| &p.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Encodable, Decodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(HashStable_Generic)]
|
2020-08-02 12:24:22 +00:00
|
|
|
pub enum CrateDepKind {
|
2016-10-28 05:56:06 +00:00
|
|
|
/// A dependency that is only used for its macros.
|
|
|
|
MacrosOnly,
|
2016-10-27 09:18:45 +00:00
|
|
|
/// A dependency that is always injected into the dependency list and so
|
2018-11-27 02:59:49 +00:00
|
|
|
/// doesn't need to be linked to an rlib, e.g., the injected allocator.
|
2016-10-27 09:18:45 +00:00
|
|
|
Implicit,
|
|
|
|
/// A dependency that is required by an rlib version of this crate.
|
|
|
|
/// Ordinary `extern crate`s result in `Explicit` dependencies.
|
|
|
|
Explicit,
|
|
|
|
}
|
|
|
|
|
2020-08-02 12:24:22 +00:00
|
|
|
impl CrateDepKind {
|
2021-10-03 14:07:26 +00:00
|
|
|
#[inline]
|
2016-11-27 02:57:15 +00:00
|
|
|
pub fn macros_only(self) -> bool {
|
|
|
|
match self {
|
2020-08-02 12:24:22 +00:00
|
|
|
CrateDepKind::MacrosOnly => true,
|
|
|
|
CrateDepKind::Implicit | CrateDepKind::Explicit => false,
|
2016-11-27 02:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable_Generic)]
|
2015-11-24 22:00:26 +00:00
|
|
|
pub enum LinkagePreference {
|
|
|
|
RequireDynamic,
|
|
|
|
RequireStatic,
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Debug, Encodable, Decodable, HashStable_Generic)]
|
2020-05-17 21:18:50 +00:00
|
|
|
pub struct NativeLib {
|
|
|
|
pub kind: NativeLibKind,
|
2018-07-16 18:31:14 +00:00
|
|
|
pub name: Option<Symbol>,
|
2016-11-15 10:17:24 +00:00
|
|
|
pub cfg: Option<ast::MetaItem>,
|
2018-02-10 22:28:17 +00:00
|
|
|
pub foreign_module: Option<DefId>,
|
2018-07-16 18:31:14 +00:00
|
|
|
pub wasm_import_module: Option<Symbol>,
|
2021-03-25 04:45:09 +00:00
|
|
|
pub verbatim: Option<bool>,
|
2021-03-08 20:42:54 +00:00
|
|
|
pub dll_imports: Vec<DllImport>,
|
|
|
|
}
|
|
|
|
|
2022-02-11 07:08:35 +00:00
|
|
|
impl NativeLib {
|
|
|
|
pub fn has_modifiers(&self) -> bool {
|
|
|
|
self.verbatim.is_some() || self.kind.has_modifiers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 20:52:35 +00:00
|
|
|
/// Different ways that the PE Format can decorate a symbol name.
|
|
|
|
/// From <https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-name-type>
|
|
|
|
#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq, Eq)]
|
|
|
|
pub enum PeImportNameType {
|
|
|
|
/// IMPORT_ORDINAL
|
|
|
|
/// Uses the ordinal (i.e., a number) rather than the name.
|
|
|
|
Ordinal(u16),
|
|
|
|
/// Same as IMPORT_NAME
|
|
|
|
/// Name is decorated with all prefixes and suffixes.
|
|
|
|
Decorated,
|
|
|
|
/// Same as IMPORT_NAME_NOPREFIX
|
|
|
|
/// Prefix (e.g., the leading `_` or `@`) is skipped, but suffix is kept.
|
|
|
|
NoPrefix,
|
|
|
|
/// Same as IMPORT_NAME_UNDECORATE
|
|
|
|
/// Prefix (e.g., the leading `_` or `@`) and suffix (the first `@` and all
|
|
|
|
/// trailing characters) are skipped.
|
|
|
|
Undecorated,
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
|
2021-03-08 20:42:54 +00:00
|
|
|
pub struct DllImport {
|
|
|
|
pub name: Symbol,
|
2022-07-12 20:52:35 +00:00
|
|
|
pub import_name_type: Option<PeImportNameType>,
|
2021-06-08 20:56:06 +00:00
|
|
|
/// Calling convention for the function.
|
|
|
|
///
|
|
|
|
/// On x86_64, this is always `DllCallingConvention::C`; on i686, it can be any
|
|
|
|
/// of the values, and we use `DllCallingConvention::C` to represent `"cdecl"`.
|
|
|
|
pub calling_convention: DllCallingConvention,
|
|
|
|
/// Span of import's "extern" declaration; used for diagnostics.
|
|
|
|
pub span: Span,
|
2022-07-12 20:52:35 +00:00
|
|
|
/// Is this for a function (rather than a static variable).
|
|
|
|
pub is_fn: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DllImport {
|
|
|
|
pub fn ordinal(&self) -> Option<u16> {
|
|
|
|
if let Some(PeImportNameType::Ordinal(ordinal)) = self.import_name_type {
|
|
|
|
Some(ordinal)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Calling convention for a function defined in an external library.
|
|
|
|
///
|
|
|
|
/// The usize value, where present, indicates the size of the function's argument list
|
|
|
|
/// in bytes.
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
|
2021-06-08 20:56:06 +00:00
|
|
|
pub enum DllCallingConvention {
|
|
|
|
C,
|
|
|
|
Stdcall(usize),
|
|
|
|
Fastcall(usize),
|
|
|
|
Vectorcall(usize),
|
2018-02-10 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Clone, Encodable, Decodable, HashStable_Generic, Debug)]
|
2018-02-10 22:28:17 +00:00
|
|
|
pub struct ForeignModule {
|
2017-09-12 15:07:09 +00:00
|
|
|
pub foreign_items: Vec<DefId>,
|
2018-02-10 22:28:17 +00:00
|
|
|
pub def_id: DefId,
|
rustc: Implement #[link(cfg(..))] and crt-static
This commit is an implementation of [RFC 1721] which adds a new target feature
to the compiler, `crt-static`, which can be used to select how the C runtime for
a target is linked. Most targets dynamically linke the C runtime by default with
the notable exception of some of the musl targets.
[RFC 1721]: https://github.com/rust-lang/rfcs/blob/master/text/1721-crt-static.md
This commit first adds the new target-feature, `crt-static`. If enabled, then
the `cfg(target_feature = "crt-static")` will be available. Targets like musl
will have this enabled by default. This feature can be controlled through the
standard target-feature interface, `-C target-feature=+crt-static` or
`-C target-feature=-crt-static`.
Next this adds an gated and unstable `#[link(cfg(..))]` feature to enable the
`crt-static` semantics we want with libc. The exact behavior of this attribute
is a little squishy, but it's intended to be a forever-unstable
implementation detail of the liblibc crate.
Specifically the `#[link(cfg(..))]` annotation means that the `#[link]`
directive is only active in a compilation unit if that `cfg` value is satisfied.
For example when compiling an rlib, these directives are just encoded and
ignored for dylibs, and all staticlibs are continued to be put into the rlib as
usual. When placing that rlib into a staticlib, executable, or dylib, however,
the `cfg` is evaluated *as if it were defined in the final artifact* and the
library is decided to be linked or not.
Essentially, what'll happen is:
* On MSVC with `-C target-feature=-crt-static`, the `msvcrt.lib` library will be
linked to.
* On MSVC with `-C target-feature=+crt-static`, the `libcmt.lib` library will be
linked to.
* On musl with `-C target-feature=-crt-static`, the object files in liblibc.rlib
are removed and `-lc` is passed instead.
* On musl with `-C target-feature=+crt-static`, the object files in liblibc.rlib
are used and `-lc` is not passed.
This commit does **not** include an update to the liblibc module to implement
these changes. I plan to do that just after the 1.14.0 beta release is cut to
ensure we get ample time to test this feature.
cc #37406
2016-10-31 23:40:13 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
2016-03-16 09:50:38 +00:00
|
|
|
pub struct ExternCrate {
|
2018-04-10 14:01:24 +00:00
|
|
|
pub src: ExternCrateSource,
|
2016-03-16 09:50:38 +00:00
|
|
|
|
|
|
|
/// span of the extern crate that caused this to be loaded
|
|
|
|
pub span: Span,
|
|
|
|
|
2018-04-13 00:31:43 +00:00
|
|
|
/// Number of links to reach the extern;
|
|
|
|
/// used to select the extern with the shortest path
|
|
|
|
pub path_len: usize,
|
|
|
|
|
2019-09-27 13:32:04 +00:00
|
|
|
/// Crate that depends on this crate
|
|
|
|
pub dependency_of: CrateNum,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExternCrate {
|
2016-03-16 09:50:38 +00:00
|
|
|
/// If true, then this crate is the crate named by the extern
|
|
|
|
/// crate referenced above. If false, then this crate is a dep
|
|
|
|
/// of the crate.
|
2021-10-03 14:07:26 +00:00
|
|
|
#[inline]
|
2019-09-27 13:32:04 +00:00
|
|
|
pub fn is_direct(&self) -> bool {
|
|
|
|
self.dependency_of == LOCAL_CRATE
|
|
|
|
}
|
2019-11-23 20:01:57 +00:00
|
|
|
|
2021-10-03 14:07:26 +00:00
|
|
|
#[inline]
|
2019-11-23 20:01:57 +00:00
|
|
|
pub fn rank(&self) -> impl PartialOrd {
|
|
|
|
// Prefer:
|
|
|
|
// - direct extern crate to indirect
|
|
|
|
// - shorter paths to longer
|
|
|
|
(self.is_direct(), !self.path_len)
|
|
|
|
}
|
2018-04-10 14:01:24 +00:00
|
|
|
}
|
2016-03-16 09:50:38 +00:00
|
|
|
|
2020-11-14 02:02:03 +00:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
2018-04-10 14:01:24 +00:00
|
|
|
pub enum ExternCrateSource {
|
|
|
|
/// Crate is loaded by `extern crate`.
|
2018-04-13 00:31:43 +00:00
|
|
|
Extern(
|
2018-04-10 14:01:24 +00:00
|
|
|
/// def_id of the item in the current crate that caused
|
|
|
|
/// this crate to be loaded; note that there could be multiple
|
|
|
|
/// such ids
|
2018-04-13 00:31:43 +00:00
|
|
|
DefId,
|
|
|
|
),
|
2019-09-29 18:14:29 +00:00
|
|
|
/// Crate is implicitly loaded by a path resolving through extern prelude.
|
2018-04-10 14:01:24 +00:00
|
|
|
Path,
|
2016-03-16 09:50:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 21:22:45 +00:00
|
|
|
/// The backend's way to give the crate store access to the metadata in a library.
|
|
|
|
/// Note that it returns the raw metadata bytes stored in the library file, whether
|
|
|
|
/// it is compressed, uncompressed, some weird mix, etc.
|
|
|
|
/// rmeta files are backend independent and not handled here.
|
|
|
|
///
|
|
|
|
/// At the time of this writing, there is only one backend and one way to store
|
|
|
|
/// metadata in library -- this trait just serves to decouple rustc_metadata from
|
|
|
|
/// the archive reader, which depends on LLVM.
|
|
|
|
pub trait MetadataLoader {
|
2018-03-03 05:17:06 +00:00
|
|
|
fn get_rlib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
|
|
|
|
fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
|
2017-04-26 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 18:26:13 +00:00
|
|
|
pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
|
|
|
|
|
2019-08-16 09:16:40 +00:00
|
|
|
/// A store of Rust crates, through which their metadata can be accessed.
|
2017-08-31 15:07:39 +00:00
|
|
|
///
|
|
|
|
/// Note that this trait should probably not be expanding today. All new
|
|
|
|
/// functionality should be driven through queries instead!
|
|
|
|
///
|
|
|
|
/// If you find a method on this trait named `{name}_untracked` it signifies
|
|
|
|
/// that it's *not* tracked for dependency information throughout compilation
|
|
|
|
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
|
|
|
|
/// during resolve)
|
2021-04-04 12:40:35 +00:00
|
|
|
pub trait CrateStore: std::fmt::Debug {
|
2019-11-24 15:12:02 +00:00
|
|
|
fn as_any(&self) -> &dyn Any;
|
2016-09-28 23:30:53 +00:00
|
|
|
|
2021-07-12 19:20:16 +00:00
|
|
|
// Foreign definitions.
|
|
|
|
// This information is safe to access, since it's hashed as part of the DefPathHash, which incr.
|
|
|
|
// comp. uses to identify a DefId.
|
2016-12-16 22:24:27 +00:00
|
|
|
fn def_key(&self, def: DefId) -> DefKey;
|
2020-03-11 11:49:08 +00:00
|
|
|
fn def_path(&self, def: DefId) -> DefPath;
|
|
|
|
fn def_path_hash(&self, def: DefId) -> DefPathHash;
|
2021-07-12 19:20:16 +00:00
|
|
|
|
|
|
|
// This information is safe to access, since it's hashed as part of the StableCrateId, which
|
|
|
|
// incr. comp. uses to identify a CrateNum.
|
|
|
|
fn crate_name(&self, cnum: CrateNum) -> Symbol;
|
|
|
|
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
|
2021-07-20 11:59:12 +00:00
|
|
|
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
|
2021-07-12 19:20:16 +00:00
|
|
|
|
|
|
|
/// Fetch a DefId from a DefPathHash for a foreign crate.
|
2021-07-20 12:18:37 +00:00
|
|
|
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
|
2021-10-02 15:35:27 +00:00
|
|
|
fn expn_hash_to_expn_id(
|
|
|
|
&self,
|
|
|
|
sess: &Session,
|
|
|
|
cnum: CrateNum,
|
|
|
|
index_guess: u32,
|
|
|
|
hash: ExpnHash,
|
|
|
|
) -> ExpnId;
|
2021-12-21 19:01:10 +00:00
|
|
|
|
|
|
|
/// Imports all `SourceFile`s from the given crate into the current session.
|
|
|
|
/// This normally happens automatically when we decode a `Span` from
|
|
|
|
/// that crate's metadata - however, the incr comp cache needs
|
|
|
|
/// to trigger this manually when decoding a foreign `Span`
|
|
|
|
fn import_source_files(&self, sess: &Session, cnum: CrateNum);
|
2015-11-24 22:00:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 10:05:10 +00:00
|
|
|
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
|