Skip to content

Commit

Permalink
Fixes #30: Remove JSON file descriptor parsing.
Browse files Browse the repository at this point in the history
This didn't seem to be really used, and didn't match any rustc feature.
  • Loading branch information
bnjbvr committed Nov 4, 2019
1 parent fb10c91 commit c97c282
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 97 deletions.
7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
91 changes: 1 addition & 90 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
Expand Down

0 comments on commit c97c282

Please sign in to comment.