Skip to content

Commit 601fc8b

Browse files
committed
[link] enable packed bundled lib in non stable cases
1 parent dffea43 commit 601fc8b

File tree

19 files changed

+146
-127
lines changed

19 files changed

+146
-127
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+39-96
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,9 @@ pub fn each_linked_rlib(
270270

271271
/// Create an 'rlib'.
272272
///
273-
/// An rlib in its current incarnation is essentially a renamed .a file. The rlib primarily contains
274-
/// the object file of the crate, but it also contains all of the object files from native
275-
/// libraries. This is done by unzipping native libraries and inserting all of the contents into
276-
/// this archive.
273+
/// An rlib in its current incarnation is essentially a renamed .a file (with "dummy" object files).
274+
/// The rlib primarily contains the object file of the crate, but it also some of the object files
275+
/// from native libraries.
277276
fn link_rlib<'a>(
278277
sess: &'a Session,
279278
archive_builder_builder: &dyn ArchiveBuilderBuilder,
@@ -347,44 +346,23 @@ fn link_rlib<'a>(
347346
// loaded from the libraries found here and then encode that into the
348347
// metadata of the rlib we're generating somehow.
349348
for lib in codegen_results.crate_info.used_libraries.iter() {
350-
match lib.kind {
351-
NativeLibKind::Static { bundle: None | Some(true), whole_archive: Some(true) }
352-
if flavor == RlibFlavor::Normal && sess.opts.unstable_opts.packed_bundled_libs => {}
353-
NativeLibKind::Static { bundle: None | Some(true), whole_archive: Some(true) }
354-
if flavor == RlibFlavor::Normal =>
355-
{
356-
// Don't allow mixing +bundle with +whole_archive since an rlib may contain
357-
// multiple native libs, some of which are +whole-archive and some of which are
358-
// -whole-archive and it isn't clear how we can currently handle such a
359-
// situation correctly.
360-
// See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897
361-
sess.emit_err(errors::IncompatibleLinkingModifiers);
362-
}
363-
NativeLibKind::Static { bundle: None | Some(true), .. } => {}
364-
NativeLibKind::Static { bundle: Some(false), .. }
365-
| NativeLibKind::Dylib { .. }
366-
| NativeLibKind::Framework { .. }
367-
| NativeLibKind::RawDylib
368-
| NativeLibKind::LinkArg
369-
| NativeLibKind::Unspecified => continue,
370-
}
371-
if let Some(name) = lib.name {
372-
let location =
349+
let NativeLibKind::Static { bundle: None | Some(true), whole_archive } = lib.kind else {
350+
continue;
351+
};
352+
if whole_archive == Some(true) && !codegen_results.crate_info.feature_packed_bundled_libs {
353+
sess.emit_err(errors::IncompatibleLinkingModifiers);
354+
}
355+
if flavor == RlibFlavor::Normal && let Some(filename) = lib.filename {
356+
let path = find_native_static_library(filename.as_str(), true, &lib_search_paths, sess);
357+
let src = read(path).map_err(|e| sess.emit_fatal(errors::ReadFileError {message: e }))?;
358+
let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src);
359+
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
360+
packed_bundled_libs.push(wrapper_file);
361+
} else if let Some(name) = lib.name {
362+
let path =
373363
find_native_static_library(name.as_str(), lib.verbatim, &lib_search_paths, sess);
374-
if sess.opts.unstable_opts.packed_bundled_libs && flavor == RlibFlavor::Normal {
375-
let filename = lib.filename.unwrap();
376-
let lib_path =
377-
find_native_static_library(filename.as_str(), true, &lib_search_paths, sess);
378-
let src = read(lib_path)
379-
.map_err(|e| sess.emit_fatal(errors::ReadFileError { message: e }))?;
380-
let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src);
381-
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
382-
packed_bundled_libs.push(wrapper_file);
383-
continue;
384-
}
385-
ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|error| {
386-
sess.emit_fatal(errors::AddNativeLibrary { library_path: location, error });
387-
});
364+
ab.add_archive(&path, Box::new(|_| false)).unwrap_or_else(|error| {
365+
sess.emit_fatal(errors::AddNativeLibrary { library_path: path, error })});
388366
}
389367
}
390368

