Skip to content

Commit dd0b7a2

Browse files
committed
Add target directory parameter --target-dir
1 parent 5556ba9 commit dd0b7a2

File tree

16 files changed

+165
-17
lines changed

16 files changed

+165
-17
lines changed

src/bin/cli.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
7272
}
7373

7474
fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
75+
let (cmd, subcommand_args) = match args.subcommand() {
76+
(cmd, Some(args)) => (cmd, args),
77+
_ => {
78+
cli().print_help()?;
79+
return Ok(());
80+
}
81+
};
82+
83+
let arg_target_dir = &subcommand_args.value_of_path("target-dir", config);
84+
7585
config.configure(
7686
args.occurrences_of("verbose") as u32,
7787
if args.is_present("quiet") {
@@ -82,35 +92,28 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
8292
&args.value_of("color").map(|s| s.to_string()),
8393
args.is_present("frozen"),
8494
args.is_present("locked"),
95+
arg_target_dir,
8596
&args.values_of_lossy("unstable-features")
8697
.unwrap_or_default(),
8798
)?;
8899

89-
let (cmd, args) = match args.subcommand() {
90-
(cmd, Some(args)) => (cmd, args),
91-
_ => {
92-
cli().print_help()?;
93-
return Ok(());
94-
}
95-
};
96-
97100
if let Some(exec) = commands::builtin_exec(cmd) {
98-
return exec(config, args);
101+
return exec(config, subcommand_args);
99102
}
100103

101104
if let Some(mut alias) = super::aliased_command(config, cmd)? {
102105
alias.extend(
103-
args.values_of("")
104-
.unwrap_or_default()
105-
.map(|s| s.to_string()),
106+
subcommand_args.values_of("")
107+
.unwrap_or_default()
108+
.map(|s| s.to_string()),
106109
);
107-
let args = cli()
110+
let subcommand_args = cli()
108111
.setting(AppSettings::NoBinaryName)
109112
.get_matches_from_safe(alias)?;
110-
return execute_subcommand(config, args);
113+
return execute_subcommand(config, subcommand_args);
111114
}
112115
let mut ext_args: Vec<&str> = vec![cmd];
113-
ext_args.extend(args.values_of("").unwrap_or_default());
116+
ext_args.extend(subcommand_args.values_of("").unwrap_or_default());
114117
super::execute_external_subcommand(config, cmd, &ext_args)
115118
}
116119

src/bin/command_prelude.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ pub trait AppExt: Sized {
109109
self._arg(opt("target", target).value_name("TRIPLE"))
110110
}
111111

112+
fn arg_target_dir(self) -> Self {
113+
self._arg(opt("target-dir", "Directory for all generated artifacts").value_name("DIRECTORY"))
114+
}
115+
112116
fn arg_manifest_path(self) -> Self {
113117
self._arg(opt("manifest-path", "Path to Cargo.toml").value_name("PATH"))
114118
}

