Skip to content

Commit 82fb85a

Browse files
committed
rustc: Mix extra-filename in temp outputs
When invoking the compiler in parallel, the intermediate output of the object files and bytecode can stomp over one another if two crates with the same name are being compiled. The output file is already being disambiguated with `-C extra-filename`, so this commit alters the naming of the temporary files to also mix in the extra filename to ensure that file names don't clash.
1 parent 50868db commit 82fb85a

File tree

8 files changed

+41
-16
lines changed

8 files changed

+41
-16
lines changed

src/librustc/back/link.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,9 @@ pub fn find_crate_name(sess: Option<&Session>,
571571
};
572572

573573
// Look in attributes 100% of the time to make sure the attribute is marked
574-
// as used. After doing this, however, favor crate names from the command
575-
// line.
574+
// as used. After doing this, however, we still prioritize a crate name from
575+
// the command line over one found in the #[crate_name] attribute. If we
576+
// find both we ensure that they're the same later on as well.
576577
let attr_crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
577578
.and_then(|at| at.value_str().map(|s| (at, s)));
578579

@@ -1558,7 +1559,7 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
15581559
add_dynamic_crate(cmd, sess, src.dylib.unwrap())
15591560
}
15601561
cstore::RequireStatic => {
1561-
add_static_crate(cmd, sess, tmpdir, cnum, src.rlib.unwrap())
1562+
add_static_crate(cmd, sess, tmpdir, src.rlib.unwrap())
15621563
}
15631564
}
15641565

@@ -1575,7 +1576,7 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
15751576

15761577
// Adds the static "rlib" versions of all crates to the command line.
15771578
fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path,
1578-
cnum: ast::CrateNum, cratepath: Path) {
1579+
cratepath: Path) {
15791580
// When performing LTO on an executable output, all of the
15801581
// bytecode from the upstream libraries has already been
15811582
// included in our object file output. We need to modify all of
@@ -1591,7 +1592,8 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
15911592
// If we're not doing LTO, then our job is simply to just link
15921593
// against the archive.
15931594
if sess.lto() {
1594-
let name = sess.cstore.get_crate_data(cnum).name.clone();
1595+
let name = cratepath.filename_str().unwrap();
1596+
let name = name.slice(3, name.len() - 5); // chop off lib/.rlib
15951597
time(sess.time_passes(),
15961598
format!("altering {}.rlib", name).as_slice(),
15971599
(), |()| {

src/librustc/back/lto.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
5454
};
5555

5656
let archive = ArchiveRO::open(&path).expect("wanted an rlib");
57-
debug!("reading {}", name);
57+
let file = path.filename_str().unwrap();
58+
let file = file.slice(3, file.len() - 5); // chop off lib/.rlib
59+
debug!("reading {}", file);
5860
let bc = time(sess.time_passes(),
5961
format!("read {}.bytecode.deflate", name).as_slice(),
6062
(),
6163
|_| {
6264
archive.read(format!("{}.bytecode.deflate",
63-
name).as_slice())
65+
file).as_slice())
6466
});
6567
let bc = bc.expect("missing compressed bytecode in archive!");
6668
let bc = time(sess.time_passes(),
67-
format!("inflate {}.bc", name).as_slice(),
69+
format!("inflate {}.bc", file).as_slice(),
6870
(),
6971
|_| {
7072
match flate::inflate_bytes(bc) {

src/librustc/driver/driver.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ pub struct OutputFilenames {
936936
pub out_directory: Path,
937937
pub out_filestem: String,
938938
pub single_output_file: Option<Path>,
939+
extra: String,
939940
}
940941

941942
impl OutputFilenames {
@@ -948,7 +949,7 @@ impl OutputFilenames {
948949
}
949950

950951
pub fn temp_path(&self, flavor: link::OutputType) -> Path {
951-
let base = self.out_directory.join(self.out_filestem.as_slice());
952+
let base = self.out_directory.join(self.filestem());
952953
match flavor {
953954
link::OutputTypeBitcode => base.with_extension("bc"),
954955
link::OutputTypeAssembly => base.with_extension("s"),
@@ -959,8 +960,11 @@ impl OutputFilenames {
959960
}
960961

961962
pub fn with_extension(&self, extension: &str) -> Path {
962-
let stem = self.out_filestem.as_slice();
963-
self.out_directory.join(stem).with_extension(extension)
963+
self.out_directory.join(self.filestem()).with_extension(extension)
964+
}
965+
966+
fn filestem(&self) -> String {
967+
format!("{}{}", self.out_filestem, self.extra)
964968
}
965969
}
966970

@@ -1000,6 +1004,7 @@ pub fn build_output_filenames(input: &Input,
10001004
out_directory: dirpath,
10011005
out_filestem: stem,
10021006
single_output_file: None,
1007+
extra: sess.opts.cg.extra_filename.clone(),
10031008
}
10041009
}
10051010

@@ -1018,6 +1023,7 @@ pub fn build_output_filenames(input: &Input,
10181023
out_directory: out_file.dir_path(),
10191024
out_filestem: out_file.filestem_str().unwrap().to_string(),
10201025
single_output_file: ofile,
1026+
extra: sess.opts.cg.extra_filename.clone(),
10211027
}
10221028
}
10231029
}

src/test/compile-fail/crate-name-mismatch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-args: --crate-name foo
11+
// compile-flags: --crate-name foo
1212

1313
#![crate_name = "bar"]
1414
//~^ ERROR: --crate-name and #[crate_name] are required to match, but `foo` != `bar`

src/test/run-make/crate-name-priority/Makefile

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ all:
77
rm $(TMPDIR)/$(call BIN,bar)
88
$(RUSTC) foo1.rs
99
rm $(TMPDIR)/$(call BIN,foo)
10-
$(RUSTC) foo1.rs --crate-name bar
11-
rm $(TMPDIR)/$(call BIN,bar)
12-
$(RUSTC) foo1.rs --crate-name bar -o $(TMPDIR)/bar1
10+
$(RUSTC) foo1.rs -o $(TMPDIR)/bar1
1311
rm $(TMPDIR)/$(call BIN,bar1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -C extra-filename=bar foo.rs -C save-temps
5+
rm $(TMPDIR)/foobar.o
6+
rm $(TMPDIR)/$(call BIN,foobar)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {}

src/test/run-pass/crate-name-attr-used.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
// compile-flags:--crate-name crate-name-attr-used -F unused-attribute
1212

13-
#![crate_name = "test"]
13+
#![crate_name = "crate-name-attr-used"]
1414

1515
fn main() {}

0 commit comments

Comments
 (0)