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