-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
rlibs retain reference to proc-macro dependencies - possibly unnecessary? #73047
Comments
Proc macro crate is a dynamic library + metadata. The dynamic library part is used for actually executing the macro during compilation of the rlib (via As for reexports, the proc macro dependencies can be dropped if nothing was actually reexported. These optimization was recently removed in #69432. As for spans, the proc macro dependencies can also be dropped if no spans from them are "reexported" through the rlib, but it's not a trivial optimization and it needs some design work (#69976 (comment)). If proc macros were produced as two files (e.g. |
Thanks for the reply and especially for the links to the existing PRs and issues from which I have learned a lot :) @petrochenkov, I'm not sure it's especially useful for this issue to remain open, as it sounds like the extra dependencies here are a known issue and are hard to solve, so feel free to close this issue if you like. |
Yeah, closing as not immediately actionable. |
Summary
If a Rust
.rlib
project depends on a procedural macro crate, then the producedrlib
will forever depend on the crate containing the proc macro, as shown byrustc -Z ls <path to rlib>
. When creating downstream binaries, e.g. executables or staticlibs,rustc
will require the location of the proc-macro to be provided as an-Ldependency
argument.I don't believe that the downstream
rustc
necessarily needs to know about the proc-macro. I have two bits of evidence:rlib
into a functional executable without providing the location of the proc-macro on which it depends.Why this might matter
Perhaps there's an opportunity for some dependency tree pruning here? The procedural macro would obviously be needed for initial compilation of the
rlib
, but may not be needed for downstream builds which depend on thatrlib
.I can't think of a circumstance when this would allow more parallelism, but perhaps in some cases of pipelined compilation (rust-lang/cargo#6883) there might be some kind of saving?
And even if not, if these dependencies can be removed, it's presumably just fewer crates for
rustc
to search through and analyze so might marginally speed those downstreamrustc
invocations.Unless of course the proc-macro is sometimes required by the linker steps, e.g. if LTO is enabled. In which case ignore this issue.
Test case
See https://github.com/adetaylor/repro-unexpected-macro-dependency.
This contains three ways of building the same code, in the
clientc
directory:build-ok-1.sh
: this usesclang
(or specificallylld
) to link the rlibs directly into the final C executable. This is unsupported, but works. The linker command has no reference to the procedural macro, proving that it isn't necessary in the final linking step.build-ok-2.sh
: this usesrustc
to make all the Rust code into astaticlib
then links that into the C executable. In this case,rustc
needs to be told the location of the procedural macro.build-fails.sh
: this also usesrustc
to make all the Rust code into astaticlib
then links that into the C executable. In this case, therustc
invocation isn't given the location of the macro, so building thestaticlib
fails.In each case, we're building the final executable for ARM (Android), whilst the macro is built for the host OS (in my case x64). This of course proves that no part of the macro is actually linked into the final binary.
The dependency chain is thus:
[C executable] -> <staticlib_crate, omitted in build-ok-1.sh> -> [rlib_crate] -> [macro_crate]
Unfortunately, this means for the scripts to work, you'll need a cross-compiling clang toolchain. I used one from a handy copy of Chromium, but you can alter the scripts to point to an Android NDK or similar.
rustc 1.45.0-nightly (fa51f810e 2020-04-29)
The text was updated successfully, but these errors were encountered: