about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-07-19 03:11:34 +0000
committerbors <bors@rust-lang.org>2014-07-19 03:11:34 +0000
commit44a71dee377bebd39a45ba3fe0ccc31e59ac2821 (patch)
tree366307b6f69792b7a8486222154e0b1878fb9ec7
parentef352faea84fa16616b773bd9aa5020d7c76bff0 (diff)
parent82fb85a15223bf2e7345197bbbc96c399292d54f (diff)
downloadrust-44a71dee377bebd39a45ba3fe0ccc31e59ac2821.tar.gz
rust-44a71dee377bebd39a45ba3fe0ccc31e59ac2821.zip
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.
-rw-r--r--src/librustc/back/link.rs25
-rw-r--r--src/librustc/back/lto.rs8
-rw-r--r--src/librustc/driver/driver.rs12
-rw-r--r--src/test/compile-fail/crate-name-mismatch.rs16
-rw-r--r--src/test/run-make/crate-name-priority/Makefile4
-rw-r--r--src/test/run-make/extra-filename-with-temp-outputs/Makefile6
-rw-r--r--src/test/run-make/extra-filename-with-temp-outputs/foo.rs11
-rw-r--r--src/test/run-pass/crate-name-attr-used.rs2
8 files changed, 68 insertions, 16 deletions
diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs
index 88b42dba8e9..cc73cdce6a8 100644
--- a/src/librustc/back/link.rs
+++ b/src/librustc/back/link.rs
@@ -571,15 +571,27 @@ pub fn find_crate_name(sess: Option<&Session>,
     };
 
     // Look in attributes 100% of the time to make sure the attribute is marked
-    // as used. After doing this, however, favor crate names from the command
-    // line.
+    // as used. After doing this, however, we still prioritize a crate name from
+    // the command line over one found in the #[crate_name] attribute. If we
+    // find both we ensure that they're the same later on as well.
     let attr_crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
                                .and_then(|at| at.value_str().map(|s| (at, s)));
 
     match sess {
         Some(sess) => {
             match sess.opts.crate_name {
-                Some(ref s) => return validate(s.clone(), None),
+                Some(ref s) => {
+                    match attr_crate_name {
+                        Some((attr, ref name)) if s.as_slice() != name.get() => {
+                            let msg = format!("--crate-name and #[crate_name] \
+                                               are required to match, but `{}` \
+                                               != `{}`", s, name);
+                            sess.span_err(attr.span, msg.as_slice());
+                        }
+                        _ => {},
+                    }
+                    return validate(s.clone(), None);
+                }
                 None => {}
             }
         }
@@ -1547,7 +1559,7 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
                 add_dynamic_crate(cmd, sess, src.dylib.unwrap())
             }
             cstore::RequireStatic => {
-                add_static_crate(cmd, sess, tmpdir, cnum, src.rlib.unwrap())
+                add_static_crate(cmd, sess, tmpdir, src.rlib.unwrap())
             }
         }
 
