Debian Codegraph @

debian/missing-sources/target-lexicon/build.rs

raw
1 //! build.rs file to obtain the host information.
2
3 // Allow dead code in triple.rs and targets.rs for our purposes here.
4 #![allow(dead_code)]
5
6 use std::env;
7 use std::fs::File;
8 use std::io::{self, Write};
9 use std::path::PathBuf;
10 use std::process::Command;
11 use std::str::FromStr;
12
13 #[cfg(not(feature = "std"))]
14 extern crate alloc;
15 #[cfg(feature = "std")]
16 extern crate std as alloc;
17
18 // Include triple.rs and targets.rs so we can parse the TARGET environment variable.
19 // targets.rs depends on data_model
20 mod data_model {
21 include!("src/data_model.rs");
22 }
23 mod triple {
24 include!("src/triple.rs");
25 }
26 mod targets {
27 include!("src/targets.rs");
28 }
29
30 // Stub out `ParseError` to minimally support triple.rs and targets.rs.
31 mod parse_error {
32 #[derive(Debug)]
33 pub enum ParseError {
34 UnrecognizedArchitecture(String),
35 UnrecognizedVendor(String),
36 UnrecognizedOperatingSystem(String),
37 UnrecognizedEnvironment(String),
38 UnrecognizedBinaryFormat(String),
39 UnrecognizedField(String),
40 }
41 }
42
43 use self::targets::Vendor;
44 use self::triple::Triple;
45
46 fn main() {
47 let out_dir = PathBuf::from(
48 env::var_os("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
49 );
50 let target = env::var("TARGET").expect("The TARGET environment variable must be set");
51 let triple =
52 Triple::from_str(&target).unwrap_or_else(|_| panic!("Invalid target name: '{}'", target));
53 let out = File::create(out_dir.join("host.rs")).expect("error creating host.rs");
54 write_host_rs(out, triple).expect("error writing host.rs");
55 if using_1_40() {
56 println!("cargo:rustc-cfg=feature=\"rust_1_40\"");
57 }
58 }
59
60 fn using_1_40() -> bool {
61 match (|| {
62 let rustc = env::var_os("RUSTC").unwrap();
63 let output = Command::new(rustc).arg("--version").output().ok()?;
64 let stdout = if output.status.success() {
65 output.stdout
66 } else {
67 return None;
68 };
69 std::str::from_utf8(&stdout)
70 .ok()?
71 .split(' ')
72 .nth(1)?
73 .split('.')
74 .nth(1)?
75 .parse::<i32>()
76 .ok()
77 })() {
78 Some(version) => version >= 40,
79 None => true, // assume we're using an up-to-date compiler
80 }
81 }
82
83 fn write_host_rs(mut out: File, triple: Triple) -> io::Result<()> {
84 // The generated Debug implementation for the inner architecture variants
85 // doesn't print the enum name qualifier, so import them here. There
86 // shouldn't be any conflicts because these enums all share a namespace
87 // in the triple string format.
88 writeln!(
89 out,
90 r#"
91 #[allow(unused_imports)]
92 use crate::Aarch64Architecture::*;
93 #[allow(unused_imports)]
94 use crate::ArmArchitecture::*;
95 #[allow(unused_imports)]
96 use crate::CustomVendor;
97 #[allow(unused_imports)]
98 use crate::Mips32Architecture::*;
99 #[allow(unused_imports)]
100 use crate::Mips64Architecture::*;
101 #[allow(unused_imports)]
102 use crate::Riscv32Architecture::*;
103 #[allow(unused_imports)]
104 use crate::Riscv64Architecture::*;
105 #[allow(unused_imports)]
106 use crate::X86_32Architecture::*;
107
108 /// The `Triple` of the current host.
109 pub const HOST: Triple = Triple {{
110 architecture: Architecture::{architecture:?},
111 vendor: Vendor::{vendor},
112 operating_system: OperatingSystem::{operating_system:?},
113 environment: Environment::{environment:?},
114 binary_format: BinaryFormat::{binary_format:?},
115 }};
116
117 impl Architecture {{
118 /// Return the architecture for the current host.
119 pub const fn host() -> Self {{
120 Architecture::{architecture:?}
121 }}
122 }}
123
124 impl Vendor {{
125 /// Return the vendor for the current host.
126 pub const fn host() -> Self {{
127 Vendor::{vendor}
128 }}
129 }}
130
131 impl OperatingSystem {{
132 /// Return the operating system for the current host.
133 pub const fn host() -> Self {{
134 OperatingSystem::{operating_system:?}
135 }}
136 }}
137
138 impl Environment {{
139 /// Return the environment for the current host.
140 pub const fn host() -> Self {{
141 Environment::{environment:?}
142 }}
143 }}
144
145 impl BinaryFormat {{
146 /// Return the binary format for the current host.
147 pub const fn host() -> Self {{
148 BinaryFormat::{binary_format:?}
149 }}
150 }}
151
152 impl Triple {{
153 /// Return the triple for the current host.
154 pub const fn host() -> Self {{
155 Self {{
156 architecture: Architecture::{architecture:?},
157 vendor: Vendor::{vendor},
158 operating_system: OperatingSystem::{operating_system:?},
159 environment: Environment::{environment:?},
160 binary_format: BinaryFormat::{binary_format:?},
161 }}
162 }}
163 }}"#,
164 architecture = triple.architecture,
165 vendor = vendor_display(&triple.vendor),
166 operating_system = triple.operating_system,
167 environment = triple.environment,
168 binary_format = triple.binary_format,
169 )?;
170
171 Ok(())
172 }
173
174 fn vendor_display(vendor: &Vendor) -> String {
175 match vendor {
176 Vendor::Custom(custom) => format!("Custom(CustomVendor::Static({:?}))", custom.as_str()),
177 known => format!("{:?}", known),
178 }
179 }