rustc: Add a new codegen flag, -C metadata=foo

This metadata is used to drive the hash of all symbols in a crate. The flag can
be specified a multiple number of times

RFC: 0035-remove-crate-id
This commit is contained in:
Alex Crichton 2014-06-30 22:52:48 -07:00
parent 50ee1ec1b4
commit 1007739b20
5 changed files with 20 additions and 1 deletions

View file

@ -593,7 +593,10 @@ pub fn crate_name_hash(sess: &Session, crate_name: &str) -> String {
// the crate id in the hash because lookups are only done by (name/vers),
// not by path.
let mut s = Sha256::new();
s.input_str(crate_id.short_name_with_version().as_slice());
s.input_str(crate_name);
for meta in sess.crate_metadata.borrow().iter() {
s.input_str(meta.as_slice());
}
truncated_hash_result(&mut s).as_slice().slice_to(8).to_string()
}
@ -626,6 +629,9 @@ fn symbol_hash(tcx: &ty::ctxt,
symbol_hasher.input_str(link_meta.crate_name.as_slice());
symbol_hasher.input_str("-");
symbol_hasher.input_str(link_meta.crate_hash.as_str());
for meta in tcx.sess.crate_metadata.borrow().iter() {
symbol_hasher.input_str(meta.as_slice());
}
symbol_hasher.input_str("-");
symbol_hasher.input_str(encoder::encoded_ty(tcx, t).as_slice());
// Prefix with 'h' so that it never blends into adjacent digits

View file

@ -318,6 +318,8 @@ cgoptions!(
"use an external assembler rather than LLVM's integrated one"),
relocation_model: String = ("pic".to_string(), parse_string,
"choose the relocation model to use (llc -relocation-model for details)"),
metadata: Vec<String> = (Vec::new(), parse_list,
"metadata to mangle symbol names with"),
)
pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions

View file

@ -186,6 +186,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
*sess.crate_types.borrow_mut() =
collect_crate_types(sess, krate.attrs.as_slice());
*sess.crate_metadata.borrow_mut() =
collect_crate_metadata(sess, krate.attrs.as_slice());
time(time_passes, "gated feature checking", (), |_|
front::feature_gate::check_crate(sess, &krate));
@ -848,6 +850,11 @@ pub fn collect_crate_types(session: &Session,
}).collect()
}
pub fn collect_crate_metadata(session: &Session,
_attrs: &[ast::Attribute]) -> Vec<String> {
session.opts.cg.metadata.clone()
}
pub struct OutputFilenames {
pub out_directory: Path,
pub out_filestem: String,

View file

@ -311,6 +311,8 @@ fn print_crate_info(sess: &Session,
}
if crate_file_name {
let crate_types = driver::collect_crate_types(sess, attrs.as_slice());
let metadata = driver::collect_crate_metadata(sess, attrs.as_slice());
*sess.crate_metadata.borrow_mut() = metadata;
for &style in crate_types.iter() {
let fname = link::filename_for_input(sess, style, id.as_slice(),
&t_outputs.with_extension(""));

View file

@ -47,6 +47,7 @@ pub struct Session {
pub lints: RefCell<NodeMap<Vec<(lint::LintId, codemap::Span, String)>>>,
pub node_id: Cell<ast::NodeId>,
pub crate_types: RefCell<Vec<config::CrateType>>,
pub crate_metadata: RefCell<Vec<String>>,
pub features: front::feature_gate::Features,
/// The maximum recursion limit for potentially infinitely recursive
@ -243,6 +244,7 @@ pub fn build_session_(sopts: config::Options,
lints: RefCell::new(NodeMap::new()),
node_id: Cell::new(1),
crate_types: RefCell::new(Vec::new()),
crate_metadata: RefCell::new(Vec::new()),
features: front::feature_gate::Features::new(),
recursion_limit: Cell::new(64),
};