-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·398 lines (353 loc) · 12.1 KB
/
setup
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env python
# vim:ft=python
#
# primitive frontend to cmake
# (c) Radovan Bast <[email protected]>
# (c) Jonas Juselius <[email protected]>
# licensed under the GNU Lesser General Public License
import os
import sys
import string
import re
import subprocess
import shutil
import argparse
if sys.version < '2.4':
print('requires python version >= 2.4')
sys.exit(1)
root_directory = os.path.realpath(__file__)[:-5]
# initialize parser
parser = argparse.ArgumentParser(description="Setup build configurations.")
# define options
parser.add_argument('builddir', nargs='?',
action='store',
help='build directory [default: %(default)s]',
metavar='build path')
group = parser.add_argument_group('Basic options')
group.add_argument('--fc',
action='store',
default=None,
help='set the Fortran compiler [default: pick automatically or based on FC=...]',
metavar='STRING')
group.add_argument('--cc',
action='store',
default=None,
help='set the C compiler [default: pick automatically or based on CC=...]',
metavar='STRING')
group.add_argument('--cxx',
action='store',
default=None,
help='set the C++ compiler [default: pick automatically or based on CXX=...]',
metavar='STRING')
group.add_argument('--mpi',
nargs='?',
action='store',
choices=('on','off'),
default='off',
const='on',
help='enable MPI [default: %(default)s]')
group.add_argument('--openmp',
nargs='?',
action='store',
choices=('on','off'),
default='off',
const='on',
help='enable OpenMP [default: %(default)s]')
group.add_argument('--blas',
nargs='?',
action='store',
choices=('on','off'),
default='off',
const='on',
help='enable BLAS [default: %(default)s]')
group.add_argument('--lapack',
nargs='?',
action='store',
choices=('on','off'),
default='off',
const='on',
help='enable LAPACK [default: %(default)s]')
group.add_argument('--blas-dir',
action='store',
default=None,
help='directory containing BLAS libraries [default: pick automatically based on BLAS_ROOT (see INSTALL)]',
metavar='PATH')
group.add_argument('--lapack-dir',
action='store',
default=None,
help='directory containing LAPACK libraries [default: pick automatically based on LAPACK_ROOT (see INSTALL)]',
metavar='PATH')
group.add_argument('--show',
action='store_true',
default=False,
help='show cmake command and exit [default: %(default)s]')
group = parser.add_argument_group('Change default paths')
group.add_argument('--prefix',
action='store',
default=None,
help='set the install path for make install [default: %(default)s]',
metavar='PATH')
group.add_argument('--cmake',
action='store',
dest='alternative_cmake',
default=None,
help='give full path to alternative cmake binary (use this if default cmake is too old)',
metavar='PATH')
group = parser.add_argument_group('Advanced options')
group.add_argument('--force',
action='store_true',
default=False,
help='run setup even if build directory exists [default: %(default)s]')
group.add_argument('--clean',
action='store_true',
default=False,
help='remove the build directory exists [default: %(default)s]')
group.add_argument('--debug',
action='store_true',
default=False,
help='build in debug mode [default: %(default)s]')
group.add_argument('--release',
action='store_true',
default=False,
help='build in release mode (full optimization) [default: %(default)s]')
group.add_argument('--tests',
nargs='?',
action='store',
choices=('on','off'),
default='on',
const='on',
help='build test suite [default: %(default)s]')
group.add_argument('--check',
action='store_true',
default=False,
help='enable bounds checking [default: %(default)s]')
group.add_argument('--coverage',
action='store_true',
default=False,
help='enable code coverage [default: %(default)s]')
group.add_argument('--profiling',
action='store_true',
default=False,
help='enable profiling [default: %(default)s]')
group.add_argument('--nochecks',
action='store_true',
default=False,
help='disable compatibility checks [default: %(default)s]')
group.add_argument('--static',
action='store_true',
default=False,
help='link statically [default: %(default)s]')
group.add_argument('-D',
action="append",
dest='define',
default=[],
help='forward directly to cmake (example: -D ENABLE_THIS=1 -D ENABLE_THAT=1); \
you can also forward CPP defintions all the way to the program \
(example: -D CPP="-DDEBUG")',
metavar='STRING')
group.add_argument('--host',
action='store',
default=None,
help="use predefined defaults for 'host'",
metavar='STRING')
#group.add_argument('--netcdf-dir',
# action='store',
# default=None,
# help='directory containing NETCDF libraries [default: pick automatically based on NETCDF_ROOT (see INSTALL.rst)]',
# metavar='PATH')
#group.add_argument('--eigen3-dir',
# action='store',
# help='path to Eigen3 [default: %(default)s]',
# metavar='PATH')
#group.add_argument('--boost-dir',
# action='store',
# help='path to Boost C++ libraries [default: %(default)s]',
# metavar='PATH')
# process input
args = parser.parse_args()
if not args.builddir:
if args.debug and args.release:
args.builddir = root_directory + 'relwithdeb/'
elif args.release:
args.builddir = root_directory + 'release/'
else:
args.builddir = root_directory + 'build/'
elif args.builddir[0] not in '/~':
args.builddir = root_directory + args.builddir
def check_cmake_exists():
p = subprocess.Popen('cmake --version',
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
if not ('cmake version' in p.communicate()[0]):
print(' This code is built using CMake')
print('')
print(' CMake is not found')
print(' get CMake at http://www.cmake.org/')
print(' on many clusters CMake is installed')
print(' but you have to load it first:')
print(' $ module load cmake')
sys.exit()
def gen_cmake_command():
# create cmake command from flags
command = ''
if args.fc:
command += ' FC=%s' % args.fc
if args.cc:
command += ' CC=%s' % args.cc
if args.cxx:
command += ' CXX=%s' % args.cxx
if args.alternative_cmake:
command += ' %s' % args.alternative_cmake
else:
command += ' cmake'
# if compiler starts with 'mp' turn on mpi
# it is possible to call compilers with long paths
if args.cc and (os.path.basename(args.cc)[:2].lower() == 'mp') or \
args.cxx and (os.path.basename(args.cxx)[:2].lower() == 'mp') or \
args.fc and (os.path.basename(args.fc)[:2].lower() == 'mp'):
args.mpi = 'on'
command += ' -DENABLE_MPI=%s' % args.mpi.upper()
command += ' -DENABLE_OPENMP=%s' % args.openmp.upper()
command += ' -DENABLE_BLAS=%s' % args.blas.upper()
command += ' -DENABLE_LAPACK=%s' % args.lapack.upper()
command += ' -DENABLE_TESTS=%s' % args.tests.upper()
if args.blas_dir:
command += ' -DBLAS_ROOT={0}'.format(abspath(args.blas_dir))
if args.lapack_dir:
command += ' -DLAPACK_ROOT={0}'.format(abspath(args.lapack_dir))
if args.check:
command += ' -DENABLE_BOUNDS_CHECK=ON'
if args.coverage:
command += ' -DENABLE_CODE_COVERAGE=ON'
if args.profiling:
command += ' -DENABLE_PROFILING=ON'
if args.nochecks:
command += ' -DENABLE_SANITY_CHECKS=OFF'
if args.static:
command += ' -DENABLE_STATIC_LINKING=ON'
if args.prefix:
command += ' -DCMAKE_INSTALL_PREFIX=' + args.prefix
else:
command += ' -DCMAKE_INSTALL_PREFIX=' + args.builddir
if args.debug and args.release:
command += ' -DCMAKE_BUILD_TYPE=Relwithdebinfo'
elif args.release:
command += ' -DCMAKE_BUILD_TYPE=Release'
else:
command += ' -DCMAKE_BUILD_TYPE=Debug'
if args.eigen3_dir:
command += ' -DEIGEN3_ROOT={0}'.format(args.eigen3_dir)
if args.boost_dir:
command += ' -DBOOST_ROOT={0}'.format(args.boost_dir)
if args.define:
for definition in args.define:
command += ' -D%s' % definition
command += ' %s' % root_directory
print('%s\n' % command)
if args.show:
sys.exit()
return command
def print_build_help(build_path):
print(' configure step is done')
print(' now you need to compile the sources')
print('')
print(' to compile with configured parameters (recommended):')
print(' $ cd ' + build_path)
print(' $ make')
print('')
print(' to modify configured parameters and then compile:')
print(' $ cd ' + build_path)
print(' $ ccmake ' + root_directory)
print(' $ make')
def save_setup_command(argv, build_path):
file_name = os.path.join(build_path, 'setup_command')
f = open(file_name, 'w')
f.write(" ".join(sys.argv[:]))
f.close()
def setup_build_path(build_path, only_show_command):
if args.show:
return
if os.path.isdir(build_path):
if not only_show_command:
if args.clean:
shutil.rmtree(build_path)
os.makedirs(build_path, 0755)
elif args.force:
fname = os.path.join(build_path, 'CMakeCache.txt')
if os.path.exists(fname):
os.unlink(fname)
else:
print('aborting setup - build directory %s exists already' % build_path)
print('you have two options: first remove it and then rerun setup')
print(' or force configuration with --force')
sys.exit()
else:
os.makedirs(build_path, 0755)
def run_cmake(command, build_path):
topdir = os.getcwd()
os.chdir(build_path)
p = subprocess.Popen(command,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
s = p.communicate()[0]
print(s)
os.chdir(topdir)
return s
def main():
check_cmake_exists()
build_path = args.builddir
setup_build_path(build_path, args.show)
if not configure_host():
configure_default_compilers()
command = gen_cmake_command()
status = run_cmake(command, build_path)
if not 'Configuring incomplete' in status:
save_setup_command(sys.argv, build_path)
print_build_help(build_path)
# host/system specific configurations
def configure_host():
if args.host:
host = args.host
else:
u = os.uname()
host = string.join(u)
msg = None
# Generic systems
if re.search('ubuntu', host, re.I):
msg = "Configuring system: Ubuntu"
configure_ubuntu()
if re.search('fedora', host, re.I):
msg = "Configuring system: Fedora"
configure_fedora()
if re.search('osx', host, re.I):
msg = "Configuring system: MacOSX"
configure_osx()
if msg is None:
return False
if not args.show:
print msg
return True
def configure_default_compilers():
if not args.cc:
if args.mpi == 'on':
args.cc = 'mpicc'
else:
args.cc = 'gcc'
if not args.cxx:
if args.mpi == 'on':
args.cxx = 'mpicxx'
else:
args.cxx = 'g++'
if not args.fc:
if args.mpi == 'on':
args.fc = 'mpif90'
else:
args.fc = 'gfortran'
configure_ubuntu = configure_default_compilers
configure_fedora = configure_default_compilers
configure_osx = configure_default_compilers
if __name__ == '__main__':
main()
# vim:et:ts=4:sw=4