Skip to content

Commit 38f3c7c

Browse files
committed
ensure JSON-defined targets are consistent
1 parent c49a687 commit 38f3c7c

File tree

4 files changed

+328
-187
lines changed

4 files changed

+328
-187
lines changed

compiler/rustc_target/src/spec/mod.rs

+326-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
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+
// ignore-tidy-filelength
38+
3739
use std::borrow::Cow;
3840
use std::collections::BTreeMap;
3941
use std::hash::{Hash, Hasher};
@@ -1605,13 +1607,11 @@ macro_rules! supported_targets {
16051607

16061608
#[cfg(test)]
16071609
mod tests {
1608-
mod tests_impl;
1609-
16101610
// Cannot put this into a separate file without duplication, make an exception.
16111611
$(
16121612
#[test] // `#[test]`
16131613
fn $module() {
1614-
tests_impl::test_target(crate::spec::targets::$module::target());
1614+
crate::spec::targets::$module::target().test_target()
16151615
}
16161616
)+
16171617
}
@@ -1998,6 +1998,14 @@ impl TargetWarnings {
19981998
}
19991999
}
20002000

2001+
/// For the [`Target::check_consistency`] function, determines whether the given target is a builtin or a JSON
2002+
/// target.
2003+
#[derive(Copy, Clone, Debug, PartialEq)]
2004+
enum TargetKind {
2005+
Json,
2006+
Builtin,
2007+
}
2008+
20012009
/// Everything `rustc` knows about how to compile for a specific target.
20022010
///
20032011
/// Every field here must be specified, and has no default value.
@@ -2849,6 +2857,320 @@ impl Target {
28492857
self.max_atomic_width.unwrap_or_else(|| self.pointer_width.into())
28502858
}
28512859

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

34583780
base.update_from_cli();
3781+
base.check_consistency(TargetKind::Json)?;
34593782

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

0 commit comments

Comments
 (0)