| 1 |
// Copyright (c) The camino Contributors |
| 2 |
// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 |
|
| 4 |
//! Adapted from |
| 5 |
//! https://github.com/dtolnay/syn/blob/a54fb0098c6679f1312113ae2eec0305c51c7390/build.rs. |
| 6 |
|
| 7 |
use std::{env, process::Command, str}; |
| 8 |
|
| 9 |
// The rustc-cfg strings below are *not* public API. Please let us know by |
| 10 |
// opening a GitHub issue if your build environment requires some way to enable |
| 11 |
// these cfgs other than by executing our build script. |
| 12 |
fn main() { |
| 13 |
let compiler = match rustc_version() { |
| 14 |
Some(compiler) => compiler, |
| 15 |
None => return, |
| 16 |
}; |
| 17 |
|
| 18 |
// NOTE: |
| 19 |
// Adding a new cfg gated by Rust version MUST be accompanied by an addition to the matrix in |
| 20 |
// .github/workflows/ci.yml. |
| 21 |
if compiler.minor >= 44 { |
| 22 |
println!("cargo:rustc-cfg=path_buf_capacity"); |
| 23 |
} |
| 24 |
if compiler.minor >= 56 { |
| 25 |
println!("cargo:rustc-cfg=shrink_to"); |
| 26 |
} |
| 27 |
// Stable and beta 1.63 have a stable try_reserve_2. |
| 28 |
if (compiler.minor >= 63 |
| 29 |
&& (compiler.channel == ReleaseChannel::Stable || compiler.channel == ReleaseChannel::Beta)) |
| 30 |
|| compiler.minor >= 64 |
| 31 |
{ |
| 32 |
println!("cargo:rustc-cfg=try_reserve_2"); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
struct Compiler { |
| 37 |
minor: u32, |
| 38 |
channel: ReleaseChannel, |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 42 |
enum ReleaseChannel { |
| 43 |
Stable, |
| 44 |
Beta, |
| 45 |
Nightly, |
| 46 |
} |
| 47 |
|
| 48 |
fn rustc_version() -> Option<Compiler> { |
| 49 |
let rustc = env::var_os("RUSTC")?; |
| 50 |
let output = Command::new(rustc).arg("--version").output().ok()?; |
| 51 |
let version = str::from_utf8(&output.stdout).ok()?; |
| 52 |
let mut pieces = version.split('.'); |
| 53 |
if pieces.next() != Some("rustc 1") { |
| 54 |
return None; |
| 55 |
} |
| 56 |
let minor = pieces.next()?.parse().ok()?; |
| 57 |
let channel = if version.contains("nightly") { |
| 58 |
ReleaseChannel::Nightly |
| 59 |
} else if version.contains("beta") { |
| 60 |
ReleaseChannel::Beta |
| 61 |
} else { |
| 62 |
ReleaseChannel::Stable |
| 63 |
}; |
| 64 |
Some(Compiler { minor, channel }) |
| 65 |
} |