c4ev3 / C4EV3.Toolchain

Recent GCC cross-compilers for the stock EV3
https://github.com/c4ev3/toolchain/releases
4 stars 1 forks source link

Using musl-based toolchain #5

Open JakubVanek opened 4 years ago

JakubVanek commented 4 years ago

Hi all,

I've recently discovered musl and a simple script to build a musl-based toolchain: https://github.com/richfelker/musl-cross-make

I've tried building that toolchain for armv5-linux-musleabi and it successfully statically compiled EV3-API and a test program. The resulting program then ran on the brick OK.

I think it might be worth looking into it as the toolchain setup looks much simpler - in the basic mode, it is just a single command (make TARGET=armv5-linux-musleabi).

Best regards,

Jakub

JakubVanek commented 4 years ago

Using https://github.com/richfelker/musl-cross-make/pull/90 as a base, the following config.mak should work:

TARGET = armv5-linux-musleabi

OUTPUT = /home/kuba/musl-ev3

BINUTILS_VER = 2.33.1
GCC_VER   = 8.4.0
MUSL_VER  = 1.2.0
GMP_VER   = 6.1.2
MPC_VER   = 1.1.0
MPFR_VER  = 4.0.2
ISL_VER   = 0.21
LINUX_VER = headers-4.19.88

COMMON_CONFIG += CFLAGS="-g0 -Os" CXXFLAGS="-g0 -Os" LDFLAGS="-s"
COMMON_CONFIG += --disable-nls
COMMON_CONFIG += --with-debug-prefix-map=$(CURDIR)=

GCC_CONFIG += --disable-libquadmath --disable-decimal-float --disable-fixed-point
GCC_CONFIG += --disable-libgomp --disable-libitm
GCC_CONFIG += --with-cpu=arm926ej-s --enable-lto
GCC_CONFIG += --enable-languages=c,c++
GCC_CONFIG += --disable-shared
MUSL_CONFIG += --disable-shared
TOOLCHAIN_CONFIG += --with-pkgversion="C4EV3.Toolchain musl-gcc 8.4.0" --with-bugurl="https://github.com/c4ev3/C4EV3.Toolchain/issues/new"
JakubVanek commented 4 years ago

Wow, it seems that even external toolchains could be used:

At least the Linux one works for me (if the final program is staticaly linked using -static).

a3f commented 4 years ago

Can you build something against the Linux v2.6.33-rc4 headers? I don't mind shipping musl instead as long it has the correct headers. But with your current setup, you can write code that compiles fine because the headers are there, but it will crash at runtime, because the old EV3 kernel doesn't support these interfaces.

JakubVanek commented 4 years ago

It seems that this could be done ad-hoc: https://www.openwall.com/lists/musl/2016/12/06/4 Musl itself apparently doesn't need kernel headers for its build and operation: https://unix.stackexchange.com/a/528918

However apart from patches, the change to v2.6.33-rc4 should be relatively easy to make - it is just necessary to add the hash to the hashes directory.

JakubVanek commented 4 years ago

Hmmmm, it seems that the older kernel headers aren't going to work with musl 1.2.0 :/ https://github.com/richfelker/musl-cross-make/commit/1645ae3476e0c7e0ce16f25b037a3f524662aa93#diff-ad13e3e85208780288a2389c791b5c03

The time64_t changes are new to musl 1.2.0, so downgrading to 1.1.24 may fix this (if we really need the old headers, or if we need them at all).

I've been successful in building a portable Linux toolchain (using a portable portable-building toolchain) with the 2.6.33 headers (and musl 1.2.0, so it may not work well). It can be downloaded here.

Hash file for the linux kernel sources:

diff --git a/hashes/linux-2.6.33.tar.xz.sha1 b/hashes/linux-2.6.33.tar.xz.sha1
new file mode 100644
index 0000000..12b6fb6
--- /dev/null
+++ b/hashes/linux-2.6.33.tar.xz.sha1
@@ -0,0 +1 @@
+3e1993b30afc6c6f2d801942c88259b5e1626fa7  linux-2.6.33.tar.xz

config.mak:

#HOST   = i686-linux-musl
TARGET = armv5-linux-musleabi

OUTPUT = /home/kuba/musl-ev3

BINUTILS_VER = 2.33.1
GCC_VER      = 8.4.0
MUSL_VER     = 1.2.0
GMP_VER      = 6.1.2
MPC_VER      = 1.1.0
MPFR_VER     = 4.0.2
ISL_VER      = 0.21
LINUX_VER    = 2.6.33

COMMON_CONFIG += CC="i686-linux-musl-gcc -static --static" CXX="i686-linux-musl-g++ -static --static"
COMMON_CONFIG += CFLAGS="-g0 -Os" CXXFLAGS="-g0 -Os" LDFLAGS="-s"
COMMON_CONFIG += --disable-nls
COMMON_CONFIG += --with-debug-prefix-map=$(CURDIR)=
COMMON_CONFIG += --with-pkgversion="C4EV3.Toolchain musl-gcc 8.4.0" --with-bugurl="https://github.com/c4ev3/C4EV3.Toolchain/issues/new"

GCC_CONFIG += --disable-host-shared
GCC_CONFIG += --disable-libquadmath --disable-decimal-float --disable-fixed-point
GCC_CONFIG += --disable-libgomp --disable-libitm
GCC_CONFIG += --with-cpu=arm926ej-s --enable-lto
GCC_CONFIG += --enable-languages=c,c++
GCC_CONFIG += --disable-shared
MUSL_CONFIG += --disable-shared