src/bin/commands/bench.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub fn cli() -> App {
3737
.arg_jobs()
3838
.arg_features()
3939
.arg_target_triple("Build for the target triple")
40+
.arg_target_dir()
4041
.arg_manifest_path()
4142
.arg_message_format()
4243
.arg(opt(

src/bin/commands/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn cli() -> App {
2727
.arg_release("Build artifacts in release mode, with optimizations")
2828
.arg_features()
2929
.arg_target_triple("Build for the target triple")
30+
.arg_target_dir()
3031
.arg(opt("out-dir", "Copy final artifacts to this directory").value_name("PATH"))
3132
.arg_manifest_path()
3233
.arg_message_format()

src/bin/commands/check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn cli() -> App {
2727
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
2828
.arg_features()
2929
.arg_target_triple("Check for the target triple")
30+
.arg_target_dir()
3031
.arg_manifest_path()
3132
.arg_message_format()
3233
.after_help(

src/bin/commands/clean.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub fn cli() -> App {
88
.arg_package_spec_simple("Package to clean artifacts for")
99
.arg_manifest_path()
1010
.arg_target_triple("Target triple to clean output for (default all)")
11+
.arg_target_dir()
1112
.arg_release("Whether or not to clean release artifacts")
1213
.after_help(
1314
"\

src/bin/commands/doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn cli() -> App {
2424
.arg_release("Build artifacts in release mode, with optimizations")
2525
.arg_features()
2626
.arg_target_triple("Build for the target triple")
27+
.arg_target_dir()
2728
.arg_manifest_path()
2829
.arg_message_format()
2930
.after_help(

src/bin/commands/package.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn cli() -> App {
2424
"Allow dirty working directories to be packaged",
2525
))
2626
.arg_target_triple("Build for the target triple")
27+
.arg_target_dir()
2728
.arg_manifest_path()
2829
.arg_jobs()
2930
}

src/bin/commands/publish.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn cli() -> App {
1616
"Allow dirty working directories to be packaged",
1717
))
1818
.arg_target_triple("Build for the target triple")
19+
.arg_target_dir()
1920
.arg_manifest_path()
2021
.arg_jobs()
2122
.arg(opt("dry-run", "Perform all checks without uploading"))

src/bin/commands/run.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn cli() -> App {
1818
.arg_release("Build artifacts in release mode, with optimizations")
1919
.arg_features()
2020
.arg_target_triple("Build for the target triple")
21+
.arg_target_dir()
2122
.arg_manifest_path()
2223
.arg_message_format()
2324
.after_help(

src/bin/commands/rustc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn cli() -> App {
2525
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
2626
.arg_features()
2727
.arg_target_triple("Target triple which compiles will be for")
28+
.arg_target_dir()
2829
.arg_manifest_path()
2930
.arg_message_format()
3031
.after_help(

src/bin/commands/rustdoc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn cli() -> App {
2727
)
2828
.arg_release("Build artifacts in release mode, with optimizations")
2929
.arg_features()
30+
.arg_target_dir()
3031
.arg_manifest_path()
3132
.arg_message_format()
3233
.after_help(

src/bin/commands/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub fn cli() -> App {
4141
.arg_release("Build artifacts in release mode, with optimizations")
4242
.arg_features()
4343
.arg_target_triple("Build for the target triple")
44+
.arg_target_dir()
4445
.arg_manifest_path()
4546
.arg_message_format()
4647
.after_help(

src/cargo/util/config.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ pub struct Config {
7070
cache_rustc_info: bool,
7171
/// Creation time of this config, used to output the total build time
7272
creation_time: Instant,
73+
/// Target Directory via resolved Cli parameter
74+
cli_target_dir: Option<Filesystem>,
7375
}
7476

7577
impl Config {
@@ -113,6 +115,7 @@ impl Config {
113115
crates_io_source_id: LazyCell::new(),
114116
cache_rustc_info,
115117
creation_time: Instant::now(),
118+
cli_target_dir: None,
116119
}
117120
}
118121

@@ -240,7 +243,9 @@ impl Config {
240243
}
241244

242245
pub fn target_dir(&self) -> CargoResult<Option<Filesystem>> {
243-
if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
246+
if let Some(ref dir) = self.cli_target_dir {
247+
Ok(Some(dir.clone()))
248+
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
244249
Ok(Some(Filesystem::new(self.cwd.join(dir))))
245250
} else if let Some(val) = self.get_path("build.target-dir")? {
246251
let val = self.cwd.join(val.val);
@@ -461,6 +466,7 @@ impl Config {
461466
color: &Option<String>,
462467
frozen: bool,
463468
locked: bool,
469+
target_dir: &Option<PathBuf>,
464470
unstable_flags: &[String],
465471
) -> CargoResult<()> {
466472
let extra_verbose = verbose >= 2;
@@ -494,11 +500,17 @@ impl Config {
494500
| (None, None, None) => Verbosity::Normal,
495501
};
496502

503+
let cli_target_dir = match target_dir.as_ref() {
504+
Some(dir) => Some(Filesystem::new(dir.clone())),
505+
None => None,
506+
};
507+
497508
self.shell().set_verbosity(verbosity);
498509
self.shell().set_color_choice(color.map(|s| &s[..]))?;
499510
self.extra_verbose = extra_verbose;
500511
self.frozen = frozen;
501512
self.locked = locked;
513+
self.cli_target_dir = cli_target_dir;
502514
self.cli_flags.parse(unstable_flags)?;
503515

504516
Ok(())

tests/testsuite/build.rs

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3603,7 +3603,7 @@ fn dotdir_root() {
36033603
}
36043604

36053605
#[test]
3606-
fn custom_target_dir() {
3606+
fn custom_target_dir_env() {
36073607
let p = project("foo")
36083608
.file(
36093609
"Cargo.toml",
@@ -3670,6 +3670,123 @@ fn custom_target_dir() {
36703670
);
36713671
}
36723672

3673+
#[test]
3674+
fn custom_target_dir_line_parameter() {
3675+
let p = project("foo")
3676+
.file(
3677+
"Cargo.toml",
3678+
r#"
3679+
[package]
3680+
name = "foo"
3681+
version = "0.0.1"
3682+
authors = []
3683+
"#,
3684+
)
3685+
.file("src/main.rs", "fn main() {}")
3686+
.build();
3687+
3688+
let exe_name = format!("foo{}", env::consts::EXE_SUFFIX);
3689+
3690+
assert_that(
3691+
p.cargo("build").arg("--target-dir").arg("foo/target"),
3692+
execs().with_status(0),
3693+
);
3694+
assert_that(
3695+
&p.root().join("foo/target/debug").join(&exe_name),
3696+
existing_file(),
3697+
);
3698+
assert_that(
3699+
&p.root().join("target/debug").join(&exe_name),
3700+
is_not(existing_file()),
3701+
);
3702+
3703+
assert_that(p.cargo("build"), execs().with_status(0));
3704+
assert_that(
3705+
&p.root().join("foo/target/debug").join(&exe_name),
3706+
existing_file(),
3707+
);
3708+
assert_that(
3709+
&p.root().join("target/debug").join(&exe_name),
3710+
existing_file(),
3711+
);
3712+
3713+
fs::create_dir(p.root().join(".cargo")).unwrap();
3714+
File::create(p.root().join(".cargo/config"))
3715+
.unwrap()
3716+
.write_all(
3717+
br#"
3718+
[build]
3719+
target-dir = "foo/target"
3720+
"#,
3721+
)
3722+
.unwrap();
3723+
assert_that(
3724+
p.cargo("build").arg("--target-dir").arg("bar/target"),
3725+
execs().with_status(0),
3726+
);
3727+
assert_that(
3728+
&p.root().join("bar/target/debug").join(&exe_name),
3729+
existing_file(),
3730+
);
3731+
assert_that(
3732+
&p.root().join("foo/target/debug").join(&exe_name),
3733+
existing_file(),
3734+
);
3735+
assert_that(
3736+
&p.root().join("target/debug").join(&exe_name),
3737+
existing_file(),
3738+
);
3739+
3740+
assert_that(
3741+
p.cargo("build")
3742+
.arg("--target-dir")
3743+
.arg("foobar/target")
3744+
.env("CARGO_TARGET_DIR", "bar/target"),
3745+
execs().with_status(0),
3746+
);
3747+
assert_that(
3748+
&p.root().join("foobar/target/debug").join(&exe_name),
3749+
existing_file(),
3750+
);
3751+
assert_that(
3752+
&p.root().join("bar/target/debug").join(&exe_name),
3753+
existing_file(),
3754+
);
3755+
assert_that(
3756+
&p.root().join("foo/target/debug").join(&exe_name),
3757+
existing_file(),
3758+
);
3759+
assert_that(
3760+
&p.root().join("target/debug").join(&exe_name),
3761+
existing_file(),
3762+
);
3763+
}
3764+
3765+
#[test]
3766+
fn rustc_no_trans() {
3767+
if !is_nightly() {
3768+
return;
3769+
}
3770+
3771+
let p = project("foo")
3772+
.file(
3773+
"Cargo.toml",
3774+
r#"
3775+
[package]
3776+
name = "foo"
3777+
version = "0.0.1"
3778+
authors = []
3779+
"#,
3780+
)
3781+
.file("src/main.rs", "fn main() {}")
3782+
.build();
3783+
3784+
assert_that(
3785+
p.cargo("rustc").arg("-v").arg("--").arg("-Zno-trans"),
3786+
execs().with_status(0),
3787+
);
3788+
}
3789+
36733790
#[test]
36743791
fn build_multiple_packages() {
36753792
let p = project("foo")

tests/testsuite/resolve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ fn test_resolving_minimum_version_with_transitive_deps() {
362362
&None,
363363
false,
364364
false,
365+
&None,
365366
&["minimal-versions".to_string()],
366367
)
367368
.unwrap();

0 commit comments

Comments
 (0)