forked from tomopy/tomopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
330 lines (280 loc) · 11.3 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
'''install necessary external libraries for TomoPy'''
from __future__ import print_function
import os
import platform
import tempfile
import sys
import tarfile
import hashlib
import shlex
from distutils import version
import subprocess
import urllib
VERBOSE = False
def get_cmd_opts():
import argparse
doc = __doc__.strip()
parser = argparse.ArgumentParser(prog=sys.argv[0], description=doc)
prefix = '/usr/local'
msg = 'Installation directory (default: ' + prefix + ')'
parser.add_argument('installation_path',
action='store',
nargs='?',
help=msg,
default=prefix)
parser.add_argument('--fftw',
action='store_true',
dest='install_fftw',
help='install fftw library',
default=False)
parser.add_argument('--boost',
action='store_true',
dest='install_boost',
help='install Boost library',
default=False)
parser.add_argument('--hdf5',
action='store_true',
dest='install_hdf5',
help='install HDF5 library',
default=False)
fc = 'gfortran'
msg = 'specify the Fortran compiler (default: '+fc+')'
parser.add_argument('--fc-compiler',
action='store',
dest='fc_compiler',
help=msg,
default=fc)
cc = 'gcc'
msg = 'specify the C compiler (default: '+cc+')'
parser.add_argument('--cc-compiler',
action='store',
dest='cc_compiler',
help=msg,
default=cc)
cxx = 'g++'
msg = 'specify the C++ compiler (default: '+cxx+')'
parser.add_argument('--cxx-compiler',
action='store',
dest='cxx_compiler',
help=msg,
default=cxx)
parser.add_argument('--verbose',
action='store_true',
dest='verbose',
help='send standard output to terminal',
default=False)
return parser.parse_args()
def run(command, logfile):
def do_bash(command):
return subprocess.call(command,
shell=True,
executable="/bin/bash")
if VERBOSE:
status = do_bash(command + ' 2>&1 | tee ' + logfile)
else:
status = do_bash(command + ' >& ' + logfile)
if status != 0:
print("Installation failed.")
sys.exit(1)
else:
return
def p_communicate(shell_command):
'''return the output of the shell shell_command'''
p = subprocess.Popen(shlex.split(shell_command), stdout=subprocess.PIPE)
return (p.communicate()[0]).decode('ascii')
def p_get_first_line(shell_command):
'''return the first line of output from the shell command'''
return p_communicate(shell_command).strip().splitlines()[0]
def download_expand_tarball(name, URL, SHA1):
print(" ")
print("Installing " + name)
ext_file = os.path.basename(URL)
download_it = True
if os.path.exists(ext_file):
sha1 = hashlib.sha1(open(ext_file, 'rb').read()).hexdigest()
download_it = sha1 != SHA1
if download_it:
print(" -> file exists but incorrect SHA1, re-downloading")
else:
print(" -> file exists, skipping download")
if download_it:
print(" -> downloading")
open(ext_file, 'wb').write(urllib.urlopen(URL).read())
print(" -> expanding tarfile")
t = tarfile.open(ext_file, 'r:gz')
t.extractall()
os.chdir(ext_file.replace('.tar.gz', ''))
def install_fftw(prefix, fc, cc, cxx):
FFTW_URL = "http://www.fftw.org/fftw-3.3.3.tar.gz"
FFTW_SHA1 = '11487180928d05746d431ebe7a176b52fe205cf9'
download_expand_tarball('FFTW', FFTW_URL, FFTW_SHA1)
print(" -> configuring")
command = './configure '
command += ' F77="' + fc + '"'
command += ' FC="' + fc + '"'
command += ' CC="' + cc + '"'
command += ' CXX="' + cxx + '"'
command += ' --enable-float'
command += ' --enable-shared'
command += ' --prefix=' + prefix
run(command, 'log_configure')
print(" -> making")
run('make', 'log_make')
print(" -> installing")
run('make install', 'log_make_install')
def install_boost(prefix):
BOOST_URL = "http://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.gz"
BOOST_SHA1 = '61ed0e57d3c7c8985805bb0682de3f4c65f4b6e5'
download_expand_tarball('Boost C++', BOOST_URL, BOOST_SHA1)
print(" -> configuring")
command = './bootstrap.sh'
command += ' --with-libraries=system,thread,date_time'
command += ' --prefix=' + prefix
run(command, 'log_configure')
print(" -> making")
run('./b2', 'log_make')
print(" -> installing")
run('./b2 install', 'log_make_install')
def install_zlib(prefix):
ZLIB_URL = "http://downloads.sourceforge.net/project/libpng/zlib/1.2.7/zlib-1.2.7.tar.gz"
ZLIB_SHA1 = '4aa358a95d1e5774603e6fa149c926a80df43559'
download_expand_tarball('ZLIB (for HDF5)', ZLIB_URL, ZLIB_SHA1)
print(" -> configuring")
run('./configure --prefix=' + prefix, 'log_configure')
print(" -> making")
run('make', 'log_make')
print(" -> installing")
run('make install', 'log_make_install')
def install_hdf5(prefix, fc, is_g95, is_gfortran, gfortran_version):
HDF5_URL = 'http://www.hdfgroup.org/ftp/HDF5/releases/hdf5-1.8.12/src/hdf5-1.8.12.tar.gz'
HDF5_SHA1 = '965d954d596cfa694f3260856a6406ea69e46e68'
download_expand_tarball('HDF5', HDF5_URL, HDF5_SHA1)
# SPECIAL CASE - g95 requires patching
if is_g95:
print(" -> SPECIAL CASE: patching for g95")
conf = open('config/gnu-fflags', 'r').read()
conf = conf.replace('-Wconversion -Wunderflow ', '')
open('config/gnu-fflags', 'w').write(conf)
# SPECIAL CASE - gfortran 4.5 and prior requires patching
if is_gfortran and gfortran_version < version.LooseVersion('4.6.0'):
print(" -> SPECIAL CASE: patching for gfortran 4.5 and prior")
conf = open('fortran/src/H5test_kind_SIZEOF.f90', 'r').read()
conf = conf.replace('DO i = 1,100', 'DO i = 1,18')
open('fortran/src/H5test_kind_SIZEOF.f90', 'w').write(conf)
print(" -> configuring")
command = './configure'
command += ' FC="' + fc + '"'
command += ' --with-zlib={prefix}/include,{prefix}/lib'.format(prefix=prefix)
command += ' --prefix={prefix}'.format(prefix=prefix)
run(command, 'log_configure')
print(" -> making")
run('make', 'log_make')
print(" -> installing")
run('make install', 'log_make_install')
def main():
global VERBOSE
cmd_opts = get_cmd_opts()
INSTALL_HDF5 = cmd_opts.install_hdf5
INSTALL_FFTW = cmd_opts.install_fftw
INSTALL_BOOST = cmd_opts.install_boost
VERBOSE = cmd_opts.verbose
fc = cmd_opts.fc_compiler
cc = cmd_opts.cc_compiler
cxx = cmd_opts.cxx_compiler
prefix = cmd_opts.installation_path
if (INSTALL_HDF5 or INSTALL_FFTW or INSTALL_BOOST) is False:
print("Nothing to install! Use 'python install.py --help' for options.")
sys.exit(1)
print(" ")
print("Determining system setup")
start_dir = os.path.abspath('.')
prefix = os.path.abspath(prefix)
print(" -> changing to work directory... ")
work_dir = tempfile.mkdtemp()
os.chdir(work_dir)
print(" %s" % work_dir)
print(" -> determining platform... ", end=' ')
# Determine system
system = platform.uname()[0]
# Determine system version
if system == 'Darwin':
# Determine MacOS X version
system_version = "MacOS " + platform.mac_ver()[0]
else:
system_version = ''
print(system, '/', system_version)
# The following section deals with issues that occur
# when using the intel fortran compiler with gcc.
# Check whether C compiler is gcc
# FIXME: does not find 'gcc' : gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
output = p_get_first_line(cc + ' --version')
# is_gcc = 'GCC' in output
is_gcc = 'GCC' in output.lower()
# get the Fortran compiler response to a version request
try:
output = p_get_first_line(fc + ' --version')
except OSError:
msg = 'Fortran compiler not found: ' + fc
raise OSError, msg
# Check whether Fortran compiler is ifort
# FIXME: is_ifort is unused
is_ifort = '(IFORT)' in output
# Check whether Fortran compiler is g95
is_g95 = 'g95' in output
# Check whether Fortran compiler is pgfortran
# FIXME: is_pgfortran is unused
is_pgfortran = 'pgfortran' in output or \
'pgf95' in output
# Check whether Fortran compiler is gfortran
is_gfortran = 'GNU Fortran' in output
if is_gfortran:
gfortran_version = version.LooseVersion(p_get_first_line(fc + ' -dumpversion').split()[-1])
# On MacOS X, when using gcc 4.5 or 4.6, the fortran compiler needs to link
# with libgcc_eh that is in the gcc library directory. This is not needed if
# using gfortran 4.5 or 4.6, but it's easier to just add it for all
# compilers.
if system == 'Darwin' and is_gcc:
# Determine gcc version
gcc_version = version.LooseVersion(p_get_first_line(cc + ' -dumpversion'))
if gcc_version >= version.LooseVersion('4.5.0'):
output = p_get_first_line(cc + ' -print-search-dirs')
if output.startswith('install:'):
libs = output.split(':', 1)[1].strip()
libs = ' -L' + libs.replace(':', '-L')
libs += ' -lgcc_eh'
fc += libs
print(" -> SPECIAL CASE: adjusting fortran compiler:", fc)
else:
print("ERROR: unexpected output for %s -print-search-dirs: %s" % (cc, output))
sys.exit(1)
if INSTALL_FFTW:
install_fftw(prefix, fc, cc, cxx)
os.chdir(work_dir)
if INSTALL_BOOST:
install_boost(prefix)
os.chdir(work_dir)
if INSTALL_HDF5:
install_zlib(prefix)
os.chdir(work_dir)
install_hdf5(prefix, fc, is_g95, is_gfortran, gfortran_version)
os.chdir(work_dir)
# Go back to starting directory
os.chdir(start_dir)
print(" ")
print("Installation successful!!!")
print(" ")
print("Before you start installing TomoPy, don't forget to:")
print(" ")
print(" 1) Set LD_LIBRARY_PATH permanently in your shell to: %s" % prefix + '/lib')
print(" hint: setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:%s" % prefix + '/lib')
print(" ")
print(" 2) Set C_INCLUDE_PATH permanently in your shell to: %s" % prefix + '/include')
print(" hint: setenv C_INCLUDE_PATH ${C_INCLUDE_PATH}:%s" % prefix + '/include')
print(" ")
if __name__ == '__main__':
#sys.argv = sys.argv[:1]
# sys.argv.append('-h')
#sys.argv += '/tmp/sandbox --verbose --boost --fftw'.split()
#sys.argv += '/tmp/sandbox --verbose --hdf5'.split()
main()