Skip to content

Add --print target-spec-json-schema #144498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,12 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8975ffdaa0ef3661bfe02dbdcc06c9f829dfafe6a3c474de366a8d5e44276921"

[[package]]
name = "dyn-clone"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c7a8fb8a9fbf66c1f703fe16184d10ca0ee9d23be5b4436400408ba54a95005"

[[package]]
name = "either"
version = "1.15.0"
Expand Down Expand Up @@ -3077,6 +3083,26 @@ dependencies = [
"thiserror 2.0.12",
]

[[package]]
name = "ref-cast"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a0ae411dbe946a674d89546582cea4ba2bb8defac896622d6496f14c23ba5cf"
dependencies = [
"ref-cast-impl",
]

[[package]]
name = "ref-cast-impl"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1165225c21bff1f3bbce98f5a1f889949bc902d3575308cc7b0de30b4f6d27c7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.104",
]

[[package]]
name = "regex"
version = "1.11.1"
Expand Down Expand Up @@ -4563,6 +4589,7 @@ dependencies = [
"rustc_macros",
"rustc_serialize",
"rustc_span",
"schemars",
"serde",
"serde_derive",
"serde_json",
Expand Down Expand Up @@ -4878,6 +4905,31 @@ dependencies = [
"windows-sys 0.59.0",
]

[[package]]
name = "schemars"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0"
dependencies = [
"dyn-clone",
"ref-cast",
"schemars_derive",
"serde",
"serde_json",
]

[[package]]
name = "schemars_derive"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d020396d1d138dc19f1165df7545479dcd58d93810dc5d646a16e55abefa80"
dependencies = [
"proc-macro2",
"quote",
"serde_derive_internals",
"syn 2.0.104",
]

[[package]]
name = "scoped-tls"
version = "1.0.1"
Expand Down Expand Up @@ -4946,6 +4998,17 @@ dependencies = [
"syn 2.0.104",
]

[[package]]
name = "serde_derive_internals"
version = "0.29.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.104",
]

[[package]]
name = "serde_json"
version = "1.0.141"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Command {
}
Program::Lld(ref p, flavor) => {
let mut c = process::Command::new(p);
c.arg("-flavor").arg(flavor.as_str());
c.arg("-flavor").arg(flavor.desc());
c
}
};
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,10 @@ fn print_crate_info(
TargetSpecJson => {
println_info!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap());
}
TargetSpecJsonSchema => {
let schema = rustc_target::spec::json_schema();
println_info!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
AllTargetSpecsJson => {
let mut targets = BTreeMap::new();
for name in rustc_target::spec::TARGETS {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub const PRINT_KINDS: &[(&str, PrintKind)] = &[
("target-libdir", PrintKind::TargetLibdir),
("target-list", PrintKind::TargetList),
("target-spec-json", PrintKind::TargetSpecJson),
("target-spec-json-schema", PrintKind::TargetSpecJsonSchema),
("tls-models", PrintKind::TlsModels),
// tidy-alphabetical-end
];
Expand Down Expand Up @@ -1052,6 +1053,7 @@ pub enum PrintKind {
TargetLibdir,
TargetList,
TargetSpecJson,
TargetSpecJsonSchema,
TlsModels,
// tidy-alphabetical-end
}
Expand Down Expand Up @@ -2314,7 +2316,8 @@ fn is_print_request_stable(print_kind: PrintKind) -> bool {
| PrintKind::CheckCfg
| PrintKind::CrateRootLintLevels
| PrintKind::SupportedCrateTypes
| PrintKind::TargetSpecJson => false,
| PrintKind::TargetSpecJson
| PrintKind::TargetSpecJsonSchema => false,
_ => true,
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rustc_fs_util = { path = "../rustc_fs_util" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
schemars = "1.0.4"
serde = "1.0.219"
serde_derive = "1.0.219"
serde_json = "1.0.59"
Expand Down
62 changes: 62 additions & 0 deletions compiler/rustc_target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,65 @@ fn find_relative_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
Some(libdir) => libdir.into(),
}
}

macro_rules! target_spec_enum {
(
$( #[$attr:meta] )*
pub enum $name:ident {
$(
$( #[$variant_attr:meta] )*
$variant:ident = $string:literal,
)*
}
parse_error_type = $parse_error_type:literal;
) => {
$( #[$attr] )*
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
#[derive(schemars::JsonSchema)]
pub enum $name {
$(
$( #[$variant_attr] )*
#[serde(rename = $string)] // for JSON schema generation only
$variant,
)*
}

impl FromStr for $name {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
$( $string => Self::$variant, )*
_ => {
let all = [$( concat!("'", $string, "'") ),*].join(", ");
return Err(format!("invalid {}: '{s}'. allowed values: {all}", $parse_error_type));
}
})
}
}

impl $name {
pub fn desc(&self) -> &'static str {
match self {
$( Self::$variant => $string, )*
}
}
}

impl crate::json::ToJson for $name {
fn to_json(&self) -> crate::json::Json {
self.desc().to_json()
}
}

crate::json::serde_deserialize_from_str!($name);


impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.desc())
}
}
};
}
use target_spec_enum;
38 changes: 34 additions & 4 deletions compiler/rustc_target/src/spec/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ impl ToJson for Target {
}
}

#[derive(serde_derive::Deserialize)]
#[derive(serde_derive::Deserialize, schemars::JsonSchema)]
struct LinkSelfContainedComponentsWrapper {
components: Vec<LinkSelfContainedComponents>,
}

#[derive(serde_derive::Deserialize)]
#[derive(serde_derive::Deserialize, schemars::JsonSchema)]
#[serde(untagged)]
enum TargetFamiliesJson {
Array(StaticCow<[StaticCow<str>]>),
Expand All @@ -434,6 +434,18 @@ impl FromStr for EndianWrapper {
}
}
crate::json::serde_deserialize_from_str!(EndianWrapper);
impl schemars::JsonSchema for EndianWrapper {
fn schema_name() -> std::borrow::Cow<'static, str> {
"Endian".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema! ({
"type": "string",
"enum": ["big", "little"]
})
.into()
}
}

/// `ExternAbi` is in `rustc_abi`, which doesn't have access to the macro and serde.
struct ExternAbiWrapper(rustc_abi::ExternAbi);
Expand All @@ -446,16 +458,30 @@ impl FromStr for ExternAbiWrapper {
}
}
crate::json::serde_deserialize_from_str!(ExternAbiWrapper);
impl schemars::JsonSchema for ExternAbiWrapper {
fn schema_name() -> std::borrow::Cow<'static, str> {
"ExternAbi".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
let all =
rustc_abi::ExternAbi::ALL_VARIANTS.iter().map(|abi| abi.as_str()).collect::<Vec<_>>();
schemars::json_schema! ({
"type": "string",
"enum": all,
})
.into()
}
}

#[derive(serde_derive::Deserialize)]
#[derive(serde_derive::Deserialize, schemars::JsonSchema)]
struct TargetSpecJsonMetadata {
description: Option<StaticCow<str>>,
tier: Option<u64>,
host_tools: Option<bool>,
std: Option<bool>,
}

#[derive(serde_derive::Deserialize)]
#[derive(serde_derive::Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "kebab-case")]
// Ensure that all unexpected fields get turned into errors.
// This helps users stay up to date when the schema changes instead of silently
Expand Down Expand Up @@ -598,3 +624,7 @@ struct TargetSpecJson {
supports_xray: Option<bool>,
entry_abi: Option<ExternAbiWrapper>,
}

pub fn json_schema() -> schemars::Schema {
schemars::schema_for!(TargetSpecJson)
}
Loading
Loading