1
0

setup 7.4 KB

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