emk / rust-musl-builder

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.
Apache License 2.0
1.54k stars 193 forks source link

Use Alpine Linux chroot to get a musl bundle with package management #6

Closed sybblow closed 6 years ago

sybblow commented 8 years ago

It's will be nice to use Alpine Linux chroot environment as the musl header and library bundle, so users can use package management tool to add C libraries instead of compile it themselves. They can just chroot into Alpine root and then use apk, or even use apk.static directly. Is there any technical issue to stop from doing that?

emk commented 8 years ago

Well, the big issue is that the Rust install lives outside the chroot, so it's hard to see both the compiler and the Alpine environment at the same time. Unless you only wanted the chroot for the install?

I'd be very interested in looking at a patch which implemented this. I use Alpine a fair bit, but I'm pretty clueless about how it handles static libraries.

sybblow commented 8 years ago

I mean just use Alpine root as bundle of musl C libraries. chroot is only needed when install package for Alpine root, we still use Rust toolchains outside. But we set the gcc to link to Alpine's musl C libraries. It's still a cross-compile, the only different is we can use Alpine's library binaries without need to build them ourselves. And with its package management and package build system, there may be easy library remove and packaging reuse.

I've done some experiment on it, base on the gcc spec file you put in this container environment. My host system is Fedora 24 with gcc 6, and simple hello world C program can be built statically with Alpine musl C (with '-static' option) in this way correctly.

But the problem is its cooperation with cmake and cargo. First, we need to specify many directory config pointed to Alpine root through system environment variable to make cmake work. And this seems to have lot's of problems, the libssh2-sys seems to be compiled improperly, I think I've already stuck on this.

I think maybe cargo has some options to solve this, but since cargo is too complicated, I haven't find a answer now.

emk commented 8 years ago

I can get cargo to do whatever we need it to do. My concern here is that I don't know how to get Alpine Linux to use static libraries exclusively. It might be pretty easy, but I've never looked.

Le 22 juil. 2016 10:24 PM, "Siliang Cao" notifications@github.com a écrit :

I mean just use Alpine root as bundle of musl C libraries. chroot is only needed when install package for Alpine root, we still use Rust toolchains outside. But we set the gcc to link to Alpine's musl C libraries. It's still a cross-compile, the only different is we can use Alpine's library binaries without need to build them ourselves. And with its package management and package build system, there may be easy library remove and packaging reuse.

I've done some experiment on it, base on the gcc spec file you put in this container environment. My host system is Fedora 24 with gcc 6, and simple hello world C program can be built statically with Alpine musl C (with '-static' option) in this way correctly.

But the problem is its cooperation with cmake and cargo. First, we need to specify many directory config pointed to Alpine root through system environment variable to make cmake work. And this seems to have lot's of problems, the libssh2-sys seems to be compiled improperly, I think I've already stuck on this.

I think maybe cargo has some options to solve this, but since cargo is too complicated, I haven't find a answer now.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/emk/rust-musl-builder/issues/6#issuecomment-234693906, or mute the thread https://github.com/notifications/unsubscribe-auth/AACQY1B-JHtoEZ35AU9lEjJyDkOs-CKUks5qYXtfgaJpZM4JRh1a .

emk commented 8 years ago

I'm leaving this bug open because I'd love to have some way to install static C libraries using apk and link them into Rust executables, but I don't have the time to figure it out myself. If somebody has a demonstration of how to make this work, please let me know.

sybblow commented 8 years ago

Alpine Linux has its own port of rust now. This can make the work much easier, but there still has the same problem as mentioned above: install static C libraries (which I think is not a big deal for most general libraries, e.g. openssl-dev), and link them into Rust executables (which I haven't found a way).

I also find some other guys are talking about Alpine Linux for static building here: Continous integration against musl-libc

jirutka commented 7 years ago

FYI, you can use alpine-chroot-install to easily install and use Alpine in chroot on CI, incl. emulation of other arch.

emk commented 7 years ago

@sybblow The Alpine Rust port was still 1.10 as of a few weeks ago. It's from before the ? operator landed. I wonder if they've updated to 1.13 or 1.14 stable yet? Once they start tracking the 6-week update cycle with some reasonable regularity, then I'll be happy to look at that approach.

@jirutka Interesting! But won't that chroot contain shared Alpine libraries and not static ones? One of my goals is a single binary that works on both Alpine and Ubuntu.

sybblow commented 7 years ago

I just suggest to use chroot Alpine Linux as a "filesystem tree" of musl libraries, you can still use offical glibc version rustc compiler and normal gcc in the "host" system as the toolchain, but just with some configurations to make the target to link with Alpine's musl static libraries.

It seems a bit hacky, but I thought it maybe a good way for cross-compiling musl target.

emk commented 6 years ago

Thank you for the suggestions!

Since nobody has any working code showing how this would actually work, and I certainly don't have either the time or need to tackle it myself, I'm going to close this issue. If anybody has working code, please feel free to submit a PR.

jirutka commented 6 years ago

@emk We have updated Alpine Rust port to the latest version some time ago (currently 1.22.1). Both static and dynamic linking against musl libc is fully working.

emk commented 6 years ago

@jirutka Nice!

For me, the big challenge isn't linking statically against musl-libc. It's figuring out how to link statically against OpenSSL and libpq, primarily. Does Alpine provide static versions of those libraries in an easy-to-link form?