From a0480e288750b3387d5d1ccb05cb29282bb7a97f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 4 Apr 2014 11:32:52 -0700 Subject: [PATCH] rustc: Always name bytecode based on crate ids When performing LTO, bytecode is loaded from an archive by using the crate id's name. If the -o flag was specified when building the original library, it previously also mangled the name of the bytecode. This switches to always using the crate id's name when naming the bytecode file, ensuring that it can always be loaded at a later date, regardless of -o. Closes #13317 --- src/librustc/back/link.rs | 18 +++++++++++------- .../run-make/o-flag-and-some-bitcode/Makefile | 5 +++++ .../run-make/o-flag-and-some-bitcode/foo.rs | 4 ++++ .../run-make/o-flag-and-some-bitcode/main.rs | 4 ++++ 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/test/run-make/o-flag-and-some-bitcode/Makefile create mode 100644 src/test/run-make/o-flag-and-some-bitcode/foo.rs create mode 100644 src/test/run-make/o-flag-and-some-bitcode/main.rs diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index e9292f54a4b26..9965af05019e2 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -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); @@ -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); @@ -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 @@ -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) => { @@ -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); diff --git a/src/test/run-make/o-flag-and-some-bitcode/Makefile b/src/test/run-make/o-flag-and-some-bitcode/Makefile new file mode 100644 index 0000000000000..0aaee14a869c6 --- /dev/null +++ b/src/test/run-make/o-flag-and-some-bitcode/Makefile @@ -0,0 +1,5 @@ +-include ../tools.mk + +all: + $(RUSTC) foo.rs -o $(TMPDIR)/libfoo-somehash-0.0.rlib + $(RUSTC) main.rs -Zlto diff --git a/src/test/run-make/o-flag-and-some-bitcode/foo.rs b/src/test/run-make/o-flag-and-some-bitcode/foo.rs new file mode 100644 index 0000000000000..1e1ddaaabb914 --- /dev/null +++ b/src/test/run-make/o-flag-and-some-bitcode/foo.rs @@ -0,0 +1,4 @@ +#![crate_id = "foo"] +#![crate_type = "rlib"] + +pub fn foo() {} diff --git a/src/test/run-make/o-flag-and-some-bitcode/main.rs b/src/test/run-make/o-flag-and-some-bitcode/main.rs new file mode 100644 index 0000000000000..664e9f1c7775c --- /dev/null +++ b/src/test/run-make/o-flag-and-some-bitcode/main.rs @@ -0,0 +1,4 @@ +extern crate foo; + +fn main() { +}