ncval_test: Make test failures easier to understand
[nativeclient.git] / tools / linux.x86_64.prep.sh
blob3de9636a060f64cd193c7d854d6fc55181508ef9
1 #!/bin/bash
2 PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
4 umask=077
5 d=${TMPDIR:-/tmp}/nacl64.$$
6 if ! mkdir "$d" > /dev/null 2>&1
7 then
8 cat >&2 << EOF
9 Could not create safe temporary directory "$d".
11 ABORTING.
12 EOF
13 exit 1
15 f="$d/x.c"
16 fout="$d/x"
17 trap 'rm -fr "$d"; exit' 0 1 2 3 15
19 function isRunningAsRoot() {
20 whoami | grep -q 'root'
23 function try() {
24 cat > "$f" << EOF
25 #include <openssl/aes.h>
26 int main(void) {
27 AES_KEY k;
28 char key[AES_BLOCK_SIZE];
30 AES_set_encrypt_key(key, AES_BLOCK_SIZE * 8, &k);
31 return 0;
33 EOF
34 if ! gcc -o "$fout" "$f" -lssl -lcrypto > /dev/null 2>&1
35 then
36 return 1
38 return 0
41 if ! [ -f /usr/include/openssl/aes.h ]
42 then
43 cat >&2 << \EOF
44 libssl-dev not installed. Installing...
45 EOF
46 if apt-get install libssl-dev
47 then
48 echo "Success." >&2
49 else
50 cat >&2 << \EOF
51 ... could not install libssl-dev.
53 ABORTING.
54 EOF
55 exit 1
59 cat > "$f" << \EOF
60 int main(void) { return sizeof(long) == 8; }
61 EOF
62 gcc -o "$fout" "$f" > /dev/null 2>&1
64 if "$fout"
65 then
66 cat << \EOF
67 You do not appear to be using an x86_64 system. This rest of this script
68 is not required.
69 EOF
70 exit 0
73 if ! isRunningAsRoot
74 then
75 cat >&2 << \EOF
76 Not running as root, so cannot install libraries/links.
77 Note: you probably will need to copy this script to the local file system
78 (and off of NFS) in order to run this script as root.
80 ABORTING.
81 EOF
82 exit 1
85 if ! [ -d /usr/lib32 ]
86 then
87 cat >&2 << \EOF
88 ... you do not have /usr/lib32 at all!
90 First, must install ia32-libs...
91 EOF
92 if apt-get install ia32-libs > /dev/null 2>&1
93 then
94 echo "... done" >&2
95 else
96 cat >&2 << \EOF
97 ... failed to install ia32-libs.
99 ABORTING.
101 exit 1
105 if ! [ -f /usr/include/gtk-2.0/gtk/gtk.h ]
106 then
107 cat >&2 << \EOF
108 ... you do not have libgtk2.0-dev. Installing...
110 if apt-get install libgtk2.0-dev > /dev/null 2>&1
111 then
112 echo "... done" >&2
113 else
114 cat >&2 << \EOF
115 ... failed to install libgtk2.0-dev.
117 ABORTING
119 exit 1
123 # now check for symlinks
125 function do_symlink {
126 declare -a vers
128 target="/usr/lib32/$1"
130 if ! [ -f "$target" ]
131 then
132 vers=($(echo "$target".*))
133 if (( ${#vers[@]} != 1 ))
134 then
135 cat >&2 << EOF
136 More than one version of $target.* exists. This script will
137 picks the lexicographically greatest one.
139 vers=("${vers[${#vers[@]}-1]}")
140 echo "Using $vers" >&2
142 cat >&2 <<EOF
143 Linking $target to $vers
145 ln -s "$vers" "$target"
149 declare -a dev_libs
150 dev_libs=(libatk-1.0.so libgtk-x11-2.0.so libgdk-x11-2.0.so \
151 libgdk_pixbuf-2.0.so libpangocairo-1.0.so libpango-1.0.so \
152 libcairo.so libgobject-2.0.so libgmodule-2.0.so libglib-2.0.so \
153 libXt.so libX11.so)
155 for lib in "${dev_libs[@]}"
157 do_symlink "$lib"
158 done
160 if [ -f /usr/lib32/libcrypto.so -a -f /usr/lib32/libssl.so ] && try
161 then
162 echo "You already have /usr/lib32/libcrypto.so and libssl.so"
163 else
165 # we omit libssl.so and libcrypto.so from dev_libs so we can ensure
166 # that ssl_vers is consistent between the two.
168 declare -a ssl_libs
169 ssl_libs=($(echo /usr/lib32/libssl.so.*))
170 if (( ${#ssl_libs[@]} != 1 ))
171 then
172 cat >&2 << \EOF
173 WARNING: More than one versions of /usr/lib32/libssl.so.* This script will
174 pick the lexicographically greatest one.
176 ssl_libs=(${ssl_libs[${#ssl_libs[@]}-1]})
177 echo "Using $ssl_libs" >&2
179 ssl_lib=${ssl_libs[0]}
181 ssl_vers=$(expr \( $ssl_lib : '/usr/lib32/libssl\.so\.\(.*\)' \))
182 case "$ssl_vers" in
183 ''|'*')
184 cat >&2 << \EOF
185 /usr/lib32/libssl.so.SSLVERSION missing. This should have been installed
186 as part of the ia32-libs package.
188 ABORTING.
190 exit 1
192 esac
193 if ! [ -f /usr/lib32/libcrypto.so.$ssl_vers ]
194 then
195 cat >&2 << EOF
196 Version mismatch: /usr/lib32/libssl.so.$ssl_vers exists, but
197 /usr/lib32/libcrypto.so.$ssl_vers does not!
199 ABORTING.
201 exit 1
203 echo "SSL version $ssl_vers shared libraries are available"
205 echo "Attempting to install ia32-libs-dev to get crypto libraries..." >&2
206 apt-get install ia32-libs-dev > /dev/null 2>&1
207 if [ -f /usr/lib32/libcrypto.so -a -f /usr/lib32/libssl.so ] && try
208 then
209 echo "Done."
210 exit 0
212 cat >&2 << \EOF
213 ... failed.
215 Attempting to install lib32bz2-dev to get crypto libraries...
217 apt-get install lib32bz2-dev > /dev/null 2>&1
218 if [ -f /usr/lib32/libcrypto.so -a -f /usr/lib32/libssl.so ] && try
219 then
220 echo "Done."
221 exit 0
223 cat >&2 << \EOF
224 ... failed.
226 Falling back to installing symlinks manually....
228 if ! ln -s libcrypto.so.$ssl_vers /usr/lib32/libcrypto.so
229 then
230 cat >&2 << \EOF
231 ... failed. Could not make symbolic links in /usr/lib32.
233 ABORTING.
235 exit 1
237 ln -s libssl.so.$ssl_vers /usr/lib32/libssl.so
239 if try
240 then
241 echo "Success."
242 else
243 echo "FAILED." >&2
244 exit 1