Upstreaming cross compilers to Alpine
It would be nice to upstream the cross compiler related packages like gcc-armhf
to Alpine at some point. But this is not trivial and when we do it, we must make sure to not introduce more complexity and maintenance burden that we have now. Because right now, we are generating the related packages from Alpine's packages automatically (pmbootstrap aportgen gcc-armhf
etc), which makes maintenance effort rather low. We have hourly monitoring set up that lets us know if Alpine's gcc/binutils/musl has changed and we need to regenerate our cross compiler packages. Nevertheless, I think that I should write down somewhere why I think this is a complex task so I can refer to it when talking about this in the future.
Let's take armhf
as example, the same goes for all the other architectures we support cross compiling to (currently armhf
, armv7
, aarch64
). There is not just the gcc-armhf
package, but also binutils-armhf
and musl-armhf
.
gcc
Alpine's gcc
APKBUILD is pretty flexible, scripts/bootstrap.sh calls it with various environment variables to produce cross compilers of various stages, which will at some point produce a gcc-armhf
package that is almost the same as the one in postmarketOS, and use that to compile the Alpine base system to the armhf
architecture (or whatever new architecture was specified as parameter). This is then used to boot into the system on a real machine (or qemu) and do further compiling there as I understand it.
So what pmbootstrap does with pmbootstrap aportgen gcc-armhf
(see gcc.py), is set these variables as we need them at the beginning of the APKBUILD to pretend that it was called by bootstrap.sh and do a few more adjustments.
The naive approach to upstreaming this would be, to create a gcc-armhf aport in Alpine that duplicates Alpine's gcc aport but with a few things adjusted. We would need to do this for every architecture that Alpine supports. So we end up with gcc-armhf, gcc-aarch64, gcc-armv7, gcc-x86_64, gcc-x86, gcc-mips64 and more, each with the full APKBUILD and all patch files! To make this feasible, we would need some mechanism to keep it all in sync (a shell script, that also runs in CI?)... but still, having all these duplicated files would be pretty ugly.
Another approach might be something like that as gcc-armhf/APKBUILD
:
# configure the GCC APKBUILD to produce gcc-armhf, something along those lines:
CTARGET_ARCH=armhf
BOOTSTRAP="nobuildbase"
CBUILDROOT="/"
_cross_configure="--disable-bootstrap --with-sysroot=/usr/$CTARGET"
cd ../gcc
. APKBUILD
At least we would not have as many duplicated files then, but still it's a big hack and might break some other things in Alpine's infrastructure so I'm not sure if this would get accepted. If I had to choose, I would probably try this route.
In any case, there would need to be a workflow in Alpine that makes sure that all the cross compilers are tested (make sure they still build at least) when changing the gcc package.
Another approach would be blowing up the gcc APKBUILD even more, and to add a loop at the end that builds not only the native gcc, but also all gcc cross compilers and put them into subpackages. This would be a huge increase of the build time. The benefit is, that there is only one APKBUILD though.
binutils
Similar story to gcc, except that there are usually not so many patch files involved and that the APKBUILD it self is much less complex.
musl
I cheated and just repackaged the musl and musl-dev apks that Alpine builds for armhf. We would need to properly cross compile musl somehow and also solve how to either avoid having a separate APKBUILD for each architecture, or how to sync them automatically.
EDIT: musl-dev-armv7 is required to build gcc-armv7, and same with other arches
more ranting
When I started with postmarketOS, I spent weeks on getting working cross compilers. I tried all sorts of crazy things, even using cross compilers from other distros (but that failed of course as they would not work with musl). So I was quite happy that I had a working solution, even if it meant generating APKBUILDs based on Alpine's. But I understand that this is not perfect and I would be happy if we could improve it more and even find a generic great solution that works in Alpine, so everybody could use the cross compilers without hassle. Other distributions have solved this as well, and there it is just possible to install a cross compiler from any arch to any arch.