Skip to content

Commit 5196ca8

Browse files
authored
Auto merge of rust-lang#37681 - nrc:crate-metadata, r=@alexcrichton
add --crate-type metadata r? @alexcrichton
2 parents 1cabe21 + af1b195 commit 5196ca8

25 files changed

+374
-81
lines changed

src/librustc/middle/cstore.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ pub struct LinkMeta {
5656
pub crate_hash: Svh,
5757
}
5858

59-
// Where a crate came from on the local filesystem. One of these two options
59+
// Where a crate came from on the local filesystem. One of these three options
6060
// must be non-None.
6161
#[derive(PartialEq, Clone, Debug)]
6262
pub struct CrateSource {
6363
pub dylib: Option<(PathBuf, PathKind)>,
6464
pub rlib: Option<(PathBuf, PathKind)>,
65+
pub rmeta: Option<(PathBuf, PathKind)>,
6566
}
6667

6768
#[derive(RustcEncodable, RustcDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
@@ -76,6 +77,30 @@ pub enum DepKind {
7677
Explicit,
7778
}
7879

80+
#[derive(PartialEq, Clone, Debug)]
81+
pub enum LibSource {
82+
Some(PathBuf),
83+
MetadataOnly,
84+
None,
85+
}
86+
87+
impl LibSource {
88+
pub fn is_some(&self) -> bool {
89+
if let LibSource::Some(_) = *self {
90+
true
91+
} else {
92+
false
93+
}
94+
}
95+
96+
pub fn option(&self) -> Option<PathBuf> {
97+
match *self {
98+
LibSource::Some(ref p) => Some(p.clone()),
99+
LibSource::MetadataOnly | LibSource::None => None,
100+
}
101+
}
102+
}
103+
79104
#[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
80105
pub enum LinkagePreference {
81106
RequireDynamic,
@@ -244,7 +269,7 @@ pub trait CrateStore<'tcx> {
244269
// utility functions
245270
fn metadata_filename(&self) -> &str;
246271
fn metadata_section_name(&self, target: &Target) -> &str;
247-
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, Option<PathBuf>)>;
272+
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
248273
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
249274
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
250275
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -427,7 +452,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
427452
// utility functions
428453
fn metadata_filename(&self) -> &str { bug!("metadata_filename") }
429454
fn metadata_section_name(&self, target: &Target) -> &str { bug!("metadata_section_name") }
430-
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, Option<PathBuf>)>
455+
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
431456
{ vec![] }
432457
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
433458
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }

