Skip to content

Commit

Permalink
Add metadata to targets
Browse files Browse the repository at this point in the history
This adds four pieces of metadata to every target:
- description
- tier
- host tools
- std

This information is currently scattered across target docs and both
- not machine readable, making validation harder
- sometimes subtly encoding by the table it's in, causing mistakes and
  making it harder to review changes to the properties

By putting it in the compiler, we improve this. Later, we will use this
canonical information to generate target documentation from it.
  • Loading branch information
Noratrieb committed Mar 10, 2024
1 parent af69f4c commit 5bcb66c
Show file tree
Hide file tree
Showing 232 changed files with 1,422 additions and 238 deletions.
15 changes: 14 additions & 1 deletion compiler/rustc_target/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::borrow::Cow;
use std::collections::BTreeMap;

pub use serde_json::Value as Json;
use serde_json::{Map, Number};
use serde_json::{json, Map, Number};

use crate::spec::TargetMetadata;

pub trait ToJson {
fn to_json(&self) -> Json;
Expand Down Expand Up @@ -120,3 +122,14 @@ impl ToJson for crate::abi::call::Conv {
Json::String(s.to_owned())
}
}

impl ToJson for TargetMetadata {
fn to_json(&self) -> Json {
json!({
"description": self.description,
"tier": self.tier,
"host_tools": self.host_tools,
"std": self.std,
})
}
}
7 changes: 6 additions & 1 deletion compiler/rustc_target/src/spec/base/avr_gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ use object::elf;
pub fn target(target_cpu: &'static str, mmcu: &'static str) -> Target {
Target {
arch: "avr".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
data_layout: "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8".into(),
llvm_target: "avr-unknown-unknown".into(),
pointer_width: 16,
Expand Down
45 changes: 38 additions & 7 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1743,11 +1743,9 @@ impl TargetWarnings {
pub struct Target {
/// Target triple to pass to LLVM.
pub llvm_target: StaticCow<str>,
/// A short description of the target including platform requirements,
/// for example "64-bit Linux (kernel 3.2+, glibc 2.17+)".
/// Optional for now, intended to be required in the future.
/// Part of #120745.
pub description: Option<StaticCow<str>>,
/// Metadata about a target, for example the description or tier.
/// Used for generating target documentation.
pub metadata: TargetMetadata,
/// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable.
pub pointer_width: u32,
/// Architecture to use for ABI considerations. Valid options include: "x86",
Expand All @@ -1759,6 +1757,23 @@ pub struct Target {
pub options: TargetOptions,
}

/// Metadata about a target like the description or tier.
/// Part of #120745.
/// All fields are optional for now, but intended to be required in the future.
#[derive(Default, PartialEq, Clone, Debug)]
pub struct TargetMetadata {
/// A short description of the target including platform requirements,
/// for example "64-bit Linux (kernel 3.2+, glibc 2.17+)".
pub description: Option<StaticCow<str>>,
/// The tier of the target. 1, 2 or 3.
pub tier: Option<u64>,
/// Whether the Rust project ships host tools for a target.
pub host_tools: Option<bool>,
/// Whether a target has the `std` library. This is usually true for targets running
/// on an operating system.
pub std: Option<bool>,
}

impl Target {
pub fn parse_data_layout(&self) -> Result<TargetDataLayout, TargetDataLayoutErrors<'_>> {
let mut dl = TargetDataLayout::parse_from_llvm_datalayout_string(&self.data_layout)?;
Expand Down Expand Up @@ -2549,7 +2564,7 @@ impl Target {

let mut base = Target {
llvm_target: get_req_field("llvm-target")?.into(),
description: get_req_field("description").ok().map(Into::into),
metadata: Default::default(),
pointer_width: get_req_field("target-pointer-width")?
.parse::<u32>()
.map_err(|_| "target-pointer-width must be an integer".to_string())?,
Expand All @@ -2558,6 +2573,22 @@ impl Target {
options: Default::default(),
};

// FIXME: This doesn't properly validate anything and just ignores the data if it's invalid.
// That's okay for now, the only use of this is when generating docs, which we don't do for
// custom targets.
if let Some(Json::Object(mut metadata)) = obj.remove("metadata") {
base.metadata.description = metadata
.remove("description")
.and_then(|desc| desc.as_str().map(|desc| desc.to_owned().into()));
base.metadata.tier = metadata
.remove("tier")
.and_then(|tier| tier.as_u64())
.filter(|tier| (1..=3).contains(tier));
base.metadata.host_tools =
metadata.remove("host_tools").and_then(|host| host.as_bool());
base.metadata.std = metadata.remove("std").and_then(|host| host.as_bool());
}

let mut incorrect_type = vec![];

macro_rules! key {
Expand Down Expand Up @@ -3253,7 +3284,7 @@ impl ToJson for Target {
}

target_val!(llvm_target);
target_val!(description);
target_val!(metadata);
d.insert("target-pointer-width".to_string(), self.pointer_width.to_string().to_json());
target_val!(arch);
target_val!(data_layout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ pub fn target() -> Target {
// MACOSX_DEPLOYMENT_TARGET. To enable cross-language LTO to work
// correctly, we do too.
llvm_target: macos_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_target/src/spec/targets/aarch64_apple_ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ pub fn target() -> Target {
// This is required for the target to pick the right
// MACH-O commands, so we do too.
llvm_target: ios_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ pub fn target() -> Target {

Target {
llvm_target: mac_catalyst_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ pub fn target() -> Target {
// This is required for the simulator target to pick the right
// MACH-O commands, so we do too.
llvm_target: ios_sim_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_target/src/spec/targets/aarch64_apple_tvos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ pub fn target() -> Target {
let arch = Arch::Arm64;
Target {
llvm_target: tvos_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ pub fn target() -> Target {
let arch = Arch::Arm64_sim;
Target {
llvm_target: tvos_sim_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ pub fn target() -> Target {
let base = opts("watchos", Arch::Arm64);
Target {
llvm_target: "aarch64-apple-watchos".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ pub fn target() -> Target {
// This is required for the simulator target to pick the right
// MACH-O commands, so we do too.
llvm_target: watchos_sim_llvm_target(arch).into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:o-i64:64-i128:128-n32:64-S128".into(),
arch: arch.target_arch(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use crate::spec::{base, StackProbeType, Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "aarch64_be-unknown-linux-gnu".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64_be-unknown-linux-gnu_ilp32".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 32,
data_layout: "E-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use crate::spec::{base, StackProbeType, Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "aarch64_be-unknown-netbsd".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ pub fn target() -> Target {
let base = base::solid::opts("asp3");
Target {
llvm_target: "aarch64-unknown-none".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use crate::spec::{base, SanitizerSet, StackProbeType, Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "aarch64-linux-android".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const LINKER_SCRIPT: &str = include_str!("./aarch64_nintendo_switch_freestanding
pub fn target() -> Target {
Target {
llvm_target: "aarch64-unknown-none".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-pc-windows-gnu".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ pub fn target() -> Target {

Target {
llvm_target: "aarch64-pc-windows-msvc".into(),
description: Some("ARM64 Windows MSVC".into()),
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use crate::spec::{base, SanitizerSet, StackProbeType, Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "aarch64-unknown-freebsd".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use crate::spec::{base, SanitizerSet, StackProbeType, Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "aarch64-unknown-fuchsia".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use crate::spec::{base, StackProbeType, Target, TargetOptions};
pub fn target() -> Target {
Target {
llvm_target: "aarch64-unknown-hermit".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
arch: "aarch64".into(),
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ pub fn target() -> Target {
// LLVM does not currently have a separate illumos target,
// so we still pass Solaris to it
llvm_target: "aarch64-unknown-solaris2.11".into(),
description: None,
metadata: crate::spec::TargetMetadata {
description: None,
tier: None,
host_tools: None,
std: None,
},
pointer_width: 64,
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
arch: "aarch64".into(),
Expand Down
Loading

0 comments on commit 5bcb66c

Please sign in to comment.