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-2021 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.2.1.tar.bz2'
31mpfr='mpfr-4.1.0.tar.bz2'
32mpc='mpc-1.2.1.tar.gz'
33isl='isl-0.24.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
49if type wget > /dev/null ; then
50  fetch='wget'
51else
52  fetch='curl -LO'
53fi
54chksum_extension='sha512'
55directory='.'
56
57helptext="usage: ${program} [OPTION...]
58
59Downloads some prerequisites needed by GCC.  Run this from the top level of the
60GCC source tree and the GCC build will do the right thing.
61
62The following options are available:
63
64 --directory=DIR  download and unpack packages into DIR instead of '.'
65 --force          download again overwriting existing packages
66 --no-force       do not download existing packages again (default)
67 --isl            download ISL, needed for Graphite loop optimizations (default)
68 --graphite       same as --isl
69 --no-isl         don't download ISL
70 --no-graphite    same as --no-isl
71 --verify         verify package integrity after download (default)
72 --no-verify      don't verify package integrity
73 --sha512         use SHA512 checksum to verify package integrity (default)
74 --md5            use MD5 checksum to verify package integrity
75 --help           show this text and exit
76 --version        show version information and exit
77"
78
79versiontext="${program} ${version}
80Copyright (C) 2016-2021 Free Software Foundation, Inc.
81This is free software; see the source for copying conditions.  There is NO
82warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
83
84die() {
85    echo "error: $@" >&2
86    exit 1
87}
88
89for arg in "$@"
90do
91    case "${arg}" in
92        --help)
93            echo "${helptext}"
94            exit
95            ;;
96        --version)
97            echo "${versiontext}"
98            exit
99            ;;
100    esac
101done
102unset arg
103
104# Emulate Linux's 'md5sum --check' on macOS
105md5_check() {
106  # Store the standard input: a line from contrib/prerequisites.md5:
107  md5_checksum_line=$(cat -)
108  # Grab the text before the first space
109  md5_checksum_expected="${md5_checksum_line%% *}"
110  # Grab the text after the first space
111  file_to_check="${md5_checksum_line##* }"
112  # Calculate the md5 checksum for the downloaded file
113  md5_checksum_output=$(md5 -r "${file_to_check}")
114  # Grab the text before the first space
115  md5_checksum_detected="${md5_checksum_output%% *}"
116  [ "${md5_checksum_expected}" = "${md5_checksum_detected}" ] \
117    || die "Cannot verify integrity of possibly corrupted file ${file_to_check}"
118  echo "${file_to_check}: OK"
119}
120
121
122argnext=
123for arg in "$@"
124do
125    if [ "x${argnext}" = x ]
126    then
127        case "${arg}" in
128            --directory)
129                argnext='directory'
130                ;;
131            --directory=*)
132                directory="${arg#--directory=}"
133                ;;
134            --force)
135                force=1
136                ;;
137            --no-force)
138                force=0
139                ;;
140            --isl|--graphite)
141                graphite=1
142                ;;
143            --no-isl|--no-graphite)
144                graphite=0
145                ;;
146            --verify)
147                verify=1
148                ;;
149            --no-verify)
150                verify=0
151                ;;
152            --sha512)
153                chksum_extension='sha512'
154                verify=1
155                ;;
156            --md5)
157                chksum_extension='md5'
158                verify=1
159                ;;
160            -*)
161                die "unknown option: ${arg}"
162                ;;
163            *)
164                die "too many arguments"
165                ;;
166        esac
167    else
168        case "${arg}" in
169            -*)
170                die "Missing argument for option --${argnext}"
171                ;;
172        esac
173        case "${argnext}" in
174            directory)
175                directory="${arg}"
176                ;;
177            *)
178                die "The impossible has happened"
179                ;;
180        esac
181        argnext=
182    fi
183done
184[ "x${argnext}" = x ] || die "Missing argument for option --${argnext}"
185unset arg argnext
186
187case $chksum_extension in
188  sha512)
189    case $OS in
190      "Darwin"|"FreeBSD"|"DragonFly"|"AIX")
191        chksum='shasum -a 512 --check'
192      ;;
193      "OpenBSD")
194        chksum='sha512 -c'
195      ;;
196      *)
197        chksum='sha512sum -c'
198      ;;
199    esac
200  ;;
201  md5)
202    case $OS in
203      "Darwin")
204        chksum='md5_check'
205      ;;
206      *)
207        chksum='md5sum -c'
208      ;;
209    esac
210    ;;
211  *)
212    die "Unkown checksum $chksum_extension"
213  ;;
214esac
215
216[ -e ./gcc/BASE-VER ]                                                         \
217    || die "You must run this script in the top-level GCC source directory"
218
219[ -d "${directory}" ]                                                         \
220    || die "No such directory: ${directory}"
221
222for ar in $(echo_archives)
223do
224    if [ ${force} -gt 0 ]; then rm -f "${directory}/${ar}"; fi
225    [ -e "${directory}/${ar}" ]                                               \
226        || ( cd "${directory}" && ${fetch} --no-verbose "${base_url}${ar}" )  \
227        || die "Cannot download ${ar} from ${base_url}"
228done
229unset ar
230
231if [ ${verify} -gt 0 ]
232then
233    chksumfile="contrib/prerequisites.${chksum_extension}"
234    [ -r "${chksumfile}" ] || die "No checksums available"
235    for ar in $(echo_archives)
236    do
237        grep "${ar}" "${chksumfile}"                                          \
238            | ( cd "${directory}" && ${chksum} )                              \
239            || die "Cannot verify integrity of possibly corrupted file ${ar}"
240    done
241    unset chksumfile
242fi
243unset ar
244
245for ar in $(echo_archives)
246do
247    package="${ar%.tar*}"
248    if [ ${force} -gt 0 ]; then rm -rf "${directory}/${package}"; fi
249    case $ar in
250    *.gz)
251	uncompress='gzip -d'
252	;;
253    *.bz2)
254	uncompress='bzip2 -d'
255	;;
256    *)
257	uncompress='cat'
258	;;
259    esac
260    [ -e "${directory}/${package}" ]                                          \
261        || ( cd "${directory}" && $uncompress <"${ar}" | tar -xf - )          \
262        || die "Cannot extract package from ${ar}"
263    unset package
264done
265unset ar
266
267for ar in $(echo_archives)
268do
269    target="${directory}/${ar%.tar*}/"
270    linkname="${ar%-*}"
271    if [ ${force} -gt 0 ]; then rm -f "${linkname}"; fi
272    [ -e "${linkname}" ]                                                      \
273        || ln -s "${target}" "${linkname}"                                    \
274        || die "Cannot create symbolic link ${linkname} --> ${target}"
275    unset target linkname
276done
277unset ar
278
279echo "All prerequisites downloaded successfully."
280