detect_targets/
lib.rs

1//! Detect the target at the runtime.
2//!
3//! It runs `$CARGO -vV` if environment variable `CARGO` is present
4//! for cargo subcommands, otherwise it would try running `rustc -vV`.
5//!
6//! If both `rustc` isn't present on the system, it will fallback
7//! to using syscalls plus `ldd` on Linux to detect targets.
8//!
9//! Example use cases:
10//!  - The binary is built with musl libc to run on anywhere, but
11//!    the runtime supports glibc.
12//!  - The binary is built for x86_64-apple-darwin, but run on
13//!    aarch64-apple-darwin.
14//!
15//! This crate provides two API:
16//!  - [`detect_targets`] provides the API to get the target
17//!    at runtime, but the code is run on the current thread.
18//!  - [`get_desired_targets`] provides the API to either
19//!    use override provided by the users, or run [`detect_targets`]
20//!    in the background using [`tokio::spawn`].
21//!
22//! # Example
23//!
24//! `detect_targets`:
25//!
26//! ```rust
27//! use detect_targets::detect_targets;
28//! # #[tokio::main(flavor = "current_thread")]
29//! # async fn main() {
30//!
31//! let targets = detect_targets().await;
32//! eprintln!("Your platform supports targets: {targets:#?}");
33//! # }
34//! ```
35//!
36//! `get_desired_targets` with user override:
37//!
38//! ```rust
39//! use detect_targets::get_desired_targets;
40//! # #[tokio::main(flavor = "current_thread")]
41//! # async fn main() {
42//!
43//! assert_eq!(
44//!     get_desired_targets(Some(vec![
45//!         "x86_64-apple-darwin".to_string(),
46//!         "aarch64-apple-darwin".to_string(),
47//!     ])).get().await,
48//!     &["x86_64-apple-darwin", "aarch64-apple-darwin"],
49//! );
50//! # }
51//! ```
52//!
53//! `get_desired_targets` without user override:
54//!
55//! ```rust
56//! use detect_targets::get_desired_targets;
57//! # #[tokio::main(flavor = "current_thread")]
58//! # async fn main() {
59//!
60//! eprintln!(
61//!     "Your platform supports targets: {:#?}",
62//!     get_desired_targets(None).get().await
63//! );
64//! # }
65//! ```
66
67mod detect;
68pub use detect::detect_targets;
69
70mod desired_targets;
71pub use desired_targets::{get_desired_targets, DesiredTargets};
72
73/// Compiled target triple, used as default for binary fetching
74pub const TARGET: &str = env!("TARGET");