-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support static linking crates #552
Comments
A small change of plans: |
just noticed that we don't need --shard and --static. --shared can just be the default.: *) rustc --lib foo.rc -o foo.so |
Part of this is implemented now. Some open questions: *) should we build a static rt library |
I believe this has bitrotted some but it's important to support. Needs a new champion. |
nominating for backwards compatible. it'll change enough about the command line and workspace that I think it's a BC risk. |
accepted for feature-complete milestone |
Visiting for triage. Still important; would be a great sorta-self-contained project for someone who's ready to jump in and sink their teeth into the compiler! |
High, but not 1.0 |
Probably related #9910 |
I've started to fix up the current static linking code here: https://github.com/Aatch/rust/tree/static-link That gets it back into a state where it will output a static crate. The code for loading a static crate was already there, so that works the same as before. I'd be interested to know if what the requirements are, if any, for this before I go much further. I think that the original idea of steering away from the standard extension (e.g. One thing I ran into was handling executables, since they can link to both static and dynamic libraries. This currently means that an executable will only work if built with |
I was thinking about this the other day, and here were some thoughts that I had about this:
I guess not all of these are entirely related to static linking, but it seems like we shouldn't get static linking just to get static linking working, but rather to maximize interoperability where static linking would be used. Right now I imagine that the use cases are
If we just get generation of libraries working, it's a good first step, but I think it's also important to not ignore these use cases. |
@alexcrichton For point 1 I do understand wanting to have a normal static archive, but we'd have to roll our own system for reading them on Linux anyway, since LLVM doesn't handle regular That's the motivation for using I don't see the use case of people linking to a rust crate from a non-rust system being particularly common. While I agree we shouldn't exclude it, if it's slightly tricky to set up, I can live with that. As for the rest of your points, they're kinda outside the scope of what I'm looking at right now, while I'm not just getting static linking to work for the sake of it, I'm not really thinking about the distribution of the core libraries at the moment. |
@Aatch I noticed your recent blog post about linking, as well as this comment, and I wanted to mention that I'm planning on working on the rustc linker model in the immediate future. It sounds like my plans are orthogonal to yours (I'm going to be cleaning up link args, focusing on linking with native libraries), so there's still plenty of space for you to contribute. Just wanted to let you know I'm going to be working in that area, though :-) |
This commit implements the support necessary for generating both intermediate and result static rust libraries. This is an implementation of my thoughts in https://mail.mozilla.org/pipermail/rust-dev/2013-November/006686.html. When compiling a library, we still retain the "lib" option, although now there are "rlib", "staticlib", and "dylib" as options for crate_type (and these are stackable). The idea of "lib" is to generate the "compiler default" instead of having too choose (although all are interchangeable). For now I have left the "complier default" to be a dynamic library for size reasons. Of the rust libraries, lib{std,extra,rustuv} will bootstrap with an rlib/dylib pair, but lib{rustc,syntax,rustdoc,rustpkg} will only be built as a dynamic object. I chose this for size reasons, but also because you're probably not going to be embedding the rustc compiler anywhere any time soon. Other than the options outlined above, there are a few defaults/preferences that are now opinionated in the compiler: * If both a .dylib and .rlib are found for a rust library, the compiler will prefer the .rlib variant. This is overridable via the -Z prefer-dynamic option * If generating a "lib", the compiler will generate a dynamic library. This is overridable by explicitly saying what flavor you'd like (rlib, staticlib, dylib). * If no options are passed to the command line, and no crate_type is found in the destination crate, then an executable is generated With this change, you can successfully build a rust program with 0 dynamic dependencies on rust libraries. There is still a dynamic dependency on librustrt, but I plan on removing that in a subsequent commit. This change includes no tests just yet. Our current testing infrastructure/harnesses aren't very amenable to doing flavorful things with linking, so I'm planning on adding a new mode of testing which I believe belongs as a separate commit. Closes rust-lang#552
"I don't see the use case of people linking to a rust crate from a non-rust system being particularly common." It's hard to say how common this would be given no major modern languages support this well. I have a hunch that there is a latent demand for libraries that are linkable to existing, large, C/C++ programs. |
This would be a very important feature. One of the reasons people don't implement libraries in Rust is precisely because you can't create a static (or dynamic) library that can easily be linked into a C program. |
@omalley You should have said "One of the reasons people don't implement libraries in Rust that are to be used within C and C++ programs ..." Otherwise you're stating an absolute that is false, even though I know what you mean. |
@omalley can you expand more about the difficulty? This shouldn't actually be hard to do. |
This issue is quite old and this statement is no longer true. You can create a static rust library today to link to a C program (we have many tests exercising this feature!). If you would like to create a static rust library, you should use the |
Solaris/Illumos: Add several more filio ioctls MIO needs these
Currently a crate is always compiled to a shared library. We should support both regular static linking and LTO. I think we should rename --shared to --lib. This is what it would look at:
*) rustc --lib --shared foo.rc -o foo.so
*) rustc --lib --static foo.rc -o foo.lib (really just a .o, but has a different ending)
*) rustc --lib --static --emit-llvm foo.rc -o foo.bclib (really just a .bc, but has a different ending)
rustc then looks for .lib and .bclib when resolving use.
The text was updated successfully, but these errors were encountered: