1
Fork 0

metadata: Ignore sysroot when doing the manual native lib search in rustc

This commit is contained in:
Vadim Petrochenkov 2025-03-09 14:06:51 +03:00
parent a96fa317d7
commit d577883f92
3 changed files with 46 additions and 47 deletions

View file

@ -22,7 +22,9 @@ use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_macros::LintDiagnostic; use rustc_macros::LintDiagnostic;
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file}; use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
use rustc_metadata::{find_native_static_library, walk_native_lib_search_dirs}; use rustc_metadata::{
NativeLibSearchFallback, find_native_static_library, walk_native_lib_search_dirs,
};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::lint::lint_level; use rustc_middle::lint::lint_level;
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile; use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
@ -2129,19 +2131,15 @@ fn add_library_search_dirs(
return; return;
} }
walk_native_lib_search_dirs( let fallback = Some(NativeLibSearchFallback { self_contained_components, apple_sdk_root });
sess, walk_native_lib_search_dirs(sess, fallback, |dir, is_framework| {
self_contained_components,
apple_sdk_root,
|dir, is_framework| {
if is_framework { if is_framework {
cmd.framework_path(dir); cmd.framework_path(dir);
} else { } else {
cmd.include_path(&fix_windows_verbatim_for_gcc(dir)); cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
} }
ControlFlow::<()>::Continue(()) ControlFlow::<()>::Continue(())
}, });
);
} }
/// Add options making relocation sections in the produced ELF files read-only /// Add options making relocation sections in the produced ELF files read-only

View file

@ -35,8 +35,8 @@ pub mod locator;
pub use creader::{DylibError, load_symbol_from_dylib}; pub use creader::{DylibError, load_symbol_from_dylib};
pub use fs::{METADATA_FILENAME, emit_wrapper_file}; pub use fs::{METADATA_FILENAME, emit_wrapper_file};
pub use native_libs::{ pub use native_libs::{
find_native_static_library, try_find_native_dynamic_library, try_find_native_static_library, NativeLibSearchFallback, find_native_static_library, try_find_native_dynamic_library,
walk_native_lib_search_dirs, try_find_native_static_library, walk_native_lib_search_dirs,
}; };
pub use rmeta::{EncodedMetadata, METADATA_HEADER, encode_metadata, rendered_const}; pub use rmeta::{EncodedMetadata, METADATA_HEADER, encode_metadata, rendered_const};

View file

@ -21,10 +21,17 @@ use rustc_target::spec::{BinaryFormat, LinkSelfContainedComponents};
use crate::{errors, fluent_generated}; use crate::{errors, fluent_generated};
/// The fallback directories are passed to linker, but not used when rustc does the search,
/// because in the latter case the set of fallback directories cannot always be determined
/// consistently at the moment.
pub struct NativeLibSearchFallback<'a> {
pub self_contained_components: LinkSelfContainedComponents,
pub apple_sdk_root: Option<&'a Path>,
}
pub fn walk_native_lib_search_dirs<R>( pub fn walk_native_lib_search_dirs<R>(
sess: &Session, sess: &Session,
self_contained_components: LinkSelfContainedComponents, fallback: Option<NativeLibSearchFallback<'_>>,
apple_sdk_root: Option<&Path>,
mut f: impl FnMut(&Path, bool /*is_framework*/) -> ControlFlow<R>, mut f: impl FnMut(&Path, bool /*is_framework*/) -> ControlFlow<R>,
) -> ControlFlow<R> { ) -> ControlFlow<R> {
// Library search paths explicitly supplied by user (`-L` on the command line). // Library search paths explicitly supplied by user (`-L` on the command line).
@ -38,6 +45,11 @@ pub fn walk_native_lib_search_dirs<R>(
} }
} }
let Some(NativeLibSearchFallback { self_contained_components, apple_sdk_root }) = fallback
else {
return ControlFlow::Continue(());
};
// The toolchain ships some native library components and self-contained linking was enabled. // The toolchain ships some native library components and self-contained linking was enabled.
// Add the self-contained library directory to search paths. // Add the self-contained library directory to search paths.
if self_contained_components.intersects( if self_contained_components.intersects(
@ -93,12 +105,7 @@ pub fn try_find_native_static_library(
if os == unix { vec![os] } else { vec![os, unix] } if os == unix { vec![os] } else { vec![os, unix] }
}; };
// FIXME: Account for self-contained linking settings and Apple SDK. walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
walk_native_lib_search_dirs(
sess,
LinkSelfContainedComponents::empty(),
None,
|dir, is_framework| {
if !is_framework { if !is_framework {
for (prefix, suffix) in &formats { for (prefix, suffix) in &formats {
let test = dir.join(format!("{prefix}{name}{suffix}")); let test = dir.join(format!("{prefix}{name}{suffix}"));
@ -108,8 +115,7 @@ pub fn try_find_native_static_library(
} }
} }
ControlFlow::Continue(()) ControlFlow::Continue(())
}, })
)
.break_value() .break_value()
} }
@ -132,11 +138,7 @@ pub fn try_find_native_dynamic_library(
vec![os, meson, mingw] vec![os, meson, mingw]
}; };
walk_native_lib_search_dirs( walk_native_lib_search_dirs(sess, None, |dir, is_framework| {
sess,
LinkSelfContainedComponents::empty(),
None,
|dir, is_framework| {
if !is_framework { if !is_framework {
for (prefix, suffix) in &formats { for (prefix, suffix) in &formats {
let test = dir.join(format!("{prefix}{name}{suffix}")); let test = dir.join(format!("{prefix}{name}{suffix}"));
@ -146,8 +148,7 @@ pub fn try_find_native_dynamic_library(
} }
} }
ControlFlow::Continue(()) ControlFlow::Continue(())
}, })
)
.break_value() .break_value()
} }