src/librustc/middle/dependency_format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn calculate_type(sess: &session::Session,
114114

115115
// No linkage happens with rlibs, we just needed the metadata (which we
116116
// got long ago), so don't bother with anything.
117-
config::CrateTypeRlib => return Vec::new(),
117+
config::CrateTypeRlib | config::CrateTypeMetadata => return Vec::new(),
118118

119119
// Staticlibs and cdylibs must have all static dependencies. If any fail
120120
// to be found, we generate some nice pretty errors.
@@ -192,7 +192,7 @@ fn calculate_type(sess: &session::Session,
192192
if src.dylib.is_none() &&
193193
!formats.contains_key(&cnum) &&
194194
sess.cstore.dep_kind(cnum) == DepKind::Explicit {
195-
assert!(src.rlib.is_some());
195+
assert!(src.rlib.is_some() || src.rmeta.is_some());
196196
info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
197197
add_library(sess, cnum, RequireStatic, &mut formats);
198198
ret[cnum.as_usize() - 1] = Linkage::Static;

src/librustc/middle/reachable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
140140
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ReachableContext<'a, 'tcx> {
141141
let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
142142
*ty == config::CrateTypeRlib || *ty == config::CrateTypeDylib ||
143-
*ty == config::CrateTypeProcMacro
143+
*ty == config::CrateTypeProcMacro || *ty == config::CrateTypeMetadata
144144
});
145145
ReachableContext {
146146
tcx: tcx,

src/librustc/middle/weak_lang_items.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ fn verify(sess: &Session, items: &lang_items::LanguageItems) {
7575
config::CrateTypeCdylib |
7676
config::CrateTypeExecutable |
7777
config::CrateTypeStaticlib => true,
78-
config::CrateTypeRlib => false,
78+
config::CrateTypeRlib |
79+
config::CrateTypeMetadata => false,
7980
}
8081
});
8182
if !needs_check {

src/librustc/session/config.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,6 @@ pub enum OutputType {
7878
DepInfo,
7979
}
8080

81-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
82-
pub enum ErrorOutputType {
83-
HumanReadable(ColorConfig),
84-
Json,
85-
}
86-
87-
impl Default for ErrorOutputType {
88-
fn default() -> ErrorOutputType {
89-
ErrorOutputType::HumanReadable(ColorConfig::Auto)
90-
}
91-
}
92-
9381
impl OutputType {
9482
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
9583
match *self {
@@ -125,6 +113,18 @@ impl OutputType {
125113
}
126114
}
127115

116+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
117+
pub enum ErrorOutputType {
118+
HumanReadable(ColorConfig),
119+
Json,
120+
}
121+
122+
impl Default for ErrorOutputType {
123+
fn default() -> ErrorOutputType {
124+
ErrorOutputType::HumanReadable(ColorConfig::Auto)
125+
}
126+
}
127+
128128
// Use tree-based collections to cheaply get a deterministic Hash implementation.
129129
// DO NOT switch BTreeMap out for an unsorted container type! That would break
130130
// dependency tracking for commandline arguments.
@@ -483,6 +483,7 @@ pub enum CrateType {
483483
CrateTypeStaticlib,
484484
CrateTypeCdylib,
485485
CrateTypeProcMacro,
486+
CrateTypeMetadata,
486487
}
487488

488489
#[derive(Clone, Hash)]
@@ -1147,7 +1148,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
11471148
assumed.", "[KIND=]NAME"),
11481149
opt::multi_s("", "crate-type", "Comma separated list of types of crates
11491150
for the compiler to emit",
1150-
"[bin|lib|rlib|dylib|cdylib|staticlib]"),
1151+
"[bin|lib|rlib|dylib|cdylib|staticlib|metadata]"),
11511152
opt::opt_s("", "crate-name", "Specify the name of the crate being built",
11521153
"NAME"),
11531154
opt::multi_s("", "emit", "Comma separated list of types of output for \
@@ -1539,6 +1540,7 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
15391540
"cdylib" => CrateTypeCdylib,
15401541
"bin" => CrateTypeExecutable,
15411542
"proc-macro" => CrateTypeProcMacro,
1543+
"metadata" => CrateTypeMetadata,
15421544
_ => {
15431545
return Err(format!("unknown crate type: `{}`",
15441546
part));
@@ -1623,6 +1625,7 @@ impl fmt::Display for CrateType {
16231625
CrateTypeStaticlib => "staticlib".fmt(f),
16241626
CrateTypeCdylib => "cdylib".fmt(f),
16251627
CrateTypeProcMacro => "proc-macro".fmt(f),
1628+
CrateTypeMetadata => "metadata".fmt(f),
16261629
}
16271630
}
16281631
}

src/librustc_driver/driver.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,9 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
11821182
Some(ref n) if *n == "rlib" => {
11831183
Some(config::CrateTypeRlib)
11841184
}
1185+
Some(ref n) if *n == "metadata" => {
1186+
Some(config::CrateTypeMetadata)
1187+
}
11851188
Some(ref n) if *n == "dylib" => {
11861189
Some(config::CrateTypeDylib)
11871190
}

src/librustc_metadata/creader.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use log;
4444
pub struct Library {
4545
pub dylib: Option<(PathBuf, PathKind)>,
4646
pub rlib: Option<(PathBuf, PathKind)>,
47+
pub rmeta: Option<(PathBuf, PathKind)>,
4748
pub metadata: MetadataBlob,
4849
}
4950

@@ -62,10 +63,11 @@ fn dump_crates(cstore: &CStore) {
6263
info!(" cnum: {}", data.cnum);
6364
info!(" hash: {}", data.hash());
6465
info!(" reqd: {:?}", data.dep_kind.get());
65-
let CrateSource { dylib, rlib } = data.source.clone();
66+
let CrateSource { dylib, rlib, rmeta } = data.source.clone();
6667
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
6768
rlib.map(|rl| info!(" rlib: {}", rl.0.display()));
68-
})
69+
rmeta.map(|rl| info!(" rmeta: {}", rl.0.display()));
70+
});
6971
}
7072

7173
#[derive(Debug)]
@@ -278,14 +280,15 @@ impl<'a> CrateLoader<'a> {
278280
ident: ident.to_string(),
279281
dylib: lib.dylib.clone().map(|p| p.0),
280282
rlib: lib.rlib.clone().map(|p| p.0),
283+
rmeta: lib.rmeta.clone().map(|p| p.0),
281284
})
282285
} else {
283286
None
284287
};
285288
// Maintain a reference to the top most crate.
286289
let root = if root.is_some() { root } else { &crate_paths };
287290

288-
let Library { dylib, rlib, metadata } = lib;
291+
let Library { dylib, rlib, rmeta, metadata } = lib;
289292

290293
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
291294

