Skip to content

Commit bd9338f

Browse files
authored
Merge pull request #57 from dtolnay/buildrs
Add build.rs example
2 parents a1f0c09 + 54a2749 commit bd9338f

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ fn duration_as_days(dur: Duration) -> u64 {
126126
}
127127
```
128128

129+
Emitting Cargo cfg directives from a build script. Note that this requires
130+
listing `rustversion` under `[build-dependencies]` in Cargo.toml, not
131+
`[dependencies]`.
132+
133+
```rust
134+
// build.rs
135+
136+
fn main() {
137+
if rustversion::cfg!(since(1.36)) {
138+
println!("cargo:rustc-cfg=no_std");
139+
}
140+
}
141+
```
142+
143+
```rust
144+
// src/lib.rs
145+
146+
#![cfg_attr(no_std, no_std)]
147+
148+
#[cfg(no_std)]
149+
extern crate alloc;
150+
```
151+
129152
<br>
130153

131154
#### License

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,29 @@
150150
//! }
151151
//! ```
152152
//!
153+
//! Emitting Cargo cfg directives from a build script. Note that this requires
154+
//! listing `rustversion` under `[build-dependencies]` in Cargo.toml, not
155+
//! `[dependencies]`.
156+
//!
157+
//! ```
158+
//! // build.rs
159+
//!
160+
//! fn main() {
161+
//! if rustversion::cfg!(since(1.36)) {
162+
//! println!("cargo:rustc-cfg=no_std");
163+
//! }
164+
//! }
165+
//! ```
166+
//!
167+
//! ```
168+
//! // src/lib.rs
169+
//!
170+
//! #![cfg_attr(no_std, no_std)]
171+
//!
172+
//! #[cfg(no_std)]
173+
//! extern crate alloc;
174+
//! ```
175+
//!
153176
//! <br>
154177
155178
#![doc(html_root_url = "https://docs.rs/rustversion/1.0.18")]

0 commit comments

Comments
 (0)