mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00

* Fix fallback to `cargo-install`: Use the default target which is guaranteed to work, instead of providing a custom `--target` from `detect-targets`. The one from `detect-targets` might report musl even if the `rustc` reports gnu, since OSes like NixOS could use a custom glibc location that prevent glibc binaries built for other OSes be run for them. However, if we are compiling the crate and `rustc` defaults to glibc, then it should be guaranteed to work. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Impl fn `DesiredTargets::get_initialized` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix fallback to `cargo-install`: Use user-provided override targets Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> --------- Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
69 lines
2 KiB
Rust
69 lines
2 KiB
Rust
use crate::detect_targets;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::OnceCell;
|
|
|
|
#[derive(Debug)]
|
|
enum DesiredTargetsInner {
|
|
AutoDetect(Arc<OnceCell<Vec<String>>>),
|
|
Initialized(Vec<String>),
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct DesiredTargets(DesiredTargetsInner);
|
|
|
|
impl DesiredTargets {
|
|
fn initialized(targets: Vec<String>) -> Self {
|
|
Self(DesiredTargetsInner::Initialized(targets))
|
|
}
|
|
|
|
fn auto_detect() -> Self {
|
|
let arc = Arc::new(OnceCell::new());
|
|
|
|
let once_cell = arc.clone();
|
|
tokio::spawn(async move {
|
|
once_cell.get_or_init(detect_targets).await;
|
|
});
|
|
|
|
Self(DesiredTargetsInner::AutoDetect(arc))
|
|
}
|
|
|
|
pub async fn get(&self) -> &[String] {
|
|
use DesiredTargetsInner::*;
|
|
|
|
match &self.0 {
|
|
Initialized(targets) => targets,
|
|
|
|
// This will mostly just wait for the spawned task,
|
|
// on rare occausion though, it will poll the future
|
|
// returned by `detect_targets`.
|
|
AutoDetect(once_cell) => once_cell.get_or_init(detect_targets).await,
|
|
}
|
|
}
|
|
|
|
/// If `DesiredTargets` is provided with a list of desired targets instead
|
|
/// of detecting the targets, then this function would return `Some`.
|
|
pub fn get_initialized(&self) -> Option<&[String]> {
|
|
use DesiredTargetsInner::*;
|
|
|
|
match &self.0 {
|
|
Initialized(targets) => Some(targets),
|
|
AutoDetect(..) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// If opts_targets is `Some`, then it will be used.
|
|
/// Otherwise, call `detect_targets` using `tokio::spawn` to detect targets.
|
|
///
|
|
/// Since `detect_targets` internally spawns a process and wait for it,
|
|
/// it's pretty costy, it is recommended to run this fn ASAP and
|
|
/// reuse the result.
|
|
pub fn get_desired_targets(opts_targets: Option<Vec<String>>) -> DesiredTargets {
|
|
if let Some(targets) = opts_targets {
|
|
DesiredTargets::initialized(targets)
|
|
} else {
|
|
DesiredTargets::auto_detect()
|
|
}
|
|
}
|