Skip to content

Commit 28410c1

Browse files
authored
Unrolled build for rust-lang#138273
Rollup merge of rust-lang#138273 - petrochenkov:nonatroot, r=bjorn3 metadata: Ignore sysroot when doing the manual native lib search in rustc This is the opposite alternative to rust-lang#138170 and another way to make native library search consistent between rustc and linker. This way the directory list searched by rustc is still a prefix of the directory list considered by linker, but it's a shorter prefix than in rust-lang#138170. We can include the sysroot directories into rustc's search again later if the issues with rust-lang#138170 are resolved, it will be a backward compatible change. This may break some code doing weird things on unstable rustc, or tier 2-3 targets, like bundling `libunwind.a` or sanitizers into something. Note that this doesn't affect shipped `libc.a`, because it lives in `self-contained` directories in sysroot, and `self-contained` sysroot is already not included into the rustc's search. All libunwind and sanitizer libs should be moved to `self-contained` sysroot too eventually. With the consistent search directory list between rustc and linker we can make rustc own the native library search (at least for static libs) and use linker search only as a fallback (like in rust-lang#123436). This will allow addressing issues like rust-lang#132394 once and for all on all targets. r? ``@bjorn3``
2 parents 961351c + d577883 commit 28410c1

File tree

3 files changed

+46
-47
lines changed

3 files changed

+46
-47
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
2222
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2323
use rustc_macros::LintDiagnostic;
2424
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
25-
use rustc_metadata::{find_native_static_library, walk_native_lib_search_dirs};
25+
use rustc_metadata::{
26+
NativeLibSearchFallback, find_native_static_library, walk_native_lib_search_dirs,
27+
};
2628
use rustc_middle::bug;
2729
use rustc_middle::lint::lint_level;
2830
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
@@ -2129,19 +2131,15 @@ fn add_library_search_dirs(
21292131
return;
21302132
}
21312133

2132-
walk_native_lib_search_dirs(
2133-
sess,
2134-
self_contained_components,
2135-
apple_sdk_root,
2136-
|dir, is_framework| {
2137-
if is_framework {
2138-
cmd.framework_path(dir);
2139-
} else {
2140-
cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
2141-
}
2142-
ControlFlow::<()>::Continue(())
2143-
},
2144-
);
2134+
let fallback = Some(NativeLibSearchFallback { self_contained_components, apple_sdk_root });
2135+
walk_native_lib_search_dirs(sess, fallback, |dir, is_framework| {
2136+
if is_framework {
2137+
cmd.framework_path(dir);
2138+
} else {
2139+
cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
2140+
}
2141+
ControlFlow::<()>::Continue(())
2142+
});
21452143
}
21462144

21472145
/// Add options making relocation sections in the produced ELF files read-only

compiler/rustc_metadata/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub mod locator;
3535
pub use creader::{DylibError, load_symbol_from_dylib};
3636
pub use fs::{METADATA_FILENAME, emit_wrapper_file};
3737
pub use native_libs::{
38-
find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library,
39-
walk_native_lib_search_dirs,
38+
NativeLibSearchFallback, find_native_static_library, try_find_native_dynamic_library,
39+
try_find_native_static_library, walk_native_lib_search_dirs,
4040
};
4141
pub use rmeta::{EncodedMetadata, METADATA_HEADER, encode_metadata, rendered_const};
4242

compiler/rustc_metadata/src/native_libs.rs

+32-31
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ use rustc_target::spec::{BinaryFormat, LinkSelfContainedComponents};
2121

2222
use crate::{errors, fluent_generated};
2323

24+
/// The fallback directories are passed to linker, but not used when rustc does the search,
25+
/// because in the latter case the set of fallback directories cannot always be determined
26+
/// consistently at the moment.
27+
pub struct NativeLibSearchFallback<'a> {
28+
pub self_contained_components: LinkSelfContainedComponents,
29+
pub apple_sdk_root: Option<&'a Path>,
30+
}
31+
2432
pub fn walk_native_lib_search_dirs<R>(
2533
sess: &Session,
26-
self_contained_components: LinkSelfContainedComponents,
27-
apple_sdk_root: Option<&Path>,
34+
fallback: Option<NativeLibSearchFallback<'_>>,
2835
mut f: impl FnMut(&Path, bool /*is_framework*/) -> ControlFlow<R>,
2936
) -> ControlFlow<R> {
3037
// Library search paths explicitly supplied by user (`-L` on the command line).
@@ -38,6 +45,11 @@ pub fn walk_native_lib_search_dirs<R>(
3845
}
3946
}
4047

48+
let Some(NativeLibSearchFallback { self_contained_components, apple_sdk_root }) = fallback
49+
else {
50+
return ControlFlow::Continue(());
51+
};
52+
4153
// The toolchain ships some native library components and self-contained linking was enabled.
4254
// Add the self-contained library directory to search paths.
4355
if self_contained_components.intersects(
@@ -93,23 +105,17 @@ pub fn try_find_native_static_library(
93105
if os == unix { vec![os] } else { vec![os, unix] }
94106
};
95107

96-
// FIXME: Account for self-contained linking settings and Apple SDK.
97-
walk_native_lib_search_dirs(
98-
sess,
99-
LinkSelfContainedComponents::empty(),
100-
None,
101-
|dir, is_framework| {
102-
if !is_framework {
103-
for (prefix, suffix) in &formats {
104-
let test = dir.join(format!("{prefix}{name}{suffix}"));
105-
if test.exists() {
106-
return ControlFlow::Break(test);
107-
}
108+
walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
109+
if !is_framework {
110+
for (prefix, suffix) in &formats {
111+
let test = dir.join(format!("{prefix}{name}{suffix}"));
112+
if test.exists() {
113+
return ControlFlow::Break(test);
108114
}
109115
}
110-
ControlFlow::Continue(())
111-
},
112-
)
116+
}
117+
ControlFlow::Continue(())
118+
})
113119
.break_value()
114120
}
115121

@@ -132,22 +138,17 @@ pub fn try_find_native_dynamic_library(
132138
vec![os, meson, mingw]
133139
};
134140

135-
walk_native_lib_search_dirs(
136-
sess,
137-
LinkSelfContainedComponents::empty(),
138-
None,
139-
|dir, is_framework| {
140-
if !is_framework {
141-
for (prefix, suffix) in &formats {
142-
let test = dir.join(format!("{prefix}{name}{suffix}"));
143-
if test.exists() {
144-
return ControlFlow::Break(test);
145-
}
141+
walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
142+
if !is_framework {
143+
for (prefix, suffix) in &formats {
144+
let test = dir.join(format!("{prefix}{name}{suffix}"));
145+
if test.exists() {
146+
return ControlFlow::Break(test);
146147
}
147148
}
148-
ControlFlow::Continue(())
149-
},
150-
)
149+
}
150+
ControlFlow::Continue(())
151+
})
151152
.break_value()
152153
}
153154

0 commit comments

Comments
 (0)