oxidize-rb / rb-sys

Easily build Ruby native extensions in Rust
https://oxidize-rb.github.io/rb-sys/
Apache License 2.0
222 stars 34 forks source link

static linking ruby for debian #334

Closed WeepingClown13 closed 5 months ago

WeepingClown13 commented 6 months ago

Hi, I am trying to package rb-sys for debian. In fact, I already have a basically finished packaging. To make the tests work, I had to introduce the line println!("cargo:rustc-link-lib=dylib=ruby-3.1"); to build/main.rs, which solved the issues I was facing. But I am facing some problems when it comes to the ruby-static feature. rb-sys builds and passes tests fine because it does not need ruby-static for anything, but when I try to package the magnus crate, it searches for ruby-3.1-static, which, as I understand it, is caused by needing ruby-static feature from rb-sys dependency, albeit indirectly. So my understanding is that I probably need to provide a static link as well in rb-sys. I have tried some thing but linkers truly aren't my strong suit. So I'd appreciate it I can get the way to do that static linking from here. Or am I misunderstanding something and is this something I need to work out in magnus?

ianks commented 6 months ago

In magnus, you can enable the embed feature which will do the linking for you. That should "Just Work" out of the box, with no need to configure rb-sys.

As far as I know, libruby3.1 does not package libruby-static. If that's the case, ruby-static feature will not work.

needing ruby-static feature from rb-sys dependency

Magnus does not need this feature. It is optional, in the case that both libruby.so and libruby-static.a exist (i.e. if Ruby is built with --enable-install-static-library) and you want to ensure libruby-static.a is statically linked.

had to introduce the line println!("cargo:rustc-link-lib=dylib=ruby-3.1");

You shouldn't need to do this necessarily, that what the link-ruby feature in rb-sys is for (which is actually what magnus uses for the embed feature as well).

Without knowing more, my best guess is that you should avoid the ruby-static stuff, and try to get things working with:

Although it's been awhile since I've packaged a .deb, if you have code you can share I'm happy to take a look so I can understand the problem a bit more.

WeepingClown13 commented 6 months ago

@ianks thanks, that makes things much more clearer. That said, I am rather new to rust and even more so with linkers, so I am struggling a bit. With this update here, I am trying to rebuild rb-sys with "link-ruby" as a default feature, while keeping in mind that this may not be the proper solution at all. I am finding some uninstallable debs at the moment (probably some mirror problems) and hence am unable to continue for now, but you can see my initial packaging specifics here. That said, debian rust team follows an entirely different workflow so perhaps this is not so much help unless you are familiar with it. Nonetheless, at least some basic info should be visible.

WeepingClown13 commented 6 months ago

update: I was able to successfully rebuild rb-sys with link-ruby as a default feature, without needing the build/main.rs patch. I wasn't able to work on magnus due to some technical difficulties but I assume enabling embed would work there as well. Previously I thought embed in magnus needed ruby-static to work.

On another note, I wanted to clear another doubt. It is safe to assume that rb-sys can't do much without installing the ruby-dev package in debian, right? (ruby C headers as well as libruby.so comes from this). It is needed as a build dependency, but I was wondering whether I should mark it as a dependency as well in the debian package.

edit: I guess the latter question applies to rb-sys-build as well

ianks commented 6 months ago

rb-sys does have a hard dependency on libruby (i.e. it cannot compile ruby on its own). rb-sys-build as well.

WeepingClown13 commented 5 months ago

@ianks can you confirm that whether it is only libruby.so that is needed or other.so files as well? Because if it is the latter, that'd require a dependency on both ruby-dev and libruby in debian context as ruby3.1-dev includes libruby3.1.so while libruby3.1 installs the other .so files (it also installs a libruby3.1.so.3.1 iirc). Also it seems that enabling link-ruby (adding it to the default) does not work now. It seems that it is unable to link ruby, but the patch I added does still work.

EDIT: Please ignore this as I have figured out, that it was a dumb question. ruby-dev depends on libruby-dev in the first place and libruby-3.1.so is just a symlink to libruby-3.1.so.3.1.2 provided by libruby. And for link-ruby, it does work but the way I have been using it was wrong.