[sdk] Fix incorrect assumptions in the AVL binary search routines
[monolithium.git] / setup
1 #!/bin/sh
2
3 TOOLSDIR='tools'
4 PACKAGESDIR='packages'
5 WORKSPACEDIR='workspace'
6 TOOLSROOTDIR='root'
7 KEYRING='keyring.gpg'
8 GNUMIRROR='ftp.gnu.org'
9
10 packages=' '
11
12 package_binutils_protocols=' https http ftp '
13 package_binutils_server=$GNUMIRROR
14 package_binutils_remotedir='/gnu/binutils'
15 package_binutils_filename='binutils-2.30'
16 package_binutils_extensions=' tar.xz tar.lz tar.bz2 tar.gz '
17 package_binutils_recipe='setup_binutils'
18
19 package_gawk_protocols=' https http ftp '
20 package_gawk_server=$GNUMIRROR
21 package_gawk_remotedir='/gnu/gawk'
22 package_gawk_filename='gawk-4.2.1'
23 package_gawk_extensions=' tar.xz tar.lz tar.gz '
24 package_gawk_recipe='setup_gawk'
25
26 package_gcc_protocols=' https http ftp '
27 package_gcc_server=$GNUMIRROR
28 package_gcc_remotedir='/gnu/gcc/gcc-7.3.0'
29 package_gcc_filename='gcc-7.3.0'
30 package_gcc_extensions=' tar.xz tar.gz '
31 package_gcc_recipe='setup_gcc'
32
33 package_grub_protocols=' https http ftp '
34 package_grub_server=$GNUMIRROR
35 package_grub_remotedir='/gnu/grub'
36 package_grub_filename='grub-2.02'
37 package_grub_extensions=' tar.xz tar.gz '
38 package_grub_recipe='setup_grub'
39
40 package_nasm_protocols=' http '
41 package_nasm_server='www.nasm.us'
42 package_nasm_remotedir='/pub/nasm/releasebuilds/2.13.03'
43 package_nasm_filename='nasm-2.13.03'
44 package_nasm_extensions=' tar.xz tar.bz2 tar.gz zip '
45 package_nasm_recipe='setup_nasm'
46
47 package_xorriso_protocols=' https http ftp '
48 package_xorriso_server=$GNUMIRROR
49 package_xorriso_remotedir='/gnu/xorriso'
50 package_xorriso_filename='xorriso-1.4.8'
51 package_xorriso_extensions=' tar.gz '
52 package_xorriso_recipe='setup_xorriso'
53
54 setup_binutils() {
55     cd 'binutils-2.30' && ./configure "--prefix=$PREFIX" --target=i686-elf --disable-nls && make $MAKEFLAGS && make install
56 }
57
58 setup_gawk() {
59     cd 'gawk-4.2.1' && ./configure "--prefix=$PREFIX" && make $MAKEFLAGS && make install
60 }
61
62 setup_gcc() {
63     cd 'gcc-7.3.0' && mkdir -p build && cd build \
64         && ../configure "--prefix=$PREFIX" --target=i686-elf --enable-languages=c --without-headers --disable-nls --disable-libssp \
65         && make $MAKEFLAGS all-gcc && make $MAKEFLAGS all-target-libgcc \
66         && make install-gcc && make install-target-libgcc
67 }
68
69 setup_grub() {
70     cd 'grub-2.02' && ./configure "--prefix=$PREFIX" && make $MAKEFLAGS && make install
71 }
72
73 setup_nasm() {
74     cd 'nasm-2.13.03' && ./configure "--prefix=$PREFIX" && make $MAKEFLAGS && make install
75 }
76
77 setup_xorriso() {
78     cd 'xorriso-1.4.8' && ./configure "--prefix=$PREFIX" && make $MAKEFLAGS && make install
79 }
80
81 download() {
82     download_url="$1://$2/$3"
83
84     if [ -n "$AXEL" ]
85     then
86         "$AXEL" -o "$4" "$download_url"
87     elif [ -n "$WGET" ]
88     then
89         "$WGET" -O "$4" "$download_url"
90     elif [ -n "$CURL" ]
91     then
92         "$CURL" -L -o "$4" "$download_url"
93     elif [ -n "$PFTP" ] && [ "$1" = "ftp" ]
94     then
95         separator=`expr index + "$2" + :`
96
97         if  [ $separator -ne 0 ]
98         then
99             host=`expr substr + "$2" 1 \( "$separator" - 1 \)`
100             port=`expr substr + "$2" \( $separator + 1 \) \( length + "$2" \)`
101         else
102             host="$2"
103             port=""
104         fi
105
106         "$PFTP" -n $host $port <<EOF
107 user ftp
108 binary
109 get "$remotepath" "$localpath"
110 quit
111 EOF
112     else
113         printf 'No suitable program available to download "%s". Giving up.\n' "$url" 1>&2
114         exit 1
115     fi
116 }
117
118 extract() {
119     if expr + "$1" : '.*\.tar.*' 1>/dev/null
120     then
121         "$TAR" -C "$2" -x -f "$1"
122     elif expr + "$1" : '.*\.zip$' 1>/dev/null
123     then
124         "$UNZIP" "$1" -d "$2"
125     fi
126 }
127
128 [ -d "$TOOLSDIR" ] || mkdir "$TOOLSDIR" || exit 1
129 cd "$TOOLSDIR" || exit 1
130
131 [ -d "$PACKAGESDIR" ]  || mkdir "$PACKAGESDIR"  || exit 1
132 [ -d "$WORKSPACEDIR" ] || mkdir "$WORKSPACEDIR" || exit 1
133 [ -d "$TOOLSROOTDIR" ] || mkdir "$TOOLSROOTDIR" || exit 1
134
135 export PATH="$PWD/$TOOLSROOTDIR/bin:$PATH"
136
137 AXEL=`command -v axel`
138 WGET=`command -v wget`
139 CURL=`command -v curl`
140 FTP=`command -v ftp`
141 PFTP=`command -v pftp` || ([ -n "$FTP" ] && PFTP="$FTP -p")
142 TAR=`command -v tar`
143 UNZIP=`command -v unzip`
144
145 if ! GPG=`command -v gpg`
146 then
147     echo 'GNU Privacy Guard is required to verify signatures. Please install it.' 1>&2
148     exit 1
149 fi
150
151 ARCHIVES=' '
152
153 if [ -n "$TAR" ]
154 then
155     for comp in xz.xz lzip.lz bzip2.bz2 gzip.gz
156     do
157         program=`expr substr "$comp" 1 \( index + "$comp" . - 1 \)`
158         extension=`expr substr "$comp" \( index + "$comp" . + 1 \) \( length $comp \)`
159         command -v "$program" 1>/dev/null && ARCHIVES="${ARCHIVES}tar.$extension "
160     done
161 fi
162
163 [ -n "$UNZIP" ] && ARCHIVES="${ARCHIVES}zip "
164
165 echo 'Detecting installed packages...' 1>&2
166 command -v gawk || packages="${packages}gawk "
167 command -v i686-elf-ld || packages="${packages}binutils "
168 command -v i686-elf-gcc || packages="${packages}gcc "
169 command -v grub-mkrescue || packages="${packages}grub "
170 command -v nasm || packages="${packages}nasm "
171 command -v xorriso || packages="${packages}xorriso "
172
173 MAKEFLAGS="$MAKEFLAGS -j `nproc`"
174
175 for package in $packages
176 do
177     printf 'Installing package %s\n' "$package" 1>&2
178     [ -d "$WORKSPACEDIR/$package" ] || mkdir "$WORKSPACEDIR/$package" || exit 1
179
180     unset packagefile
181
182     eval "protocols=\"\$package_${package}_protocols\"\
183           server=\"\$package_${package}_server\"\
184           remotedir=\"\$package_${package}_remotedir\"\
185           filename=\"\$package_${package}_filename\"\
186           extensions=\"\$package_${package}_extensions\"\
187           recipe=\"\$package_${package}_recipe\""
188
189     for ext in $extensions
190     do
191         printf '%s' "$ARCHIVES" | grep -q -F " $ext " || continue
192
193         file="${filename}.$ext"
194         url="$remotedir/$file"
195
196         if ! [ -f "$PACKAGESDIR/$file" ]
197         then
198             success=false
199
200             for protocol in $protocols
201             do
202                 printf 'Downloading %s from %s://%s/%s\n' "$file" "$protocol" "$server" "$url" 1>&2
203                 if download "$protocol" "$server" "$url" "$PACKAGESDIR/$file"
204                 then
205                     success=true
206                     break
207                 fi
208             done
209
210             if ! $success
211             then
212                 echo 'Download failed.' 1>&2
213                 continue
214             fi
215         fi
216
217         packagefile="$PACKAGESDIR/$file"
218         break;
219     done
220
221     if [ -z "$packagefile" ]
222     then
223         printf 'No files could be downloaded and/or extracted to install package %s. Giving up.\n' "$package" 1>&2
224         exit 1
225     fi
226
227     if ! [ -f ${packagefile}.sig ]
228     then
229         printf 'Retrieving signature %s\n' "${packagefile}.sig" 1>&2
230         if ! download "$protocol" "$server" "${url}.sig" "${packagefile}.sig"
231         then
232             printf 'Could not retrieve the signature for %s. Giving up.\n' "$packagefile" 1>&2
233             exit 1
234         fi
235     fi
236
237     printf 'Verifying signature %s\n' "${packagefile}.sig" 1>&2
238     if ! "$GPG" --no-default-keyring --keyring "$PWD/$KEYRING" --verify "${packagefile}.sig" "$packagefile"
239     then
240         printf 'bad\nCould not verify the signature for %s. Giving up.\n' "$packagefile" 1>&2
241         exit 1
242     fi
243
244     printf 'Extracting archive %s\n' "$file" 1>&2
245     if ! extract "$packagefile" "$WORKSPACEDIR/$package"
246     then
247         printf 'The package archive %s could not be extracted. Giving up.\n' "$packagefile" 1>&2
248         exit 1
249     fi
250
251     oldwd="$PWD"
252     export PREFIX="$PWD/$TOOLSROOTDIR"
253     cd "$WORKSPACEDIR/$package"
254     $recipe
255     errorcode=$?
256     cd "$oldwd"
257
258     if [ "$errorcode" -ne 0 ]
259     then
260         printf 'Errors occurred while building package %s. Giving up.\n' "$package" 1>&2
261         exit 1
262     fi
263 done
264
265 echo 'Done installing packages' 1>&2