cross

cross-compilation toolchain using musl libc
git clone git://z3bra.org/cross
Log | Files | Refs | README

cross-gcc (5683B)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh -ex
#
# Couple of useful links:
# + http://wiki.osdev.org/GCC_Cross-Compiler
# + http://wiki.osdev.org/Cross-Compiler_Successful_Builds
# + https://git.framasoft.org/Ypnose/solyste/blob/master/scripts/create-crossenv
# + http://kegel.com/crosstool/
# + https://github.com/GregorR/musl-cross/tree/master/patches
#
# 0. download gcc, binutils, gmp, mpc, mpfr
# 1. extract them
# 2. patch everything that require patching
# 3. prepare gcc (move gmp, mpc, mpfr in)
# 4. install linux headers
# 5. build binutils
# 6. build musl
# 7. build gcc
# 8. add pkg-config wrapper

# cross compiler environment
MARCH=$(uname -m)
TARGET=${TARGET:-${MARCH}-linux-musl}
PREFIX=${PREFIX:-/opt/cross}
BLDDIR=${BLDDIR:-${PWD}/tmp/build}
SRCDIR=${SRCDIR:-${PWD}/tmp/source}
PATCHD=${PATCHD:-${PWD}/patches}

# compilation variables
CFLAGS="-Os -fomit-frame-pointer -pipe"
CXXFLAGS="${CFLAGS}"
CPPFLAGS="${CFLAGS}"
LDFLAGS="-Wl,--as-needed"
MAKEFLAGS="-j4"

# versions
GCCV=${GCCV:-5.3.0}
BINV=${BINV:-2.25}
GMPV=${GMPV:-5.1.3}
MPCV=${MPCV:-1.0.3}
MPFRV=${MPFRV:-3.1.3}
MUSLV=${MUSLV:-1.1.12}
KERNV=${KERNV:-4.1.13}


# random but useful vars
PATH="${PREFIX}/bin:${PATH}"
GNUMIRROR=ftp://ftp.gnu.org/gnu
MUSLMIRROR=http://www.musl-libc.org/releases
LINUXMIRROR=https://www.kernel.org/pub/linux/kernel/v4.x

# Preparing sources
mkdir -p "${SRCDIR}" "${BLDDIR}" "${PREFIX}"
cd "${SRCDIR}"

# 
# ┏━┓ 
# ┃┃┃ 
# ┗━┛╹
# Get all GNU tarballs
grab_sources() {
curl -O -# "${GNUMIRROR}/gcc/gcc-${GCCV}/gcc-${GCCV}.tar.gz"
curl -O -# "${GNUMIRROR}/binutils/binutils-${BINV}.tar.gz"
curl -O -# "${GNUMIRROR}/mpc/mpc-${MPCV}.tar.gz"
curl -O -# "${GNUMIRROR}/gmp/gmp-${GMPV}.tar.gz"
curl -O -# "${GNUMIRROR}/mpfr/mpfr-${MPFRV}.tar.gz"
curl -O -# "${MUSLMIRROR}/musl-${MUSLV}.tar.gz"
curl -O -# "${LINUXMIRROR}/linux-${KERNV}.tar.xz"
}

#
# ╺┓  
#  ┃  
# ╺┻╸╹
# Extract everything
extract_source() {
tar xz < gcc-${GCCV}.tar.gz
tar xz < binutils-${BINV}.tar.gz
tar xz < mpc-${MPCV}.tar.gz
tar xz < gmp-${GMPV}.tar.gz
tar xz < mpfr-${MPFRV}.tar.gz
tar xz < musl-${MUSLV}.tar.gz
tar xJ < linux-${KERNV}.tar.xz
}

#
# ┏━┓ 
# ┏━┛ 
# ┗━╸╹
# Patch all source trees.
# This will take all patches in $PATCHD matching the programs
# (eg: gcc-4.8.4-*.diff to patch gcc in version 4.8.4)
patch_sources() {
for DIR in $(find "${SRCDIR}" -maxdepth 1 -type d); do
        cd "${DIR}"
        cat "${PATCHD}/$(basename ${DIR})"-*.diff | patch -Np1
done
}

