Skip to content

Commit

Permalink
Merge #1142
Browse files Browse the repository at this point in the history
1142: Ensure ignored CLI arguments to cross are always passed at the end. r=Emilgardis a=Alexhuszagh

Closes #1141.

Co-authored-by: Alex Huszagh <[email protected]>
  • Loading branch information
bors[bot] and Alexhuszagh authored Nov 20, 2022
2 parents 6635e20 + 2ee4d61 commit ab99baf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .changes/1049.json → .changes/1049-1142.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "changed",
"description": "stop parsing arguments to `cross run` after `--`",
"issues": [1048]
"issues": [1048, 1141]
}
74 changes: 53 additions & 21 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::Target;

#[derive(Debug)]
pub struct Args {
pub all: Vec<String>,
pub cargo_args: Vec<String>,
pub rest_args: Vec<String>,
pub subcommand: Option<Subcommand>,
pub channel: Option<String>,
pub target: Option<Target>,
Expand Down Expand Up @@ -163,7 +164,8 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
let mut manifest_path: Option<PathBuf> = None;
let mut target_dir = None;
let mut sc = None;
let mut all: Vec<String> = Vec::new();
let mut cargo_args: Vec<String> = Vec::new();
let mut rest_args: Vec<String> = Vec::new();
let mut version = false;
let mut quiet = false;
let mut verbose = 0;
Expand All @@ -176,69 +178,98 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
continue;
}
if matches!(arg.as_str(), "--") {
all.push(arg);
all.extend(args.by_ref());
rest_args.push(arg);
rest_args.extend(args.by_ref());
} else if let v @ 1.. = is_verbose(arg.as_str()) {
verbose += v;
all.push(arg);
cargo_args.push(arg);
} else if matches!(arg.as_str(), "--version" | "-V") {
version = true;
} else if matches!(arg.as_str(), "--quiet" | "-q") {
quiet = true;
all.push(arg);
cargo_args.push(arg);
} else if let Some(kind) = is_value_arg(&arg, "--color") {
color = match kind {
ArgKind::Next => {
match parse_next_arg(arg, &mut all, str_to_owned, identity, &mut args)? {
match parse_next_arg(
arg,
&mut cargo_args,
str_to_owned,
identity,
&mut args,
)? {
Some(c) => Some(c),
None => shell::invalid_color(None),
}
}
ArgKind::Equal => Some(parse_equal_arg(arg, &mut all, str_to_owned, identity)?),
ArgKind::Equal => Some(parse_equal_arg(
arg,
&mut cargo_args,
str_to_owned,
identity,
)?),
};
} else if let Some(kind) = is_value_arg(&arg, "--manifest-path") {
manifest_path = match kind {
ArgKind::Next => parse_next_arg(
arg,
&mut all,
&mut cargo_args,
parse_manifest_path,
store_manifest_path,
&mut args,
)?
.flatten(),
ArgKind::Equal => {
parse_equal_arg(arg, &mut all, parse_manifest_path, store_manifest_path)?
}
ArgKind::Equal => parse_equal_arg(
arg,
&mut cargo_args,
parse_manifest_path,
store_manifest_path,
)?,
};
} else if let ("+", ch) = arg.split_at(1) {
channel = Some(ch.to_owned());
} else if let Some(kind) = is_value_arg(&arg, "--target") {
let parse_target = |t: &str| Ok(Target::from(t, target_list));
target = match kind {
ArgKind::Next => {
parse_next_arg(arg, &mut all, parse_target, identity, &mut args)?
parse_next_arg(arg, &mut cargo_args, parse_target, identity, &mut args)?
}
ArgKind::Equal => Some(parse_equal_arg(arg, &mut all, parse_target, identity)?),
ArgKind::Equal => Some(parse_equal_arg(
arg,
&mut cargo_args,
parse_target,
identity,
)?),
};
} else if let Some(kind) = is_value_arg(&arg, "--features") {
match kind {
ArgKind::Next => {
let next =
parse_next_arg(arg, &mut all, str_to_owned, identity, &mut args)?;
let next = parse_next_arg(
arg,
&mut cargo_args,
str_to_owned,
identity,
&mut args,
)?;
if let Some(feature) = next {
features.push(feature);
}
}
ArgKind::Equal => {
features.push(parse_equal_arg(arg, &mut all, str_to_owned, identity)?);
features.push(parse_equal_arg(
arg,
&mut cargo_args,
str_to_owned,
identity,
)?);
}
}
} else if let Some(kind) = is_value_arg(&arg, "--target-dir") {
match kind {
ArgKind::Next => {
target_dir = parse_next_arg(
arg,
&mut all,
&mut cargo_args,
parse_target_dir,
store_target_dir,
&mut args,
Expand All @@ -247,7 +278,7 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
ArgKind::Equal => {
target_dir = Some(parse_equal_arg(
arg,
&mut all,
&mut cargo_args,
parse_target_dir,
store_target_dir,
)?);
Expand All @@ -258,13 +289,14 @@ pub fn parse(target_list: &TargetList) -> Result<Args> {
sc = Some(Subcommand::from(arg.as_ref()));
}

all.push(arg.clone());
cargo_args.push(arg.clone());
}
}
}

Ok(Args {
all,
cargo_args,
rest_args,
subcommand: sc,
channel,
target,
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =
.map_or(false, |s| !s.needs_target_in_command())
{
let mut filtered_args = Vec::new();
let mut args_iter = args.all.clone().into_iter();
let mut args_iter = args.cargo_args.clone().into_iter();
while let Some(arg) = args_iter.next() {
if arg == "--target" {
args_iter.next();
Expand All @@ -661,14 +661,14 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =
}
filtered_args
// Make sure --target is present
} else if !args.all.iter().any(|a| a.starts_with("--target")) {
let mut args_with_target = args.all.clone();
} else if !args.cargo_args.iter().any(|a| a.starts_with("--target")) {
let mut args_with_target = args.cargo_args.clone();
args_with_target.push("--target".to_owned());
args_with_target.push(add_libc(target.triple()));
args_with_target
} else if zig_version.is_some() {
let mut filtered_args = Vec::new();
let mut args_iter = args.all.clone().into_iter();
let mut args_iter = args.cargo_args.clone().into_iter();
while let Some(arg) = args_iter.next() {
if arg == "--target" {
filtered_args.push("--target".to_owned());
Expand All @@ -683,7 +683,7 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =
}
filtered_args
} else {
args.all.clone()
args.cargo_args.clone()
};

let is_test = args.subcommand.map_or(false, |sc| sc == Subcommand::Test);
Expand All @@ -693,6 +693,7 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =
if uses_build_std {
filtered_args.push("-Zbuild-std".to_owned());
}
filtered_args.extend(args.rest_args.iter().cloned());

let needs_docker = args
.subcommand
Expand Down

0 comments on commit ab99baf

Please sign in to comment.