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

libc should just be another library #26004

Open
Ericson2314 opened this issue May 22, 2017 · 22 comments
Open

libc should just be another library #26004

Ericson2314 opened this issue May 22, 2017 · 22 comments
Labels
0.kind: enhancement 1.severity: mass-rebuild 6.topic: cross-compilation Building packages on a different sort platform than than they will be run on 9.needs: community feedback

Comments

@Ericson2314
Copy link
Member

Ericson2314 commented May 22, 2017

Right now, libc is special: instead of just being another library dependency, it is passed to cc-wrapper where a bunch of magic happens for dynamic linking. I rather it just be another library with the caveat that if you don't depend on it dynamic linking won't work. Worst case, some of that dynamic-linking logic would go from the wrapper to the setup hook. mkDerivation would add that dependency by default, but packages (e.g. libc itself for obvious reasons) would opt out.

Relatedly, when bootstrapping (a new stdenv from a tarball or a cross stdenv), we current build "static" versions of the compiler that's aren't wrapped with libc, and then build libc, and then build the compiler again. At the very last, I'd like to just re-wrap the old compiler with libc and avoid the rebuild, but if we're not wrapping libc too, then let's just build and wrap the compiler once.

I assume there's a reason for all the double wrapping and double building we do now, but I don't know what it is.

CC @copumpkin @vcunat @DavidEGrayson @elitak @dmjio @eternaleye

@Ericson2314 Ericson2314 added 0.kind: enhancement 1.severity: mass-rebuild 6.topic: cross-compilation Building packages on a different sort platform than than they will be run on 9.needs: community feedback labels May 22, 2017
@DavidEGrayson
Copy link
Contributor

DavidEGrayson commented May 22, 2017

I have some knowledge of this from my experience building a cross-compiler to target Windows in Nix. If you can figure out how to build a GCC toolchain just once instead of building it twice, that would be cool, but I'm not sure it's supported by GCC.

When compiling a mingw-w64 cross-compiler, I know libstdc++ needs to see libwinpthreads when it is configuring itself so that it can use that library to implement things like std::future. That makes me believe that more generally, libstdc++ needs to see your libc and other basic system libraries when it is being configured. Since libstdc++ is a component of GCC and I am not sure if you can compile it on its own, that makes me think you have to get those libraries compiled with some sort of compiler before compiling the final GCC, so you need to build two different GCCs.

It's certainly possible I'm missing something here. If you want to make libc just be another library, you should make sure you can also have libstdc++ just be another library.

@Ericson2314
Copy link
Member Author

@DavidEGrayson thanks for that info! Sounds like to start, we'll still need to build GCC twice, but that shouldn't impede the cc-wrapper side of things--i.e. maybe the second GCC will build fine if it has libc as a normal dep instead of from the first gcc's wrapper script?

@eternaleye
Copy link

eternaleye commented May 23, 2017

@DavidEGrayson

Since libstdc++ is a component of GCC and I am not sure if you can compile it on its own

It's worth noting here that it is possible to split the build process of GCC considerably further than even that. In particular, Exherbo has managed to split out all of the following into distinct packages:

  • libatomic
  • libcilkrts
  • libgcc (for libgcc_s.so)
  • libgfortran
  • libgomp
  • libquadmath
  • libstdc++

All of these can be found in this directory of the arbor repository; gcc itself (with libgcov still bundled, as well as crt*.o and libgcc.a) can be found here.

Exherbo does still require rebuilding, however - the threads option on GCC requires the ability for the build process of the GCC package (in libgcc.a, I think) to link against the target libc, so one builds first without threads, then the target libc, then with threads. Building libgcc.a has not yet been factored out.

@Ericson2314
Copy link
Member Author

Oooo that's so tantalizingly close.

@eternaleye
Copy link

Yup. At very least, it does allow the second GCC to treat libc as a normal dep, right out of the gate.

@vcunat
Copy link
Member

vcunat commented May 23, 2017

The (main) motivation is reducing the amount of compiling when bootstrapping?

@Ericson2314
Copy link
Member Author

Ericson2314 commented May 23, 2017

@vcunat Yes that, because when cross compiling there's no cache to rely on. But also simplicity for simplicity's sake---I'm convinced that with enough blood sweat and tears, bootstrapping can become dramatically simpler.

Ericson2314 added a commit to obsidiansystems/nixpkgs that referenced this issue May 23, 2017
We need this ugliness for cross-building until we can boot GCC. See
NixOS#26004 for progress on that.
@vcunat
Copy link
Member

vcunat commented May 23, 2017

There are some cross tools in cache, I believe, mainly from http://hydra.nixos.org/jobset/nixpkgs/cross-trunk. If there are more cross-scenarios that are "considered supported/maintained", I think it would be good to put a few basic packages in there for them.

It's not just for having binaries, but also for easier keeping an eye on breakages (and it simplifies finding when they started).

@Ericson2314
Copy link
Member Author

Oh, I'm thinking of soon proposing we try to cross build the whole world for some platform, now that the infrastructure is getting good enough for that to be a realistic goal.

But for embedded stuff and other things, I still suspect people will be forced to fine-tune crossSystem breaking the cache.

@Ericson2314
Copy link
Member Author

#38451 would be easier to write if libc and the C compiler's runtime libs were propagatedBuildInputs, which is implied by this.

@Ericson2314
Copy link
Member Author

https://github.com/richfelker/musl-cross-make is some very interesting prior art.

@DavidEGrayson
Copy link
Contributor

Yes, I referred to that project when making my own cross-compilers for Linux, though I just stripped it down to the bare essentials and I was only concerned with static linking so I didn't try to get dynamic linking working.

@stale

This comment has been minimized.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 4, 2020
@Ericson2314
Copy link
Member Author

Still do

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 4, 2020
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/gcc-separation-of-language-frontends-and-the-print-prog-name-option/9116/3

@stale

This comment has been minimized.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 31, 2021
@Ericson2314
Copy link
Member Author

Still important to me!

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Apr 1, 2021
@stale
Copy link

stale bot commented Oct 1, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 1, 2021
@HariAmoor-professional
Copy link

HariAmoor-professional commented Jun 27, 2023

Can we do this with zig cc, which uses Ziglang to bootstrap CC and CXX? This adds a lot of benefits for hermeticity.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 27, 2023
@Atemu
Copy link
Member

Atemu commented Jun 27, 2023

Finally, all of this is especially relevant when it comes to common libraries like libc, libcxx, compiler-rt, tsan, libunwind. All these libraries are bundled in Zig in source form and gain native improvements when being built for your native target.

That does not sound like it'd improve the situation at all.

@HariAmoor-professional
Copy link

@Atemu The reason I figured it would is that a derivation could just download the binary for the underlying architecture instead of having to compile it

I could be wrong

@Ericson2314
Copy link
Member Author

Zig was inspired by Nixpkgs, so I think we're good.

Clang already does a good job #132343 will help GCC. and @emilytrau's bootstrapping work will make everything easier too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: enhancement 1.severity: mass-rebuild 6.topic: cross-compilation Building packages on a different sort platform than than they will be run on 9.needs: community feedback
Projects
None yet
Development

No branches or pull requests

7 participants