get-win32-tarballs: Rework handling of hashing
[ghc.git] / mk / get-win32-tarballs.sh
1 #!/usr/bin/env sh
2
3 tarball_dir='ghc-tarballs'
4 missing_files=0
5
6 fail() {
7 echo >&2
8 echo "$1" >&2
9 exit 1
10 }
11
12 download_file() {
13 local file_url="$1"
14 local dest_file="$2"
15 local description="$3"
16 local extra_curl_opts="$4"
17 local dest_dir="$(dirname $dest_file)"
18
19 if ! test -f "${dest_file}"
20 then
21 local curl_cmd="curl -L ${file_url} -o ${dest_file} --create-dirs -# ${extra_curl_opts}"
22 if test "$download" = "0"
23 then
24 echo "ERROR: Missing ${description}" >&2
25 echo "${file_url}"
26 missing_files=1
27 return
28 else
29 echo "Downloading ${description} to ${dest_dir}..."
30 $curl_cmd || {
31 rm -f "${dest_file}"
32 fail "ERROR: Download failed."
33 }
34 fi
35 fi
36
37 if test "$sigs" = "1"
38 then
39 echo "Downloading ${description} (signature) to ${dest_dir}..."
40 local curl_cmd="curl -L ${file_url}.sig -o ${dest_file}.sig --create-dirs -# ${extra_curl_opts}"
41 $curl_cmd || {
42 rm -f "${dest_file}.sig"
43 fail "ERROR: Download failed."
44 }
45 fi
46
47 if test "$verify" = "1"
48 then
49 grep "${dest_file}" mk/win32-tarballs.md5sum | md5sum --quiet -c - ||
50 fail "ERROR: ${description} appears to be corrupted, please delete it and try again."
51 fi
52 }
53
54 download_mingw() {
55 if test "$mingw_arch" = "sources"
56 then
57 local mingw_url=`echo "$1" | sed -e 's/-any\.pkg\.tar\.xz/\.src\.tar\.gz/' \
58 -e 's/-sources-/-/' \
59 -e 's/-libwinpthread-git-/-winpthreads-git-/' `
60 else
61 local mingw_url="$1"
62 fi
63
64 local mingw_toolchain="$(basename $mingw_url)"
65 local mingw_w64="${tarball_dir}/${tarball_dest_dir}/${mingw_toolchain}"
66
67 download_file "${mingw_url}" "${mingw_w64}" "${mingw_toolchain}"
68
69 # Mark the tree as needing updates by deleting the folder
70 if test -d inplace/mingw && test inplace/mingw -ot "$mingw_w64" ; then
71 echo "In-tree MinGW-w64 tree requires updates..."
72 rm -rf inplace/mingw
73 fi
74 }
75
76 download_tarballs() {
77 local mingw_base_url="http://repo.msys2.org/mingw"
78 #local mingw_base_url="https://downloads.haskell.org/~ghc/mingw"
79 local package_prefix="mingw-w64"
80 local format_url="${mingw_base_url}/${mingw_arch}/${package_prefix}-${mingw_arch}"
81
82 download_mingw "${format_url}-crt-git-5.0.0.4795.e3d96cb1-1-any.pkg.tar.xz"
83 download_mingw "${format_url}-winpthreads-git-5.0.0.4741.2c8939a-1-any.pkg.tar.xz"
84 download_mingw "${format_url}-headers-git-5.0.0.4747.0f8f626-1-any.pkg.tar.xz"
85 download_mingw "${format_url}-libwinpthread-git-5.0.0.4741.2c8939a-1-any.pkg.tar.xz"
86 download_mingw "${format_url}-zlib-1.2.8-9-any.pkg.tar.xz"
87 download_mingw "${format_url}-isl-0.17.1-1-any.pkg.tar.xz"
88 download_mingw "${format_url}-mpc-1.0.3-2-any.pkg.tar.xz"
89 download_mingw "${format_url}-mpfr-3.1.4.p3-4-any.pkg.tar.xz"
90 download_mingw "${format_url}-gmp-6.1.1-1-any.pkg.tar.xz"
91 download_mingw "${format_url}-gcc-libs-6.2.0-2-any.pkg.tar.xz"
92 download_mingw "${format_url}-binutils-2.27-2-any.pkg.tar.xz"
93 download_mingw "${format_url}-libidn-1.32-3-any.pkg.tar.xz"
94 download_mingw "${format_url}-gcc-6.2.0-2-any.pkg.tar.xz"
95
96 download_file "https://github.com/ghc/ghc-tarballs/blob/master/perl/ghc-perl-1.tar.gz?raw=true" "ghc-tarballs/perl/ghc-perl-1.tar.gz" "Windows Perl binary distributions" "--insecure"
97
98 if ! test "$missing_files" = "0"
99 then
100 exit 2
101 fi
102 }
103
104 download_i386() {
105 mingw_arch="i686"
106 tarball_dest_dir="mingw-w64/x86"
107 download_tarballs
108 }
109
110 download_x86_64() {
111 mingw_arch="x86_64"
112 tarball_dest_dir="mingw-w64/x86_64"
113 download_tarballs
114 }
115
116 download_sources() {
117 mingw_arch="sources"
118 tarball_dest_dir="mingw-w64/sources"
119 download_tarballs
120 }
121
122 usage() {
123 echo "$0 - Download GHC mingw toolchain tarballs"
124 echo
125 echo "Usage: $0 <action> <arch>"
126 echo
127 echo "Where <action> is one of,"
128 echo " download download the necessary tarballs for the given architecture"
129 echo " fetch download the necessary tarballs for the given architecture but doesn't verify their md5."d
130 echo " verify verify the existence and correctness of the necessary tarballs"
131 echo "and <arch> is one of i386, x86_64,all or mirror (which includes sources)"
132 }
133
134 case $1 in
135 download)
136 download=1
137 verify=1
138 sigs=0
139 ;;
140 fetch)
141 download=1
142 verify=0
143 ;;
144 verify)
145 download=0
146 verify=1
147 ;;
148 *)
149 usage
150 exit 1
151 ;;
152 esac
153
154 case $2 in
155 i386)
156 download_i386
157 ;;
158 x86_64)
159 download_x86_64
160 ;;
161 all)
162 download_i386
163 download_x86_64
164 ;;
165 mirror)
166 sigs=1
167 download_i386
168 download_x86_64
169 verify=0
170 download_sources
171 ;;
172 *)
173 usage
174 exit 1
175 ;;
176 esac