Search code examples
c++clinuxcross-compilingcrosstool-ng

Can't build cross compiler on Alpine targeting GNU based linux distros (eg. Debian)?


I was trying to use crosstool-ng and get it work to build a cross compiler to target a gnu-based linux distros, since alpine comes with musl-libc I guess the cross tool couldn't directly use the type declarations used in gnu's.

More precisely, at the time of installation of the linux headers when building the toolchain, the /usr/include/linux/types.h does not contain some type declarations which errors for:

[ERROR]      rpc/types.h:78:9: error: unknown type name '__u_char'
[ERROR]      rpc/types.h:78:18: error: conflicting types for 'u_char'
[ERROR]      rpc/types.h:79:9: error: unknown type name '__u_short'
[ERROR]      rpc/types.h:79:19: error: conflicting types for 'u_short'
[ERROR]      rpc/types.h:80:9: error: unknown type name '__u_int'
[ERROR]      rpc/types.h:80:17: error: conflicting types for 'u_int'
[ERROR]      rpc/types.h:81:9: error: unknown type name '__u_long'
[ERROR]      rpc/types.h:81:18: error: conflicting types for 'u_long'
[ERROR]      rpc/types.h:82:9: error: unknown type name '__quad_t'
[ERROR]      rpc/types.h:82:18: error: conflicting types for 'quad_t'
[ERROR]      rpc/types.h:83:9: error: unknown type name '__u_quad_t'
[ERROR]      rpc/types.h:83:20: error: conflicting types for 'u_quad_t'
[ERROR]      rpc/types.h:84:9: error: unknown type name '__fsid_t'
[ERROR]      rpc/types.h:88:9: error: unknown type name '__daddr_t'
[ERROR]      rpc/types.h:89:9: error: unknown type name '__caddr_t'
[ERROR]      rpc/types.h:89:19: error: conflicting types for 'caddr_t'

I guess it is due to the fact, musl-libc does not contain those declarations in it. Though I was able to build a mingw-w64 cross compiler using mxe that uses the libgcc libstdc++ came from package manager, so I guess its not that they are the main problem as the mingw-w64 works pretty well.

I have added a issue on their repo, but so far no solutions.

Is there something that I missed that is becoming a barrier for targeting a gnu linux?

Edit: A sample build script for reproducing the issue is:

docker run --rm -it alpine:3.12.4 sh

# ======== Under the shell (alpine) ========
apk add --update-cache alpine-sdk wget xz git bash autoconf automake bison flex texinfo help2man gawk libtool ncurses-dev gettext-dev python-dev rsync && \

curl -LO http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.24.0.tar.bz2 && \
tar -xjvf crosstool-ng-1.24.0.tar.bz2 && \
cd crosstool-ng-1.24.0 && \

./configure --enable-local && \
make && \

./ct-ng x86_64-unknown-linux-gnu && \

{
    echo "CT_EXPERIMENTAL=y"
    echo "CT_ALLOW_BUILD_AS_ROOT=y"
    echo "CT_ALLOW_BUILD_AS_ROOT_SURE=y"
} >> .config && \

./ct-ng build.$(nproc)

Solution

  • After a lot of hit and trials, I was able to successfully build the toolchain.

    The musl-libc is smart, it defines those types itself. So the rpc headers https://code.woboq.org/userspace/glibc/sunrpc/rpc/types.h.html#77 here were conflicting with the types __u_char that GNU defines but does not define u_char and more.

    We can solve this by passing -D__daddr_t_defined -D__u_char_defined macros to the c flags on build.

    sed -i 's/\(^CT_EXTRA_CFLAGS_FOR_BUILD=".*\)"$/\1 -D__daddr_t_defined -D__u_char_defined"/' .config
    

    Hence, this worked well!

    I have closed the github issue and requested to add this into the docs, so that users can easily build toolchains!