target_spec/
lib.rs

1// Copyright (c) The cargo-guppy Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Evaluate `Cargo.toml` target specifications against platform triples.
5//!
6//! Cargo supports [platform-specific
7//! dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies).
8//! These dependencies can be specified in one of two ways:
9//!
10//! ```toml
11//! # 1. As Rust-like `#[cfg]` syntax.
12//! [target.'cfg(all(unix, target_arch = "x86_64"))'.dependencies]
13//! native = { path = "native/x86_64" }
14//!
15//! # 2. Listing out the full target triple.
16//! [target.x86_64-pc-windows-gnu.dependencies]
17//! winhttp = "0.4.0"
18//! ```
19//!
20//! `target-spec` provides the `eval` API which can be used to figure out whether such a dependency
21//! will be included on a particular platform.
22//!
23//! ```rust
24//! use target_spec::eval;
25//!
26//! // Evaluate Rust-like `#[cfg]` syntax.
27//! let cfg_target = "cfg(all(unix, target_arch = \"x86_64\"))";
28//! assert_eq!(eval(cfg_target, "x86_64-unknown-linux-gnu").unwrap(), Some(true));
29//! assert_eq!(eval(cfg_target, "i686-unknown-linux-gnu").unwrap(), Some(false));
30//! assert_eq!(eval(cfg_target, "x86_64-pc-windows-msvc").unwrap(), Some(false));
31//!
32//! // Evaluate a full target-triple.
33//! assert_eq!(eval("x86_64-unknown-linux-gnu", "x86_64-unknown-linux-gnu").unwrap(), Some(true));
34//! assert_eq!(eval("x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc").unwrap(), Some(false));
35//! ```
36//!
37//! For more advanced usage, see [`Platform`] and [`TargetSpec`].
38//!
39//! ## Optional features
40//!
41//! * **`custom`**: Adds support for [custom
42//!   targets](https://docs.rust-embedded.org/embedonomicon/custom-target.html) via
43//!   [`Platform::new_custom`].
44//! * **`summaries`**: Adds the [`summaries`] module to enable serialization of [`Platform`] and
45//!   [`TargetFeatures`].
46//! * **`proptest1`**: Enables support for property-based testing of [`Platform`] and
47//!   [`TargetFeatures`] using [`proptest`].
48//!
49//! ## Minimum supported Rust version
50//!
51//! The minimum supported Rust version (MSRV) is **Rust 1.82**. The MSRV history is:
52//!
53//! * For target-spec 3.0.x: **Rust 1.66**.
54//! * For target-spec 3.1.x: **Rust 1.73**.
55//! * For target-spec 3.2.x: **Rust 1.75**.
56//! * For target-spec 3.3.x and 3.4.x: **Rust 1.82**.
57//! * Unreleased: **Rust 1.81**.
58//!
59//! Within the 3.x series, MSRV bumps will be accompanied by a minor version
60//! update. The last 6 months of stable Rust releases will be supported.
61//!
62//! ## Related crates
63//!
64//! To pretty-print target-spec errors, consider using the [miette](https://docs.rs/miette)
65//! diagnostic library with [target-spec-miette](https://crates.io/crates/target-spec-miette).
66
67#![warn(missing_docs)]
68#![forbid(unsafe_code)]
69#![cfg_attr(doc_cfg, feature(doc_cfg, doc_auto_cfg))]
70
71#[cfg(feature = "custom")]
72mod custom;
73pub mod errors;
74mod platform;
75#[cfg(feature = "proptest1")]
76mod proptest_helpers;
77mod simple_eval;
78mod spec;
79#[cfg(feature = "summaries")]
80pub mod summaries;
81mod triple;
82
83pub use errors::Error;
84pub use platform::*;
85pub use simple_eval::*;
86pub use spec::*;
87pub use triple::*;