Skip to content

Commit 44a71de

Browse files
committed
auto merge of #15686 : alexcrichton/rust/same-crate-name, r=kballard
The first is to require that `#[crate_name]` and `--crate-name` always match (if both are specified). The second is to fix parallel compilation in cargo by mixing in `-C extra-filename` into the temporary outputs of the compiler.
2 parents ef352fa + 82fb85a commit 44a71de

File tree

8 files changed

+68
-16
lines changed

8 files changed

+68
-16
lines changed

src/librustc/back/link.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,27 @@ 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

579580
match sess {
580581
Some(sess) => {
581582
match sess.opts.crate_name {
582-
Some(ref s) => return validate(s.clone(), None),
583+
Some(ref s) => {
584+
match attr_crate_name {
585+
Some((attr, ref name)) if s.as_slice() != name.get() => {
586+
let msg = format!("--crate-name and #[crate_name] \
587+
are required to match, but `{}` \
588+
!= `{}`", s, name);
589+
sess.span_err(attr.span, msg.as_slice());
590+
}
591+
_ => {},
592+
}
593+
return validate(s.clone(), None);
594+
}
583595
None => {}
584596
}
585597
}
@@ -1547,7 +1559,7 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
15471559
add_dynamic_crate(cmd, sess, src.dylib.unwrap())
15481560
}
15491561
cstore::RequireStatic => {
1550-
add_static_crate(cmd, sess, tmpdir, cnum, src.rlib.unwrap())
1562+
add_static_crate(cmd, sess, tmpdir, src.rlib.unwrap())
15511563
}
15521564
}
15531565

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

15651577
// Adds the static "rlib" versions of all crates to the command line.
15661578
fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path,
1567-
cnum: ast::CrateNum, cratepath: Path) {
1579+
cratepath: Path) {
15681580
// When performing LTO on an executable output, all of the
15691581
// bytecode from the upstream libraries has already been
15701582
// included in our object file output. We need to modify all of
@@ -1580,7 +1592,8 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
15801592
// If we're not doing LTO, then our job is simply to just link
15811593
// against the archive.
15821594
if sess.lto() {
1583-
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
15841597
time(sess.time_passes(),
15851598
format!("altering {}.rlib", name).as_slice(),
15861599
(), |()| {

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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
// compile-flags: --crate-name foo
12+
13+
#![crate_name = "bar"]
14+
//~^ ERROR: --crate-name and #[crate_name] are required to match, but `foo` != `bar`
15+
16+
fn main() {}

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)