rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.37k stars 12.59k forks source link

Tracking Issue for `feature(packed_bundled_libs)` #108081

Open petrochenkov opened 1 year ago

petrochenkov commented 1 year ago

This is a tracking issue for the fix for issue "Linking modifier whole-archive and link time cfg are not supported on bundled static libraries" (https://github.com/rust-lang/rust/issues/99429). The feature gate for the issue is #![feature(packed_bundled_libs)].

Currently to combine +bundle native libraries with +whole-archive or cfg a new representation of bundled native libraries in a rlib used, in which the library is packed into the archive as a whole (and wrapped into an object file for technical reasons), and then unpacked when you need to link to it.

Other representations are also possible, e.g. keeping the bundled lib in the old "unpacked" format as a set of object files, and adding some description file telling which object files belong to which library, it would be more complex though.

It may be quite reasonable to support both packed and unpacked representations, both have their uses

Stabilizing the +bundle,+whole-archive combination doesn't need specifying any of these details, we just need to promise that it somehow works.

About tracking issues

Tracking issues are used to record the overall progress of implementation. They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions. A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature. Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.

Steps

Unresolved Questions

None yet.

Implementation history

petrochenkov commented 1 year ago

Future directions:

petrochenkov commented 1 year ago

So I think we need to eventually switch everything to using the new (packed) format, e.g. on 2024 edition, because it's a slightly breaking change.

I think the main goal is to fully preserve static library semantic, in that case

For dealing with the breakage in https://github.com/rust-lang/rust/pull/102832 we need:

Be-ing commented 1 year ago

We have an interesting use case for +bundle,+whole-archive in CXX-Qt. We have a crate cxx-qt-build which is used by downstream build.rs scripts. Under the hood, cxx-qt-build generates and compiles C++ code (using the cc crate) that contains static variables that aren't referenced from within main (for several purposes, including Qt's resource system and QML plugin registration), so that code needs to be linked with +whole-archive to be included in the final staticlib/bin. Typically cxx-qt-build is used in a single staticlib crate, which works fine (except on Rust 1.69 due to https://github.com/rust-lang/rust/issues/110912 which was fixed in 1.70). However, we'd like to be able to support having multiple rlib crates using cxx-qt-build in the Rust dependency graph. A proof-of-concept of such an arragement (https://github.com/KDAB/cxx-qt/pull/598) fails to build with:

error: link modifiers combination `+bundle,+whole-archive` is unstable when generating rlibs

I'm not sure how the discussion above about packed versus unpacked rlibs relates to this. Somehow, we need a way to link a library (or object files) built by the cc crate in build.rs to an rlib crate and have those symbols included in the final downstream staticlib and bin products produced by Cargo.

petrochenkov commented 1 year ago

I'm not sure how the discussion above about packed versus unpacked rlibs relates to this.

"Packed" supports +bundle,+whole-archive, "unpacked" do not.

The discussion above is mainly about enabling "packed" by default for everything. For the +bundle,+whole-archive and +bundle, cfg(...) cases we mostly can just stabilize it, from what I remember.

Be-ing commented 1 year ago

For the +bundle,+whole-archive and +bundle, cfg(...) cases we mostly can just stabilize it, from what I remember.

Do you mean automatically turning on packed_bundled_libs when +whole-archive and +bundle are used together?

petrochenkov commented 1 year ago

@Be-ing Yes. This case is not affected by any backward compatibility issues because it produced errors previously.