1#! /bin/sh
2#! -*- coding:utf-8; mode:shell-script; -*-
3
4# Download some prerequisites needed by GCC.
5# Run this from the top level of the GCC source tree and the GCC build will do
6# the right thing.  Run it with the `--help` option for more information.
7#
8# (C) 2010-2016 Free Software Foundation
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see http://www.gnu.org/licenses/.
22
23program='download_prerequisites'
24version='(unversioned)'
25
26# MAINTAINERS: If you update the package versions below, please
27# remember to also update the files `contrib/prerequisites.sha512` and
28# `contrib/prerequisites.md5` with the new checksums.
29
30gmp='gmp-6.1.0.tar.bz2'
31mpfr='mpfr-3.1.6.tar.bz2'
32mpc='mpc-1.0.3.tar.gz'
33isl='isl-0.18.tar.bz2'
34
35base_url='http://gcc.gnu.org/pub/gcc/infrastructure/'
36
37echo_archives() {
38    echo "${gmp}"
39    echo "${mpfr}"
40    echo "${mpc}"
41    if [ ${graphite} -gt 0 ]; then echo "${isl}"; fi
42}
43
44graphite=1
45verify=1
46force=0
47OS=$(uname)
48
49case $OS in
50  "Darwin"|"FreeBSD"|"DragonFly"|"AIX")
51    chksum='shasum -a 512 --check'
52  ;;
53  "OpenBSD")
54    chksum='sha512 -c'
55  ;;
56  *)
57    chksum='sha512sum -c'
58  ;;
59esac
60
61if type wget > /dev/null ; then
62  fetch='wget'
63else
64  fetch='curl -LO'
65fi
66chksum_extension='sha512'
67directory='.'
68
69helptext="usage: ${program} [OPTION...]
70
71Downloads some prerequisites needed by GCC.  Run this from the top level of the
72GCC source tree and the GCC build will do the right thing.
73
74The following options are available:
75
76 --directory=DIR  download and unpack packages into DIR instead of '.'
77 --force          download again overwriting existing packages
78 --no-force       do not download existing packages again (default)
79 --isl            download ISL, needed for Graphite loop optimizations (default)
80 --graphite       same as --isl
81 --no-isl         don't download ISL
82 --no-graphite    same as --no-isl
83 --verify         verify package integrity after download (default)
84 --no-verify      don't verify package integrity
85 --sha512         use SHA512 checksum to verify package integrity (default)
86 --md5            use MD5 checksum to verify package integrity
87 --help           show this text and exit
88 --version        show version information and exit
89"
90
91versiontext="${program} ${version}
92Copyright (C) 2016 Free Software Foundation, Inc.
93This is free software; see the source for copying conditions.  There is NO
94warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
95
96die() {
97    echo "error: $@" >&2
98    exit 1
99}
100
101for arg in "$@"
102do
103    case "${arg}" in
104        --help)
105            echo "${helptext}"
106            exit
107            ;;
108        --version)
109            echo "${versiontext}"
110            exit
111            ;;
112    esac
113done
114unset arg
115
116# Emulate Linux's 'md5 --check' on macOS
117md5_check() {
118  # Store the standard input: a line from contrib/prerequisites.md5:
119  md5_checksum_line=$(cat -)
120  # Grab the text before the first space
121  md5_checksum_expected="${md5_checksum_line%% *}"
122  # Grab the text after the first space
123  file_to_check="${md5_checksum_line##* }"
124  # Calculate the md5 checksum for the downloaded file
125  md5_checksum_output=$(md5 -r "${file_to_check}")
126  # Grab the text before the first space
127  md5_checksum_detected="${md5_checksum_output%% *}"
128  [ "${md5_checksum_expected}" == "${md5_checksum_detected}" ] \
129    || die "Cannot verify integrity of possibly corrupted file ${file_to_check}"
130  echo "${file_to_check}: OK"
131}
132
133
134argnext=
135for arg in "$@"
136do
137    if [ "x${argnext}" = x ]
138    then
139        case "${arg}" in
140            --directory)
141                argnext='directory'
142                ;;
143            --directory=*)
144                directory="${arg#--directory=}"
145                ;;
146            --force)
147                force=1
148                ;;
149            --no-force)
150                force=0
151                ;;
152            --isl|--graphite)
153                graphite=1
154                ;;
155            --no-isl|--no-graphite)
156                graphite=0
157                ;;
158            --verify)
159                verify=1
160                ;;
161            --no-verify)
162                verify=0
163                ;;
164            --sha512)
165                case $OS in
166                  "Darwin")
167                    chksum='shasum -a 512 --check'
168                  ;;
169                  *)
170                    chksum='sha512sum --check'
171                  ;;
172                esac
173                chksum_extension='sha512'
174                verify=1
175                ;;
176            --md5)
177                case $OS in
178                  "Darwin")
179                    chksum='md5_check'
180                  ;;
181                  *)
182                    chksum='md5 --check'
183                  ;;
184                esac
185                chksum_extension='md5'
186                verify=1
187                ;;
188            -*)
189                die "unknown option: ${arg}"
190                ;;
191            *)
192                die "too many arguments"
193                ;;
194        esac
195    else
196        case "${arg}" in
197            -*)
198                die "Missing argument for option --${argnext}"
199                ;;
200        esac
201        case "${argnext}" in
202            directory)
203                directory="${arg}"
204                ;;
205            *)
206                die "The impossible has happened"
207                ;;
208        esac
209        argnext=
210    fi
211done
212[ "x${argnext}" = x ] || die "Missing argument for option --${argnext}"
213unset arg argnext
214
215[ -e ./gcc/BASE-VER ]                                                         \
216    || die "You must run this script in the top-level GCC source directory"
217
218[ -d "${directory}" ]                                                         \
219    || die "No such directory: ${directory}"
220
221for ar in $(echo_archives)
222do
223    if [ ${force} -gt 0 ]; then rm -f "${directory}/${ar}"; fi
224    [ -e "${directory}/${ar}" ]                                               \
225        || ( cd "${directory}" && ${fetch} --no-verbose "${base_url}${ar}" )  \
226        || die "Cannot download ${ar} from ${base_url}"
227done
228unset ar
229
230if [ ${verify} -gt 0 ]
231then
232    chksumfile="contrib/prerequisites.${chksum_extension}"
233    [ -r "${chksumfile}" ] || die "No checksums available"
234    for ar in $(echo_archives)
235    do
236        grep "${ar}" "${chksumfile}"                                          \
237            | ( cd "${directory}" && ${chksum} )                              \
238            || die "Cannot verify integrity of possibly corrupted file ${ar}"
239    done
240    unset chksumfile
241fi
242unset ar
243
244for ar in $(echo_archives)
245do
246    package="${ar%.tar*}"
247    if [ ${force} -gt 0 ]; then rm -rf "${directory}/${package}"; fi
248    case $ar in
249    *.gz)
250	uncompress='gzip -d'
251	;;
252    *.bz2)
253	uncompress='bzip2 -d'
254	;;
255    *)
256	uncompress='cat'
257	;;
258    esac
259    [ -e "${directory}/${package}" ]                                          \
260        || ( cd "${directory}" && $uncompress <"${ar}" | tar -xf - )          \
261        || die "Cannot extract package from ${ar}"
262    unset package
263done
264unset ar
265
266for ar in $(echo_archives)
267do
268    target="${directory}/${ar%.tar*}/"
269    linkname="${ar%-*}"
270    if [ ${force} -gt 0 ]; then rm -f "${linkname}"; fi
271    [ -e "${linkname}" ]                                                      \
272        || ln -s "${target}" "${linkname}"                                    \
273        || die "Cannot create symbolic link ${linkname} --> ${target}"
274    unset target linkname
275done
276unset ar
277
278echo "All prerequisites downloaded successfully."
279