@@ -305,6 +308,7 @@ impl<'a> CrateLoader<'a> {
305308
source: cstore::CrateSource {
306309
dylib: dylib,
307310
rlib: rlib,
311+
rmeta: rmeta,
308312
},
309313
});
310314

@@ -767,7 +771,8 @@ impl<'a> CrateLoader<'a> {
767771
config::CrateTypeProcMacro |
768772
config::CrateTypeCdylib |
769773
config::CrateTypeStaticlib => need_lib_alloc = true,
770-
config::CrateTypeRlib => {}
774+
config::CrateTypeRlib |
775+
config::CrateTypeMetadata => {}
771776
}
772777
}
773778
if !need_lib_alloc && !need_exe_alloc { return }

src/librustc_metadata/cstore.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use rustc::util::nodemap::{FxHashMap, NodeMap, NodeSet, DefIdMap};
2525

2626
use std::cell::{RefCell, Cell};
2727
use std::rc::Rc;
28-
use std::path::PathBuf;
2928
use flate::Bytes;
3029
use syntax::{ast, attr};
3130
use syntax::ext::base::SyntaxExtension;
@@ -34,7 +33,7 @@ use syntax_pos;
3433

3534
pub use rustc::middle::cstore::{NativeLibrary, LinkagePreference};
3635
pub use rustc::middle::cstore::{NativeStatic, NativeFramework, NativeUnknown};
37-
pub use rustc::middle::cstore::{CrateSource, LinkMeta};
36+
pub use rustc::middle::cstore::{CrateSource, LinkMeta, LibSource};
3837

3938
// A map from external crate numbers (as decoded from some crate file) to
4039
// local crate numbers (as generated during this session). Each external
@@ -45,6 +44,7 @@ pub type CrateNumMap = IndexVec<CrateNum, CrateNum>;
4544
pub enum MetadataBlob {
4645
Inflated(Bytes),
4746
Archive(locator::ArchiveMetadata),
47+
Raw(Vec<u8>),
4848
}
4949

5050
/// Holds information about a syntax_pos::FileMap imported from another crate.
@@ -186,7 +186,7 @@ impl CStore {
186186
// positions.
187187
pub fn do_get_used_crates(&self,
188188
prefer: LinkagePreference)
189-
-> Vec<(CrateNum, Option<PathBuf>)> {
189+
-> Vec<(CrateNum, LibSource)> {
190190
let mut ordering = Vec::new();
191191
for (&num, _) in self.metas.borrow().iter() {
192192
self.push_dependencies_in_postorder(&mut ordering, num);
@@ -202,6 +202,16 @@ impl CStore {
202202
LinkagePreference::RequireDynamic => data.source.dylib.clone().map(|p| p.0),
203203
LinkagePreference::RequireStatic => data.source.rlib.clone().map(|p| p.0),
204204
};
205+
let path = match path {
206+
Some(p) => LibSource::Some(p),
207+
None => {
208+
if data.source.rmeta.is_some() {
209+
LibSource::MetadataOnly
210+
} else {
211+
LibSource::None
212+
}
213+
}
214+
};
205215
Some((cnum, path))
206216
})
207217
.collect::<Vec<_>>();

src/librustc_metadata/cstore_impl.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use encoder;
1313
use locator;
1414
use schema;
1515

16-
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, DepKind, ExternCrate};
16+
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, LibSource, DepKind, ExternCrate};
1717
use rustc::middle::cstore::{NativeLibrary, LinkMeta, LinkagePreference, LoadedMacro};
1818
use rustc::hir::def::{self, Def};
1919
use rustc::middle::lang_items;
@@ -28,7 +28,6 @@ use rustc::mir::Mir;
2828
use rustc::util::nodemap::{NodeSet, DefIdMap};
2929
use rustc_back::PanicStrategy;
3030

31-
use std::path::PathBuf;
3231
use syntax::ast;
3332
use syntax::attr;
3433
use syntax::parse::new_parser_from_source_str;
@@ -545,7 +544,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
545544
locator::meta_section_name(target)
546545
}
547546

548-
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, Option<PathBuf>)>
547+
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>
549548
{
550549
self.do_get_used_crates(prefer)
551550
}

src/librustc_metadata/decoder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ pub trait Metadata<'a, 'tcx>: Copy {
8888
impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
8989
fn raw_bytes(self) -> &'a [u8] {
9090
match *self {
91-
MetadataBlob::Inflated(ref vec) => &vec[..],
91+
MetadataBlob::Inflated(ref vec) => vec,
9292
MetadataBlob::Archive(ref ar) => ar.as_slice(),
93+
MetadataBlob::Raw(ref vec) => vec,
9394
}
9495
}
9596
}

0 commit comments

Comments
 (0)