refactor build.rs to not depend on rustc-cfg

it now uses the CARGO_CFG_TARGET variables provided by Cargo
This commit is contained in:
Jorge Aparicio 2017-04-10 11:19:16 -05:00
parent cc0d8ba2d1
commit dbcec72ca1
2 changed files with 396 additions and 410 deletions

View File

@ -4,9 +4,6 @@ build = "build.rs"
name = "compiler_builtins" name = "compiler_builtins"
version = "0.1.0" version = "0.1.0"
[build-dependencies]
rustc-cfg = "0.3.0"
[build-dependencies.gcc] [build-dependencies.gcc]
optional = true optional = true
version = "0.3.36" version = "0.3.36"

View File

@ -1,23 +1,50 @@
use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let target = env::var("TARGET").unwrap();
// Emscripten's runtime includes all the builtins
if target.contains("emscripten") {
return;
}
// NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
// target triple. This is usually correct for our built-in targets but can break in presence of
// custom targets, which can have arbitrary names.
let llvm_target = target.split('-').collect::<Vec<_>>();
// Build missing intrinsics from compiler-rt C source code
#[cfg(feature = "c")] #[cfg(feature = "c")]
c::compile(&llvm_target);
// To compile intrinsics.rs for thumb targets, where there is no libc
if llvm_target[0].starts_with("thumb") {
println!("cargo:rustc-cfg=thumb")
}
// compiler-rt `cfg`s away some intrinsics for thumbv6m because that target doesn't have full
// THUMBv2 support. We have to cfg our code accordingly.
if llvm_target[0] == "thumbv6m" {
println!("cargo:rustc-cfg=thumbv6m")
}
}
#[cfg(feature = "c")]
mod c {
extern crate gcc; extern crate gcc;
extern crate rustc_cfg;
#[cfg(feature = "c")]
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::env;
use std::io::Write; use std::io::Write;
#[cfg(feature = "c")]
use std::path::Path; use std::path::Path;
use std::{env, io, process};
use rustc_cfg::Cfg;
#[cfg(feature = "c")]
struct Sources { struct Sources {
// SYMBOL -> PATH TO SOURCE // SYMBOL -> PATH TO SOURCE
map: BTreeMap<&'static str, &'static str>, map: BTreeMap<&'static str, &'static str>,
} }
#[cfg(feature = "c")]
impl Sources { impl Sources {
fn new() -> Sources { fn new() -> Sources {
Sources { map: BTreeMap::new() } Sources { map: BTreeMap::new() }
@ -51,31 +78,13 @@ impl Sources {
} }
} }
fn main() { /// Compile intrinsics from the compiler-rt C source code
println!("cargo:rerun-if-changed=build.rs"); pub fn compile(llvm_target: &[&str]){
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
let target = env::var("TARGET").unwrap();
// Emscripten's runtime includes all the builtins
if target.contains("emscripten") {
return;
}
let Cfg { ref target_arch, ref target_os, ref target_env, ref target_vendor, .. } =
Cfg::new(&target).unwrap_or_else(|e| {
writeln!(io::stderr(), "{}", e).ok();
process::exit(1)
});
// NOTE we are going to assume that llvm-target, what determines our codegen option, matches the
// target triple. This is usually correct for our built-in targets but can break in presence of
// custom targets, which can have arbitrary names.
let llvm_target = target.split('-').collect::<Vec<_>>();
// Build missing intrinsics from compiler-rt C source code
match () {
#[cfg(feature = "c")]
() => {
let target_vendor = target_vendor.as_ref().unwrap();
let cfg = &mut gcc::Config::new(); let cfg = &mut gcc::Config::new();
if target_env == "msvc" { if target_env == "msvc" {
@ -404,7 +413,7 @@ fn main() {
Path::new(".") Path::new(".")
}; };
let src_dir = root.join("compiler-rt/compiler-rt-cdylib/compiler-rt/lib/builtins"); let src_dir = root.join("c/compiler-rt/lib/builtins");
for src in sources.map.values() { for src in sources.map.values() {
let src = src_dir.join(src); let src = src_dir.join(src);
cfg.file(&src); cfg.file(&src);
@ -413,24 +422,4 @@ fn main() {
cfg.compile("libcompiler-rt.a"); cfg.compile("libcompiler-rt.a");
} }
#[cfg(not(feature = "c"))]
() => {}
}
// To filter away some flaky test (see src/float/add.rs for details)
if llvm_target[0].starts_with("arm") &&
llvm_target.last().unwrap().contains("gnueabi") {
println!("cargo:rustc-cfg=arm_linux")
}
// To compile intrinsics.rs for thumb targets, where there is no libc
if llvm_target[0].starts_with("thumb") {
println!("cargo:rustc-cfg=thumb")
}
// compiler-rt `cfg`s away some intrinsics for thumbv6m because that target doesn't have full
// THUMBv2 support. We have to cfg our code accordingly.
if llvm_target[0] == "thumbv6m" {
println!("cargo:rustc-cfg=thumbv6m")
}
} }