@@ -1564,7 +1576,7 @@ fn add_upstream_rust_crates(cmd: &mut Command, sess: &Session,
 
     // Adds the static "rlib" versions of all crates to the command line.
     fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path,
-                        cnum: ast::CrateNum, cratepath: Path) {
+                        cratepath: Path) {
         // When performing LTO on an executable output, all of the
         // bytecode from the upstream libraries has already been
         // 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,
         // If we're not doing LTO, then our job is simply to just link
         // against the archive.
         if sess.lto() {
-            let name = sess.cstore.get_crate_data(cnum).name.clone();
+            let name = cratepath.filename_str().unwrap();
+            let name = name.slice(3, name.len() - 5); // chop off lib/.rlib
             time(sess.time_passes(),
                  format!("altering {}.rlib", name).as_slice(),
                  (), |()| {
diff --git a/src/librustc/back/lto.rs b/src/librustc/back/lto.rs
index 6184ea4591f..c51f1615d59 100644
--- a/src/librustc/back/lto.rs
+++ b/src/librustc/back/lto.rs
@@ -54,17 +54,19 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
         };
 
         let archive = ArchiveRO::open(&path).expect("wanted an rlib");
-        debug!("reading {}", name);
+        let file = path.filename_str().unwrap();
+        let file = file.slice(3, file.len() - 5); // chop off lib/.rlib
+        debug!("reading {}", file);
         let bc = time(sess.time_passes(),
                       format!("read {}.bytecode.deflate", name).as_slice(),
                       (),
                       |_| {
                           archive.read(format!("{}.bytecode.deflate",
-                                               name).as_slice())
+                                               file).as_slice())
                       });
         let bc = bc.expect("missing compressed bytecode in archive!");
         let bc = time(sess.time_passes(),
-                      format!("inflate {}.bc", name).as_slice(),
+                      format!("inflate {}.bc", file).as_slice(),
                       (),
                       |_| {
                           match flate::inflate_bytes(bc) {
diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs
index 43aa3f9041f..81ace4d015c 100644
--- a/src/librustc/driver/driver.rs
+++ b/src/librustc/driver/driver.rs
@@ -936,6 +936,7 @@ pub struct OutputFilenames {
     pub out_directory: Path,
     pub out_filestem: String,
     pub single_output_file: Option<Path>,
+    extra: String,
 }
 
 impl OutputFilenames {
@@ -948,7 +949,7 @@ impl OutputFilenames {
     }
 
     pub fn temp_path(&self, flavor: link::OutputType) -> Path {
-        let base = self.out_directory.join(self.out_filestem.as_slice());
+        let base = self.out_directory.join(self.filestem());
         match flavor {
             link::OutputTypeBitcode => base.with_extension("bc"),
             link::OutputTypeAssembly => base.with_extension("s"),
@@ -959,8 +960,11 @@ impl OutputFilenames {
     }
 
     pub fn with_extension(&self, extension: &str) -> Path {
-        let stem = self.out_filestem.as_slice();
-        self.out_directory.join(stem).with_extension(extension)
+        self.out_directory.join(self.filestem()).with_extension(extension)
+    }
+
+    fn filestem(&self) -> String {
+        format!("{}{}", self.out_filestem, self.extra)
     }
 }
 
@@ -1000,6 +1004,7 @@ pub fn build_output_filenames(input: &Input,
                 out_directory: dirpath,
                 out_filestem: stem,
                 single_output_file: None,
+                extra: sess.opts.cg.extra_filename.clone(),
             }
         }
 
@@ -1018,6 +1023,7 @@ pub fn build_output_filenames(input: &Input,
                 out_directory: out_file.dir_path(),
                 out_filestem: out_file.filestem_str().unwrap().to_string(),
                 single_output_file: ofile,
+                extra: sess.opts.cg.extra_filename.clone(),
             }
         }
     }
diff --git a/src/test/compile-fail/crate-name-mismatch.rs b/src/test/compile-fail/crate-name-mismatch.rs
new file mode 100644
index 00000000000..589c0beb760
--- /dev/null
+++ b/src/test/compile-fail/crate-name-mismatch.rs
@@ -0,0 +1,16 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --crate-name foo
+
+#![crate_name = "bar"]
+//~^ ERROR: --crate-name and #[crate_name] are required to match, but `foo` != `bar`
+
+fn main() {}
diff --git a/src/test/run-make/crate-name-priority/Makefile b/src/test/run-make/crate-name-priority/Makefile
index 250602710f5..2fe51832433 100644
--- a/src/test/run-make/crate-name-priority/Makefile
+++ b/src/test/run-make/crate-name-priority/Makefile
@@ -7,7 +7,5 @@ all:
 	rm $(TMPDIR)/$(call BIN,bar)
 	$(RUSTC) foo1.rs
 	rm $(TMPDIR)/$(call BIN,foo)
-	$(RUSTC) foo1.rs --crate-name bar
-	rm $(TMPDIR)/$(call BIN,bar)
-	$(RUSTC) foo1.rs --crate-name bar -o $(TMPDIR)/bar1
+	$(RUSTC) foo1.rs -o $(TMPDIR)/bar1
 	rm $(TMPDIR)/$(call BIN,bar1)
diff --git a/src/test/run-make/extra-filename-with-temp-outputs/Makefile b/src/test/run-make/extra-filename-with-temp-outputs/Makefile
new file mode 100644
index 00000000000..28c22a173cc
--- /dev/null
+++ b/src/test/run-make/extra-filename-with-temp-outputs/Makefile
@@ -0,0 +1,6 @@
+-include ../tools.mk
+
+all:
+	$(RUSTC) -C extra-filename=bar foo.rs -C save-temps
+	rm $(TMPDIR)/foobar.o
+	rm $(TMPDIR)/$(call BIN,foobar)
diff --git a/src/test/run-make/extra-filename-with-temp-outputs/foo.rs b/src/test/run-make/extra-filename-with-temp-outputs/foo.rs
new file mode 100644
index 00000000000..8ae3d072362
--- /dev/null
+++ b/src/test/run-make/extra-filename-with-temp-outputs/foo.rs
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {}
diff --git a/src/test/run-pass/crate-name-attr-used.rs b/src/test/run-pass/crate-name-attr-used.rs
index abc565d3175..95c7d331264 100644
--- a/src/test/run-pass/crate-name-attr-used.rs
+++ b/src/test/run-pass/crate-name-attr-used.rs
@@ -10,6 +10,6 @@
 
 // compile-flags:--crate-name crate-name-attr-used -F unused-attribute
 
-#![crate_name = "test"]
+#![crate_name = "crate-name-attr-used"]
 
 fn main() {}