Refactor: Extract new crate detect-targets and improve code quality (#307)

* Refactor: Extract new crate `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Extract new mod `detect` for `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Extract `desired_targets` in crate `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Extract `detect::linux` in crate `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Extract `detect::macos` in crate `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Extract `detect::windows` in crate `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Add new dep cfg-if v1.0.0 for `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Simplify mod declaration in `detect` using `cfg_if!`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Refactor: Simplify `detect_targets` using `cfg_if!`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Add crate doc for `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Enable feature "macros" of tokio in `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Enable feature "io-util" of dep tokio

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Rm unused feature "io-util" & "macros" of dep tokio

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Set stdin & stderr to null in `get_target_from_rustc`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Improve doc of `get_desired_targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Improve `detect_targets_linux`: Run `ldd` with stdin set to null

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix potential panic in `windows::detect_alternative_targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* FIx fmt of `detect_targets_linux`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Do not re-export dep `detect-targets` in `crates/lib`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix typo in crate doc for `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Enable feature "macros" of tokio in dev mode

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Add example to crate doc of `detect-targets`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Improve API `get_desired_targets`: Take `Option<&str>`

instead of `&Option<String>`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-08-21 22:21:33 +10:00 committed by GitHub
parent 1102284684
commit 62f9450d2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 574 additions and 263 deletions

View file

@ -0,0 +1,65 @@
//! Detect the target at the runtime.
//!
//! Example use cases:
//! - The binary is built with musl libc to run on anywhere, but
//! the runtime supports glibc.
//! - The binary is built for x86_64-apple-darwin, but run on
//! aarch64-apple-darwin.
//!
//! This crate provides two API:
//! - [`detect_targets`] provides the API to get the target
//! at runtime, but the code is run on the current thread.
//! - [`get_desired_targets`] provides the API to either
//! parse `$target1,$target2,...` override provided by the users,
//! or run [`detect_targets`] in the background using [`tokio::spawn`].
//!
//! # Example
//!
//! `detect_targets`:
//!
//! ```rust
//! use detect_targets::detect_targets;
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//!
//! let targets = detect_targets().await;
//! eprintln!("Your platform supports targets: {targets:#?}");
//! # }
//! ```
//!
//! `get_desired_targets` with user override:
//!
//! ```rust
//! use detect_targets::get_desired_targets;
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//!
//! assert_eq!(
//! get_desired_targets(Some("x86_64-apple-darwin,aarch64-apple-darwin")).get().await,
//! &["x86_64-apple-darwin", "aarch64-apple-darwin"],
//! );
//! # }
//! ```
//!
//! `get_desired_targets` without user override:
//!
//! ```rust
//! use detect_targets::get_desired_targets;
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//!
//! eprintln!(
//! "Your platform supports targets: {:#?}",
//! get_desired_targets(None).get().await
//! );
//! # }
//! ```
mod detect;
pub use detect::detect_targets;
mod desired_targets;
pub use desired_targets::{get_desired_targets, DesiredTargets};
/// Compiled target triple, used as default for binary fetching
pub const TARGET: &str = env!("TARGET");