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

Added self-linking support to sys mode #1207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xanathar
Copy link
Contributor

@xanathar xanathar commented Aug 5, 2021

The purpose of this is to support a scenario where a single module is half implemented in C and half implemented in Rust (statically linked, resolving circular dependencies at the linker level).
This, while not ideal, is a likely possibility for a codebase which is in the process of migrating from C to Rust. (Note: caveats apply to the execution of cargo test, but a stub can be linked in that case to allow unit tests to run).

In this scenario, the sys bindings are generated correctly, but with a few caveats that need to be corrected:

  • The #[link(...)] attribute should not be emitted as there is no external library to link with... yet (the linker is linking everything in that precise moment)
  • The build.rs should not be generated for the same reason
  • The tests/ directory does no harm, but is superflous at that point

This adds a self_linking configuration key to the sys work mode that would skip the above parts, in order to support the self linking scenario I depicted without having to resort to post modification of the generated code.

I'm not sure if this fits the intended purpose of the gir tool, but I think it can be possibly useful to others.

@sdroege
Copy link
Member

sdroege commented Aug 27, 2021

Do you have an example project where this is useful?

@pbor
Copy link
Contributor

pbor commented Aug 30, 2021

We use Marco's patch in our project at work. The use case is the one Marco described, we have library we are incrementally porting to rust and we want to bind some of its API since the parts that we are migrating to rust need to call into the C part, however we want to keep generating a single .so/.dll.

@sdroege
Copy link
Member

sdroege commented Aug 31, 2021

Yeah I meant something public that we can link to for others to understand how this can be used :)

@@ -35,6 +35,13 @@ work_mode = "sys"
# and build.rs that generated only if not exists.
# Defaults to false
split_build_rs = false
# If true, then build.rs and tests do not get generated, and
# no #[link] attribute is emitted. This is supposed to be used
# when a library consists of an hybrid of C and Rust code, and
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# when a library consists of an hybrid of C and Rust code, and
# when a library consists of a hybrid of C and Rust code, and

Copy link
Contributor Author

@xanathar xanathar Nov 16, 2022

Choose a reason for hiding this comment

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

Fixed! Thanks

@MarijnS95
Copy link
Contributor

This sounds very similar to what I did for windows-rs in microsoft/windows-rs#1863, allowing some Rust code to request extern "system" fn(...) functions that are then provided by some other crate/source, also in Rust 🎉

@xanathar
Copy link
Contributor Author

Yeah I meant something public that we can link to for others to understand how this can be used :)

I'll try to resurrect this and provide the example, sorry for the huge delay.

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.

4 participants