chksum_build/lib.rs
1//! Tiny library for setting/getting build-time values for your crate.
2//!
3//! # Setup
4//!
5//! ## Create `build.rs`
6//!
7//! Create new file `build.rs` at the top level of your crate (next to `Cargo.toml`).
8//!
9//! ```rust,no_run
10//! use chksum_build::{BuildScript, Result};
11//!
12//! fn main() -> Result<()> {
13//! BuildScript::default().setup()
14//! }
15//! ```
16//!
17//! Optionally you can use along with [`anyhow`].
18//!
19//! ```rust,no_run
20//! use anyhow::Result;
21//! use chksum_build::{setup, BuildScript};
22//!
23//! fn main() -> Result<()> {
24//! setup(&BuildScript::default())
25//! }
26//! ```
27//!
28//! ## Update `Cargo.toml`
29//!
30//! ### Modify `package` section
31//!
32//! ```toml
33//! [package]
34//! # ...
35//! build = "build.rs"
36//! ```
37//!
38//! ### Modify `build-dependencies` section
39//!
40//! You can update `Cargo.toml` on your own.
41//!
42//! ```toml
43//! [build-dependencies]
44//! # ...
45//! chksum-build = "0.0.3"
46//! ```
47//!
48//! Or use [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand.
49//!
50//! ```sh
51//! cargo add --build chksum-build
52//! ```
53//!
54//! ### Modify `dependencies` section
55//!
56//! As in the example above you can add entry manually.
57//!
58//! ```toml
59//! [dependencies]
60//! # ...
61//! chksum-build = "0.0.3"
62//! ```
63//!
64//! Or by using subcommand.
65//!
66//! ```sh
67//! cargo add chksum-build
68//! ```
69//!
70//! # Usage
71//!
72//! ## `build_info` macro
73//!
74//! [`build_info`] macro creates [`BuildInfo`].
75//!
76//! ```rust,ignore
77//! use chksum_build::build_info;
78//! # use chksum_build::Result;
79//!
80//! # fn wrapper() -> Result<()> {
81//! let build_info = build_info!();
82//! # Ok(())
83//! # }
84//! ```
85//!
86//! ## `env` or `option_env` macros
87//!
88//! [`env`] or [`option_env`] macros.
89//!
90//! **Notice:** Type conversion need to be done manually.
91//!
92//! ```rust,ignore
93//! use std::str::FromStr;
94//! use chksum_build::cargo::Profile;
95//!
96//! // ...
97//!
98//! let profile = env!("CHKSUM_BUILD_INFO_CARGO_PROFILE");
99//! let profile = Profile::from_str(profile)?;
100//!
101//! // or
102//!
103//! let profile = option_env!("CHKSUM_BUILD_INFO_CARGO_PROFILE")
104//! .map(Profile::from_str)
105//! .transpose()?;
106//! ```
107//!
108//! ## `cfg` or `cfg_attr` options
109//!
110//! Some variables are available as configuration options for [`cfg`](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#the-cfg-attribute) or [`cfg_attr`](https://doc.rust-lang.org/stable/reference/conditional-compilation.html#the-cfg_attr-attribute).
111//!
112//! ### `Profile` variants
113//!
114//! Check [`Profile`] for more details.
115//!
116//! ```rust
117//! #[cfg(debug)]
118//! fn debug_function() {
119//! // ...
120//! }
121//!
122//! #[cfg_attr(release, inline)]
123//! fn inline_when_release_function() {
124//! // ...
125//! }
126//! ```
127//!
128//! ### `Channel` variants
129//!
130//! Check [`Channel`] for more details.
131//!
132//! ```rust
133//! #[cfg(stable)]
134//! fn stable_function() {
135//! // ...
136//! }
137//!
138//! #[cfg(nightly)]
139//! fn nightly_function() {
140//! // ...
141//! }
142//!
143//! #[cfg_attr(nightly, optimize(size))]
144//! fn optimize_when_nightly_function() {
145//! // ...
146//! }
147//! ```
148//!
149//! # Feature flags
150//!
151//! * `info`: Enables items required by library or application.
152//! * `script`: Enables items required by build script.
153//!
154//! By default both of them are enabled.
155//!
156//! # Alternatives
157//!
158//! * [build-data](https://crates.io/crates/build-data)
159//! * [build-info](https://crates.io/crates/build-info)
160//! * [built](https://crates.io/crates/built)
161//! * [shadow-rs](https://crates.io/crates/shadow-rs)
162//! * [vergen](https://crates.io/crates/vergen)
163//!
164//! # License
165//!
166//! MIT
167
168#![cfg_attr(docsrs, feature(doc_cfg))]
169#![cfg_attr(tarpaulin, feature(no_coverage))]
170#![forbid(unsafe_code)]
171
172#[cfg_attr(docsrs, doc(hidden))]
173mod cargo;
174#[cfg_attr(docsrs, doc(hidden))]
175pub mod error;
176#[cfg(feature = "info")]
177#[cfg_attr(docsrs, doc(cfg(feature = "info")))]
178#[cfg_attr(tarpaulin, no_coverage)]
179mod info;
180#[cfg_attr(docsrs, doc(hidden))]
181mod rust;
182#[cfg(feature = "script")]
183#[cfg_attr(docsrs, doc(cfg(feature = "script")))]
184mod script;
185
186pub use cargo::Profile;
187pub use error::{Error, Result};
188#[cfg(feature = "info")]
189pub use info::{Build, BuildInfo, Cargo, Rust};
190pub use rust::{Channel, ChannelVersion};
191#[cfg(feature = "script")]
192pub use script::{setup, BuildScript};