Add metadata to targets

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.
This commit is contained in:
Nilstrieb
2024-03-10 20:46:08 +01:00
parent af69f4c48c
commit 5bcb66cfb3
232 changed files with 1422 additions and 238 deletions

View File

@@ -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",
@@ -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)?;
@@ -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())?,
@@ -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 {
@@ -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);