-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_setup.sh
415 lines (382 loc) · 11.9 KB
/
env_setup.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
export TOPDIR=$(pwd)
export SYSTEM=$TOPDIR/out/root
export PATH=$TOPDIR/tools/bin:$PATH
export SYSIMG=$TOPDIR/out/system.img
export CLFS_TARGET=aarch64-linux-gnu
export CLFS_HOST=$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')
export TOOLDIR=$TOPDIR/tools
export SYSROOT=$TOOLDIR/root
JOBS=$(cat /proc/cpuinfo | grep processor | wc -l)
mkdir -p $TOPDIR/{tools,source,build,out,repo}
gdb_attach() {
aarch64-linux-gnu-gdb --command=./.gdb.cmd
}
croot() {
cd $TOPDIR
}
download_source() {
declare -a tarball_list=( \
"ftp://ftp.gnu.org/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2" \
"http://ftp.gnu.org/gnu/mpfr/mpfr-3.1.4.tar.xz" \
"ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2" \
"http://ftp.gnu.org/gnu/gmp/gmp-6.1.0.tar.xz" \
"http://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz" \
"http://ftp.gnu.org/gnu/libc/glibc-2.23.tar.bz2" \
"http://ftp.gnu.org/gnu/gperf/gperf-3.0.4.tar.gz" \
"http://ftp.gnu.org/gnu/bash/bash-4.4-rc1.tar.gz" \
"http://downloads.sourceforge.net/project/strace/strace/4.11/strace-4.11.tar.xz" \
"https://github.com/bminor/binutils-gdb/archive/gdb-7.11-release.tar.gz" \
"http://busybox.net/downloads/busybox-1.24.2.tar.bz2" \
)
mkdir -p $TOPDIR/tarball
pushd $TOPDIR/tarball
for i in "${tarball_list[@]}"; do
filename=${i##*/}
if [ ! -f $filename ]; then
# retry 3 times
wget $i || wget $i || wget $i || return 1
fi
done
popd
}
build_kernel() {
mkdir -p $TOPDIR/build/kernel
pushd $TOPDIR/kernel
if [ ! -f $TOPDIR/build/kernel/.config ]; then
ln -sf $TOPDIR/configs/kernel_defconfig $TOPDIR/kernel/arch/arm64/configs/defconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- O=$TOPDIR/build/kernel defconfig
git checkout $TOPDIR/kernel/arch/arm64/configs/defconfig
fi
popd
pushd $TOPDIR/build/kernel
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j${JOBS} || return 1
ln -sf $PWD/arch/arm64/boot/Image $TOPDIR/out/
ln -sf $PWD/vmlinux $TOPDIR/out
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- tools/perf
cp tools/perf/perf $SYSTEM/usr/bin
popd
}
# sudo apt-get install libglib2.0-dev libpixman-1-dev libfdt-dev
build_qemu() {
mkdir -p $TOPDIR/build/qemu
pushd $TOPDIR/build/qemu
CFLAGS=-O2 $TOPDIR/source/qemu/configure \
--prefix=$TOOLDIR \
--target-list=aarch64-softmmu \
--source-path=$TOPDIR/source/qemu || return 1
make -j${JOBS} || return 1
make install
popd
}
do_update() {
pushd $1
git pull
popd
}
run() {
qemu-system-aarch64 \
-machine virt \
-cpu cortex-a53 \
-m 512M \
-kernel $TOPDIR/out/Image \
-smp 1 \
-drive "file=$SYSIMG,media=disk,format=raw" \
--append "rootfstype=ext4 rw root=/dev/vda earlycon" \
-nographic $*
}
prepare_build_env() {
export CC=${CROSS_COMPILE}gcc
export LD=${CROSS_COMPILE}ld
export AR=${CROSS_COMPILE}ar
export AS=${CROSS_COMPILE}as
export RANDLIB=${CROSS_COMPILE}randlib
export STRIP=${CROSS_COMPILE}strip
export CXX=${CROSS_COMPILE}g++
export CFLAGS=-O2
export LDFLAGS=
export LDFLAGS=
export LIBS=-lpthread
}
clean_build_env() {
unset CC
unset LD
unset AR
unset AS
unset RANDLIB
unset STRIP
unset CXX
unset LDFLAGS
unset CFLAGS
unset LIBS
}
build_ltp() {
if [ ! -d $TOPDIR/repo/ltp ]; then
pushd $TOPDIR/repo
git clone https://github.com/linux-test-project/ltp.git
popd
fi
if [ ! -e $TOPDIR/source/ltp ]; then
pushd $TOPDIR/source
ln -sf ../repo/ltp
popd
fi
pushd $TOPDIR/source/ltp
make autotools
$TOPDIR/source/ltp/configure --host=$CLFS_TARGET --prefix=$SYSTEM/opt/ltp || return 1
make -j${JOBS} || return 1
make install
popd
}
build_strace() {
if [ ! -d $TOPDIR/source/strace-4.11 ]; then
tar -xf $TOPDIR/tarball/strace-4.11.tar.xz -C $TOPDIR/source/
fi
mkdir -p $TOPDIR/build/strace
pushd $TOPDIR/build/strace
$TOPDIR/source/strace-4.11/configure --host=$CLFS_TARGET --prefix=$SYSTEM/usr || return 1
make -j${JOBS} || return 1
make install
popd
}
# sudo apt-get install zlib1g-dev
build_toolchain() {
## kernel headers
pushd $TOPDIR/kernel
ARCH=arm64 CROSS_COMPILE= make INSTALL_HDR_PATH=$SYSROOT/usr headers_install
CROSS_COMPILE= make mrproper
popd
## binutils
if [ ! -d $TOPDIR/source/binutils-gdb ]; then
tar -xzf $TOPDIR/tarball/gdb-7.11-release.tar.gz -C $TOPDIR/source/
mv $TOPDIR/source/binutils-gdb-gdb-7.11-release $TOPDIR/source/binutils-gdb
fi
mkdir -p $TOPDIR/build/binutils
pushd $TOPDIR/build/binutils
AR=ar AS=as CFLAGS=-O2 $TOPDIR/source/binutils-gdb/configure \
--prefix=$TOOLDIR \
--host=$CLFS_HOST \
--target=$CLFS_TARGET \
--with-sysroot=$SYSROOT \
--disable-nls \
--enable-shared \
--disable-multilib || return 1
make configure-host || return 1
make -j${JOBS} || return 1
make install || return 1
popd
## gcc stage 1
if [ ! -d $TOPDIR/source/gcc-5.4.0 ]; then
tar -xjf $TOPDIR/tarball/gcc-5.4.0.tar.bz2 -C $TOPDIR/source
pushd $TOPDIR/source/gcc-5.4.0
tar -xf $TOPDIR/tarball/mpfr-3.1.4.tar.xz && ln -sf mpfr-3.1.4 mpfr
tar -xf $TOPDIR/tarball/gmp-6.1.0.tar.xz && ln -sf gmp-6.1.0 gmp
tar -xzf $TOPDIR/tarball/mpc-1.0.3.tar.gz && ln -sf mpc-1.0.3 mpc
tar -xjf $TOPDIR/tarball/isl-0.16.1.tar.bz2 && ln -sf isl-0.16.1 isl
popd
fi
mkdir -p $TOPDIR/build/gcc-stage-1
pushd $TOPDIR/build/gcc-stage-1
CFLAGS=-O2 $TOPDIR/source/gcc-5.4.0/configure \
--build=$CLFS_HOST \
--host=$CLFS_HOST \
--target=$CLFS_TARGET \
--prefix=$TOOLDIR \
--with-sysroot=$SYSROOT \
--with-newlib \
--without-headers \
--with-native-system-header-dir=/usr/include \
--disable-nls \
--disable-shared \
--disable-decimal-float \
--disable-libgomp \
--disable-libmudflap \
--disable-libssp \
--disable-libatomic \
--disable-libitm \
--disable-libsanitizer \
--disable-libquadmath \
--disable-threads \
--disable-multilib \
--disable-target-zlib \
--with-system-zlib \
--enable-languages=c \
--enable-checking=release || return 1
make -j${JOBS} all-gcc all-target-libgcc || return 1
make install-gcc install-target-libgcc || return 1
popd
## glibc
if [ ! -d $TOPDIR/source/glibc-2.23 ]; then
tar -xjf $TOPDIR/tarball/glibc-2.23.tar.bz2 -C $TOPDIR/source
fi
VER=$(grep -o '[0-9]\.[0-9]\.[0-9]' $TOPDIR/build/kernel/.config)
mkdir -p $TOPDIR/build/glibc
pushd $TOPDIR/build/glibc
echo "libc_cv_forced_unwind=yes" > config.cache
echo "libc_cv_c_cleanup=yes" >> config.cache
echo "install_root=$SYSROOT" > configparms
BUILD_CC="gcc" CC="${CLFS_TARGET}-gcc" AR="${CLFS_TARGET}-ar" \
RANLIB="${CLFS_TARGET}-ranlib" CFLAGS=-O2 $TOPDIR/source/glibc-2.23/configure \
--build=$CLFS_HOST \
--host=$CLFS_TARGET \
--prefix=/usr \
--libexecdir=/usr/lib/glibc \
--enable-kernel=$VER \
--with-binutils=$TOOLDIR/bin/ \
--with-headers=$SYSROOT/usr/include || return 1
make -j${JOBS} || return 1
make install || return 1
popd
## gcc stage 2
mkdir -p $TOPDIR/build/gcc-stage-2
pushd $TOPDIR/build/gcc-stage-2
AR=ar LDFLAGS="-Wl,-rpath,$TOOLDIR/lib" CFLAGS=-O2 \
$TOPDIR/source/gcc-5.4.0/configure \
--prefix=$TOOLDIR \
--build=$CLFS_HOST \
--target=$CLFS_TARGET \
--host=$CLFS_HOST \
--with-sysroot=$SYSROOT \
--enable-shared \
--enable-c99 \
--enable-linker-build-id \
--enable-long-long \
--with-arch=armv8-a \
--with-gnu-ld \
--with-gnu-as \
--enable-lto \
--enable-nls \
--enable-plugin \
--enable-multiarch \
--enable-languages=c,c++ \
--enable-__cxa_atexit \
--enable-threads=posix \
--with-system-zlib \
--enable-checking=release \
--enable-libstdcxx-time || return 1
make -j${JOBS} AS_FOR_TARGET="${CLFS_TARGET}-as" LD_FOR_TARGET="${CLFS_TARGET}-ld" || return 1
make install || return 1
popd
## gperf
if [ ! -d $TOPDIR/source/gperf-3.0.4 ]; then
tar -xzf $TOPDIR/tarball/gperf-3.0.4.tar.gz -C $TOPDIR/source
fi
mkdir -p $TOPDIR/build/cross-gperf
pushd $TOPDIR/build/cross-gperf
$TOPDIR/source/gperf-3.0.4/configure \
--prefix=$TOOLDIR \
--host=$CLFS_HOST \
--target=$CLFS_TARGET \
|| return 1
make -j${JOBS} || return 1
make install || return 1
popd
}
build_gcc () {
mkdir -p $TOPDIR/build/gcc
pushd $TOPDIR/build/gcc
$TOPDIR/source/gcc-5.4.0/configure \
--build=$CLFS_HOST \
--target=$CLFS_TARGET \
--host=$CLFS_TARGET \
--prefix=$SYSTEM/usr/ \
--enable-shared \
--disable-nls \
--enable-c99 \
--enable-long-long \
--enable-languages=c,c++ \
--enable-__cxa_atexit \
--enable-threads=posix \
--with-system-zlib \
--enable-checking=release || return 1
make -j${JOBS} AS_FOR_TARGET="${CLFS_TARGET}-as" LD_FOR_TARGET="${CLFS_TARGET}-ld" || return 1
make install || return 1
popd
}
build_bash() {
if [ ! -d $TOPDIR/source/bash-4.4-rc1 ]; then
tar -xzf $TOPDIR/tarball/bash-4.4-rc1.tar.gz -C $TOPDIR/source
sed -i '/#define SYS_BASHRC/c\#define SYS_BASHRC "/etc/bash.bashrc"' $TOPDIR/source/bash-4.4-rc1/config-top.h
fi
mkdir -p $TOPDIR/build/bash
pushd $TOPDIR/build/bash
$TOPDIR/source/bash-4.4-rc1/configure --host=$CLFS_TARGET --prefix=$SYSTEM/usr || return 1
make -j${JOBS} || return 1
make install
mv -v $SYSTEM/usr/bin/bash $SYSTEM/bin/
cd $SYSTEM/bin && ln -sf bash sh
popd
}
# make sure these packages is installed:
# sudo apt-get install texinfo bison flex
build_binutils_gdb() {
if [ ! -d $TOPDIR/source/binutils-gdb ]; then
tar -xzf $TOPDIR/tarball/gdb-7.11-release.tar.gz -C $TOPDIR/source
mv $TOPDIR/source/binutils-gdb-gdb-7.11-release $TOPDIR/source/binutils-gdb
fi
mkdir -p $TOPDIR/build/binutils-gdb
pushd $TOPDIR/build/binutils-gdb
$TOPDIR/source/binutils-gdb/configure \
--host=$CLFS_TARGET \
--target=$CLFS_TARGET \
--prefix=$SYSTEM/ \
--enable-shared || return 1
make -j${JOBS} || return 1
make install
popd
}
build_busybox() {
if [ ! -d $TOPDIR/source/busybox-1.24.2 ]; then
tar -xjf $TOPDIR/tarball/busybox-1.24.2.tar.bz2 -C $TOPDIR/source
fi
pushd $TOPDIR/source/busybox-1.24.2
if [ "x$1" == "xstatic" ]; then
sed "s/# CONFIG_STATIC is not set/CONFIG_STATIC=y/" $TOPDIR/configs/busybox.config > .config
else
mkdir -p $SYSTEM/lib64 $SYSTEM/usr/lib64 $SYSTEM/lib
cp -a $SYSROOT/lib64/* $SYSTEM/lib64
cp -a $SYSROOT/usr/lib64/*.so $SYSTEM/usr/lib64
cp -a $SYSROOT/lib/* $SYSTEM/lib
cp $TOPDIR/configs/busybox.config .config
fi
make || return 1
make install || return 1
cd $SYSTEM
rm -f linuxrc
ln -sf bin/busybox init
mkdir -p $SYSTEM/etc/init.d
echo "mount -t proc procfs /proc" > $SYSTEM/etc/init.d/rcS
echo "mount -t sysfs sysfs /sys" >> $SYSTEM/etc/init.d/rcS
echo "echo /sbin/mdev > /proc/sys/kernel/hotplug" >> $SYSTEM/etc/init.d/rcS
echo "mdev -s" >> $SYSTEM/etc/init.d/rcS
echo "ln -sf /dev/null /dev/tty2" >> $SYSTEM/etc/init.d/rcS
echo "ln -sf /dev/null /dev/tty3" >> $SYSTEM/etc/init.d/rcS
echo "ln -sf /dev/null /dev/tty4" >> $SYSTEM/etc/init.d/rcS
chmod +x $SYSTEM/etc/init.d/rcS
popd
}
do_strip () {
for i in $(find $SYSTEM/); do
echo $i
test -f $i && file $i | grep ELF &>/dev/null
if [ $? -eq 0 ]; then
${CROSS_COMPILE}strip --strip-unneeded $i
fi
done
}
pack_ramdisk() {
pushd $SYSTEM
sudo mknod $SYSTEM/dev/console c 5 1 # initrd must provide /dev/console
find . | cpio -ovHnewc | gzip > ../root.cpio.gz
popd
}
# new_disk <disk name> <size>
new_disk() {
size=$(expr $2 \* 1048576)
qemu-img create -f raw $1 $size
yes | /sbin/mkfs.ext4 $1
sudo mount $1 /mnt
sudo cp -rf $SYSTEM/* /mnt/ &> /dev/null
sudo umount /mnt
}