Skip to content

Commit 480d743

Browse files
author
Jorge Aparicio
committed
compiler_builtins: base conditional compilation on the specification
of the target rather than on its triple (i.e. its name). As explained in rust-lang#35474, the current conditional compilation logic, which is based on the target triple (e.g. x86_64-unknown-linux-gnu), is not robust in the presence of custom targets as those can have arbitrary triples (e.g. cortex-m3). To fix that, this commit changes the conditional compilation logic to use the specification of the target (e.g. target_arch, target_os, etc.). For that, compiler_builtins build script now depends on the rustc-cfg crate, whose role is to shell out to `rustc --print cfg` and return its output parsed as a `struct`. With the goal of completely removing any direct dependency of the conditional compilation logic on the target triple, this commit also exposes the llvm-target field of the target specification via `rustc --print cfg`. closes rust-lang#35474
1 parent dc75933 commit 480d743

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/libcompiler_builtins/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ core = { path = "../libcore" }
1313

1414
[build-dependencies]
1515
gcc = "0.3.27"
16+
rustc-cfg = { git = "https://github.com/japaric/rustc-cfg", branch = "llvm-target" }

src/libcompiler_builtins/build.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434
//! far far less than working with compiler-rt's build system over time.
3535
3636
extern crate gcc;
37+
extern crate rustc_cfg;
3738

3839
use std::collections::BTreeMap;
3940
use std::env;
4041
use std::path::Path;
4142

43+
use rustc_cfg::Cfg;
44+
4245
struct Sources {
4346
// SYMBOL -> PATH TO SOURCE
4447
map: BTreeMap<&'static str, &'static str>,
@@ -73,9 +76,23 @@ impl Sources {
7376

7477
fn main() {
7578
let target = env::var("TARGET").unwrap();
79+
let Cfg {
80+
ref llvm_target,
81+
ref target_arch,
82+
ref target_os,
83+
ref target_env,
84+
ref target_vendor,
85+
..
86+
} = Cfg::new(&target).unwrap();
87+
// TODO(stage0) use `unwrap` instead of `unwrap_or`
88+
// NOTE in the latest stable/beta release, `rustc --print cfg` doesn't include `llvm_target` in
89+
// its output. In those cases simply fallback to the target triple, which is usually similar to
90+
// llvm-target, as a workaround.
91+
let llvm_target = llvm_target.as_ref().unwrap_or(&target).split('-').collect::<Vec<_>>();
92+
let target_vendor = target_vendor.as_ref().unwrap();
7693
let cfg = &mut gcc::Config::new();
7794

78-
if target.contains("msvc") {
95+
if target_env == "msvc" {
7996
// Don't pull in extra libraries on MSVC
8097
cfg.flag("/Zl");
8198

@@ -183,7 +200,7 @@ fn main() {
183200
"umoddi3.c",
184201
"umodsi3.c"]);
185202

186-
if !target.contains("ios") {
203+
if target_os != "ios" {
187204
sources.extend(&["absvti2.c",
188205
"addtf3.c",
189206
"addvti3.c",
@@ -226,7 +243,7 @@ fn main() {
226243
"umodti3.c"]);
227244
}
228245

229-
if target.contains("apple") {
246+
if target_vendor == "apple" {
230247
sources.extend(&["atomic_flag_clear.c",
231248
"atomic_flag_clear_explicit.c",
232249
"atomic_flag_test_and_set.c",
@@ -235,20 +252,20 @@ fn main() {
235252
"atomic_thread_fence.c"]);
236253
}
237254

238-
if !target.contains("windows") {
255+
if target_os != "windows" {
239256
sources.extend(&["emutls.c"]);
240257
}
241258

242-
if target.contains("msvc") {
243-
if target.contains("x86_64") {
259+
if target_env == "msvc" {
260+
if llvm_target[0] == "x86_64" {
244261
sources.extend(&["x86_64/floatdidf.c", "x86_64/floatdisf.c", "x86_64/floatdixf.c"]);
245262
}
246263
} else {
247-
if !target.contains("freebsd") {
264+
if target_os != "freebsd" {
248265
sources.extend(&["gcc_personality_v0.c"]);
249266
}
250267

251-
if target.contains("x86_64") {
268+
if target_arch == "x86_64" {
252269
sources.extend(&["x86_64/chkstk.S",
253270
"x86_64/chkstk2.S",
254271
"x86_64/floatdidf.c",
@@ -259,7 +276,7 @@ fn main() {
259276
"x86_64/floatundixf.S"]);
260277
}
261278

262-
if target.contains("i386") || target.contains("i586") || target.contains("i686") {
279+
if llvm_target[0] == "i386" || llvm_target[0] == "i586" || llvm_target[0] == "i686" {
263280
sources.extend(&["i386/ashldi3.S",
264281
"i386/ashrdi3.S",
265282
"i386/chkstk.S",
@@ -279,7 +296,7 @@ fn main() {
279296
}
280297
}
281298

282-
if target.contains("arm") && !target.contains("ios") {
299+
if target_arch == "arm" && target_os != "ios" {
283300
sources.extend(&["arm/aeabi_cdcmp.S",
284301
"arm/aeabi_cdcmpeq_check_nan.c",
285302
"arm/aeabi_cfcmp.S",
@@ -315,7 +332,7 @@ fn main() {
315332
"arm/umodsi3.S"]);
316333
}
317334

318-
if target.contains("armv7") {
335+
if llvm_target[0] == "armv7" {
319336
sources.extend(&["arm/sync_fetch_and_add_4.S",
320337
"arm/sync_fetch_and_add_8.S",
321338
"arm/sync_fetch_and_and_4.S",
@@ -338,7 +355,7 @@ fn main() {
338355
"arm/sync_fetch_and_xor_8.S"]);
339356
}
340357

341-
if target.contains("eabihf") {
358+
if llvm_target.last().unwrap().ends_with("eabihf") {
342359
sources.extend(&["arm/adddf3vfp.S",
343360
"arm/addsf3vfp.S",
344361
"arm/divdf3vfp.S",
@@ -377,7 +394,7 @@ fn main() {
377394
"arm/unordsf2vfp.S"]);
378395
}
379396

380-
if target.contains("aarch64") {
397+
if target_arch == "aarch64" {
381398
sources.extend(&["comparetf2.c",
382399
"extenddftf2.c",
383400
"extendsftf2.c",

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
930930
let os = &sess.target.target.target_os;
931931
let env = &sess.target.target.target_env;
932932
let vendor = &sess.target.target.target_vendor;
933+
let llvm_target = &sess.target.target.llvm_target;
933934
let max_atomic_width = sess.target.target.options.max_atomic_width;
934935

935936
let fam = if let Some(ref fam) = sess.target.target.options.target_family {
@@ -949,6 +950,7 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
949950
mk(InternedString::new("target_pointer_width"), intern(wordsz)),
950951
mk(InternedString::new("target_env"), intern(env)),
951952
mk(InternedString::new("target_vendor"), intern(vendor)),
953+
mk(InternedString::new("llvm_target"), intern(llvm_target)),
952954
];
953955
match &fam[..] {
954956
"windows" | "unix" => ret.push(attr::mk_word_item(fam)),

src/rustc/std_shim/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)