Skip to content
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

psm: allow manual opt out of #cfg[link("psm_s")] #95

Merged
merged 1 commit into from
Sep 1, 2024

Conversation

thoughtpolice
Copy link
Contributor

Currenty, when using the asm configuration, the lib.rs file will manually add a link("psm_s") attribute to the build causing a flag like -lpsm_s to appear on the link line. psm_s is the library of assembly code, built by build.rs in Cargo projects.

However, when using Cargo packages with build systems like Buck2, we have to manually replace the build.rs script, and build the bits of C/assembly code that come with psm and stacker as a separate build item (an actual library), then link them into the Rust libraries.

The name of the library often can't be easily made identical to psm_s as desired by the link() call, but we're building and explicitly linking the library anyway, removing the need for this.

Therefore, introduce a new asm_manual_link cfg option to give "expert" users the ability to link in code manually.

@nagisa
Copy link
Member

nagisa commented Aug 26, 2024

Since you're replacing the build.rs script anyway, is there any reason why you aren't modifying the build.rs script to also not set the cfg(asm)? Or rather, do you think it would be reasonable enough to keep this all inside the build.rs, possibly by splitting cfg(asm) into two cfgs, one of which would only control emission of the #[link] attribute?

Err, well, I shouldn't be looking at code when its one past midnight outside. I'll look at this again tomorrow.

Copy link
Member

@nagisa nagisa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I think would be an improvement here is to invert the newly added cfg here, have the build.rs script enable it whenever this link(...) attribute has to be emitted.

So the code in psm/src/lib.rs looks more like

#![cfg_attr(link_asm, link(name="psm_s"))]

and then in your buck2-specific build.rs you can remove the print that enables the cfg. Since build.rs is going to have a println! to enable emission of this attribute, that is going to be a perfect location to also add a short comment motivating the split of attributes (just one sentence is fine.)


That said, there is one important detail. Not using link(...) on extern is actually depriving rustc of sufficient information to correctly invoke the linker. Unless you're invoking the linker directly, omitting this isn't actually correct.

@thoughtpolice
Copy link
Contributor Author

OK, thanks! I wasn't sure if you wanted it on or off by default, but I can make that change.

That said, there is one important detail. Not using link(...) on extern is actually depriving rustc of sufficient information to correctly invoke the linker. Unless you're invoking the linker directly, omitting this isn't actually correct.

Yeah, speaking briefly, in Buck the dependency graph might look something like cxx_library() -> rust_library() -> rust_binary() to show that libpsm_s is depended on by (rust crate) psm and then used by some app. Buck and Bazel are smart enough to have the rust_library() "propagate" the cxx_library() dependencies it has, so even if you only list the rust library, you will also link against the cxx library — which is exactly what happens here. So by simply depending on the psm crate I'll get the proper bits built and installed.

The real thing is mainly just that the library name can't be made identical to psm_s for "reasons" and this is probably the easiest fix among a handful of places to do it (e.g. maybe I could fix it in the upstream Buck2 rule? But it's a lot harder and this works with older versions of Buck2.) So this has to be disabled for that reason alone, if nothing else.

Currenty, when using the `asm` configuration, the `lib.rs` file will manually
add a `link("psm_s")` attribute to the build causing a flag like `-lpsm_s`
to appear on the link line. `psm_s` is the library of assembly code, built by
`build.rs` in Cargo projects.

However, when using Cargo packages with build systems like Buck2, we have to
manually replace the `build.rs` script, and build the bits of C/assembly code
that come with `psm` and `stacker` as a separate build item (an actual library),
then link them into the Rust libraries.

The name of the library often can't be easily made identical to `psm_s` as
desired by the `link()` call, meaning that just blindly compiling this crate
causes an inevitable linking failure. But we're building the code and explicitly
linking the library anyway, removing the need for this `#[link]` clause.

Therefore, introduce a new `link_asm` cfg option to give "expert" users the
ability to link in code manually. This is always enabled by the Cargo build for
`asm`-compatible targets, but Buck users can leave it off.

Signed-off-by: Austin Seipp <aseipp@pobox.com>
@thoughtpolice
Copy link
Contributor Author

Fixed with your review comments.

@nagisa nagisa merged commit d0c34d5 into rust-lang:master Sep 1, 2024
131 of 143 checks passed
@thoughtpolice thoughtpolice deleted the aseipp/push-rttlnkoktlyt branch September 1, 2024 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants