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-cfg`**: Adds support for custom targets via
42//! `rustc --print=cfg` output, through
43//! [`Platform::new_custom_cfg`].
44//! * **`custom`**: Adds support for [custom
45//! targets](https://docs.rust-embedded.org/embedonomicon/custom-target.html) via
46//! target JSON, through [`Platform::new_custom`]. Implies
47//! `custom-cfg`.
48//! * **`summaries`**: Adds the [`summaries`] module to enable serialization of [`Platform`] and
49//! [`TargetFeatures`].
50//! * **`proptest1`**: Enables support for property-based testing of [`Platform`] and
51//! [`TargetFeatures`] using [`proptest`].
52//!
53//! ## Minimum supported Rust version
54//!
55//! The minimum supported Rust version (MSRV) is **Rust 1.82**. The MSRV history is:
56//!
57//! * For target-spec 3.0.x: **Rust 1.66**.
58//! * For target-spec 3.1.x: **Rust 1.73**.
59//! * For target-spec 3.2.x: **Rust 1.75**.
60//! * For target-spec 3.3.x and 3.4.x: **Rust 1.82**.
61//! * For target-spec 3.5.x: **Rust 1.86**.
62//!
63//! Within the 3.x series, MSRV bumps will be accompanied by a minor version
64//! update. The last 6 months of stable Rust releases will be supported.
65//!
66//! ## Related crates
67//!
68//! To pretty-print target-spec errors, consider using the [miette](https://docs.rs/miette)
69//! diagnostic library with [target-spec-miette](https://crates.io/crates/target-spec-miette).
70
71#![warn(missing_docs)]
72#![forbid(unsafe_code)]
73#![cfg_attr(doc_cfg, feature(doc_cfg))]
74
75#[cfg(feature = "custom")]
76mod custom;
77#[cfg(feature = "custom-cfg")]
78mod custom_cfg;
79pub mod errors;
80mod platform;
81#[cfg(feature = "proptest1")]
82mod proptest_helpers;
83mod simple_eval;
84mod spec;
85#[cfg(feature = "summaries")]
86pub mod summaries;
87mod triple;
88
89pub use errors::Error;
90pub use platform::*;
91pub use simple_eval::*;
92pub use spec::*;
93pub use triple::*;