Skip to content

Commit

Permalink
Don't put hashes in libraries of the root crate
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Apr 29, 2015
1 parent e3a6425 commit 27efa7b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_clean.rs
Expand Up @@ -63,7 +63,7 @@ pub fn clean(manifest_path: &Path, opts: &CleanOptions) -> CargoResult<()> {
try!(rm_rf(&layout.fingerprint(&pkg)));
let profiles = [Profile::default_dev(), Profile::default_test()];
for profile in profiles.iter() {
for filename in try!(cx.target_filenames(target, profile)).iter() {
for filename in try!(cx.target_filenames(&pkg, target, profile)).iter() {
try!(rm_rf(&layout.dest().join(&filename)));
try!(rm_rf(&layout.deps().join(&filename)));
}
Expand Down
25 changes: 16 additions & 9 deletions src/cargo/ops/cargo_rustc/context.rs
Expand Up @@ -257,8 +257,8 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
}

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

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

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

let mut ret = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_rustc/fingerprint.rs
Expand Up @@ -57,7 +57,7 @@ pub fn prepare_target<'a, 'b>(cx: &mut Context<'a, 'b>,
let root = cx.out_dir(pkg, kind, target);
let mut missing_outputs = false;
if !profile.doc {
for filename in try!(cx.target_filenames(target, profile)).iter() {
for filename in try!(cx.target_filenames(pkg, target, profile)).iter() {
missing_outputs |= fs::metadata(root.join(filename)).is_err();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/cargo/ops/cargo_rustc/mod.rs
Expand Up @@ -133,7 +133,7 @@ pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)],
cx.compilation.extra_env.insert("OUT_DIR".to_string(), out_dir);

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

let v = try!(cx.target_filenames(target, profile));
let v = try!(cx.target_filenames(pkg, target, profile));
let v = v.into_iter().map(|f| {
(target.clone(),
cx.out_dir(pkg, Kind::Target, target).join(f))
Expand Down Expand Up @@ -342,7 +342,7 @@ fn rustc(package: &Package, target: &Target, profile: &Profile,
}
let exec_engine = cx.exec_engine.clone();

let filenames = try!(cx.target_filenames(target, profile));
let filenames = try!(cx.target_filenames(package, target, profile));
let root = cx.out_dir(package, kind, target);

// Prepare the native lib state (extra -L and -l flags)
Expand All @@ -367,7 +367,7 @@ fn rustc(package: &Package, target: &Target, profile: &Profile,
let rustc_dep_info_loc = if do_rename {
root.join(&crate_name)
} else {
root.join(&cx.file_stem(target, profile))
root.join(&cx.file_stem(package, target, profile))
}.with_extension("d");
let dep_info_loc = fingerprint::dep_info_loc(cx, package, target,
profile, kind);
Expand Down Expand Up @@ -678,7 +678,7 @@ fn build_base_args(cx: &Context,
None => {}
}

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

for filename in try!(cx.target_filenames(target, profile)).iter() {
for filename in try!(cx.target_filenames(pkg, target, profile)).iter() {
if filename.ends_with(".a") { continue }
let mut v = OsString::new();
v.push(&target.crate_name());
Expand Down
2 changes: 0 additions & 2 deletions tests/test_cargo_build_lib.rs
Expand Up @@ -10,8 +10,6 @@ fn verbose_output_for_lib(p: &ProjectBuilder) -> String {
format!("\
{compiling} {name} v{version} ({url})
{running} `rustc src{sep}lib.rs --crate-name {name} --crate-type lib -g \
-C metadata=[..] \
-C extra-filename=-[..] \
--out-dir {dir}{sep}target{sep}debug \
--emit=dep-info,link \
-L dependency={dir}{sep}target{sep}debug \
Expand Down
30 changes: 24 additions & 6 deletions tests/test_cargo_compile.rs
Expand Up @@ -842,8 +842,6 @@ test!(verbose_build {
execs().with_status(0).with_stdout(&format!("\
{compiling} test v0.0.0 ({url})
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib -g \
-C metadata=[..] \
-C extra-filename=-[..] \
--out-dir {dir}[..]target[..]debug \
--emit=dep-info,link \
-L dependency={dir}[..]target[..]debug \
Expand Down Expand Up @@ -871,8 +869,6 @@ test!(verbose_release_build {
{compiling} test v0.0.0 ({url})
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib \
-C opt-level=3 \
-C metadata=[..] \
-C extra-filename=-[..] \
--out-dir {dir}[..]target[..]release \
--emit=dep-info,link \
-L dependency={dir}[..]target[..]release \
Expand Down Expand Up @@ -925,8 +921,6 @@ test!(verbose_release_build_deps {
{compiling} test v0.0.0 ({url})
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib \
-C opt-level=3 \
-C metadata=[..] \
-C extra-filename=-[..] \
--out-dir {dir}[..]target[..]release \
--emit=dep-info,link \
-L dependency={dir}[..]target[..]release \
Expand Down Expand Up @@ -1621,6 +1615,30 @@ cyclic package dependency: package `foo v0.0.1 ([..])` depends on itself
"));
});

test!(predictable_filenames {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[lib]
name = "foo"
crate-type = ["staticlib", "dylib", "rlib"]
"#)
.file("src/lib.rs", "");

assert_that(p.cargo_process("build").arg("-v"),
execs().with_status(0));
assert_that(&p.root().join("target/debug/libfoo.a"), existing_file());
assert_that(&p.root().join("target/debug/libfoo.rlib"), existing_file());
let dylib_name = format!("{}foo{}", env::consts::DLL_PREFIX,
env::consts::DLL_SUFFIX);
assert_that(&p.root().join("target/debug").join(dylib_name),
existing_file());
});

test!(dashes_to_underscores {
let p = project("foo")
.file("Cargo.toml", r#"
Expand Down
1 change: 0 additions & 1 deletion tests/test_cargo_compile_custom_build.rs
Expand Up @@ -768,7 +768,6 @@ test!(build_cmd_with_a_build_cmd {
--extern a=[..]liba-[..].rlib`
{running} `[..]foo-[..]build-script-build[..]`
{running} `rustc [..]lib.rs --crate-name foo --crate-type lib -g \
-C metadata=[..] -C extra-filename=-[..] \
--out-dir [..]target[..]debug --emit=dep-info,link \
-L [..]target[..]debug -L [..]target[..]deps`
", compiling = COMPILING, running = RUNNING)));
Expand Down
4 changes: 0 additions & 4 deletions tests/test_cargo_profiles.rs
Expand Up @@ -30,8 +30,6 @@ test!(profile_overrides {
{running} `rustc src{sep}lib.rs --crate-name test --crate-type lib \
-C opt-level=1 \
-C debug-assertions=on \
-C metadata=[..] \
-C extra-filename=-[..] \
-C rpath \
--out-dir {dir}{sep}target{sep}debug \
--emit=dep-info,link \
Expand Down Expand Up @@ -95,8 +93,6 @@ test!(top_level_overrides_deps {
{running} `rustc src{sep}lib.rs --crate-name test --crate-type lib \
-C opt-level=1 \
-g \
-C metadata=[..] \
-C extra-filename=-[..] \
--out-dir {dir}{sep}target{sep}release \
--emit=dep-info,link \
-L dependency={dir}{sep}target{sep}release \
Expand Down

0 comments on commit 27efa7b

Please sign in to comment.