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-01-09 08:23:44 +00:00
|
|
|
use crate::ty::TyCtxt;
|
2016-12-27 09:15:26 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::expand::allocator::AllocatorKind;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_data_structures::sync::{self, MetadataRef};
|
2020-10-15 18:19:54 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
2020-07-29 16:26:15 +00:00
|
|
|
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_macros::HashStable;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::search_paths::PathKind;
|
2020-05-17 21:18:50 +00:00
|
|
|
use rustc_session::utils::NativeLibKind;
|
2021-05-29 10:09:23 +00:00
|
|
|
use rustc_session::StableCrateId;
|
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;
|
2019-12-22 22:42:04 +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-06-11 14:49:57 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, HashStable, 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 {
|
|
|
|
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-03-23 14:48:59 +00:00
|
|
|
#[derive(HashStable)]
|
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 {
|
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-06-11 14:49:57 +00:00
|
|
|
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
|
2016-11-10 02:57:30 +00:00
|
|
|
pub enum LibSource {
|
|
|
|
Some(PathBuf),
|
|
|
|
MetadataOnly,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LibSource {
|
|
|
|
pub fn is_some(&self) -> bool {
|
2020-09-18 17:11:06 +00:00
|
|
|
matches!(self, LibSource::Some(_))
|
2016-11-10 02:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn option(&self) -> Option<PathBuf> {
|
|
|
|
match *self {
|
|
|
|
LibSource::Some(ref p) => Some(p.clone()),
|
|
|
|
LibSource::MetadataOnly | LibSource::None => None,
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 09:18:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Copy, Debug, PartialEq, Clone, Encodable, Decodable, HashStable)]
|
2015-11-24 22:00:26 +00:00
|
|
|
pub enum LinkagePreference {
|
|
|
|
RequireDynamic,
|
|
|
|
RequireStatic,
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
|
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>,
|
2018-02-10 22:28:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 14:19:16 +00:00
|
|
|
#[derive(Clone, TyEncodable, TyDecodable, HashStable, 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
|
|
|
}
|
|
|
|
|
2018-12-03 00:14:35 +00:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable)]
|
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.
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2018-12-03 00:14:35 +00:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable)]
|
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
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Encodable, Decodable)]
|
2017-04-05 21:39:02 +00:00
|
|
|
pub struct EncodedMetadata {
|
2019-12-22 22:42:04 +00:00
|
|
|
pub raw_data: Vec<u8>,
|
2017-04-27 14:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EncodedMetadata {
|
|
|
|
pub fn new() -> EncodedMetadata {
|
2019-12-22 22:42:04 +00:00
|
|
|
EncodedMetadata { raw_data: Vec::new() }
|
2017-04-27 14:12:57 +00:00
|
|
|
}
|
2017-04-05 21:39:02 +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 {
|
2019-12-22 22:42:04 +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-06-01 07:05:22 +00:00
|
|
|
pub trait CrateStore {
|
2019-11-24 15:12:02 +00:00
|
|
|
fn as_any(&self) -> &dyn Any;
|
2016-09-28 23:30:53 +00:00
|
|
|
|
2015-11-24 22:00:26 +00:00
|
|
|
// resolve
|
2016-12-16 22:24:27 +00:00
|
|
|
fn def_key(&self, def: DefId) -> DefKey;
|
2020-10-15 18:19:54 +00:00
|
|
|
fn def_kind(&self, def: DefId) -> DefKind;
|
2020-03-11 11:49:08 +00:00
|
|
|
fn def_path(&self, def: DefId) -> DefPath;
|
|
|
|
fn def_path_hash(&self, def: DefId) -> DefPathHash;
|
2020-07-29 16:26:15 +00:00
|
|
|
fn def_path_hash_to_def_id(
|
|
|
|
&self,
|
|
|
|
cnum: CrateNum,
|
|
|
|
index_guess: u32,
|
|
|
|
hash: DefPathHash,
|
|
|
|
) -> Option<DefId>;
|
2017-08-31 15:07:39 +00:00
|
|
|
|
|
|
|
// "queries" used in resolve that aren't tracked for incremental compilation
|
|
|
|
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
|
2021-05-29 10:09:23 +00:00
|
|
|
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId;
|
2017-09-22 11:03:15 +00:00
|
|
|
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
|
2015-11-24 22:00:26 +00:00
|
|
|
|
|
|
|
// This is basically a 1-based range of ints, which is a little
|
|
|
|
// silly - I may fix that.
|
2017-09-07 15:13:41 +00:00
|
|
|
fn crates_untracked(&self) -> Vec<CrateNum>;
|
2015-11-24 22:00:26 +00:00
|
|
|
|
|
|
|
// utility functions
|
2019-06-21 21:49:03 +00:00
|
|
|
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
|
2021-06-01 07:05:22 +00:00
|
|
|
fn metadata_encoding_version(&self) -> &[u8];
|
2019-11-11 19:57:34 +00:00
|
|
|
fn allocator_kind(&self) -> Option<AllocatorKind>;
|
2015-11-24 22:00:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 10:05:10 +00:00
|
|
|
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
|
2018-03-03 05:19:15 +00:00
|
|
|
|
2017-08-31 19:08:29 +00:00
|
|
|
// This method is used when generating the command line to pass through to
|
|
|
|
// system linker. The linker expects undefined symbols on the left of the
|
|
|
|
// command line to be defined in libraries on the right, not the other way
|
|
|
|
// around. For more info, see some comments in the add_used_library function
|
|
|
|
// below.
|
|
|
|
//
|
|
|
|
// In order to get this left-to-right dependency ordering, we perform a
|
|
|
|
// topological sort of all crates putting the leaves at the right-most
|
|
|
|
// positions.
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn used_crates(tcx: TyCtxt<'_>, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> {
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut libs = tcx
|
|
|
|
.crates()
|
2017-09-07 15:13:41 +00:00
|
|
|
.iter()
|
|
|
|
.cloned()
|
2017-08-31 19:08:29 +00:00
|
|
|
.filter_map(|cnum| {
|
|
|
|
if tcx.dep_kind(cnum).macros_only() {
|
2019-12-22 22:42:04 +00:00
|
|
|
return None;
|
2017-08-31 19:08:29 +00:00
|
|
|
}
|
|
|
|
let source = tcx.used_crate_source(cnum);
|
|
|
|
let path = match prefer {
|
|
|
|
LinkagePreference::RequireDynamic => source.dylib.clone().map(|p| p.0),
|
|
|
|
LinkagePreference::RequireStatic => source.rlib.clone().map(|p| p.0),
|
|
|
|
};
|
|
|
|
let path = match path {
|
|
|
|
Some(p) => LibSource::Some(p),
|
|
|
|
None => {
|
|
|
|
if source.rmeta.is_some() {
|
|
|
|
LibSource::MetadataOnly
|
|
|
|
} else {
|
|
|
|
LibSource::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some((cnum, path))
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-05-11 12:50:54 +00:00
|
|
|
let mut ordering = tcx.postorder_cnums(()).to_owned();
|
2018-12-01 15:57:29 +00:00
|
|
|
ordering.reverse();
|
2019-12-22 22:42:04 +00:00
|
|
|
libs.sort_by_cached_key(|&(a, _)| ordering.iter().position(|x| *x == a));
|
2017-08-31 19:08:29 +00:00
|
|
|
libs
|
|
|
|
}
|