-
Notifications
You must be signed in to change notification settings - Fork 45
/
01-toolchain
executable file
·154 lines (129 loc) · 2.84 KB
/
01-toolchain
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
#!/bin/bash -e
if type -p "sudo" &>/dev/null; then
PRIV="sudo"
elif type -p "doas" &>/dev/null; then
PRIV="doas"
else
echo -e "\e[1;31merror:\e[0m sudo/doas not found!"
exit 1
fi
fetch() {
tarballname=$(echo $1 | rev | cut -d / -f 1 | rev)
WGETCMD="wget --passive-ftp --tries=3 --waitretry=3 --output-document=$2/$tarballname.partial"
WGETRESUME="-c"
if [ -f $2/$tarballname ]; then
echo "Source file $tarballname found."
return 0
else
if [ -f $2/$tarballname.partial ]; then
echo "Resuming $1"
$WGETCMD $WGETRESUME $1
else
mkdir -p "$2"
echo "Downloading $1"
$WGETCMD $1
fi
fi
if [ $? = 0 ]; then
mv $2/$tarballname.partial $2/$tarballname
else
echo "Failed fetching source: $tarballname"
exit 1
fi
}
fetch_src() {
if [ "${#source[@]}" -gt 0 ]; then
for s in ${source[@]}; do
fetch $s $SRCDIR
done
fi
}
extract_src() {
[ "$name" ] && rm -fr $WORKDIR/$name
mkdir -p $WORKDIR/$name
if [ "${#source[@]}" -gt 0 ]; then
for s in ${source[@]}; do
filename=$(basename $s)
case $filename in
*.tar|*.tar.gz|*.tar.Z|*.tgz|*.tar.bz2|*.tbz2|*.tar.xz|*.txz|*.tar.lzma|*.zip|*.rpm)
tar xvf $SRCDIR/$filename -C $WORKDIR/$name || exit 1;;
*)
cp -v $SRCDIR/$filename $WORKDIR/$name || exit 1;;
esac
done
fi
}
build_src() {
cd $WORKDIR/$name
if [ "$(type -t build)" = function ]; then
(set -e -x; build)
fi
if [ $? -ne 0 ]; then
echo "!!! build $name-$version failed !!!"
exit 1
else
echo "--- build $name-$version success ---"
fi
[ "$name" ] && rm -fr $WORKDIR/$name
register
}
register() {
echo $name-$version > $LFS/tools/$(basename $script)
}
checkdone() {
if [ -e $LFS/tools/$(basename $script) ]; then
echo "*** skip $(basename $script) ***"
return 1
else
echo ">>> building $(basename $script) <<<"
return 0
fi
}
build_stage1() {
cd $CWD
source $1 || exit 1
fetch_src
extract_src
build_src
unset source name version build
}
main() {
for script in $TCDIR/[0-9][0-9]-*; do
checkdone || continue
build_stage1 $script
done
$PRIV chown -Rv 0:0 $LFS
}
if [ $(id -u) = 0 ]; then
echo "$0 script need to run as regular user!"
exit 1
fi
CWD=$PWD
if [ -f $CWD/config ]; then
source $CWD/config
fi
PATH=/tools/bin:/bin:/usr/bin
TCDIR=$CWD/toolchain
PATCHDIR=$CWD/patches
FILEDIR=$CWD/files
LFS_TGT=x86_64-lfs-linux-gnu
LFS_TGT32=i686-lfs-linux-gnu
MAKEFLAGS="${MAKEFLAGS:--j$(nproc)}"
LFS="${LFS:-/mnt/lfs}"
PKGDIR="${PKGDIR:-$CWD/pkg}"
SRCDIR="${SRCDIR:-$CWD/src}"
WORKDIR="${WORKDIR:-/tmp}"
CFLAGS="${CFLAGS:--O2 -march=x86-64 -pipe}"
export LC_ALL=C PATH MAKEFLAGS LFS LFS_TGT LFS_TGT32
mkdir -p $SRCDIR $PKGDIR $WORKDIR
if [ ! -d $LFS/tools ]; then
$PRIV mkdir -pv $LFS/tools
fi
if [ ! -w $LFS ]; then
$PRIV chown -Rv $USER:$USER $LFS
fi
if [ ! -L /tools ] || [ $(realpath /tools) != $LFS/tools ]; then
$PRIV ln -svf $LFS/tools /
fi
main $@
exit 0