Skip to content

rustc: Always name bytecode based on crate ids #13321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ fn link_binary_output(sess: &Session,

match crate_type {
session::CrateTypeRlib => {
link_rlib(sess, Some(trans), &obj_filename, &out_filename);
link_rlib(sess, Some((trans, id)), &obj_filename, &out_filename);
}
session::CrateTypeStaticlib => {
link_staticlib(sess, &obj_filename, &out_filename);
Expand All @@ -869,7 +869,8 @@ fn link_binary_output(sess: &Session,
// all of the object files from native libraries. This is done by unzipping
// native libraries and inserting all of the contents into this archive.
fn link_rlib<'a>(sess: &'a Session,
trans: Option<&CrateTranslation>, // None == no metadata/bytecode
// None == no metadata/bytecode
trans: Option<(&CrateTranslation, &CrateId)>,
obj_filename: &Path,
out_filename: &Path) -> Archive<'a> {
let mut a = Archive::create(sess, out_filename, obj_filename);
Expand Down Expand Up @@ -905,7 +906,7 @@ fn link_rlib<'a>(sess: &'a Session,
// Basically, all this means is that this code should not move above the
// code above.
match trans {
Some(trans) => {
Some((trans, id)) => {
// Instead of putting the metadata in an object file section, rlibs
// contain the metadata in a separate file. We use a temp directory
// here so concurrent builds in the same directory don't try to use
Expand All @@ -925,11 +926,15 @@ fn link_rlib<'a>(sess: &'a Session,
remove(sess, &metadata);

// For LTO purposes, the bytecode of this library is also inserted
// into the archive.
// into the archive. Note that we ensure that the bytecode has the
// same name as the crate id's name because it's how we'll search
// for the bytecode later on.
let bc = obj_filename.with_extension("bc");
let bc_deflated = obj_filename.with_extension("bc.deflate");
let bc_deflated = tmpdir.path().join(id.name.as_slice())
.with_extension("bc.deflate");
match fs::File::open(&bc).read_to_end().and_then(|data| {
fs::File::create(&bc_deflated).write(flate::deflate_bytes(data).as_slice())
let bytes = flate::deflate_bytes(data);
fs::File::create(&bc_deflated).write(bytes.as_slice())
}) {
Ok(()) => {}
Err(e) => {
Expand All @@ -938,7 +943,6 @@ fn link_rlib<'a>(sess: &'a Session,
}
}
a.add_file(&bc_deflated, false);
remove(sess, &bc_deflated);
if !sess.opts.cg.save_temps &&
!sess.opts.output_types.contains(&OutputTypeBitcode) {
remove(sess, &bc);
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-make/o-flag-and-some-bitcode/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-include ../tools.mk

all:
$(RUSTC) foo.rs -o $(TMPDIR)/libfoo-somehash-0.0.rlib
$(RUSTC) main.rs -Zlto
4 changes: 4 additions & 0 deletions src/test/run-make/o-flag-and-some-bitcode/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_id = "foo"]
#![crate_type = "rlib"]

pub fn foo() {}
4 changes: 4 additions & 0 deletions src/test/run-make/o-flag-and-some-bitcode/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extern crate foo;

fn main() {
}