Skip to content

Commit

Permalink
rustc_target: Further simplify loading of built-in targets
Browse files Browse the repository at this point in the history
using the fact that it is infallible.

JSON roundtrip check on every rustc run is also removed, it's already performed by unit tests.
  • Loading branch information
petrochenkov committed Oct 5, 2020
1 parent 021fcbd commit 1444ad7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,8 @@ impl RustcDefaultCalls {
for req in &sess.opts.prints {
match *req {
TargetList => {
let mut targets = rustc_target::spec::get_targets().collect::<Vec<String>>();
let mut targets =
rustc_target::spec::TARGETS.iter().copied().collect::<Vec<_>>();
targets.sort();
println!("{}", targets.join("\n"));
}
Expand Down
51 changes: 13 additions & 38 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,46 +430,23 @@ impl fmt::Display for LinkOutputKind {
}
}

pub enum LoadTargetError {
BuiltinTargetNotFound(String),
Other(String),
}

pub type LinkArgs = BTreeMap<LinkerFlavor, Vec<String>>;

macro_rules! supported_targets {
( $(($( $triple:literal, )+ $module:ident ),)+ ) => {
$(mod $module;)+

/// List of supported targets
const TARGETS: &[&str] = &[$($($triple),+),+];

fn load_specific(target: &str) -> Result<Target, LoadTargetError> {
match target {
$(
$($triple)|+ => {
let mut t = $module::target();
t.options.is_builtin = true;

// round-trip through the JSON parser to ensure at
// run-time that the parser works correctly
t = Target::from_json(t.to_json())
.map_err(LoadTargetError::Other)?;
debug!("got builtin target: {:?}", t);
Ok(t)
},
)+
_ => Err(LoadTargetError::BuiltinTargetNotFound(
format!("Unable to find target: {}", target)))
}
}

pub fn get_targets() -> impl Iterator<Item = String> {
TARGETS.iter().filter_map(|t| -> Option<String> {
load_specific(t)
.and(Ok(t.to_string()))
.ok()
})
pub const TARGETS: &[&str] = &[$($($triple),+),+];

fn load_builtin(target: &str) -> Option<Target> {
let mut t = match target {
$( $($triple)|+ => $module::target(), )+
_ => return None,
};
t.options.is_builtin = true;
debug!("got builtin target: {:?}", t);
Some(t)
}

#[cfg(test)]
Expand Down Expand Up @@ -1529,11 +1506,9 @@ impl Target {

match *target_triple {
TargetTriple::TargetTriple(ref target_triple) => {
// check if triple is in list of supported targets
match load_specific(target_triple) {
Ok(t) => return Ok(t),
Err(LoadTargetError::BuiltinTargetNotFound(_)) => (),
Err(LoadTargetError::Other(e)) => return Err(e),
// check if triple is in list of built-in targets
if let Some(t) = load_builtin(target_triple) {
return Ok(t);
}

// search for a file named `target_triple`.json in RUST_TARGET_PATH
Expand Down

0 comments on commit 1444ad7

Please sign in to comment.