| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 1 | musl-host-gcc |
| 2 | ============= |
| 3 | |
| Leopold | bc93c2b | 2023-01-14 13:12:23 +0100 | [diff] [blame^] | 4 | musl-host-gcc is a Bazel C++ toolchain that uses the sandbox sysroot gcc in combination with a pre-built musl, musl headers, and Linux headers. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 5 | |
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 6 | It is currently used to build the few C binaries we need on Metropolis nodes. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 7 | |
| Leopold | bc93c2b | 2023-01-14 13:12:23 +0100 | [diff] [blame^] | 8 | At some point, this toolchain should be improved to directly consume a static compiler toolchain and sysroot, so we can eventually get rid of the sandbox (like Aspect's [gcc-toolchain](https://github.com/aspect-build/gcc-toolchain) is doing). |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 9 | |
| 10 | Usage |
| 11 | ----- |
| 12 | |
| Leopold | 79c13a2 | 2023-01-04 23:15:13 +0100 | [diff] [blame] | 13 | To use this toolchain explicitly while building a `cc_binary`, do: |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 14 | |
| Leopold | bc93c2b | 2023-01-14 13:12:23 +0100 | [diff] [blame^] | 15 | bazel build --platforms=//build/platforms:linux_amd64_static //foo/bar |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 16 | |
| Leopold | bc93c2b | 2023-01-14 13:12:23 +0100 | [diff] [blame^] | 17 | During an actual build however, the right toolchain should be selected using transitions |
| 18 | or other configuration mechanisms. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 19 | |
| 20 | Building Toolchain Sysroot Tarball |
| 21 | ---------------------------------- |
| 22 | |
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 23 | The toolchain's musl/linux components are currently built ahead of time and committed to this repository as `//build/toolchain/musl-host-gcc/toolchain.tar.xz`. This is the 'sysroot' tarball, that contains all headers and libraries required to build for Metropolis nodes. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 24 | |
| 25 | To build this tarball, run the following commands: |
| 26 | |
| 27 | bazel build //build/toolchain/musl-host-gcc/sysroot |
| 28 | cp -f bazel-bin/build/toolchain/musl-host-gcc/sysroot/sysroot.tar.xz build/toolchain/musl-host-gcc/sysroot.tar.xz |
| 29 | |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 30 | As a temporary hack the compiler-specific headers of our current development container have been manually merged in. This is expected to be replaced by a proper LLVM-based toolchain. |
| 31 | |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 32 | Internals |
| 33 | --------- |
| 34 | |
| 35 | The toolchain is implemented in the following way: |
| 36 | |
| Leopold Schabel | 0ea448a | 2022-03-16 13:49:32 +0100 | [diff] [blame] | 37 | 1. `//build/toolchain/musl-host-gcc/sysroot` is used to build `//build/toolchain/musl-host-gcc/sysroot.tar.xz` which is a tarball that contains all include and binary library files for building against musl for Metropolis nodes (x86\_64 / k8) - these are musl headers, musl libraries, and linux headers. This tarball is committed to source control. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 38 | 1. When building a target that uses the toolchain, the `sysroot.tar.xz` tarball is extracted into an external repository `@musl_sysroot`, via `sysroot.bzl` and `sysroot_repository.bzl`. |
| 39 | 1. A toolchain config is built using `//build/toolchain:cc_toolchain_config.bzl`, which points at `gcc-wrapper.sh` as its gcc entrypoint. `gcc-wrapper.sh` expects to be able to call the host gcc with `musl.spec`. |
| Leopold | bc93c2b | 2023-01-14 13:12:23 +0100 | [diff] [blame^] | 40 | 1. A toolchain is defined in `//build/toolchain/musl-host-gcc:musl_host_toolchain` with a `//build/platforms/linkmode:musl-static` constraint, which is selected by the `//build/platforms:linux_amd64_static` platform. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 41 | |
| 42 | Quirks |
| 43 | ------ |
| 44 | |
| 45 | As mentioned above, the musl sysroot is kept in a tarball in this repository. This is obviously suboptimal, but on the other hand gives us an effectively pre-built part of a toolchain. In the future, once we have a hermetic toolchain, a similar tarball might actually contain a fully hermetic toolchain pre-built for k8. |