@@ -516,36 +494,14 @@ fn link_staticlib<'a>(
516494
&codegen_results.crate_info,
517495
Some(CrateType::Staticlib),
518496
&mut |cnum, path| {
519-
let name = codegen_results.crate_info.crate_name[&cnum];
520-
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
521-
522-
// Here when we include the rlib into our staticlib we need to make a
523-
// decision whether to include the extra object files along the way.
524-
// These extra object files come from statically included native
525-
// libraries, but they may be cfg'd away with #[link(cfg(..))].
526-
//
527-
// This unstable feature, though, only needs liblibc to work. The only
528-
// use case there is where musl is statically included in liblibc.rlib,
529-
// so if we don't want the included version we just need to skip it. As
530-
// a result the logic here is that if *any* linked library is cfg'd away
531-
// we just skip all object files.
532-
//
533-
// Clearly this is not sufficient for a general purpose feature, and
534-
// we'd want to read from the library's metadata to determine which
535-
// object files come from where and selectively skip them.
536-
let skip_object_files = native_libs.iter().any(|lib| {
537-
matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
538-
&& !relevant_lib(sess, lib)
539-
});
540-
541497
let lto = are_upstream_rust_objects_already_included(sess)
542498
&& !ignored_for_lto(sess, &codegen_results.crate_info, cnum);
543499

544-
// Ignoring obj file starting with the crate name
545-
// as simple comparison is not enough - there
546-
// might be also an extra name suffix
547-
let obj_start = name.as_str().to_owned();
500+
let native_libs = codegen_results.crate_info.native_libraries[&cnum].iter();
501+
let relevant = native_libs.clone().filter(|lib| relevant_lib(sess, &lib));
502+
let relevant_libs: FxHashSet<_> = relevant.filter_map(|lib| lib.filename).collect();
548503

504+
let bundled_libs: FxHashSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
549505
ab.add_archive(
550506
path,
551507
Box::new(move |fname: &str| {
@@ -559,20 +515,25 @@ fn link_staticlib<'a>(
559515
return true;
560516
}
561517

562-
// Otherwise if this is *not* a rust object and we're skipping
563-
// objects then skip this file
564-
if skip_object_files
565-
&& (!fname.starts_with(&obj_start) || !fname.ends_with(".o"))
566-
{
518+
// Skip objects for bundled libs.
519+
if bundled_libs.contains(&Symbol::intern(fname)) {
567520
return true;
568521
}
569522

570-
// ok, don't skip this
571523
false
572524
}),
573525
)
574526
.unwrap();
575527

528+
archive_builder_builder
529+
.extract_bundled_libs(path, tempdir.as_ref(), &relevant_libs)
530+
.unwrap_or_else(|e| sess.emit_fatal(e));
531+
for filename in relevant_libs {
532+
let joined = tempdir.as_ref().join(filename.as_str());
533+
let path = joined.as_path();
534+
ab.add_archive(path, Box::new(|_| false)).unwrap();
535+
}
536+
576537
all_native_libs
577538
.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned());
578539
},
@@ -2590,18 +2551,8 @@ fn add_static_crate<'a>(
25902551
cmd.link_rlib(&fix_windows_verbatim_for_gcc(path));
25912552
};
25922553

2593-
// See the comment above in `link_staticlib` and `link_rlib` for why if
2594-
// there's a static library that's not relevant we skip all object
2595-
// files.
2596-
let native_libs = &codegen_results.crate_info.native_libraries[&cnum];
2597-
let skip_native = native_libs.iter().any(|lib| {
2598-
matches!(lib.kind, NativeLibKind::Static { bundle: None | Some(true), .. })
2599-
&& !relevant_lib(sess, lib)
2600-
});
2601-
2602-
if (!are_upstream_rust_objects_already_included(sess)
2603-
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum))
2604-
&& !skip_native
2554+
if !are_upstream_rust_objects_already_included(sess)
2555+
|| ignored_for_lto(sess, &codegen_results.crate_info, cnum)
26052556
{
26062557
link_upstream(cratepath);
26072558
return;
@@ -2632,17 +2583,13 @@ fn add_static_crate<'a>(
26322583
let is_rust_object =
26332584
canonical.starts_with(&canonical_name) && looks_like_rust_object_file(&f);
26342585

2635-
// If we've been requested to skip all native object files
2636-
// (those not generated by the rust compiler) then we can skip
2637-
// this file. See above for why we may want to do this.
2638-
let skip_because_cfg_say_so = skip_native && !is_rust_object;
2639-
26402586
// If we're performing LTO and this is a rust-generated object
26412587
// file, then we don't need the object file as it's part of the
26422588
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
26432589
// though, so we let that object file slide.
2644-
let skip_because_lto =
2645-
upstream_rust_objects_already_included && is_rust_object && is_builtins;
2590+
if upstream_rust_objects_already_included && is_rust_object && is_builtins {
2591+
return true;
2592+
}
26462593

26472594
// We skip native libraries because:
26482595
// 1. This native libraries won't be used from the generated rlib,
@@ -2653,10 +2600,6 @@ fn add_static_crate<'a>(
26532600
return true;
26542601
}
26552602

