Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add custom completer for completing target triple #14535

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ pub trait CommandExt: Sized {
};
self._arg(
optional_multi_opt("target", "TRIPLE", target)
.help_heading(heading::COMPILATION_OPTIONS),
.help_heading(heading::COMPILATION_OPTIONS)
.add(clap_complete::ArgValueCandidates::new(get_target_triples)),
)
._arg(unsupported_short_arg)
}
Expand Down Expand Up @@ -1067,6 +1068,62 @@ fn get_targets_from_metadata() -> CargoResult<Vec<Target>> {
Ok(targets)
}

fn get_target_triples() -> Vec<clap_complete::CompletionCandidate> {
let mut candidates = Vec::new();

if is_rustup() {
if let Ok(targets) = get_target_triples_from_rustup() {
candidates.extend(targets);
}
} else {
if let Ok(targets) = get_target_triples_from_rustc() {
candidates.extend(targets);
}
}

candidates
}

fn get_target_triples_from_rustup() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
let output = std::process::Command::new("rustup")
.arg("target")
.arg("list")
.output()?;

if !output.status.success() {
return Ok(vec![]);
}

let stdout = String::from_utf8(output.stdout)?;

Ok(stdout
.lines()
.map(|line| {
let target = line.split_once(' ');
match target {
None => clap_complete::CompletionCandidate::new(line.to_owned()).hide(true),
Some((target, _installed)) => clap_complete::CompletionCandidate::new(target),
}
})
.collect())
}

fn get_target_triples_from_rustc() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
let cwd = std::env::current_dir()?;
let gctx = GlobalContext::new(shell::Shell::new(), cwd.clone(), cargo_home_with_cwd(&cwd)?);
let ws = Workspace::new(&find_root_manifest_for_wd(&PathBuf::from(&cwd))?, &gctx);

let rustc = gctx.load_global_rustc(ws.as_ref().ok())?;

let (stdout, _stderr) =
rustc.cached_output(rustc.process().arg("--print").arg("target-list"), 0)?;

Ok(stdout
.lines()
.map(|line| clap_complete::CompletionCandidate::new(line.to_owned()))
.collect())
}

#[track_caller]
pub fn ignore_unknown<T: Default>(r: Result<T, clap::parser::MatchesError>) -> T {
match r {
Expand Down