1rust: oe-selftest issue fix with v1.82
2
3A new feature "Link std statically in rustc_driver" was introduced
4in rust_1.82 [https://github.com/rust-lang/rust/pull/122362],and
5which is causing the below failure in oe-selftest.
6
7Running unittests src/main.rs (build/x86_64-unknown-linux-gnu/stage1-rustc/
8x86_64-poky-linux-gnu/release/deps/rustc_main-92223b15c9f2d827)
9uploaded ".../build/x86_64-unknown-linux-gnu/stage1-rustc/x86_64-poky-linux-gnu/
10release/deps/rustc_main-92223b15c9f2d827", waiting for result
11/tmp/work/test4056/rustc_main-92223b15c9f2d827: error while loading shared
12libraries: librustc_driver-fb0866b1cd913c20.so: cannot open shared object file: No
13such file or directory
14
15The rustc_main binary depends on the librustc_driver-*.so file. However,
16this file has not been copied to QEMU. If we manually copy the file into
17QEMU and export the LD_LIBRARY_PATH, the issue does not occur. Issue
18reprorted to upstream and reverted the buggy code as a workaround.
19
20Upstream-Status: Inappropriate [reported at https://github.com/rust-lang/rust/issues/136237]
21
22Signed-off-by: Deepesh Varatharajan <Deepesh.Varatharajan@windriver.com>
23diff --git a/compiler/rustc/src/main.rs b/compiler/rustc/src/main.rs
24index e9a7397557..29766fc9d8 100644
25--- a/compiler/rustc/src/main.rs
26+++ b/compiler/rustc/src/main.rs
27@@ -1,5 +1,3 @@
28-// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
29-#![feature(rustc_private)]
30 // Several crates are depended upon but unused so that they are present in the sysroot
31 #![expect(unused_crate_dependencies)]
32
33diff --git a/compiler/rustc_metadata/src/dependency_format.rs b/compiler/rustc_metadata/src/dependency_format.rs
34index 39fa23766b..51d86b4009 100644
35--- a/compiler/rustc_metadata/src/dependency_format.rs
36+++ b/compiler/rustc_metadata/src/dependency_format.rs
37@@ -51,7 +51,7 @@
38 //! Additionally, the algorithm is geared towards finding *any* solution rather
39 //! than finding a number of solutions (there are normally quite a few).
40
41-use rustc_data_structures::fx::{FxHashMap, FxHashSet};
42+use rustc_data_structures::fx::FxHashMap;
43 use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
44 use rustc_index::IndexVec;
45 use rustc_middle::bug;
46@@ -161,44 +161,19 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
47         }
48         Linkage::Dynamic | Linkage::IncludedFromDylib => {}
49     }
50-
51-    let all_dylibs = || {
52-        tcx.crates(()).iter().filter(|&&cnum| {
53-            !tcx.dep_kind(cnum).macros_only() && tcx.used_crate_source(cnum).dylib.is_some()
54-        })
55-    };
56-
57-    let mut upstream_in_dylibs = FxHashSet::default();
58-
59-    if tcx.features().rustc_private() {
60-        // We need this to prevent users of `rustc_driver` from linking dynamically to `std`
61-        // which does not work as `std` is also statically linked into `rustc_driver`.
62-
63-        // Find all libraries statically linked to upstream dylibs.
64-        for &cnum in all_dylibs() {
65-            let deps = tcx.dylib_dependency_formats(cnum);
66-            for &(depnum, style) in deps.iter() {
67-                if let RequireStatic = style {
68-                    upstream_in_dylibs.insert(depnum);
69-                }
70-            }
71-        }
72-    }
73-
74     let mut formats = FxHashMap::default();
75
76     // Sweep all crates for found dylibs. Add all dylibs, as well as their
77     // dependencies, ensuring there are no conflicts. The only valid case for a
78     // dependency to be relied upon twice is for both cases to rely on a dylib.
79-    for &cnum in all_dylibs() {
80-        if upstream_in_dylibs.contains(&cnum) {
81-            info!("skipping dylib: {}", tcx.crate_name(cnum));
82-            // If this dylib is also available statically linked to another dylib
83-            // we try to use that instead.
84+    for &cnum in tcx.crates(()).iter() {
85+        if tcx.dep_kind(cnum).macros_only() {
86             continue;
87         }
88
89         let name = tcx.crate_name(cnum);
90+        let src = tcx.used_crate_source(cnum);
91+        if src.dylib.is_some() {
92         info!("adding dylib: {}", name);
93         add_library(tcx, cnum, RequireDynamic, &mut formats, &mut unavailable_as_static);
94         let deps = tcx.dylib_dependency_formats(cnum);
95@@ -207,6 +182,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
96             add_library(tcx, depnum, style, &mut formats, &mut unavailable_as_static);
97         }
98     }
99+    }
100
101     // Collect what we've got so far in the return vector.
102     let last_crate = tcx.crates(()).len();
103diff --git a/src/bootstrap/src/bin/rustc.rs b/src/bootstrap/src/bin/rustc.rs
104index d04e2fbeb7..011c289d93 100644
105--- a/src/bootstrap/src/bin/rustc.rs
106+++ b/src/bootstrap/src/bin/rustc.rs
107@@ -89,24 +89,6 @@ fn main() {
108         rustc_real
109     };
110
111-    // Get the name of the crate we're compiling, if any.
112-    let crate_name = parse_value_from_args(&orig_args, "--crate-name");
113-
114-    // When statically linking `std` into `rustc_driver`, remove `-C prefer-dynamic`
115-    if env::var("RUSTC_LINK_STD_INTO_RUSTC_DRIVER").unwrap() == "1"
116-        && crate_name == Some("rustc_driver")
117-    {
118-        if let Some(pos) = args.iter().enumerate().position(|(i, a)| {
119-            a == "-C" && args.get(i + 1).map(|a| a == "prefer-dynamic").unwrap_or(false)
120-        }) {
121-            args.remove(pos);
122-            args.remove(pos);
123-        }
124-        if let Some(pos) = args.iter().position(|a| a == "-Cprefer-dynamic") {
125-            args.remove(pos);
126-        }
127-    }
128-
129     let mut cmd = match env::var_os("RUSTC_WRAPPER_REAL") {
130         Some(wrapper) if !wrapper.is_empty() => {
131             let mut cmd = Command::new(wrapper);
132@@ -117,6 +99,9 @@ fn main() {
133     };
134     cmd.args(&args).env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
135
136+    // Get the name of the crate we're compiling, if any.
137+    let crate_name = parse_value_from_args(&orig_args, "--crate-name");
138+
139     if let Some(crate_name) = crate_name {
140         if let Some(target) = env::var_os("RUSTC_TIME") {
141             if target == "all"
142diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs
143index 0688a1d689..066e6bf53f 100644
144--- a/src/bootstrap/src/core/builder/cargo.rs
145+++ b/src/bootstrap/src/core/builder/cargo.rs
146@@ -1146,7 +1146,7 @@ impl Builder<'_> {
147         // When we build Rust dylibs they're all intended for intermediate
148         // usage, so make sure we pass the -Cprefer-dynamic flag instead of
149         // linking all deps statically into the dylib.
150-        if matches!(mode, Mode::Std) {
151+        if matches!(mode, Mode::Std | Mode::Rustc) {
152             rustflags.arg("-Cprefer-dynamic");
153         }
154         if matches!(mode, Mode::Rustc) && !self.link_std_into_rustc_driver(target) {
155diff --git a/src/tools/clippy/src/main.rs b/src/tools/clippy/src/main.rs
156index c9853e53f3..c9af2138a7 100644
157--- a/src/tools/clippy/src/main.rs
158+++ b/src/tools/clippy/src/main.rs
159@@ -1,6 +1,3 @@
160-// We need this feature as it changes `dylib` linking behavior and allows us to link to
161-// `rustc_driver`.
162-#![feature(rustc_private)]
163 // warn on lints, that are included in `rust-lang/rust`s bootstrap
164 #![warn(rust_2018_idioms, unused_lifetimes)]
165
166diff --git a/src/tools/clippy/tests/compile-test.rs b/src/tools/clippy/tests/compile-test.rs
167index 9754254cdd..dd95cc71cd 100644
168--- a/src/tools/clippy/tests/compile-test.rs
169+++ b/src/tools/clippy/tests/compile-test.rs
170@@ -1,4 +1,4 @@
171-#![feature(rustc_private, let_chains)]
172+#![feature(let_chains)]
173 #![warn(rust_2018_idioms, unused_lifetimes)]
174 #![allow(unused_extern_crates)]
175
176diff --git a/src/tools/rustdoc/main.rs b/src/tools/rustdoc/main.rs
177index d4099cafe5..5b499a1fa1 100644
178--- a/src/tools/rustdoc/main.rs
179+++ b/src/tools/rustdoc/main.rs
180@@ -1,6 +1,3 @@
181-// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
182-#![feature(rustc_private)]
183-
184 fn main() {
185     rustdoc::main()
186 }
187diff --git a/src/tools/rustfmt/src/git-rustfmt/main.rs b/src/tools/rustfmt/src/git-rustfmt/main.rs
188index b8b0432aa9..b5bd71e015 100644
189--- a/src/tools/rustfmt/src/git-rustfmt/main.rs
190+++ b/src/tools/rustfmt/src/git-rustfmt/main.rs
191@@ -1,7 +1,3 @@
192-// We need this feature as it changes `dylib` linking behavior and allows us to link to
193-// `rustc_driver`.
194-#![feature(rustc_private)]
195-
196 use std::env;
197 use std::io::stdout;
198 use std::path::{Path, PathBuf};
199diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs
200index 27bbc8bd8f..a6fc4df2eb 100644
201--- a/src/bootstrap/src/core/build_steps/compile.rs
202+++ b/src/bootstrap/src/core/build_steps/compile.rs
203@@ -1940,24 +1940,8 @@ impl Step for Assemble {
204         let src_libdir = builder.sysroot_target_libdir(build_compiler, host);
205         for f in builder.read_dir(&src_libdir) {
206             let filename = f.file_name().into_string().unwrap();
207-
208-            let is_proc_macro = proc_macros.contains(&filename);
209-            let is_dylib_or_debug = is_dylib(&f.path()) || is_debug_info(&filename);
210-
211-            // If we link statically to stdlib, do not copy the libstd dynamic library file
212-            // FIXME: Also do this for Windows once incremental post-optimization stage0 tests
213-            // work without std.dll (see https://github.com/rust-lang/rust/pull/131188).
214-            let can_be_rustc_dynamic_dep = if builder
215-                .link_std_into_rustc_driver(target_compiler.host)
216-                && !target_compiler.host.is_windows()
217+            if (is_dylib(Path::new(&filename)) || is_debug_info(&filename)) && !proc_macros.contains(&filename)
218             {
219-                let is_std = filename.starts_with("std-") || filename.starts_with("libstd-");
220-                !is_std
221-            } else {
222-                true
223-            };
224-
225-            if is_dylib_or_debug && can_be_rustc_dynamic_dep && !is_proc_macro {
226                 builder.copy_link(&f.path(), &rustc_libdir.join(&filename));
227             }
228         }
229