2656-
if skip_because_cfg_say_so || skip_because_lto {
2657-
return true;
2658-
}
2659-
26602603
false
26612604
}),
26622605
) {

compiler/rustc_codegen_ssa/src/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ impl CrateInfo {
858858
dependency_formats: tcx.dependency_formats(()).clone(),
859859
windows_subsystem,
860860
natvis_debugger_visualizers: Default::default(),
861+
feature_packed_bundled_libs: tcx.features().packed_bundled_libs,
861862
};
862863
let crates = tcx.crates(());
863864

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ pub struct CrateInfo {
159159
pub dependency_formats: Lrc<Dependencies>,
160160
pub windows_subsystem: Option<String>,
161161
pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
162+
pub feature_packed_bundled_libs: bool, // unstable feature flag.
162163
}
163164

164165
#[derive(Encodable, Decodable)]

compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files w
2222
2323
codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}
2424
25-
codegen_ssa_incompatible_linking_modifiers = the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
25+
codegen_ssa_incompatible_linking_modifiers = link modifiers combination `+bundle,+whole-archive` is unstable when generating rlibs
2626
2727
codegen_ssa_add_native_library = failed to add native library {$library_path}: {$error}
2828

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ declare_features! (
164164
(active, multiple_supertrait_upcastable, "CURRENT_RUSTC_VERSION", None, None),
165165
/// Allows using `#[omit_gdb_pretty_printer_section]`.
166166
(active, omit_gdb_pretty_printer_section, "1.5.0", None, None),
167+
/// Allows using `+bundled,+whole-archive` native libs.
168+
(active, packed_bundled_libs, "1.67.0", None, None),
167169
/// Allows using `#[prelude_import]` on glob `use` items.
168170
(active, prelude_import, "1.2.0", None, None),
169171
/// Used to identify crates that contain the profiler runtime.

compiler/rustc_metadata/src/native_libs.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ fn find_bundled_library(
4949
name: Option<Symbol>,
5050
verbatim: Option<bool>,
5151
kind: NativeLibKind,
52+
has_cfg: bool,
5253
sess: &Session,
5354
) -> Option<Symbol> {
54-
if sess.opts.unstable_opts.packed_bundled_libs &&
55-
sess.crate_types().iter().any(|ct| ct == &CrateType::Rlib || ct == &CrateType::Staticlib) &&
56-
let NativeLibKind::Static { bundle: Some(true) | None, .. } = kind {
57-
find_native_static_library(
58-
name.unwrap().as_str(),
59-
verbatim.unwrap_or(false),
60-
&sess.target_filesearch(PathKind::Native).search_path_dirs(),
61-
sess,
62-
).file_name().and_then(|s| s.to_str()).map(Symbol::intern)
63-
} else {
64-
None
55+
if let NativeLibKind::Static { bundle: Some(true) | None, whole_archive } = kind
56+
&& sess.crate_types().iter().any(|t| matches!(t, &CrateType::Rlib | CrateType::Staticlib))
57+
&& (sess.opts.unstable_opts.packed_bundled_libs || has_cfg || whole_archive == Some(true))
58+
{
59+
let verbatim = verbatim.unwrap_or(false);
60+
let search_paths = &sess.target_filesearch(PathKind::Native).search_path_dirs();
61+
return find_native_static_library(name.unwrap().as_str(), verbatim, search_paths, sess)
62+
.file_name()
63+
.and_then(|s| s.to_str())
64+
.map(Symbol::intern);
6565
}
66+
None
6667
}
6768

6869
pub(crate) fn collect(tcx: TyCtxt<'_>) -> Vec<NativeLib> {
@@ -385,7 +386,7 @@ impl<'tcx> Collector<'tcx> {
385386

386387
let name = name.map(|(name, _)| name);
387388
let kind = kind.unwrap_or(NativeLibKind::Unspecified);
388-
let filename = find_bundled_library(name, verbatim, kind, sess);
389+
let filename = find_bundled_library(name, verbatim, kind, cfg.is_some(), sess);
389390
self.libs.push(NativeLib {
390391
name,
391392
filename,
@@ -475,7 +476,7 @@ impl<'tcx> Collector<'tcx> {
475476
let name = Some(Symbol::intern(new_name.unwrap_or(&passed_lib.name)));
476477
let sess = self.tcx.sess;
477478
let filename =
478-
find_bundled_library(name, passed_lib.verbatim, passed_lib.kind, sess);
479+
find_bundled_library(name, passed_lib.verbatim, passed_lib.kind, false, sess);
479480
self.libs.push(NativeLib {
480481
name,
481482
filename,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ symbols! {
10491049
overlapping_marker_traits,
10501050
owned_box,
10511051
packed,
1052+
packed_bundled_libs,
10521053
panic,
10531054
panic_2015,
10541055
panic_2021,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# ignore-cross-compile
4+
# only-linux
5+
6+
# Make sure -Zpacked_bundled_libs-like behavior activates with whole-archive.
7+
8+
# We're using the llvm-nm instead of the system nm to ensure it is compatible
9+
# with the LLVM bitcode generated by rustc.
10+
NM = "$(LLVM_BIN_DIR)"/llvm-nm
11+
12+
all: $(call NATIVE_STATICLIB,native_dep_1) $(call NATIVE_STATICLIB,native_dep_2) $(call NATIVE_STATICLIB,native_dep_3) $(call NATIVE_STATICLIB,native_dep_4)
13+
# test cfg with packed bundle
14+
$(RUSTC) rust_dep_cfg.rs --crate-type=rlib -Zpacked_bundled_libs
15+
$(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep_cfg.rlib --crate-type=staticlib --cfg should_add
16+
$(AR) t $(TMPDIR)/librust_dep_cfg.rlib | $(CGREP) -e "libnative_dep_1.a"
17+
$(AR) t $(TMPDIR)/librust_dep_cfg.rlib | $(CGREP) -e "libnative_dep_2.a"
18+
$(AR) t $(TMPDIR)/libmain.a | $(CGREP) -e "libnative_dep_1.o"
19+
$(AR) t $(TMPDIR)/libmain.a | $(CGREP) -ev "libnative_dep_2.o"
20+
21+
22+
# test bundle with whole_archive
23+
$(RUSTC) rust_dep.rs --crate-type=rlib
24+
$(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -e "native_dep_1"
25+
$(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -e "native_dep_3"
26+
$(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -ev "native_dep_2"
27+
$(AR) t $(TMPDIR)/librust_dep.rlib | $(CGREP) -ev "native_dep_4"
28+
29+
# Make sure compiler doesn't use files, that it shouldn't know about.
30+
rm $(TMPDIR)/libnative_dep_1.a
31+
rm $(TMPDIR)/libnative_dep_3.a
32+
33+
$(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep.rlib --print link-args > $(TMPDIR)/link_args
34+
cat $(TMPDIR)/link_args | $(CGREP) -ev "native_dep_3"
35+
cat $(TMPDIR)/link_args | $(CGREP) -e "--whole-archive.*native_dep_1.*--whole-archive.*lnative_dep_2.*no-whole-archive.*lnative_dep_4"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate rust_dep;
2+
3+
pub fn main() {
4+
rust_dep::rust_dep();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int native_f1() { return 1; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int native_f2() { return 2; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int native_f3() { return 3; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int native_f4() { return 4; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(packed_bundled_libs)]
2+
3+
#[link(name = "native_dep_1", kind = "static", modifiers = "+whole-archive,+bundle")]
4+
extern "C" {}
5+
6+
#[link(name = "native_dep_2", kind = "static", modifiers = "+whole-archive,-bundle")]
7+
extern "C" {}
8+
9+
#[link(name = "native_dep_3", kind = "static", modifiers = "+bundle")]
10+
extern "C" {}
11+
12+
#[link(name = "native_dep_4", kind = "static", modifiers = "-bundle")]
13+
extern "C" {}
14+
15+
#[no_mangle]
16+
pub fn rust_dep() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(link_cfg)]
2+
3+
#[link(name = "native_dep_1", kind = "static", cfg(should_add))]
4+
extern "C" {}
5+
6+
#[link(name = "native_dep_2", kind = "static", cfg(should_not_add))]
7+
extern "C" {}
8+
9+
#[no_mangle]
10+
pub fn rust_dep() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
// compile-flags: -Zunstable-options --crate-type rlib
1+
// gate-test-packed_bundled_libs
2+
3+
// ignore-wasm32-bare
4+
// compile-flags: --crate-type rlib
5+
// error-pattern: link modifiers combination `+bundle,+whole-archive` is unstable when generating rlibs
26
// build-fail
3-
// error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
47

5-
#[link(name = "mylib", kind = "static", modifiers = "+bundle,+whole-archive")]
6-
extern "C" { }
8+
#[link(name = "rust_test_helpers", kind = "static", modifiers = "+bundle,+whole-archive")]
9+
extern "C" {}
710

8-
fn main() { }
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
error: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
1+
error: link modifiers combination `+bundle,+whole-archive` is unstable when generating rlibs
22

3-
error: could not find native static library `mylib`, perhaps an -L flag is missing?
4-
5-
error: aborting due to 2 previous errors
3+
error: aborting due to previous error
64

0 commit comments

Comments
 (0)