Skip to content

Commit e91592c

Browse files
committed
Determine CFG_COMPILER_HOST_TRIPLE in build.rs
1 parent a0aa69c commit e91592c

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

src/librustc_session/build.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,146 @@
1+
use std::error::Error;
2+
use std::process::Command;
3+
4+
fn output(cmd: &mut Command) -> Result<String, Box<dyn Error>> {
5+
Ok(String::from_utf8(cmd.output()?.stdout)?.trim().into())
6+
}
7+
8+
fn default_build_triple() -> Result<String, Box<dyn Error>> {
9+
let ostype = output(Command::new("uname").arg("-s"));
10+
let cputype = output(Command::new("uname").arg("-m"));
11+
let (ostype, cputype) = match (ostype, cputype) {
12+
(Ok(ostype), Ok(cputype)) => (ostype, cputype),
13+
(ostype, cputype) => {
14+
if cfg!(windows) {
15+
return Ok("x86_64-pc-windows-msvc".into());
16+
} else {
17+
return ostype.and(cputype);
18+
}
19+
}
20+
};
21+
22+
let mut cputype = cputype;
23+
let ostype = match ostype.as_str() {
24+
"Darwin" => "apple-darwin",
25+
"DragonFly" => "unknown-dragonfly",
26+
"FreeBSD" => "unknown-freebsd",
27+
"Haiku" => "unknown-haiku",
28+
"NetBSD" => "unknown-netbsd",
29+
"OpenBSD" => "unknown-openbsd",
30+
"Linux" => match output(Command::new("uname").arg("-o"))?.as_str() {
31+
"Android" => "linux-android",
32+
_ => "unknown-linux-gnu",
33+
},
34+
"SunOS" => {
35+
cputype = output(Command::new("isainfo").arg("-k"))?;
36+
"sun-solaris"
37+
}
38+
ostype if ostype.starts_with("MINGW") => {
39+
cputype = if std::env::var("MSYSTEM").map(|x| x == "MINGW64").unwrap_or_default() {
40+
"x86_64"
41+
} else {
42+
"i686"
43+
}
44+
.into();
45+
"pc-windows-gnu"
46+
}
47+
ostype if ostype.starts_with("MSYS") => "pc-windows-gnu",
48+
ostype if ostype.starts_with("CYGWIN_NT") => {
49+
cputype = if ostype.ends_with("WOW64") { "x86_64" } else { "i686" }.into();
50+
"pc-windows-gnu"
51+
}
52+
_ => {
53+
return Err(format!("unknown OS type: {}", ostype).into());
54+
}
55+
};
56+
57+
if cputype == "powerpc" && ostype == "unknown-freebsd" {
58+
cputype = output(Command::new("uname").arg("-p"))?;
59+
}
60+
61+
let mut ostype: String = ostype.into();
62+
let mut set_cputype_from_uname_p = false;
63+
match cputype.as_str() {
64+
"BePC" => "i686",
65+
"aarch64" => "aarch64",
66+
"amd64" => "x86_64",
67+
"arm64" => "aarch64",
68+
"i386" => "i686",
69+
"i486" => "i686",
70+
"i686" => "i686",
71+
"i786" => "i686",
72+
"powerpc" => "powerpc",
73+
"powerpc64" => "powerpc64",
74+
"powerpc64le" => "powerpc64le",
75+
"ppc" => "powerpc",
76+
"ppc64" => "powerpc64",
77+
"ppc64le" => "powerpc64le",
78+
"s390x" => "s390x",
79+
"x64" => "x86_64",
80+
"x86" => "i686",
81+
"x86-64" => "x86_64",
82+
"x86_64" => "x86_64",
83+
"xscale" | "arm" => match ostype.as_str() {
84+
"linux-android" => {
85+
ostype = "linux-androideabi".into();
86+
"arm"
87+
}
88+
"unknown-freebsd" => {
89+
ostype = "unknown-freebsd".into();
90+
set_cputype_from_uname_p = true;
91+
""
92+
}
93+
_ => cputype.as_str(),
94+
},
95+
"armv6l" => {
96+
match ostype.as_str() {
97+
"linux-android" => {
98+
ostype = "linux-androideabi".into();
99+
}
100+
_ => {
101+
ostype += "eabihf";
102+
}
103+
};
104+
"arm"
105+
}
106+
"armv7l" | "armv8l" => {
107+
match ostype.as_str() {
108+
"linux-android" => {
109+
ostype = "linux-androideabi".into();
110+
}
111+
_ => {
112+
ostype += "eabihf";
113+
}
114+
};
115+
"armv7"
116+
}
117+
"mips" => {
118+
if cfg!(target_endian = "big") {
119+
"mips"
120+
} else {
121+
"mipsel"
122+
}
123+
}
124+
"mips64" => {
125+
ostype += "abi64";
126+
if cfg!(target_endian = "big") { "mips64" } else { "mips64el" }
127+
}
128+
"sparc" => "sparc",
129+
"sparcv9" => "sparcv9",
130+
"sparc64" => "sparc64",
131+
_ => {
132+
return Err(format!("unknown cpu type: {}", cputype).into());
133+
}
134+
};
135+
let cputype = if set_cputype_from_uname_p {
136+
output(Command::new("uname").arg("-p"))?
137+
} else {
138+
cputype.into()
139+
};
140+
141+
Ok(format!("{}-{}", cputype, ostype))
142+
}
143+
1144
fn main() {
2145
println!("cargo:rerun-if-changed=build.rs");
3146
println!("cargo:rerun-if-env-changed=CFG_RELEASE");
@@ -9,4 +152,11 @@ fn main() {
9152
println!("cargo:rerun-if-env-changed=CFG_VIRTUAL_RUST_SOURCE_BASE_DIR");
10153
println!("cargo:rerun-if-env-changed=CFG_COMPILER_HOST_TRIPLE");
11154
println!("cargo:rerun-if-env-changed=CFG_LIBDIR_RELATIVE");
155+
156+
if std::env::var_os("CFG_COMPILER_HOST_TRIPLE").is_none() {
157+
println!(
158+
"cargo:rustc-env=CFG_COMPILER_HOST_TRIPLE={}",
159+
default_build_triple().expect("Unable to determine build triple not found")
160+
)
161+
};
12162
}

0 commit comments

Comments
 (0)