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.
main()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 // NOTE: the below checks use == rather than `matches!`. This is because `matches!` isn't stable
28 // on Rust 1.34.
29 // try_reserve_2 was added in a 1.63 nightly.
30 if (compiler.minor >= 63
31 && (compiler.channel == ReleaseChannel::Stable || compiler.channel == ReleaseChannel::Beta))
32 || compiler.minor >= 64
33 {
34 println!("cargo:rustc-cfg=try_reserve_2");
35 }
36 // path_buf_deref_mut was added in a 1.68 nightly.
37 if (compiler.minor >= 68
38 && (compiler.channel == ReleaseChannel::Stable || compiler.channel == ReleaseChannel::Beta))
39 || compiler.minor >= 69
40 {
41 println!("cargo:rustc-cfg=path_buf_deref_mut");
42 }
43 }
44
45 struct Compiler {
46 minor: u32,
47 channel: ReleaseChannel,
48 }
49
50 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
51 enum ReleaseChannel {
52 Stable,
53 Beta,
54 Nightly,
55 }
56
rustc_version() -> Option<Compiler>57 fn rustc_version() -> Option<Compiler> {
58 let rustc = env::var_os("RUSTC")?;
59 let output = Command::new(rustc).arg("--version").output().ok()?;
60 let version = str::from_utf8(&output.stdout).ok()?;
61 let mut pieces = version.split('.');
62 if pieces.next() != Some("rustc 1") {
63 return None;
64 }
65 let minor = pieces.next()?.parse().ok()?;
66 let channel = if version.contains("nightly") {
67 ReleaseChannel::Nightly
68 } else if version.contains("beta") {
69 ReleaseChannel::Beta
70 } else {
71 ReleaseChannel::Stable
72 };
73 Some(Compiler { minor, channel })
74 }
75