#
# ┏━┓ 
# ╺━┫ 
# ┗━┛╹
# Prepare the gcc dir
prepare_gcc() {
mv "${SRCDIR}/gmp-${GMPV}"   "${SRCDIR}/gcc-${GCCV}/gmp"
mv "${SRCDIR}/mpc-${MPCV}"   "${SRCDIR}/gcc-${GCCV}/mpc"
mv "${SRCDIR}/mpfr-${MPFRV}" "${SRCDIR}/gcc-${GCCV}/mpfr"
}

#
# ╻ ╻ 
# ┗━┫ 
#   ╹╹
# Get linux headers
install_headers() {
cd "${SRCDIR}/linux-${KERNV}"
make ARCH=$(uname -m) headers_check
make ARCH=$(uname -m) INSTALL_HDR_PATH=${PREFIX}/${TARGET} headers_install
rm -r "${SRCDIR}/linux-${KERNV}"
}

#
# ┏━╸ 
# ┗━┓ 
# ┗━┛╹
# Build binutils and install them to ${PREFIX}
install_binutils() {
mkdir -p "${BLDDIR}/binutils"
cd "${BLDDIR}/binutils"
${SRCDIR}/binutils-${BINV}/configure --target=${TARGET} \
                                     --prefix=${PREFIX} \
                                     --with-sysroot=${PREFIX}/${TARGET} \
                                     --disable-nls \
                                     --disable-shared \
                                     --disable-multilib
make configure-host
make LDFLAGS="${LDFLAGS} -all-static -static"
make install
rm -rf "${BLDDIR}/binutils"
rm -rf "${SRCDIR}/binutils-${BINV}"
}

#
# ┏━┓ 
# ┣━┓ 
# ┗━┛╹
# Build the musl libc
install_musl() {
cd "${SRCDIR}/musl-${MUSLV}"
./configure --prefix=${PREFIX}/${TARGET} \
            --target=${TARGET} \
            --disable-gcc-wrapper \
            --disable-debug \
            --disable-shared \
            --disable-warning
make LDFLAGS="${LDFLAGS}"
make install
rm -rf "${SRCDIR}/musl-${MUSLV}"
}

#
# ┏━┓ 
#   ┃ 
#   ╹╹
# Build gcc linked against musl
install_gcc() {
mkdir -p "${BLDDIR}/gcc"
cd "${BLDDIR}/gcc"
${SRCDIR}/gcc-${GCCV}/configure --target=${TARGET} \
                                --prefix=${PREFIX} \
                                --with-sysroot=${PREFIX}/${TARGET} \
                                --with-native-system-header-dir=/include \
                                --disable-nls \
                                --disable-shared \
                                --disable-multilib \
                                --disable-libmudflap \
                                --enable-threads=posix \
                                --enable-languages=c
make LDFLAGS="${LDFLAGS} -static" all-gcc all-target-libgcc
make install-gcc install-target-libgcc
rm -rf "${BLDDIR}/gcc"
rm -rf "${SRCDIR}/gcc-${GCCV}"
}

#
# ┏━┓ 
# ┣━┫ 
# ┗━┛╹
# Add pkg-config wrapper
install_pkgconfig() {
cat << EOF > "${PREFIX}/bin/${TARGET}-pkg-config"
#!/bin/sh
export PKG_CONFIG_SYSROOT_DIR=${PREFIX}/${TARGET}
export PKG_CONFIG_LIBDIR=${PREFIX}/${TARGET}/usr/lib/pkgconfig
export PKG_CONFIG_PATH=\$PKG_CONFIG_LIBDIR

exec pkg-config --static "\$@"
EOF
chmod 755 "${PREFIX}/bin/${TARGET}-pkg-config"
}

grab_sources
extract_source
patch_sources
prepare_gcc
install_headers
install_binutils
install_musl
install_gcc
install_pkgconfig

rm -rf "${SRCDIR}"
rm -rf "${BLDDIR}"
rm -rf "${PREFIX}/share"
rm -f  "${PREFIX}/lib/libiberty.a"

cat << EOF | tee ${PREFIX}/README
TRIPLET :       $TARGET
PREFIX  :       $PREFIX

GCC     :       $GCCV
BINUTILS:       $BINV
MUSL    :       $MUSLV
KERNEL  :       $KERNV
EOF