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.91**. 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 and 3.6.x: **Rust 1.86**.
62//! * For target-spec 3.7.x: **Rust 1.91**.
63//!
64//! Within the 3.x series, MSRV bumps will be accompanied by a minor version
65//! update. The last 6 months of stable Rust releases will be supported.
66//!
67//! ## Related crates
68//!
69//! To pretty-print target-spec errors, consider using the [miette](https://docs.rs/miette)
70//! diagnostic library with [target-spec-miette](https://crates.io/crates/target-spec-miette).
71
72#![warn(missing_docs)]
73#![forbid(unsafe_code)]
74#![cfg_attr(doc_cfg, feature(doc_cfg))]
75
76#[cfg(feature = "custom")]
77mod custom;
78#[cfg(feature = "custom-cfg")]
79mod custom_cfg;
80pub mod errors;
81mod platform;
82#[cfg(feature = "proptest1")]
83mod proptest_helpers;
84mod simple_eval;
85mod spec;
86#[cfg(feature = "summaries")]
87pub mod summaries;
88mod triple;
89
90pub use errors::Error;
91pub use platform::*;
92pub use simple_eval::*;
93pub use spec::*;
94pub use triple::*;