diff --git a/Cargo.lock b/Cargo.lock index eed83df86b9..8b9b5e88bb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2352,7 +2352,6 @@ dependencies = [ "regex-automata 0.4.5", "regex-syntax 0.8.2", "rustix", - "semver", "serde", "serde_json", "syn 2.0.90", @@ -3548,15 +3547,6 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" -[[package]] -name = "rustc_version" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" -dependencies = [ - "semver", -] - [[package]] name = "rustfix" version = "0.8.4" @@ -4013,7 +4003,6 @@ dependencies = [ "cfg-expr", "guppy-workspace-hack", "proptest", - "rustc_version", "serde", "serde_json", "target-lexicon", diff --git a/target-spec-miette/tests/snapshot/custom-invalid/input/invalid-endian.json b/target-spec-miette/tests/snapshot/custom-invalid/input/invalid-endian.json new file mode 100644 index 00000000000..ef2abee2383 --- /dev/null +++ b/target-spec-miette/tests/snapshot/custom-invalid/input/invalid-endian.json @@ -0,0 +1,36 @@ +{ + "arch": "powerpc64", + "abi-return-struct-as-int": true, + "cpu": "ppc64", + "crt-objects-fallback": "false", + "crt-static-respected": true, + "data-layout": "E-m:e-Fn32-i64:64-n32:64", + "default-dwarf-version": 2, + "dynamic-linking": true, + "has-rpath": true, + "has-thread-local": true, + "is-builtin": true, + "linker-flavor": "gnu-cc", + "llvm-target": "powerpc64-unknown-freebsd", + "max-atomic-width": 64, + "metadata": { + "description": "PPC64 FreeBSD (ELFv1 and ELFv2)", + "host_tools": true, + "std": true, + "tier": 3 + }, + "os": "freebsd", + "position-independent-executables": true, + "pre-link-args": { + "gnu-cc": ["-m64"], + "gnu-lld-cc": ["-m64"] + }, + "relro-level": "full", + "stack-probes": { + "kind": "inline" + }, + "target-endian": "middle", + "target-family": ["unix"], + "target-mcount": "_mcount", + "target-pointer-width": "64" +} diff --git a/target-spec-miette/tests/snapshot/custom-invalid/input/invalid-family.json b/target-spec-miette/tests/snapshot/custom-invalid/input/invalid-family.json new file mode 100644 index 00000000000..acfbb3ed536 --- /dev/null +++ b/target-spec-miette/tests/snapshot/custom-invalid/input/invalid-family.json @@ -0,0 +1,36 @@ +{ + "arch": "powerpc64", + "abi-return-struct-as-int": true, + "cpu": "ppc64", + "crt-objects-fallback": "false", + "crt-static-respected": true, + "data-layout": "E-m:e-Fn32-i64:64-n32:64", + "default-dwarf-version": 2, + "dynamic-linking": true, + "has-rpath": true, + "has-thread-local": true, + "is-builtin": true, + "linker-flavor": "gnu-cc", + "llvm-target": "powerpc64-unknown-freebsd", + "max-atomic-width": 64, + "metadata": { + "description": "PPC64 FreeBSD (ELFv1 and ELFv2)", + "host_tools": true, + "std": true, + "tier": 3 + }, + "os": "freebsd", + "position-independent-executables": true, + "pre-link-args": { + "gnu-cc": ["-m64"], + "gnu-lld-cc": ["-m64"] + }, + "relro-level": "full", + "stack-probes": { + "kind": "inline" + }, + "target-endian": "big", + "target-family": "none", + "target-mcount": "_mcount", + "target-pointer-width": "64" +} diff --git a/target-spec-miette/tests/snapshot/custom-invalid/output/invalid-endian.json-display.snap b/target-spec-miette/tests/snapshot/custom-invalid/output/invalid-endian.json-display.snap new file mode 100644 index 00000000000..977c9006a71 --- /dev/null +++ b/target-spec-miette/tests/snapshot/custom-invalid/output/invalid-endian.json-display.snap @@ -0,0 +1,15 @@ +--- +source: target-spec-miette/tests/snapshot/custom.rs +expression: "format!(\"{:?}\", miette::Report::new_boxed(diagnostic))" +snapshot_kind: text +--- + × error deserializing custom target JSON for `my-target` + ╰─▶ unknown variant `middle`, expected `little` or `big` at line 32 column + 29 + ╭─[32:29] + 31 │ }, + 32 │ "target-endian": "middle", + · ▲ + · ╰── unknown variant `middle`, expected `little` or `big` + 33 │ "target-family": ["unix"], + ╰──── diff --git a/target-spec-miette/tests/snapshot/custom-invalid/output/invalid-family.json-display.snap b/target-spec-miette/tests/snapshot/custom-invalid/output/invalid-family.json-display.snap new file mode 100644 index 00000000000..9d7cad7fcfd --- /dev/null +++ b/target-spec-miette/tests/snapshot/custom-invalid/output/invalid-family.json-display.snap @@ -0,0 +1,14 @@ +--- +source: target-spec-miette/tests/snapshot/custom.rs +expression: "format!(\"{:?}\", miette::Report::new_boxed(diagnostic))" +snapshot_kind: text +--- + × error deserializing custom target JSON for `my-target` + ╰─▶ invalid type: string "none", expected a sequence at line 33 column 27 + ╭─[33:27] + 32 │ "target-endian": "big", + 33 │ "target-family": "none", + · ▲ + · ╰── invalid type: string "none", expected a sequence + 34 │ "target-mcount": "_mcount", + ╰──── diff --git a/target-spec/Cargo.toml b/target-spec/Cargo.toml index b864eaaa6af..69aaa0901df 100644 --- a/target-spec/Cargo.toml +++ b/target-spec/Cargo.toml @@ -29,7 +29,6 @@ unicode-ident = "1.0.14" guppy-workspace-hack.workspace = true [dev-dependencies] -rustc_version = "0.4.1" test-case = "3.3.1" toml = "0.5.11" diff --git a/target-spec/src/custom.rs b/target-spec/src/custom.rs index eba5c018fe5..34c8572903b 100644 --- a/target-spec/src/custom.rs +++ b/target-spec/src/custom.rs @@ -13,6 +13,13 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, Ord, PartialEq, PartialOrd)] #[serde(rename_all = "kebab-case")] pub(crate) struct TargetDefinition { + // TODO: it would be nice to use target-spec-json for this, but that has a + // few limitations as of v0.1: + // + // * target-pointer-width is a string, not an integer. + // * Os and Env deserialized to enums, but we would really like them to be strings. + // + // --- arch: String, #[serde(rename = "target-pointer-width", with = "target_pointer_width")] pointer_width: u8, @@ -34,9 +41,9 @@ pub(crate) struct TargetDefinition { #[serde(default)] vendor: Option, #[serde(default)] - families: Vec, + target_family: Vec, #[serde(default)] - endian: Endian, + target_endian: Endian, #[serde(default)] min_atomic_width: Option, #[serde(default)] @@ -76,9 +83,9 @@ impl TargetDefinition { arch: Arch::new(self.arch), env: self.env.map(Env::new), vendor: self.vendor.map(Vendor::new), - families: Families::new(self.families.into_iter().map(Family::new)), + families: Families::new(self.target_family.into_iter().map(Family::new)), pointer_width: self.pointer_width, - endian: self.endian.to_cfg_expr(), + endian: self.target_endian.to_cfg_expr(), has_atomics: HasAtomics::new(has_atomics), panic: self.panic_strategy.to_cfg_expr(), } @@ -156,13 +163,6 @@ mod tests { #[test] fn test_all_builtin_specs_recognized() { - let version = rustc_version::version().expect("rustc_version succeeded"); - if version.minor < 70 { - // all-target-specs-json is only present on Rust 1.70 and above. - println!("** skipping, minor version {} < 70", version.minor); - return; - } - let rustc_bin: String = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_owned()); let output = Command::new(rustc_bin) // Used for -Zunstable-options. This is test-only code so it doesn't matter. @@ -178,12 +178,28 @@ mod tests { eprintln!("*** testing {triple}"); // Just make sure this doesn't panic. (If this becomes fallible in the future, then this // shouldn't return an error either.) - target_def.clone().into_target_info(triple.into()); + target_def.clone().into_target_info(triple.clone().into()); let json = serde_json::to_string(&target_def).expect("target def serialized successfully"); eprintln!("* minified json: {json}"); let target_def_2 = serde_json::from_str(&json).expect("target def 2 deserialized"); assert_eq!(target_def, target_def_2, "matches"); + + // Do some spot checks for things like big-endian targets. + if triple.contains("powerpc") { + assert_eq!( + target_def.target_endian, + Endian::Big, + "powerpc is big-endian" + ); + } + if triple.contains("-linux") { + assert!( + target_def.target_family.contains(&"unix".to_owned()), + "linux target_family should contain unix (was {:#?})", + target_def.target_family, + ); + } } } } diff --git a/workspace-hack/Cargo.toml b/workspace-hack/Cargo.toml index 82ac3b703dd..3242f7f4f28 100644 --- a/workspace-hack/Cargo.toml +++ b/workspace-hack/Cargo.toml @@ -33,7 +33,6 @@ petgraph = { version = "0.6.5", default-features = false, features = ["graphmap" regex = { version = "1.10.5", default-features = false, features = ["perf", "std"] } regex-automata = { version = "0.4.5", default-features = false, features = ["dfa", "hybrid", "meta", "nfa", "perf", "unicode"] } regex-syntax = { version = "0.8.2" } -semver = { version = "1.0.23", features = ["serde"] } serde = { version = "1.0.215", features = ["alloc", "derive"] } serde_json = { version = "1.0.133", features = ["unbounded_depth"] } textwrap = { version = "0.16.0" }