Skip to content

Commit c326fcb

Browse files
committed
Auto merge of #7810 - giraffate:support_out_dir_in_build_section, r=alexcrichton
Support out-dir in build section of Cargo configuration file Fixed #7555.
2 parents 328b7d6 + 4a1ba1c commit c326fcb

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/bin/cargo/commands/build.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
6363
ProfileChecking::Checked,
6464
)?;
6565

66-
compile_opts.export_dir = args.value_of_path("out-dir", config);
66+
if let Some(out_dir) = args.value_of_path("out-dir", config) {
67+
compile_opts.export_dir = Some(out_dir);
68+
} else if let Some(out_dir) = config.build_config()?.out_dir.as_ref() {
69+
let out_dir = out_dir.resolve_path(config);
70+
compile_opts.export_dir = Some(out_dir);
71+
}
6772
if compile_opts.export_dir.is_some() {
6873
config
6974
.cli_unstable()

src/cargo/util/config/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,7 @@ pub struct CargoBuildConfig {
16431643
pub rustc_wrapper: Option<PathBuf>,
16441644
pub rustc: Option<PathBuf>,
16451645
pub rustdoc: Option<PathBuf>,
1646+
pub out_dir: Option<ConfigRelativePath>,
16461647
}
16471648

16481649
/// A type to deserialize a list of strings from a toml file.

src/doc/src/reference/unstable.md

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ directory. Example:
7676
cargo +nightly build --out-dir=out -Z unstable-options
7777
```
7878

79+
This can also be specified in `.cargo/config` files.
80+
81+
```toml
82+
[build]
83+
out-dir = "out"
84+
```
85+
7986
### doctest-xcompile
8087
* Tracking Issue: [#7040](https://github.com/rust-lang/cargo/issues/7040)
8188
* Tracking Rustc Issue: [#64245](https://github.com/rust-lang/rust/issues/64245)

tests/testsuite/out_dir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ fn avoid_build_scripts() {
246246
);
247247
}
248248

249+
#[cargo_test]
250+
fn cargo_build_out_dir() {
251+
let p = project()
252+
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
253+
.file(
254+
".cargo/config",
255+
r#"
256+
[build]
257+
out-dir = "out"
258+
"#,
259+
)
260+
.build();
261+
262+
p.cargo("build -Z unstable-options")
263+
.masquerade_as_nightly_cargo()
264+
.run();
265+
check_dir_contents(
266+
&p.root().join("out"),
267+
&["foo"],
268+
&["foo", "foo.dSYM"],
269+
&["foo.exe", "foo.pdb"],
270+
);
271+
}
272+
249273
fn check_dir_contents(
250274
out_dir: &Path,
251275
expected_linux: &[&str],

0 commit comments

Comments
 (0)