From c97c282ef1c0c68f649f815cdb19c4ff7f79e8d8 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 31 Oct 2019 15:27:37 +0100 Subject: [PATCH] Fixes #30: Remove JSON file descriptor parsing. This didn't seem to be really used, and didn't match any rustc feature. --- Cargo.toml | 7 ----- build.rs | 91 +----------------------------------------------------- 2 files changed, 1 insertion(+), 97 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 903cdd8..6bb51ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,13 +15,6 @@ edition = "2018" failure = { version = "0.1.3", default-features = false, features = ["derive"] } failure_derive = { version = "0.1.3", default-features = false } -[build-dependencies] -serde_json = { version = "1.0", optional = true } - -[features] -default = ["enable-serde"] -enable-serde = ["serde_json"] - [badges] maintenance = { status = "passively-maintained" } travis-ci = { repository = "CraneStation/target-lexicon" } diff --git a/build.rs b/build.rs index cb2f28c..6e94ed3 100644 --- a/build.rs +++ b/build.rs @@ -11,9 +11,6 @@ use std::str::FromStr; extern crate alloc; -#[cfg(feature = "enable-serde")] -extern crate serde_json; - // Include triple.rs and targets.rs so we can parse the TARGET environment variable. mod triple { include!("src/triple.rs"); @@ -38,97 +35,11 @@ mod parse_error { use self::triple::Triple; -#[cfg(feature = "enable-serde")] -fn triple_from_target(target: &str) -> Triple { - use self::triple::{Endianness, PointerWidth}; - use serde_json::Value; - use std::ffi::OsString; - use std::fs; - use std::path::Path; - - /// Assuming `target` is a path to a custom target json config file, open it - /// and build a `Triple` using its contents. - fn read_target_from_file(path: &Path) -> Triple { - let json = fs::read_to_string(path).expect("error reading target file"); - - let v: Value = serde_json::from_str(&json).expect("error parsing target file as json"); - let target = v["llvm-target"] - .as_str() - .expect("error parsing \"llvm-target\" as a string"); - let triple = Triple::from_str(target).expect("error parsing host target"); - - // Check that the JSON describes a known target configuration. - // - // Unfortunately, none of Rust's "arch", "os", "env", nor "vendor" - // fields directly correspond to triple fields, so we can't easily - // check them. - if let Some(endian) = v["target-endian"].as_str() { - assert_eq!( - endian, - match triple.endianness().unwrap() { - Endianness::Little => "little", - Endianness::Big => "big", - }, - "\"target-endian\" field disagrees with the target triple" - ); - } - if let Some(pointer_width) = v["target-pointer-width"].as_str() { - assert_eq!( - pointer_width, - match triple.pointer_width().unwrap() { - PointerWidth::U16 => "16", - PointerWidth::U32 => "32", - PointerWidth::U64 => "64", - }, - "\"target-pointer-width\" field disagrees with the target triple" - ); - } - - triple - } - - /// Assuming `target` is a target identifier, search for an appropriate custom - /// target json config file in the way that rustc does, and then call - /// `read_target_from_file` on that. - fn read_target_from_file_in_path(target: &str) -> Triple { - let mut target_filename = target.to_owned(); - target_filename.push_str(".json"); - let target_basename = PathBuf::from(target_filename); - let target_path = env::var_os("RUST_TARGET_PATH").unwrap_or_else(|| OsString::new()); - for dir in env::split_paths(&target_path) { - let p = dir.join(&target_basename); - if p.is_file() { - return read_target_from_file(&p); - } - } - panic!("can't find custom target {}", target); - } - - // The following intends to match the logic in rustc. - if target.ends_with(".json") { - read_target_from_file(Path::new(&target)) - } else { - Triple::from_str(&target).unwrap_or_else(|_| read_target_from_file_in_path(&target)) - } -} - -#[cfg(not(feature = "enable-serde"))] -fn triple_from_target(target: &str) -> Triple { - Triple::from_str(&target).expect(&format!( - "Invalid target name: '{}'. Use the - enable-serde feature to fallback on JSON Rust targets descriptors.", - target - )) -} - fn main() { let out_dir = PathBuf::from(env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set")); - let target = env::var("TARGET").expect("The TARGET environment variable must be set"); - - let triple = triple_from_target(&target); - + let triple = Triple::from_str(&target).expect(&format!("Invalid target name: '{}'", target)); let out = File::create(out_dir.join("host.rs")).expect("error creating host.rs"); write_host_rs(out, triple).expect("error writing host.rs"); }