Skip to content

Commit 4f9bf54

Browse files
committed
Auto merge of #1373 - alexcrichton:issue-785, r=wycats
This commit now funnels all output of Cargo by default to be in `target/debug` instead of the bare `target` directory. This change is targeted at raising awareness of whether a debug build is being used (as opposed to a release build). It is also aimed at remedying a common scenario where `cargo build` is followed by `cargo build --release` and then the debug binaries are run by accident. This does not yet explore the option of providing symlinks to the most recent build, hence this commit is a breaking change due to the restructuring of the layout of the output. Note that this commit does **not** change the output location for documentation. All output of `cargo doc` continues to be funneled into the `target/doc` directory. Closes #785
2 parents 79a9380 + 14ff482 commit 4f9bf54

14 files changed

+57
-65
lines changed

src/cargo/core/manifest.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub struct Profile {
118118
test: bool,
119119
doctest: bool,
120120
doc: bool,
121-
dest: Option<String>,
121+
dest: String,
122122
for_host: bool,
123123
harness: bool, // whether to use the test harness (--test)
124124
custom_build: bool,
@@ -135,7 +135,7 @@ impl Profile {
135135
rpath: false,
136136
test: false,
137137
doc: false,
138-
dest: None,
138+
dest: "debug".to_string(),
139139
for_host: false,
140140
doctest: false,
141141
custom_build: false,
@@ -157,7 +157,6 @@ impl Profile {
157157
env: "test".to_string(),
158158
debug: true,
159159
test: true,
160-
dest: None,
161160
.. Profile::default()
162161
}
163162
}
@@ -172,26 +171,23 @@ impl Profile {
172171
pub fn default_bench() -> Profile {
173172
Profile {
174173
env: "bench".to_string(),
175-
opt_level: 3,
176174
test: true,
177-
dest: Some("release".to_string()),
178-
.. Profile::default()
175+
.. Profile::default_release()
179176
}
180177
}
181178

182179
pub fn default_release() -> Profile {
183180
Profile {
184181
env: "release".to_string(),
185182
opt_level: 3,
186-
dest: Some("release".to_string()),
183+
dest: "release".to_string(),
187184
.. Profile::default()
188185
}
189186
}
190187

191188
pub fn default_doc() -> Profile {
192189
Profile {
193190
env: "doc".to_string(),
194-
dest: None,
195191
doc: true,
196192
.. Profile::default()
197193
}
@@ -210,10 +206,7 @@ impl Profile {
210206
pub fn opt_level(&self) -> u32 { self.opt_level }
211207
pub fn rpath(&self) -> bool { self.rpath }
212208
pub fn uses_test_harness(&self) -> bool { self.harness }
213-
214-
pub fn dest(&self) -> Option<&str> {
215-
self.dest.as_ref().map(|d| d.as_slice())
216-
}
209+
pub fn dest(&self) -> &str { &self.dest }
217210

218211
pub fn set_opt_level(mut self, level: u32) -> Profile {
219212
self.opt_level = level;

src/cargo/ops/cargo_run.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ pub fn run(manifest_path: &Path,
5353
None => dst,
5454
};
5555
let exe = match (bin.profile().dest(), bin.is_example()) {
56-
(Some(s), true) => dst.join(s).join("examples").join(bin.name()),
57-
(Some(s), false) => dst.join(s).join(bin.name()),
58-
(None, true) => dst.join("examples").join(bin.name()),
59-
(None, false) => dst.join(bin.name()),
56+
(s, true) => dst.join(s).join("examples").join(bin.name()),
57+
(s, false) => dst.join(s).join(bin.name()),
6058
};
6159
let exe = match exe.relative_from(config.cwd()) {
6260
Some(path) => path,

src/cargo/ops/cargo_rustc/layout.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,13 @@ pub struct LayoutProxy<'a> {
6868
}
6969

7070
impl Layout {
71-
pub fn new(pkg: &Package, triple: Option<&str>, dest: Option<&str>) -> Layout {
71+
pub fn new(pkg: &Package, triple: Option<&str>, dest: &str) -> Layout {
7272
let mut path = pkg.absolute_target_dir();
7373
match triple {
7474
Some(s) => path.push(s),
7575
None => {}
7676
}
77-
match dest {
78-
Some(s) => path.push(s),
79-
None => {}
80-
}
77+
path.push(dest);
8178
Layout::at(path)
8279
}
8380

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ pub fn rustc_version() -> CargoResult<(String, String)> {
7575
// This is a temporary assert that ensures the consistency of the arguments
7676
// given the current limitations of Cargo. The long term fix is to have each
7777
// Target know the absolute path to the build location.
78-
fn uniq_target_dest<'a>(targets: &[&'a Target]) -> Option<&'a str> {
79-
let mut curr: Option<Option<&str>> = None;
78+
fn uniq_target_dest<'a>(targets: &[&'a Target]) -> &'a str {
79+
let mut curr: Option<&str> = None;
8080

8181
for t in targets.iter().filter(|t| !t.profile().is_custom_build()) {
8282
let dest = t.profile().dest();
8383

8484
match curr {
85-
Some(curr) => assert!(curr == dest),
85+
Some(curr) => assert_eq!(curr, dest),
8686
None => curr = Some(dest)
8787
}
8888
}
@@ -483,7 +483,8 @@ fn prepare_rustc(package: &Package, target: &Target, crate_types: Vec<&str>,
483483
fn rustdoc(package: &Package, target: &Target,
484484
cx: &mut Context) -> CargoResult<Work> {
485485
let kind = Kind::Target;
486-
let cx_root = cx.layout(package, kind).proxy().dest().join("doc");
486+
let cx_root = cx.get_package(cx.resolve.root()).absolute_target_dir()
487+
.join("doc");
487488
let mut rustdoc = try!(process(CommandType::Rustdoc, package, target, cx));
488489
rustdoc.arg(&root_path(cx, package, target))
489490
.cwd(cx.config.cwd())

tests/support/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ impl ProjectBuilder {
103103
pub fn url(&self) -> Url { path2url(self.root()) }
104104

105105
pub fn bin(&self, b: &str) -> PathBuf {
106-
self.build_dir().join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
106+
self.build_dir().join("debug").join(&format!("{}{}", b,
107+
env::consts::EXE_SUFFIX))
107108
}
108109

109110
pub fn release_bin(&self, b: &str) -> PathBuf {
@@ -112,8 +113,8 @@ impl ProjectBuilder {
112113
}
113114

114115
pub fn target_bin(&self, target: &str, b: &str) -> PathBuf {
115-
self.build_dir().join(target).join(&format!("{}{}", b,
116-
env::consts::EXE_SUFFIX))
116+
self.build_dir().join(target).join("debug")
117+
.join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
117118
}
118119

119120
pub fn build_dir(&self) -> PathBuf {

tests/test_cargo_build_lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ fn verbose_output_for_lib(p: &ProjectBuilder) -> String {
1212
{running} `rustc src{sep}lib.rs --crate-name {name} --crate-type lib -g \
1313
-C metadata=[..] \
1414
-C extra-filename=-[..] \
15-
--out-dir {dir}{sep}target \
15+
--out-dir {dir}{sep}target{sep}debug \
1616
--emit=dep-info,link \
17-
-L dependency={dir}{sep}target \
18-
-L dependency={dir}{sep}target{sep}deps`
17+
-L dependency={dir}{sep}target{sep}debug \
18+
-L dependency={dir}{sep}target{sep}debug{sep}deps`
1919
",
2020
running = RUNNING, compiling = COMPILING, sep = old_path::SEP,
2121
dir = p.root().display(), url = p.url(),

tests/test_cargo_compile.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ test!(many_crate_types_old_style_lib_location {
631631
assert_that(p.cargo_process("build"),
632632
execs().with_status(0));
633633

634-
let files = fs::read_dir(&p.root().join("target")).unwrap();
634+
let files = fs::read_dir(&p.root().join("target/debug")).unwrap();
635635
let mut files: Vec<String> = files.map(|e| e.unwrap().path()).filter_map(|f| {
636636
match f.file_name().unwrap().to_str().unwrap() {
637637
"build" | "examples" | "deps" => None,
@@ -669,7 +669,7 @@ test!(many_crate_types_correct {
669669
assert_that(p.cargo_process("build"),
670670
execs().with_status(0));
671671

672-
let files = fs::read_dir(&p.root().join("target")).unwrap();
672+
let files = fs::read_dir(&p.root().join("target/debug")).unwrap();
673673
let mut files: Vec<String> = files.map(|f| f.unwrap().path()).filter_map(|f| {
674674
match f.file_name().unwrap().to_str().unwrap() {
675675
"build" | "examples" | "deps" => None,
@@ -840,10 +840,10 @@ test!(verbose_build {
840840
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib -g \
841841
-C metadata=[..] \
842842
-C extra-filename=-[..] \
843-
--out-dir {dir}[..]target \
843+
--out-dir {dir}[..]target[..]debug \
844844
--emit=dep-info,link \
845-
-L dependency={dir}[..]target \
846-
-L dependency={dir}[..]target[..]deps`
845+
-L dependency={dir}[..]target[..]debug \
846+
-L dependency={dir}[..]target[..]debug[..]deps`
847847
",
848848
running = RUNNING, compiling = COMPILING,
849849
dir = p.root().display(),
@@ -1049,7 +1049,7 @@ test!(release_build_ndebug {
10491049

10501050
assert_that(p.cargo_process("build").arg("--release"),
10511051
execs().with_status(0));
1052-
assert_that(process(&p.bin("release/foo")).unwrap(),
1052+
assert_that(process(&p.release_bin("foo")).unwrap(),
10531053
execs().with_stdout("fast\n"));
10541054
});
10551055

tests/test_cargo_compile_custom_build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ test!(custom_build_env_vars {
105105
let _feat = env::var("CARGO_FEATURE_FOO").unwrap();
106106
}}
107107
"#,
108-
p.root().join("target").join("build").display());
108+
p.root().join("target").join("debug").join("build").display());
109109

110110
let p = p.file("bar/build.rs", &file_content);
111111

@@ -702,13 +702,13 @@ test!(build_cmd_with_a_build_cmd {
702702
{running} `rustc build.rs --crate-name build-script-build --crate-type bin \
703703
-C prefer-dynamic -g \
704704
--out-dir [..]build[..]foo-[..] --emit=dep-info,link \
705-
-L [..]target -L [..]target[..]deps \
705+
-L [..]target[..]debug -L [..]target[..]deps \
706706
--extern a=[..]liba-[..].rlib`
707707
{running} `[..]foo-[..]build-script-build[..]`
708708
{running} `rustc [..]lib.rs --crate-name foo --crate-type lib -g \
709709
-C metadata=[..] -C extra-filename=-[..] \
710-
--out-dir [..]target --emit=dep-info,link \
711-
-L [..]target -L [..]target[..]deps`
710+
--out-dir [..]target[..]debug --emit=dep-info,link \
711+
-L [..]target[..]debug -L [..]target[..]deps`
712712
", compiling = COMPILING, running = RUNNING).as_slice()));
713713
});
714714

@@ -1024,7 +1024,7 @@ test!(build_script_with_dynamic_native_dependency {
10241024
"#);
10251025
assert_that(build.cargo_process("build"),
10261026
execs().with_status(0).with_stderr(""));
1027-
let src = build.root().join("target");
1027+
let src = build.root().join("target/debug");
10281028
let lib = fs::read_dir(&src).unwrap().map(|s| s.unwrap().path()).find(|lib| {
10291029
let lib = lib.file_name().unwrap().to_str().unwrap();
10301030
lib.starts_with(env::consts::DLL_PREFIX) &&

tests/test_cargo_compile_plugins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test!(plugin_with_dynamic_native_dependency {
100100
"#);
101101
assert_that(build.cargo_process("build"),
102102
execs().with_status(0).with_stderr(""));
103-
let src = build.root().join("target");
103+
let src = build.root().join("target/debug");
104104
let lib = fs::read_dir(&src).unwrap().map(|s| s.unwrap().path()).find(|lib| {
105105
let lib = lib.file_name().unwrap().to_str().unwrap();
106106
lib.starts_with(env::consts::DLL_PREFIX) &&

tests/test_cargo_cross_compile.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ test!(linker_and_ar {
282282
.with_stdout(format!("\
283283
{compiling} foo v0.5.0 ({url})
284284
{running} `rustc src/foo.rs --crate-name foo --crate-type bin -g \
285-
--out-dir {dir}[..]target[..]{target} \
285+
--out-dir {dir}[..]target[..]{target}[..]debug \
286286
--emit=dep-info,link \
287287
--target {target} \
288288
-C ar=my-ar-tool -C linker=my-linker-tool \
289-
-L dependency={dir}[..]target[..]{target} \
290-
-L dependency={dir}[..]target[..]{target}[..]deps`
289+
-L dependency={dir}[..]target[..]{target}[..]debug \
290+
-L dependency={dir}[..]target[..]{target}[..]debug[..]deps`
291291
",
292292
running = RUNNING,
293293
compiling = COMPILING,
@@ -464,6 +464,8 @@ test!(cross_with_a_build_script {
464464
path.pop();
465465
assert_eq!(path.filename().unwrap(), b"build");
466466
path.pop();
467+
assert_eq!(path.filename().unwrap(), b"debug");
468+
path.pop();
467469
assert_eq!(path.filename().unwrap(), b"{0}");
468470
path.pop();
469471
assert_eq!(path.filename().unwrap(), b"target");
@@ -636,7 +638,7 @@ test!(build_script_only_host {
636638
637639
fn main() {
638640
assert!(env::var("OUT_DIR").unwrap()
639-
.contains("target/build/d1-"),
641+
.contains("target/debug/build/d1-"),
640642
"bad: {:?}", env::var("OUT_DIR"));
641643
}
642644
"#);

0 commit comments

Comments
 (0)