-
Notifications
You must be signed in to change notification settings - Fork 919
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
Build static binaries on Linux using musl #2110
Conversation
e6a4a4c
to
f9b1733
Compare
89856f8
to
cebb135
Compare
d9fad20
to
33033d8
Compare
Note to self: this is failing on Windows and MacOS because the files src/exit/_Exit.c and src/unistd/_exit.c conflict with each other on a case-insensitive file system (only the base name is used, hence the conflict). |
d55dacc
to
0dddf24
Compare
This is now ready for review. |
From Windows (git bash shell):
In other words, it's possible to cross-compile Linux binaries from Windows without installing any extra tools, even including CGo support! |
Merge conflicts need resolution, please. |
This PR has some failing tests now, @aykevl |
Yeah, there was a non-code conflict with #2135. Should be fixed now. |
This seems good to me, but not really having used musl much, I am no authority. Anyone else like to comment on this PR before we merge it? |
Oops, another merge conflict @aykevl |
This brings a bit more consistency to libc configuration. It seems better to me to set the header flags all in the same place, instead of some in Go code and some in JSON target files (depending on the target).
This is really just a preparatory commit for musl support. The idea is to store not just the archive file (.a) but also an include directory. This is optional for picolibc but required for musl, so the main purpose of this commit is the refactor needed for this change.
This is very useful for debugging.
MacOS X 10.14 has a soft limit of 256 open files by default, at least on CircleCI. So don't keep object files open while writing the ar file to reduce the number of open files at once. Context: the musl libc has more than 256 object files in the .a file. This resulted in the error "too many open files" on MacOS X 10.14 when running in CircleCI.
This commit adds support for musl-libc and uses it by default on Linux. The main benefit of it is that binaries are always statically linked instead of depending on the host libc, even when using CGo. Advantages: - The resulting binaries are always statically linked. - No need for any tools on the host OS, like a compiler, linker, or libc in a release build of TinyGo. - This also simplifies cross compilation as no cross compiler is needed (it's all built into the TinyGo release build). Disadvantages: - Binary size increases by 5-6 kilobytes if -no-debug is used. Binary size increases by a much larger margin when debugging symbols are included (the default behavior) because musl is built with debugging symbols enabled. - Musl does things a bit differently than glibc, and some CGo code might rely on the glibc behavior. - The first build takes a bit longer because musl needs to be built. As an additional bonus, time is now obtained from the system in a way that fixes the Y2038 problem because musl has been a bit more agressive in switching to 64-bit time_t.
I've rebased the PR again, thanks! Now I can also see exactly how much space musl takes up:
(I've already found a quick way to avoid ~1kB in musl, but that's for a separate PR. I'm pretty sure more can be avoided with some optimizations). |
For sure a major accomplishment for cross-compiling. Now merging, thanks @aykevl |
Can we pre-build it, like |
Definitely. It's simply not implemented. |
Ah, perhaps that explains why even though I built the libraries into |
This PR adds support for musl-libc and uses it by default on Linux. The main benefit of it is that binaries are always statically linked instead of depending on the host libc, even when using CGo.
Advantages:
Disadvantages:
-no-debug
is used. Binary size increases by a much larger margin when debugging symbols are included (the default behavior) because musl is built with debugging symbols enabled.CGo doesn't work properly yet. Not quite sure how to best fix that without a somewhat big change. However, it's possible to work around that by first building a non-CGo binary and then building a CGo binary (the issue is that the libc is built after processing CGo headers).