-
Notifications
You must be signed in to change notification settings - Fork 2
/
toolchain-musl.sh
executable file
·362 lines (340 loc) · 9.92 KB
/
toolchain-musl.sh
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# !/bin/sh
if [ $# -ne 1 ]; then
echo -en "
Usage: `basename $0` <arch>
Example: `basename $0` ppc64
Make sure that binutilsv, linuxv, gccv, libcv, point to src of respective
components usually the top directories
For gcc you also need prerequisite libraries installed e.g. libmpfr-dev
libgmp-dev libmpc-dev, gcc 4.5 also need libelf-dev installed on build
system. Alternatively you can also untar them in the gcc sources.
top and srctop should also be customized to your environment
by default
top=/export/TARGET
src=top/../src
"
exit 1
fi
# change these versions depending upon
# what you want your toolchain based on
binutilsv=binutils-gdb
linuxv=linux
gccv=gcc
libcv=musl
download_src=no
host=`uname --machine`
build=${host}-linux-gnu
host=$build
cpus=`cat /proc/cpuinfo | grep '^processor' | wc -l`
proc_per_cpu=2
parallelism=$(($cpus * $proc_per_cpu))
#extra_gcc_configure_opts="--enable-gold --enable-lto"
#extra_binutils_configure_opts="--enable-ld=default --enable-gold=yes"
extra_binutils_configure_opts="--disable-werror"
extra_musl_configure_opts=
arch=$1
default_libdir_name=lib
case $arch in
arm64|aarch64)
target=aarch64-linux-musl
linux_arch=arm64
default_libdir_name=lib64
;;
arm)
target=arm-linux-musleabi
linux_arch=arm
;;
armhf)
target=arm-linux-musleabihf
linux_arch=arm
extra_gcc_configure_opts="--with-float=hard --with-fpu=vfp"
;;
mipsel|mips|mips64)
if [ "$arch" = "mips64" ]; then
default_libdir_name=lib32
fi
target=$arch-linux-musl
linux_arch=mips
extra_binutils_configure_opts="$extra_binutils_configure_opts --disable-werror"
;;
mpc)
target=powerpc-linux-muslspe
linux_arch=powerpc
extra_binutils_configure_opts="$extra_binutils_configure_opts --disable-werror"
;;
ppc)
target=powerpc-linux-musl
linux_arch=powerpc
extra_binutils_configure_opts="$extra_binutils_configure_opts --disable-werror"
;;
ppc64|powerpc64)
target=powerpc64-linux-musl
linux_arch=powerpc
default_libdir_name=lib64
extra_binutils_configure_opts="$extra_binutils_configure_opts --enable-targets=powerpc-linux --disable-werror"
;;
sh3|sh4|sh64)
target=$arch-linux-musl
linux_arch=sh
if [ "$arch" = "sh3" ]; then
extra_musl_configure_opts="--without-fp"
fi
;;
x86)
target=i686-linux-musl
linux_arch=x86
extra_gcc_configure_opts="$extra_gcc_configure_opts --with-arch=i686"
;;
amd64|x86_64)
target=x86_64-linux-musl
linux_arch=x86
default_libdir_name=lib64
extra_gcc_configure_opts="$extra_gcc_configure_opts --disable-bootstrap"
;;
*)
echo "Specify one {arm mpc ppc ppc64 mips mipsel mips64 sh3 sh4 sh64} architecture to build."
exit 1
;;
esac
if [ "$top" == "" ]; then
echo "top variable is empty. It should point to top of the build trees. Please add it to your environment"
exit 1
fi
if [ "$src" == "" ]; then
echo "src variable is empty. It should point to top dir where all source trees are. Please add it to your enviroment"
exit 1
fi
top=$top/$target
obj=$top/obj
tools=$top/tools
sysroot=$top/sysroot
download () {
mkdir -p $src
local currdir=$PWD
cd $src
rm -rf *
wget -q http://www.kernel.org/pub/linux/kernel/v2.6/linux-$linuxv.tar.bz2
tar -xjf linux-$linuxv.tar.bz2
wget -q ftp://mirrors.kernel.org/sources.redhat.com/gcc/snapshots/LATEST-$gccv/gcc-[0-9]*.tar.bz2
gccv=`ls gcc-*.tar.bz2`
gccv=${gccv:4:12}
tar -xjf gcc-$gccv.tar.bz2
wget -q ftp://mirrors.kernel.org/sources.redhat.com/binutils/snapshots/binutils-$binutilsv.tar.bz2
tar -xjf binutils-$binutilsv.tar.bz2
# git clone git://git.sourceware.org/musl musl
# cd $libcv
# do not generate configure scripts. You dont know if your
# autoconf version is right or not.
cd $currdir
}
check_return () {
if [ $? -ne 0 ]
then
echo "Something went wrong in $1. Please check"
echo "exiting .... "
exit
fi
}
finish() {
if [ $gcc_patched ]; then
mv $src/$gccv/gcc/cppdefault.$ext.orig $src/$gccv/gcc/cppdefault.$ext
fi
}
trap finish EXIT
prep_gcc () {
if [ $gcc_patched ]; then
mkdir -p gcc
touch gcc/t-oe
cp $src/$gccv/gcc/defaults.h gcc/defaults.h
sed -i '$i'"#define SYSTEMLIBS_DIR \"/\\$default_libdir_name/\"" gcc/defaults.h
fi
}
prep_src () {
if [ $gcc_patched ]; then
cp $src/$gccv/gcc/cppdefault.$ext $src/$gccv/gcc/cppdefault.$ext.orig
sed -i -e 's/\<STANDARD_STARTFILE_PREFIX_2\>//g' $src/$gccv/gcc/cppdefault.$ext
fi
# cd $src/$binutilsv
# for d in . bfd binutils gas gold gprof ld libctf opcodes; do
# cd $d
# rm -rf autom4te.cache
# autoconf
# cd -
# done
}
if [ -e $src/$gccv/gcc/cppdefault.c ]; then
ext=c
elif [ -e $src/$gccv/gcc/cppdefault.cc ]; then
ext=cc
fi
eval grep '\<STANDARD_STARTFILE_PREFIX_2\>' $src/$gccv/gcc/cppdefault.$ext >& /dev/null
gcc_patched=$?
#if [ $download_src = "yes" ]; then
#download
#else
#gccv=`ls $src/gcc-*.tar.bz2|xargs basename`
#gccv=${gccv:4:12}
#fi
prep_src
echo "Doing Binutils ..."
mkdir -p $obj/binutils
cd $obj/binutils
if [ ! -e .configured ]; then
$src/$binutilsv/configure \
--target=$target \
--prefix=$tools \
--with-sysroot=$sysroot \
--enable-deterministic-archives \
--disable-gdb \
--disable-gdbserver \
--disable-libdecnumber \
--disable-readline \
--disable-sim \
$extra_binutils_configure_opts \
&& touch .configured
# --enable-targets=all
fi
if [ ! -e .compiled ]; then
make -j $parallelism && touch .compiled
check_return "binutils compile"
fi
if [ ! -e .installed ]; then
make -j $parallelism install && touch .installed
check_return "binutils install"
fi
echo "Doing GCC stage 1 ..."
mkdir -p $obj/gcc1
cd $obj/gcc1
prep_gcc
if [ ! -e .configured ]; then
$src/$gccv/configure \
--target=$target \
--prefix=$tools \
--disable-libssp --disable-libcilkrts \
--enable-languages=c --disable-shared \
--disable-threads \
--disable-libatomic \
--disable-decimal-float \
--disable-libffi \
--disable-libgomp \
--disable-libitm \
--disable-libmpx \
--disable-libquadmath \
--disable-libsanitizer \
--without-headers --with-newlib \
$extra_gcc_configure_opts \
&& touch .configured
fi
if [ ! -e .compiled ]; then
PATH=$tools/bin:$PATH make -j $parallelism all-gcc all-target-libgcc && touch .compiled
check_return "gcc1 compile"
fi
if [ ! -e .installed ]; then
PATH=$tools/bin:$PATH make -j $parallelism install-gcc install-target-libgcc && touch .installed
check_return "gcc1 install"
fi
echo "Doing linux kernel headers ..."
#cp -r $src/$linuxv $obj/linux
mkdir -p $obj/linux
cd $src/$linuxv
if [ ! -e $obj/linux/.installed ]; then
PATH=$tools/bin:$PATH \
make -j $parallelism headers_install \
ARCH=$linux_arch \
CROSS_COMPILE=$target- \
INSTALL_HDR_PATH=$sysroot/usr \
O=$obj/linux \
&& touch $obj/linux/.installed
check_return "linux kernel headers install"
fi
echo "Doing default musl ..."
mkdir -p $obj/musl
cd $obj/musl
if [ ! -e .configured ]; then
BUILD_CC=gcc \
CC=$tools/bin/$target-gcc \
CXX=$tools/bin/$target-g++ \
AR=$tools/bin/$target-ar \
RANLIB=$tools/bin/$target-ranlib \
LD=$tools/bin/$target-ld \
AS=$tools/bin/$target-as \
NM=$tools/bin/$target-nm \
OBJCOPY=$tools/bin/$target-objcopy \
OBJDUMP=$tools/bin/$target-objdump \
RANLIB=$tools/bin/$target-ranlib \
READELF=$tools/bin/$target-readelf \
STRIP=$tools/bin/$target-strip \
$src/$libcv/configure \
--prefix=/usr \
--exec-prefix=/usr \
--syslibdir=/lib \
--host=$arch \
$extra_musl_configure_opts \
&& touch .configured
fi
if [ ! -e .compiled ]; then
PATH=$tools/bin:$PATH make -j$parallelism && touch .compiled
check_return "musl compile"
fi
if [ ! -e .installed ]; then
PATH=$tools/bin:$PATH make -j$parallelism install \
DESTDIR=$sysroot && touch .installed
check_return "musl install"
fi
echo "Doing GCC final ..."
mkdir -p $obj/gcc2
cd $obj/gcc2
prep_gcc
if [ ! -e .configured ]; then
$src/$gccv/configure \
--target=$target \
--prefix=$tools \
--with-sysroot=$sysroot \
--enable-__cxa_atexit \
--enable-shared --enable-threads \
--disable-libssp --disable-libgomp \
--disable-libmudflap --disable-libsanitizer \
--disable-gnu-indirect-function \
--enable-languages=c,c++ \
$extra_gcc_configure_opts \
&& touch .configured
fi
if [ ! -e .compiled ]; then
PATH=$tools/bin:$PATH make -j $parallelism && touch .compiled
check_return "gcc final compile"
fi
if [ ! -e .installed ]; then
PATH=$tools/bin:$PATH make -j $parallelism install && touch .installed
check_return "gcc final install"
case $arch in
aarch64)
cp -d $tools/$target/lib64/libgcc_s.so* $sysroot/lib
cp -d $tools/$target/lib64/libstdc++.so* $sysroot/usr/lib
;;
ppc64)
cp -d $tools/$target/lib64/libgcc_s.so* $sysroot/lib64
cp -d $tools/$target/lib64/libstdc++.so* $sysroot/usr/lib64
cp -d $tools/$target/lib/libgcc_s.so* $sysroot/lib
cp -d $tools/$target/lib/libstdc++.so* $sysroot/usr/lib
;;
x86_64)
cp -d $tools/lib/libgcc_s.so* $sysroot/lib
cp -d $tools/lib/libstdc++.so* $sysroot/usr/lib
cp -d $tools/lib64/libgcc_s.so* $sysroot/lib64
cp -d $tools/lib64/libstdc++.so* $sysroot/usr/lib64
;;
mips64)
cp -d $tools/$target/lib64/libgcc_s.so* $sysroot/lib64
cp -d $tools/$target/lib64/libgcc_s.so* $sysroot/lib64
cp -d $tools/$target/lib32/libstdc++.so* $sysroot/usr/lib32
cp -d $tools/$target/lib32/libstdc++.so* $sysroot/usr/lib32
cp -d $tools/$target/lib/libgcc_s.so* $sysroot/lib
cp -d $tools/$target/lib/libstdc++.so* $sysroot/usr/lib
;;
*)
cp -d $tools/$target/lib/libgcc_s.so* $sysroot/lib
cp -d $tools/$target/lib/libstdc++.so* $sysroot/usr/lib
;;
esac
fi
echo "!!! All Done !!!"