Skip to content

Commit 3be78e5

Browse files
committed
ensure JSON-defined targets are consistent
1 parent c49a687 commit 3be78e5

File tree

5 files changed

+303
-187
lines changed

5 files changed

+303
-187
lines changed

compiler/rustc_target/src/spec/mod.rs

+299-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//! the target's settings, though `target-feature` and `link-args` will *add*
3535
//! to the list specified by the target, rather than replace.
3636
37+
use std::assert_matches::assert_matches;
3738
use std::borrow::Cow;
3839
use std::collections::BTreeMap;
3940
use std::hash::{Hash, Hasher};
@@ -1605,13 +1606,11 @@ macro_rules! supported_targets {
16051606

16061607
#[cfg(test)]
16071608
mod tests {
1608-
mod tests_impl;
1609-
16101609
// Cannot put this into a separate file without duplication, make an exception.
16111610
$(
16121611
#[test] // `#[test]`
16131612
fn $module() {
1614-
tests_impl::test_target(crate::spec::targets::$module::target());
1613+
crate::spec::targets::$module::target().test_target()
16151614
}
16161615
)+
16171616
}
@@ -2849,6 +2848,302 @@ impl Target {
28492848
self.max_atomic_width.unwrap_or_else(|| self.pointer_width.into())
28502849
}
28512850

2851+
fn check_consistency(&self) -> Result<(), String> {
2852+
macro_rules! check {
2853+
($b:expr, $($msg:tt)*) => {
2854+
if !$b {
2855+
return Err(format!($($msg)*));
2856+
}
2857+
}
2858+
}
2859+
macro_rules! check_eq {
2860+
($left:expr, $right:expr, $($msg:tt)*) => {
2861+
if ($left) != ($right) {
2862+
return Err(format!($($msg)*));
2863+
}
2864+
}
2865+
}
2866+
macro_rules! check_ne {
2867+
($left:expr, $right:expr, $($msg:tt)*) => {
2868+
if ($left) == ($right) {
2869+
return Err(format!($($msg)*));
2870+
}
2871+
}
2872+
}
2873+
macro_rules! check_matches {
2874+
($left:expr, $right:pat, $($msg:tt)*) => {
2875+
if !matches!($left, $right) {
2876+
return Err(format!($($msg)*));
2877+
}
2878+
}
2879+
}
2880+
2881+
check_eq!(
2882+
self.is_like_osx,
2883+
self.vendor == "apple",
2884+
"`is_like_osx` must be set if and only if `vendor` is `apple`"
2885+
);
2886+
check_eq!(
2887+
self.is_like_solaris,
2888+
self.os == "solaris" || self.os == "illumos",
2889+
"`is_like_solaris` must be set if and only if `os` is `solaris` or `illumos`"
2890+
);
2891+
check_eq!(
2892+
self.is_like_windows,
2893+
self.os == "windows" || self.os == "uefi",
2894+
"`is_like_windows` must be set if and only if `os` is `windows` or `uefi`"
2895+
);
2896+
check_eq!(
2897+
self.is_like_wasm,
2898+
self.arch == "wasm32" || self.arch == "wasm64",
2899+
"`is_like_wasm` must be set if and only if `arch` is `wasm32` or `wasm64`"
2900+
);
2901+
if self.is_like_msvc {
2902+
check!(self.is_like_windows, "if `is_like_msvc` is set, `is_like_windows` must be set");
2903+
}
2904+
2905+
// Check that default linker flavor is compatible with some other key properties.
2906+
check_eq!(
2907+
self.is_like_osx,
2908+
matches!(self.linker_flavor, LinkerFlavor::Darwin(..)),
2909+
"`linker_flavor` must be `darwin` if and only if `is_like_osx` is set"
2910+
);
2911+
check_eq!(
2912+
self.is_like_msvc,
2913+
matches!(self.linker_flavor, LinkerFlavor::Msvc(..)),
2914+
"`linker_flavor` must be `mscv` if and only if `is_like_msvc` is set"
2915+
);
2916+
check_eq!(
2917+
self.is_like_wasm && self.os != "emscripten",
2918+
matches!(self.linker_flavor, LinkerFlavor::WasmLld(..)),
2919+
"`linker_flavor` must be `wasm-lld` if and only if `is_like_wasm` is set and the `os` is not `emscripten`",
2920+
);
2921+
check_eq!(
2922+
self.os == "emscripten",
2923+
matches!(self.linker_flavor, LinkerFlavor::EmCc),
2924+
"`linker_flavor` must be `em-cc` if and only if `os` is `emscripten`"
2925+
);
2926+
check_eq!(
2927+
self.arch == "bpf",
2928+
matches!(self.linker_flavor, LinkerFlavor::Bpf),
2929+
"`linker_flavor` must be `bpf` if and only if `arch` is `bpf`"
2930+
);
2931+
check_eq!(
2932+
self.arch == "nvptx64",
2933+
matches!(self.linker_flavor, LinkerFlavor::Ptx),
2934+
"`linker_flavor` must be `ptc` if and only if `arch` is `nvptx64`"
2935+
);
2936+
2937+
for args in [
2938+
&self.pre_link_args,
2939+
&self.late_link_args,
2940+
&self.late_link_args_dynamic,
2941+
&self.late_link_args_static,
2942+
&self.post_link_args,
2943+
] {
2944+
for (&flavor, flavor_args) in args {
2945+
check!(!flavor_args.is_empty(), "linker flavor args must not be empty");
2946+
// Check that flavors mentioned in link args are compatible with the default flavor.
2947+
match self.linker_flavor {
2948+
LinkerFlavor::Gnu(..) => {
2949+
check_matches!(
2950+
flavor,
2951+
LinkerFlavor::Gnu(..),
2952+
"mixing GNU and non-GNU linker flavors"
2953+
);
2954+
}
2955+
LinkerFlavor::Darwin(..) => {
2956+
check_matches!(
2957+
flavor,
2958+
LinkerFlavor::Darwin(..),
2959+
"mixing Darwin and non-Darwin linker flavors"
2960+
)
2961+
}
2962+
LinkerFlavor::WasmLld(..) => {
2963+
check_matches!(
2964+
flavor,
2965+
LinkerFlavor::WasmLld(..),
2966+
"mixing wasm and non-wasm linker flavors"
2967+
)
2968+
}
2969+
LinkerFlavor::Unix(..) => {
2970+
check_matches!(
2971+
flavor,
2972+
LinkerFlavor::Unix(..),
2973+
"mixing unix and non-unix linker flavors"
2974+
);
2975+
}
2976+
LinkerFlavor::Msvc(..) => {
2977+
check_matches!(
2978+
flavor,
2979+
LinkerFlavor::Msvc(..),
2980+
"mixing MSVC and non-MSVC linker flavors"
2981+
);
2982+
}
2983+
LinkerFlavor::EmCc
2984+
| LinkerFlavor::Bpf
2985+
| LinkerFlavor::Ptx
2986+
| LinkerFlavor::Llbc => {
2987+
check_eq!(flavor, self.linker_flavor, "mixing different linker flavors")
2988+
}
2989+
}
2990+
2991+
// Check that link args for cc and non-cc versions of flavors are consistent.
2992+
let check_noncc = |noncc_flavor| -> Result<(), String> {
2993+
if let Some(noncc_args) = args.get(&noncc_flavor) {
2994+
for arg in flavor_args {
2995+
if let Some(suffix) = arg.strip_prefix("-Wl,") {
2996+
check!(
2997+
noncc_args.iter().any(|a| a == suffix),
2998+
" link args for cc and non-cc versions of flavors are not consistent"
2999+
);
3000+
}
3001+
}
3002+
}
3003+
Ok(())
3004+
};
3005+
3006+
match self.linker_flavor {
3007+
LinkerFlavor::Gnu(Cc::Yes, lld) => check_noncc(LinkerFlavor::Gnu(Cc::No, lld))?,
3008+
LinkerFlavor::WasmLld(Cc::Yes) => check_noncc(LinkerFlavor::WasmLld(Cc::No))?,
3009+
LinkerFlavor::Unix(Cc::Yes) => check_noncc(LinkerFlavor::Unix(Cc::No))?,
3010+
_ => {}
3011+
}
3012+
}
3013+
3014+
// Check that link args for lld and non-lld versions of flavors are consistent.
3015+
for cc in [Cc::No, Cc::Yes] {
3016+
check_eq!(
3017+
args.get(&LinkerFlavor::Gnu(cc, Lld::No)),
3018+
args.get(&LinkerFlavor::Gnu(cc, Lld::Yes)),
3019+
"link args for lld and non-lld versions of flavors are not consistent",
3020+
);
3021+
check_eq!(
3022+
args.get(&LinkerFlavor::Darwin(cc, Lld::No)),
3023+
args.get(&LinkerFlavor::Darwin(cc, Lld::Yes)),
3024+
"link args for lld and non-lld versions of flavors are not consistent",
3025+
);
3026+
}
3027+
check_eq!(
3028+
args.get(&LinkerFlavor::Msvc(Lld::No)),
3029+
args.get(&LinkerFlavor::Msvc(Lld::Yes)),
3030+
"link args for lld and non-lld versions of flavors are not consistent",
3031+
);
3032+
}
3033+
3034+
if self.link_self_contained.is_disabled() {
3035+
check!(
3036+
self.pre_link_objects_self_contained.is_empty()
3037+
&& self.post_link_objects_self_contained.is_empty(),
3038+
"if `link_self_contained` is disabled, then `pre_link_objects_self_contained` and `post_link_objects_self_contained` must be empty",
3039+
);
3040+
}
3041+
3042+
// If your target really needs to deviate from the rules below,
3043+
// except it and document the reasons.
3044+
// Keep the default "unknown" vendor instead.
3045+
check_ne!(self.vendor, "", "`vendor` cannot be empty");
3046+
check_ne!(self.os, "", "`os` cannot be empty");
3047+
if !self.can_use_os_unknown() {
3048+
// Keep the default "none" for bare metal targets instead.
3049+
check_ne!(
3050+
self.os,
3051+
"unknown",
3052+
"`unknown` os can only be used on particular targets; use `none` for bare-metal targets"
3053+
);
3054+
}
3055+
3056+
// Check dynamic linking stuff
3057+
// BPF: when targeting user space vms (like rbpf), those can load dynamic libraries.
3058+
// hexagon: when targeting QuRT, that OS can load dynamic libraries.
3059+
// wasm{32,64}: dynamic linking is inherent in the definition of the VM.
3060+
if self.os == "none"
3061+
&& (self.arch != "bpf"
3062+
&& self.arch != "hexagon"
3063+
&& self.arch != "wasm32"
3064+
&& self.arch != "wasm64")
3065+
{
3066+
check!(
3067+
!self.dynamic_linking,
3068+
"dynamic linking is not supported on this OS/architecture"
3069+
);
3070+
}
3071+
if self.only_cdylib
3072+
|| self.crt_static_allows_dylibs
3073+
|| !self.late_link_args_dynamic.is_empty()
3074+
{
3075+
check!(
3076+
self.dynamic_linking,
3077+
"dynamic linking must be allowed when `only_cdylib` or `crt_static_allows_dylibs` or `late_link_args_dynamic` are set"
3078+
);
3079+
}
3080+
// Apparently PIC was slow on wasm at some point, see comments in wasm_base.rs
3081+
if self.dynamic_linking && !(self.is_like_wasm && self.os != "emscripten") {
3082+
check_eq!(
3083+
self.relocation_model,
3084+
RelocModel::Pic,
3085+
"targets that support dynamic linking must use the `pic` relocation model"
3086+
);
3087+
}
3088+
if self.position_independent_executables {
3089+
check_eq!(
3090+
self.relocation_model,
3091+
RelocModel::Pic,
3092+
"targets that support position-independent executables must use the `pic` relocation model"
3093+
);
3094+
}
3095+
// The UEFI targets do not support dynamic linking but still require PIC (#101377).
3096+
if self.relocation_model == RelocModel::Pic && self.os != "uefi" {
3097+
check!(
3098+
self.dynamic_linking || self.position_independent_executables,
3099+
"when the relocation model is `pic`, the target must support dynamic linking or use position-independent executables. \
3100+
Set the relocation model to `static` to avoid this requirement"
3101+
);
3102+
}
3103+
if self.static_position_independent_executables {
3104+
assert!(self.position_independent_executables);
3105+
}
3106+
if self.position_independent_executables {
3107+
assert!(self.executables);
3108+
}
3109+
3110+
// Check crt static stuff
3111+
if self.crt_static_default || self.crt_static_allows_dylibs {
3112+
assert!(self.crt_static_respected);
3113+
}
3114+
3115+
// Check that RISC-V targets always specify which ABI they use.
3116+
match &*self.arch {
3117+
"riscv32" => {
3118+
assert_matches!(&*self.llvm_abiname, "ilp32" | "ilp32f" | "ilp32d" | "ilp32e")
3119+
}
3120+
"riscv64" => {
3121+
// Note that the `lp64e` is still unstable as it's not (yet) part of the ELF psABI.
3122+
assert_matches!(&*self.llvm_abiname, "lp64" | "lp64f" | "lp64d" | "lp64q" | "lp64e")
3123+
}
3124+
_ => {}
3125+
}
3126+
3127+
Ok(())
3128+
}
3129+
3130+
/// Test target self-consistency and JSON encoding/decoding roundtrip.
3131+
#[cfg(test)]
3132+
fn test_target(mut self) {
3133+
let recycled_target = Target::from_json(self.to_json()).map(|(j, _)| j);
3134+
self.update_to_cli();
3135+
self.check_consistency().unwrap();
3136+
assert_eq!(recycled_target, Ok(self));
3137+
}
3138+
3139+
// Add your target to the whitelist if it has `std` library
3140+
// and you certainly want "unknown" for the OS name.
3141+
fn can_use_os_unknown(&self) -> bool {
3142+
self.llvm_target == "wasm32-unknown-unknown"
3143+
|| self.llvm_target == "wasm64-unknown-unknown"
3144+
|| (self.env == "sgx" && self.vendor == "fortanix")
3145+
}
3146+
28523147
/// Loads a target descriptor from a JSON object.
28533148
pub fn from_json(obj: Json) -> Result<(Target, TargetWarnings), String> {
28543149
// While ugly, this code must remain this way to retain
@@ -3456,6 +3751,7 @@ impl Target {
34563751
key!(supports_xray, bool);
34573752

34583753
base.update_from_cli();
3754+
base.check_consistency()?;
34593755

34603756
// Each field should have been read using `Json::remove` so any keys remaining are unused.
34613757
let remaining_keys = obj.keys();

0 commit comments

Comments
 (0)