]> git.proxmox.com Git - rustc.git/blobdiff - compiler/rustc_target/src/spec/tests/tests_impl.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_target / src / spec / tests / tests_impl.rs
index 1db6db78b17e4f8e0e7125270f6110ccbf843b55..0af599916a999429b33f434b2ad50575b7498401 100644 (file)
@@ -2,28 +2,31 @@ use super::super::*;
 use std::assert_matches::assert_matches;
 
 // Test target self-consistency and JSON encoding/decoding roundtrip.
-pub(super) fn test_target(target: Target) {
-    target.check_consistency();
-    assert_eq!(Target::from_json(target.to_json()).map(|(j, _)| j), Ok(target));
+pub(super) fn test_target(mut target: Target, triple: &str) {
+    let recycled_target = Target::from_json(target.to_json()).map(|(j, _)| j);
+    target.update_to_cli();
+    target.check_consistency(triple);
+    assert_eq!(recycled_target, Ok(target));
 }
 
 impl Target {
-    fn check_consistency(&self) {
+    fn check_consistency(&self, triple: &str) {
         assert_eq!(self.is_like_osx, self.vendor == "apple");
         assert_eq!(self.is_like_solaris, self.os == "solaris" || self.os == "illumos");
         assert_eq!(self.is_like_windows, self.os == "windows" || self.os == "uefi");
         assert_eq!(self.is_like_wasm, self.arch == "wasm32" || self.arch == "wasm64");
-        assert!(self.is_like_windows || !self.is_like_msvc);
+        if self.is_like_msvc {
+            assert!(self.is_like_windows);
+        }
 
         // Check that default linker flavor and lld flavor are compatible
         // with some other key properties.
         assert_eq!(self.is_like_osx, matches!(self.lld_flavor, LldFlavor::Ld64));
         assert_eq!(self.is_like_msvc, matches!(self.lld_flavor, LldFlavor::Link));
         assert_eq!(self.is_like_wasm, matches!(self.lld_flavor, LldFlavor::Wasm));
-        assert_eq!(self.os == "l4re", matches!(self.linker_flavor, LinkerFlavor::L4Bender));
-        assert_eq!(self.os == "emscripten", matches!(self.linker_flavor, LinkerFlavor::Em));
-        assert_eq!(self.arch == "bpf", matches!(self.linker_flavor, LinkerFlavor::BpfLinker));
-        assert_eq!(self.arch == "nvptx64", matches!(self.linker_flavor, LinkerFlavor::PtxLinker));
+        assert_eq!(self.os == "emscripten", matches!(self.linker_flavor, LinkerFlavor::EmCc));
+        assert_eq!(self.arch == "bpf", matches!(self.linker_flavor, LinkerFlavor::Bpf));
+        assert_eq!(self.arch == "nvptx64", matches!(self.linker_flavor, LinkerFlavor::Ptx));
 
         for args in [
             &self.pre_link_args,
@@ -63,17 +66,14 @@ impl Target {
                             LinkerFlavor::Lld(LldFlavor::Wasm) | LinkerFlavor::Gcc
                         )
                     }
-                    (LinkerFlavor::L4Bender, LldFlavor::Ld) => {
-                        assert_matches!(flavor, LinkerFlavor::L4Bender)
-                    }
-                    (LinkerFlavor::Em, LldFlavor::Wasm) => {
-                        assert_matches!(flavor, LinkerFlavor::Em)
+                    (LinkerFlavor::EmCc, LldFlavor::Wasm) => {
+                        assert_matches!(flavor, LinkerFlavor::EmCc)
                     }
-                    (LinkerFlavor::BpfLinker, LldFlavor::Ld) => {
-                        assert_matches!(flavor, LinkerFlavor::BpfLinker)
+                    (LinkerFlavor::Bpf, LldFlavor::Ld) => {
+                        assert_matches!(flavor, LinkerFlavor::Bpf)
                     }
-                    (LinkerFlavor::PtxLinker, LldFlavor::Ld) => {
-                        assert_matches!(flavor, LinkerFlavor::PtxLinker)
+                    (LinkerFlavor::Ptx, LldFlavor::Ld) => {
+                        assert_matches!(flavor, LinkerFlavor::Ptx)
                     }
                     flavors => unreachable!("unexpected flavor combination: {:?}", flavors),
                 }
@@ -94,8 +94,9 @@ impl Target {
                             check_noncc(LinkerFlavor::Ld);
                             check_noncc(LinkerFlavor::Lld(LldFlavor::Ld));
                         }
+                        LldFlavor::Ld64 => check_noncc(LinkerFlavor::Lld(LldFlavor::Ld64)),
                         LldFlavor::Wasm => check_noncc(LinkerFlavor::Lld(LldFlavor::Wasm)),
-                        LldFlavor::Ld64 | LldFlavor::Link => {}
+                        LldFlavor::Link => {}
                     },
                     _ => {}
                 }
@@ -109,20 +110,57 @@ impl Target {
             );
         }
 
-        assert!(
-            (self.pre_link_objects_fallback.is_empty()
-                && self.post_link_objects_fallback.is_empty())
-                || self.crt_objects_fallback.is_some()
-        );
+        if self.link_self_contained == LinkSelfContainedDefault::False {
+            assert!(
+                self.pre_link_objects_self_contained.is_empty()
+                    && self.post_link_objects_self_contained.is_empty()
+            );
+        }
 
         // If your target really needs to deviate from the rules below,
         // except it and document the reasons.
         // Keep the default "unknown" vendor instead.
         assert_ne!(self.vendor, "");
+        assert_ne!(self.os, "");
         if !self.can_use_os_unknown() {
             // Keep the default "none" for bare metal targets instead.
             assert_ne!(self.os, "unknown");
         }
+
+        // Check dynamic linking stuff
+        // BPF: when targeting user space vms (like rbpf), those can load dynamic libraries.
+        if self.os == "none" && self.arch != "bpf" {
+            assert!(!self.dynamic_linking);
+        }
+        if self.only_cdylib
+            || self.crt_static_allows_dylibs
+            || !self.late_link_args_dynamic.is_empty()
+        {
+            assert!(self.dynamic_linking);
+        }
+        // Apparently PIC was slow on wasm at some point, see comments in wasm_base.rs
+        if self.dynamic_linking && !(self.is_like_wasm && self.os != "emscripten") {
+            assert_eq!(self.relocation_model, RelocModel::Pic);
+        }
+        // PIEs are supported but not enabled by default with linuxkernel target.
+        if self.position_independent_executables && !triple.ends_with("-linuxkernel") {
+            assert_eq!(self.relocation_model, RelocModel::Pic);
+        }
+        // The UEFI targets do not support dynamic linking but still require PIC (#101377).
+        if self.relocation_model == RelocModel::Pic && self.os != "uefi" {
+            assert!(self.dynamic_linking || self.position_independent_executables);
+        }
+        if self.static_position_independent_executables {
+            assert!(self.position_independent_executables);
+        }
+        if self.position_independent_executables {
+            assert!(self.executables);
+        }
+
+        // Check crt static stuff
+        if self.crt_static_default || self.crt_static_allows_dylibs {
+            assert!(self.crt_static_respected);
+        }
     }
 
     // Add your target to the whitelist if it has `std` library