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

Add --static compiler option #4872

Merged
merged 1 commit into from Aug 23, 2017
Merged

Conversation

RX14
Copy link
Contributor

@RX14 RX14 commented Aug 22, 2017

When attempting to statically link the compiler, we need to statically link llvm. This means we need to pass --link-static to llvm-config. This is achieved by adding an official --static compiler option, which both adds -static to the cc invocation, and sets a "static" compiler flag. This is utilised in lib_llvm.cr to add --link-static to our llvm-config invocation.

When attempting to statically link the compiler, we need to statically link
llvm. This means we need to pass --link-static to llvm-config. This is achieved
by adding an official --static compiler option, which both adds -static to the
cc invocation, and sets a "static" compiler flag. This is utilised in
lib_llvm.cr to add --link-static to our llvm-config invocation.
@lbguilherme
Copy link
Contributor

We should probably ensure that specs pass on a static build on CI. Or at least assert that the spec can compile.

@akzhan
Copy link
Contributor

akzhan commented Aug 22, 2017

Most wanted infrastructure feature.

@RX14
Copy link
Contributor Author

RX14 commented Aug 22, 2017

I'm submitting this and #4874 because I've automated building a statically-linked portable omnibus, and I'm upstreaming the patches which made it buildable. I hope that this can replace the current fragile omnibus with something a bit more robust.

The source code is available here: https://github.com/RX14/crystal-dist-static. Simply run ./build.sh with docker installed and you should get a nice omnibus. Unfortunately shards isn't installed in it just yet but everything else should work.

@ysbaddaden
Copy link
Contributor

@RX14 until #4276 is fixed, this compiler will work, but segfault whenever trying to raise an exception (actually trying to unwind the stack). we could detect that musl was statically linked, thought, and abort instead of raising.

@rubyFeedback
Copy link

rubyFeedback commented Jun 1, 2019

Hey crystal folks,

Could this information be included into wiki + main tutorial too? Perhaps some advanced section or something like this.

I'll explain why next.

First, searching via google, I found this link:

https://github.com/crystal-lang/crystal/wiki/Static-Linking

This is a stub though; might be useful to extend it.

Anyway - I am a ruby person (no surprise with this nick, I only use the github account to provide feedback and sometimes discuss a few things), but due to various reasons I need a "base"
system that is, on the one hand ideally small; and on the other hand ideally as static as possible.

For example, I have static binaries for sed, make, most of coreutils + binutils, awk/gawk and so
forth. I do this often for various reasons, including compiling a new glibc, and compiling things from source. I use ruby for this right now.

However had, it is a bit difficult to get a statically compiled version of ruby; even a statically
compiled version of python is not trivial - there are instructions here https://wiki.python.org/moin/BuildStatically but it did not work for me. :(

So my next step was ... "what if I could just use crystal, rather than struggle with python and
ruby here". But I would need a self-contained crystal version as much as possible. A bit like
busybox, which actually has a menu-config option for a statically compiled busybox (and
that one works very, very well, by the way).

I had a look in this thread; and also other threads but the information is a bit ... dispersed
and not trivial to find. And I am not sure if it is easily possible - yes, it may be possible,
and for those who have the knowledge it may be simple, but right now I am not in that
state.

My goal would then be to actually look at the ruby code that I need, and see how to then
port it to crystal (which should not be that difficult; I only need a small part of it e. g.
the logic that I need for automatically compiling everything from source, which already
works fine in my ruby scripts).

So essentially what I am asking here - would some heroic person write some documentation
about how to get things to work, e. g. a statically compiled version of crystal? I can get things
to compile from source just fine (including llvm+clang), but I am a bit stuck in the step towards
getting a statically compiled variant of crystal. I am fine with omitting addons by the way -
this is really more of a "minimal static crystal" then a "batteries included" approach. I sort
of need a better language than e. g. bash stuff.

The two ideal places I can think of would be a) the crystal wiki and perhaps b) the official
tutorial (perhaps an advanced subsection); wiki may be better initially, because people
could perhaps contribute to it. Right now the wiki is a bit lacking information - actually there
is more information in the github issues, but these are a little bit chaotic to follow, which is
why a somewhat short entry about this on the wiki may be useful - hopefully also to more
folks, since I am evidently not the only one who may want to have such a use case. (For
anyone wondering - I also do LFS/BLFS builds, and it helps a lot if I can automate steps
in an ideally as-failsafe-manner as possible, rather than do the manual copy/paste job
of pasting the instructions into the terminal as-is; once I have a working base system,
batch-compiling the rest is quite easy actually).

@straight-shoota
Copy link
Member

@rubyFeedback The distributed linux binaries for Crystal are already fully statically linked. If you want to compile it yourself, you can use our distribution-script (or adapt the steps to the build system of your choice).

I've added a few more information to the Static Linking wiki page. A fully-featured tutorial would be nice, but that's another story.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants