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