Skip to content

Commit 4e73148

Browse files
committed
[temp] link_natively
1 parent da3c5d2 commit 4e73148

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+9-27
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ fn link_rlib<'a>(
354354
continue;
355355
};
356356
if flavor == RlibFlavor::Normal
357-
&& (sess.opts.unstable_opts.packed_bundled_libs || whole_archive == Some(true))
357+
&& (sess.opts.unstable_opts.packed_bundled_libs
358+
|| whole_archive == Some(true)
359+
|| lib.cfg.is_some())
358360
{
359361
let name = lib.filename.unwrap();
360362
let path = find_native_static_library(name.as_str(), true, &lib_search_paths, sess);
@@ -525,7 +527,7 @@ fn link_staticlib<'a>(
525527
false
526528
}),
527529
)
528-
.unwrap(); // It may be error?
530+
.unwrap();
529531

530532
let bundled: FxHashSet<_> = native_libs.iter().filter_map(|lib| lib.filename).collect();
531533
archive_builder_builder
@@ -2547,20 +2549,8 @@ fn add_static_crate<'a>(
25472549
cmd.link_rlib(&fix_windows_verbatim_for_gcc(path));
25482550
};
25492551

2550-
// See the comment above in `link_staticlib` and `link_rlib` for why if
2551-
// there's a static library that's not relevant we skip all object
2552-
// files.
2553-
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
2554-
let skip_native = native_libs.iter().any(|lib| {
2555-
matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
2556-
&& !relevant_lib(sess, lib)
2557-
});
2558-
let _ = skip_native;
2559-
let skip_native = false;
2560-
2561-
if (!are_upstream_rust_objects_already_included(sess)
2562-
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum))
2563-
&& !skip_native
2552+
if !are_upstream_rust_objects_already_included(sess)
2553+
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum)
25642554
{
25652555
link_upstream(cratepath);
25662556
return;
@@ -2591,17 +2581,13 @@ fn add_static_crate<'a>(
25912581
let is_rust_object =
25922582
canonical.starts_with(&canonical_name) && looks_like_rust_object_file(&f);
25932583

2594-
// If we've been requested to skip all native object files
2595-
// (those not generated by the rust compiler) then we can skip
2596-
// this file. See above for why we may want to do this.
2597-
let skip_because_cfg_say_so = skip_native && !is_rust_object;
2598-
25992584
// If we're performing LTO and this is a rust-generated object
26002585
// file, then we don't need the object file as it's part of the
26012586
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
26022587
// though, so we let that object file slide.
2603-
let skip_because_lto =
2604-
upstream_rust_objects_already_included && is_rust_object && is_builtins;
2588+
if upstream_rust_objects_already_included && is_rust_object && is_builtins {
2589+
return true;
2590+
}
26052591

26062592
// We skip native libraries because:
26072593
// 1. This native libraries won't be used from the generated rlib,
@@ -2612,10 +2598,6 @@ fn add_static_crate<'a>(
26122598
return true;
26132599
}
26142600

2615-
if skip_because_cfg_say_so || skip_because_lto {
2616-
return true;
2617-
}
2618-
26192601
false
26202602
}),
26212603
) {

compiler/rustc_metadata/src/native_libs.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,28 @@ fn find_bundled_library(
5959
name: Option<Symbol>,
6060
verbatim: Option<bool>,
6161
kind: NativeLibKind,
62+
cfg: &Option<rustc_ast::MetaItem>,
6263
sess: &Session,
6364
) -> Option<Symbol> {
64-
if sess.opts.unstable_opts.packed_bundled_libs &&
65-
sess.crate_types().iter().any(|ct| ct == &CrateType::Rlib || ct == &CrateType::Staticlib) &&
66-
let NativeLibKind::Static { bundle: Some(true) | None, .. } = kind {
65+
let NativeLibKind::Static { bundle: Some(true) | None, whole_archive } = kind else {
66+
return None
67+
};
68+
if !sess.crate_types().iter().any(|ct| ct == &CrateType::Rlib || ct == &CrateType::Staticlib) {
69+
return None;
70+
}
71+
if sess.opts.unstable_opts.packed_bundled_libs
72+
|| cfg.is_some()
73+
|| whole_archive.unwrap_or(false)
74+
{
6775
find_native_static_library(
6876
name.unwrap().as_str(),
6977
verbatim.unwrap_or(false),
7078
&sess.target_filesearch(PathKind::Native).search_path_dirs(),
7179
sess,
72-
).file_name().and_then(|s| s.to_str()).map(Symbol::intern)
80+
)
81+
.file_name()
82+
.and_then(|s| s.to_str())
83+
.map(Symbol::intern)
7384
} else {
7485
None
7586
}
@@ -393,7 +404,7 @@ impl<'tcx> Collector<'tcx> {
393404

394405
let name = name.map(|(name, _)| name);
395406
let kind = kind.unwrap_or(NativeLibKind::Unspecified);
396-
let filename = find_bundled_library(name, verbatim, kind, sess);
407+
let filename = find_bundled_library(name, verbatim, kind, &cfg, sess);
397408
self.libs.push(NativeLib {
398409
name,
399410
filename,
@@ -479,12 +490,12 @@ impl<'tcx> Collector<'tcx> {
479490
let new_name: Option<&str> = passed_lib.new_name.as_deref();
480491
let name = Some(Symbol::intern(new_name.unwrap_or(&passed_lib.name)));
481492
let sess = self.tcx.sess;
482-
let filename =
483-
find_bundled_library(name, passed_lib.verbatim, passed_lib.kind, sess);
493+
let kind = passed_lib.kind;
494+
let filename = find_bundled_library(name, passed_lib.verbatim, kind, &None, sess);
484495
self.libs.push(NativeLib {
485496
name,
486497
filename,
487-
kind: passed_lib.kind,
498+
kind,
488499
cfg: None,
489500
foreign_module: None,
490501
wasm_import_module: None,

0 commit comments

Comments
 (0)