Skip to content

Commit 27efa7b

Browse files
committed
Don't put hashes in libraries of the root crate
The root crate often has artifacts which are later intended for distribution of some form, so adding a hash will just make predicting the file name difficult. The hash also isn't necessary as it's guaranteed to not conflict with other files (no other dependencies are in the same directory) and all other libraries have metadata so symbols will not conflict. Closes #1484
1 parent e3a6425 commit 27efa7b

File tree

8 files changed

+48
-30
lines changed

8 files changed

+48
-30
lines changed

src/cargo/ops/cargo_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn clean(manifest_path: &Path, opts: &CleanOptions) -> CargoResult<()> {
6363
try!(rm_rf(&layout.fingerprint(&pkg)));
6464
let profiles = [Profile::default_dev(), Profile::default_test()];
6565
for profile in profiles.iter() {
66-
for filename in try!(cx.target_filenames(target, profile)).iter() {
66+
for filename in try!(cx.target_filenames(&pkg, target, profile)).iter() {
6767
try!(rm_rf(&layout.dest().join(&filename)));
6868
try!(rm_rf(&layout.deps().join(&filename)));
6969
}

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
257257
}
258258

259259
/// Get the metadata for a target in a specific profile
260-
pub fn target_metadata(&self, target: &Target, profile: &Profile)
261-
-> Option<Metadata> {
260+
pub fn target_metadata(&self, pkg: &Package, target: &Target,
261+
profile: &Profile) -> Option<Metadata> {
262262
let metadata = target.metadata();
263263
if target.is_lib() && profile.test {
264264
// Libs and their tests are built in parallel, so we need to make
@@ -269,19 +269,26 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
269269
})
270270
} else if target.is_bin() && profile.test {
271271
// Make sure that the name of this test executable doesn't
272-
// conflicts with a library that has the same name and is
272+
// conflict with a library that has the same name and is
273273
// being tested
274-
let mut metadata = self.resolve.root().generate_metadata();
274+
let mut metadata = pkg.package_id().generate_metadata();
275275
metadata.mix(&format!("bin-{}", target.name()));
276276
Some(metadata)
277+
} else if pkg.package_id() == self.resolve.root() && !profile.test {
278+
// If we're not building a unit test then the root package never
279+
// needs any metadata as it's guaranteed to not conflict with any
280+
// other output filenames. This means that we'll have predictable
281+
// file names like `target/debug/libfoo.{a,so,rlib}` and such.
282+
None
277283
} else {
278284
metadata.map(|m| m.clone())
279285
}
280286
}
281287

282288
/// Returns the file stem for a given target/profile combo
283-
pub fn file_stem(&self, target: &Target, profile: &Profile) -> String {
284-
match self.target_metadata(target, profile) {
289+
pub fn file_stem(&self, pkg: &Package, target: &Target,
290+
profile: &Profile) -> String {
291+
match self.target_metadata(pkg, target, profile) {
285292
Some(ref metadata) => format!("{}{}", target.crate_name(),
286293
metadata.extra_filename),
287294
None if target.allows_underscores() => target.name().to_string(),
@@ -291,9 +298,9 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
291298

292299
/// Return the filenames that the given target for the given profile will
293300
/// generate.
294-
pub fn target_filenames(&self, target: &Target, profile: &Profile)
295-
-> CargoResult<Vec<String>> {
296-
let stem = self.file_stem(target, profile);
301+
pub fn target_filenames(&self, pkg: &Package, target: &Target,
302+
profile: &Profile) -> CargoResult<Vec<String>> {
303+
let stem = self.file_stem(pkg, target, profile);
297304
let suffix = if target.for_host() {&self.host_exe} else {&self.target_exe};
298305

299306
let mut ret = Vec::new();

src/cargo/ops/cargo_rustc/fingerprint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn prepare_target<'a, 'b>(cx: &mut Context<'a, 'b>,
5757
let root = cx.out_dir(pkg, kind, target);
5858
let mut missing_outputs = false;
5959
if !profile.doc {
60-
for filename in try!(cx.target_filenames(target, profile)).iter() {
60+
for filename in try!(cx.target_filenames(pkg, target, profile)).iter() {
6161
missing_outputs |= fs::metadata(root.join(filename)).is_err();
6262
}
6363
}

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)],
133133
cx.compilation.extra_env.insert("OUT_DIR".to_string(), out_dir);
134134

135135
for &(target, profile) in targets {
136-
for filename in try!(cx.target_filenames(target, profile)).iter() {
136+
for filename in try!(cx.target_filenames(pkg, target, profile)).iter() {
137137
let dst = cx.out_dir(pkg, Kind::Target, target).join(filename);
138138
if profile.test {
139139
cx.compilation.tests.push((target.name().to_string(), dst));
@@ -154,7 +154,7 @@ pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)],
154154
if profile.doc { continue }
155155
if cx.compilation.libraries.contains_key(&pkgid) { continue }
156156

157-
let v = try!(cx.target_filenames(target, profile));
157+
let v = try!(cx.target_filenames(pkg, target, profile));
158158
let v = v.into_iter().map(|f| {
159159
(target.clone(),
160160
cx.out_dir(pkg, Kind::Target, target).join(f))
@@ -342,7 +342,7 @@ fn rustc(package: &Package, target: &Target, profile: &Profile,
342342
}
343343
let exec_engine = cx.exec_engine.clone();
344344

345-
let filenames = try!(cx.target_filenames(target, profile));
345+
let filenames = try!(cx.target_filenames(package, target, profile));
346346
let root = cx.out_dir(package, kind, target);
347347

348348
// Prepare the native lib state (extra -L and -l flags)
@@ -367,7 +367,7 @@ fn rustc(package: &Package, target: &Target, profile: &Profile,
367367
let rustc_dep_info_loc = if do_rename {
368368
root.join(&crate_name)
369369
} else {
370-
root.join(&cx.file_stem(target, profile))
370+
root.join(&cx.file_stem(package, target, profile))
371371
}.with_extension("d");
372372
let dep_info_loc = fingerprint::dep_info_loc(cx, package, target,
373373
profile, kind);
@@ -678,7 +678,7 @@ fn build_base_args(cx: &Context,
678678
None => {}
679679
}
680680

681-
match cx.target_metadata(target, profile) {
681+
match cx.target_metadata(pkg, target, profile) {
682682
Some(m) => {
683683
cmd.arg("-C").arg(&format!("metadata={}", m.metadata));
684684
cmd.arg("-C").arg(&format!("extra-filename={}", m.extra_filename));
@@ -753,7 +753,7 @@ fn build_deps_args(cmd: &mut CommandPrototype,
753753
Kind::Target => Kind::Target,
754754
});
755755

756-
for filename in try!(cx.target_filenames(target, profile)).iter() {
756+
for filename in try!(cx.target_filenames(pkg, target, profile)).iter() {
757757
if filename.ends_with(".a") { continue }
758758
let mut v = OsString::new();
759759
v.push(&target.crate_name());

tests/test_cargo_build_lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ fn verbose_output_for_lib(p: &ProjectBuilder) -> String {
1010
format!("\
1111
{compiling} {name} v{version} ({url})
1212
{running} `rustc src{sep}lib.rs --crate-name {name} --crate-type lib -g \
13-
-C metadata=[..] \
14-
-C extra-filename=-[..] \
1513
--out-dir {dir}{sep}target{sep}debug \
1614
--emit=dep-info,link \
1715
-L dependency={dir}{sep}target{sep}debug \

tests/test_cargo_compile.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,6 @@ test!(verbose_build {
842842
execs().with_status(0).with_stdout(&format!("\
843843
{compiling} test v0.0.0 ({url})
844844
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib -g \
845-
-C metadata=[..] \
846-
-C extra-filename=-[..] \
847845
--out-dir {dir}[..]target[..]debug \
848846
--emit=dep-info,link \
849847
-L dependency={dir}[..]target[..]debug \
@@ -871,8 +869,6 @@ test!(verbose_release_build {
871869
{compiling} test v0.0.0 ({url})
872870
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib \
873871
-C opt-level=3 \
874-
-C metadata=[..] \
875-
-C extra-filename=-[..] \
876872
--out-dir {dir}[..]target[..]release \
877873
--emit=dep-info,link \
878874
-L dependency={dir}[..]target[..]release \
@@ -925,8 +921,6 @@ test!(verbose_release_build_deps {
925921
{compiling} test v0.0.0 ({url})
926922
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib \
927923
-C opt-level=3 \
928-
-C metadata=[..] \
929-
-C extra-filename=-[..] \
930924
--out-dir {dir}[..]target[..]release \
931925
--emit=dep-info,link \
932926
-L dependency={dir}[..]target[..]release \
@@ -1621,6 +1615,30 @@ cyclic package dependency: package `foo v0.0.1 ([..])` depends on itself
16211615
"));
16221616
});
16231617

1618+
test!(predictable_filenames {
1619+
let p = project("foo")
1620+
.file("Cargo.toml", r#"
1621+
[package]
1622+
name = "foo"
1623+
version = "0.0.1"
1624+
authors = []
1625+
1626+
[lib]
1627+
name = "foo"
1628+
crate-type = ["staticlib", "dylib", "rlib"]
1629+
"#)
1630+
.file("src/lib.rs", "");
1631+
1632+
assert_that(p.cargo_process("build").arg("-v"),
1633+
execs().with_status(0));
1634+
assert_that(&p.root().join("target/debug/libfoo.a"), existing_file());
1635+
assert_that(&p.root().join("target/debug/libfoo.rlib"), existing_file());
1636+
let dylib_name = format!("{}foo{}", env::consts::DLL_PREFIX,
1637+
env::consts::DLL_SUFFIX);
1638+
assert_that(&p.root().join("target/debug").join(dylib_name),
1639+
existing_file());
1640+
});
1641+
16241642
test!(dashes_to_underscores {
16251643
let p = project("foo")
16261644
.file("Cargo.toml", r#"

tests/test_cargo_compile_custom_build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,6 @@ test!(build_cmd_with_a_build_cmd {
768768
--extern a=[..]liba-[..].rlib`
769769
{running} `[..]foo-[..]build-script-build[..]`
770770
{running} `rustc [..]lib.rs --crate-name foo --crate-type lib -g \
771-
-C metadata=[..] -C extra-filename=-[..] \
772771
--out-dir [..]target[..]debug --emit=dep-info,link \
773772
-L [..]target[..]debug -L [..]target[..]deps`
774773
", compiling = COMPILING, running = RUNNING)));

tests/test_cargo_profiles.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ test!(profile_overrides {
3030
{running} `rustc src{sep}lib.rs --crate-name test --crate-type lib \
3131
-C opt-level=1 \
3232
-C debug-assertions=on \
33-
-C metadata=[..] \
34-
-C extra-filename=-[..] \
3533
-C rpath \
3634
--out-dir {dir}{sep}target{sep}debug \
3735
--emit=dep-info,link \
@@ -95,8 +93,6 @@ test!(top_level_overrides_deps {
9593
{running} `rustc src{sep}lib.rs --crate-name test --crate-type lib \
9694
-C opt-level=1 \
9795
-g \
98-
-C metadata=[..] \
99-
-C extra-filename=-[..] \
10096
--out-dir {dir}{sep}target{sep}release \
10197
--emit=dep-info,link \
10298
-L dependency={dir}{sep}target{sep}release \

0 commit comments

Comments
 (0)