build_target/lib.rs
1#![warn(clippy::pedantic)]
2#![allow(
3 clippy::needless_doctest_main,
4 clippy::missing_errors_doc,
5 clippy::should_implement_trait,
6 clippy::doc_markdown
7)]
8
9//! A crate that provides programmatic access to information about the current build target inside `build.rs`.
10//!
11//! ## Examples
12//! Prints all available information about the current build target.
13//! ```rust no_run
14//! // inside build.rs
15//!
16//! fn main() {
17//! // The panic is just used to print the information to the console.
18//! panic!("current build target: {:#?}",
19//! build_target::target()
20//! );
21//! }
22//! ```
23//!
24//! Gets the parts of the current build target individually.
25//! ```rust no_run
26//! // inside build.rs
27//!
28//! fn main() {
29//! let arch = build_target::target_arch(); // eg. "x86_64", "aarch64", ...
30//! let endian = build_target::target_endian(); // eg. "big", "little", ...
31//! let env = build_target::target_env(); // eg. "gnu", "msvc", ...
32//! let family = build_target::target_family(); // eg. "windows", "unix", ...
33//! let pw = build_target::target_pointer_width(); // eg. "32", "64", ...
34//! let os = build_target::target_os(); // eg. "android", "linux", ...
35//! let vendor = build_target::target_vendor(); // eg. "apple", "unknown", ...
36//! let triple = build_target::target_triple(); // eg. "x86_64-unknown-linux-gnu", ...
37//! }
38//! ```
39
40mod arch;
41pub use arch::*;
42
43mod endian;
44pub use endian::*;
45
46mod env;
47pub use env::*;
48
49mod family;
50pub use family::*;
51
52mod os;
53pub use os::*;
54
55mod pointer_width;
56pub use pointer_width::*;
57
58mod profile;
59pub use profile::*;
60
61mod vendor;
62pub use vendor::*;
63
64mod target;
65pub use target::*;
66
67use crate::utils::build_env;
68
69mod utils;
70
71/// Gets the current target [`Arch`]. This function is equivalent to [`Arch::target()`].
72#[must_use]
73pub fn target_arch() -> Arch {
74 Arch::target()
75}
76/// Gets the current target [`Endian`]. This function is equivalent to [`Endian::target()`].
77#[must_use]
78pub fn target_endian() -> Endian {
79 Endian::target()
80}
81/// Gets the current target [`Env`]. This function is equivalent to [`Env::target()`].
82#[must_use]
83pub fn target_env() -> Option<Env> {
84 Env::target()
85}
86/// Gets the current target [`Family`]s. This function is equivalent to [`Family::target()`].
87#[must_use]
88pub fn target_family() -> Vec<Family> {
89 Family::target()
90}
91/// Gets the current target [`Os`]. This function is equivalent to [`Os::target()`].
92#[must_use]
93pub fn target_os() -> Os {
94 Os::target()
95}
96/// Gets the current target [`PointerWidth`]. This function is equivalent to [`PointerWidth::target()`].
97#[must_use]
98pub fn target_pointer_width() -> PointerWidth {
99 PointerWidth::target()
100}
101/// Gets the current target [`Vendor`]. This function is equivalent to [`Vendor::target()`].
102#[must_use]
103pub fn target_vendor() -> Vendor {
104 Vendor::target()
105}
106/// Gets the current target triple.
107#[must_use]
108pub fn target_triple() -> String {
109 build_env("TARGET")
110}
111/// Gets the current target information as a [`Target`]. This function is equivalent to [`Target::current()`].
112#[must_use]
113pub fn target() -> Target {
114 Target::current()
115}