Skip to content

Commit da3c5d2

Browse files
committed
[temp] link_staticlib each_linked_rlib
1 parent c14e3af commit da3c5d2

File tree

1 file changed

+17
-30
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+17
-30
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+17-30
Original file line numberDiff line numberDiff line change
@@ -497,36 +497,12 @@ fn link_staticlib<'a>(
497497
let mut all_native_libs = vec![];
498498

499499
let res = each_linked_rlib(sess, &codegen_results.crate_info, &mut |cnum, path| {
500-
let name = codegen_results.crate_info.crate_name[&cnum];
501-
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
502-
503-
// Here when we include the rlib into our staticlib we need to make a
504-
// decision whether to include the extra object files along the way.
505-
// These extra object files come from statically included native
506-
// libraries, but they may be cfg'd away with #[link(cfg(..))].
507-
//
508-
// This unstable feature, though, only needs liblibc to work. The only
509-
// use case there is where musl is statically included in liblibc.rlib,
510-
// so if we don't want the included version we just need to skip it. As
511-
// a result the logic here is that if *any* linked library is cfg'd away
512-
// we just skip all object files.
513-
//
514-
// Clearly this is not sufficient for a general purpose feature, and
515-
// we'd want to read from the library's metadata to determine which
516-
// object files come from where and selectively skip them.
517-
let skip_object_files = native_libs.iter().any(|lib| {
518-
matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
519-
&& !relevant_lib(sess, lib)
520-
});
521-
522500
let lto = are_upstream_rust_objects_already_included(sess)
523501
&& !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
524502

525-
// Ignoring obj file starting with the crate name
526-
// as simple comparison is not enough - there
527-
// might be also an extra name suffix
528-
let obj_start = name.as_str().to_owned();
503+
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
529504

505+
let bundled: FxHashSet<_> = native_libs.iter().filter_map(|lib| lib.filename).collect();
530506
ab.add_archive(
531507
path,
532508
Box::new(move |fname: &str| {
@@ -540,17 +516,26 @@ fn link_staticlib<'a>(
540516
return true;
541517
}
542518

543-
// Otherwise if this is *not* a rust object and we're skipping
544-
// objects then skip this file
545-
if skip_object_files && (!fname.starts_with(&obj_start) || !fname.ends_with(".o")) {
519+
// Skip objects for bundled libs.
520+
if bundled.iter().any(|l| l.as_str() == fname) {
546521
return true;
547522
}
548523

549524
// ok, don't skip this
550525
false
551526
}),
552527
)
553-
.unwrap();
528+
.unwrap(); // It may be error?
529+
530+
let bundled: FxHashSet<_> = native_libs.iter().filter_map(|lib| lib.filename).collect();
531+
archive_builder_builder
532+
.extract_bundled_libs(path, tempdir.as_ref(), &bundled)
533+
.unwrap_or_else(|e| sess.emit_fatal(e));
534+
for name in bundled {
535+
let joined = tempdir.as_ref().join(name.as_str());
536+
let path = joined.as_path();
537+
ab.add_archive(path, Box::new(|_| false)).unwrap();
538+
}
554539

555540
all_native_libs.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned());
556541
});
@@ -2570,6 +2555,8 @@ fn add_static_crate<'a>(
25702555
matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
25712556
&& !relevant_lib(sess, lib)
25722557
});
2558+
let _ = skip_native;
2559+
let skip_native = false;
25732560

25742561
if (!are_upstream_rust_objects_already_included(sess)
25752562
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum))

0 commit comments

Comments
 (0)