Skip to content

Commit ef719bc

Browse files
committed
Auto merge of #5460 - ehuss:conservative-link, r=alexcrichton
Be more conservative about which files are linked to the output dir. This changes it so that only top-level targets requested on the command-line will be included in the output directory. Dependencies are no longer included. Fixes #5444.
2 parents c807445 + d0c2939 commit ef719bc

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

src/cargo/core/compiler/context/compilation_files.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ pub struct CompilationFiles<'a, 'cfg: 'a> {
2525
pub(super) host: Layout,
2626
/// The target directory layout for the target (if different from then host)
2727
pub(super) target: Option<Layout>,
28-
export_dir: Option<(PathBuf, Vec<Unit<'a>>)>,
28+
/// Additional directory to include a copy of the outputs.
29+
export_dir: Option<PathBuf>,
30+
/// The root targets requested by the user on the command line (does not
31+
/// include dependencies).
32+
roots: Vec<Unit<'a>>,
2933
ws: &'a Workspace<'cfg>,
3034
metas: HashMap<Unit<'a>, Option<Metadata>>,
3135
/// For each Unit, a list all files produced.
@@ -65,7 +69,8 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
6569
ws,
6670
host,
6771
target,
68-
export_dir: export_dir.map(|dir| (dir, roots.to_vec())),
72+
export_dir,
73+
roots: roots.to_vec(),
6974
metas,
7075
outputs,
7176
}
@@ -108,13 +113,8 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
108113
}
109114
}
110115

111-
pub fn export_dir(&self, unit: &Unit<'a>) -> Option<PathBuf> {
112-
let &(ref dir, ref roots) = self.export_dir.as_ref()?;
113-
if roots.contains(unit) {
114-
Some(dir.clone())
115-
} else {
116-
None
117-
}
116+
pub fn export_dir(&self) -> Option<PathBuf> {
117+
self.export_dir.clone()
118118
}
119119

120120
pub fn pkg_dir(&self, unit: &Unit<'a>) -> String {
@@ -206,9 +206,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
206206
// we don't want to link it up.
207207
if out_dir.ends_with("deps") {
208208
// Don't lift up library dependencies
209-
if self.ws.members().find(|&p| p == unit.pkg).is_none() && !unit.target.is_bin() {
210-
None
211-
} else {
209+
if unit.target.is_bin() || self.roots.contains(unit) {
212210
Some((
213211
out_dir.parent().unwrap().to_owned(),
214212
if unit.mode.is_any_test() {
@@ -217,6 +215,8 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
217215
bin_stem
218216
},
219217
))
218+
} else {
219+
None
220220
}
221221
} else if bin_stem == file_stem {
222222
None

src/cargo/core/compiler/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fn link_targets<'a, 'cfg>(
362362
) -> CargoResult<Work> {
363363
let bcx = cx.bcx;
364364
let outputs = cx.outputs(unit)?;
365-
let export_dir = cx.files().export_dir(unit);
365+
let export_dir = cx.files().export_dir();
366366
let package_id = unit.pkg.package_id().clone();
367367
let target = unit.target.clone();
368368
let profile = unit.profile;

tests/testsuite/check.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cargotest::install::exe;
2+
use cargotest::is_nightly;
23
use cargotest::support::paths::CargoPathExt;
34
use cargotest::support::registry::Package;
45
use cargotest::support::{execs, project};
@@ -830,7 +831,12 @@ fn check_artifacts() {
830831
p.cargo("check").arg("--bin").arg("foo"),
831832
execs().with_status(0),
832833
);
833-
assert_that(&p.root().join("target/debug/libfoo.rmeta"), existing_file());
834+
if is_nightly() {
835+
// The nightly check can be removed once 1.27 is stable.
836+
// Bins now generate `rmeta` files.
837+
// See: https://github.com/rust-lang/rust/pull/49289
838+
assert_that(&p.root().join("target/debug/libfoo.rmeta"), existing_file());
839+
}
834840
assert_that(
835841
&p.root().join("target/debug/libfoo.rlib"),
836842
is_not(existing_file()),
@@ -845,7 +851,10 @@ fn check_artifacts() {
845851
p.cargo("check").arg("--test").arg("t1"),
846852
execs().with_status(0),
847853
);
848-
assert_that(&p.root().join("target/debug/libfoo.rmeta"), existing_file());
854+
assert_that(
855+
&p.root().join("target/debug/libfoo.rmeta"),
856+
is_not(existing_file()),
857+
);
849858
assert_that(
850859
&p.root().join("target/debug/libfoo.rlib"),
851860
is_not(existing_file()),
@@ -866,7 +875,10 @@ fn check_artifacts() {
866875
p.cargo("check").arg("--example").arg("ex1"),
867876
execs().with_status(0),
868877
);
869-
assert_that(&p.root().join("target/debug/libfoo.rmeta"), existing_file());
878+
assert_that(
879+
&p.root().join("target/debug/libfoo.rmeta"),
880+
is_not(existing_file()),
881+
);
870882
assert_that(
871883
&p.root().join("target/debug/libfoo.rlib"),
872884
is_not(existing_file()),
@@ -881,7 +893,10 @@ fn check_artifacts() {
881893
p.cargo("check").arg("--bench").arg("b1"),
882894
execs().with_status(0),
883895
);
884-
assert_that(&p.root().join("target/debug/libfoo.rmeta"), existing_file());
896+
assert_that(
897+
&p.root().join("target/debug/libfoo.rmeta"),
898+
is_not(existing_file()),
899+
);
885900
assert_that(
886901
&p.root().join("target/debug/libfoo.rlib"),
887902
is_not(existing_file()),

tests/testsuite/path.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::io::prelude::*;
44
use cargo::util::process;
55
use cargotest::sleep_ms;
66
use cargotest::support::paths::{self, CargoPathExt};
7-
use cargotest::support::{execs, main_file, project};
87
use cargotest::support::registry::Package;
9-
use hamcrest::{assert_that, existing_file};
8+
use cargotest::support::{execs, main_file, project};
9+
use hamcrest::{assert_that, existing_file, is_not};
1010

1111
#[test]
1212
#[cfg(not(windows))] // I have no idea why this is failing spuriously on
@@ -1278,7 +1278,10 @@ fn workspace_produces_rlib() {
12781278
assert_that(p.cargo("build"), execs().with_status(0));
12791279

12801280
assert_that(&p.root().join("target/debug/libtop.rlib"), existing_file());
1281-
assert_that(&p.root().join("target/debug/libfoo.rlib"), existing_file());
1281+
assert_that(
1282+
&p.root().join("target/debug/libfoo.rlib"),
1283+
is_not(existing_file()),
1284+
);
12821285
}
12831286

12841287
#[test]

0 commit comments

Comments
 (0)