1#!/usr/bin/env python
2# encoding: utf-8
3
4import re
5from waflib import Utils, Logs
6from waflib.Tools import fc
7
8fc_compiler = {
9	'win32'  : ['gfortran','ifort'],
10	'darwin' : ['gfortran', 'g95', 'ifort'],
11	'linux'  : ['gfortran', 'g95', 'ifort'],
12	'java'   : ['gfortran', 'g95', 'ifort'],
13	'default': ['gfortran'],
14	'aix'    : ['gfortran']
15}
16"""
17Dict mapping the platform names to lists of names of Fortran compilers to try, in order of preference::
18
19	from waflib.Tools.compiler_c import c_compiler
20	c_compiler['linux'] = ['gfortran', 'g95', 'ifort']
21"""
22
23def default_compilers():
24	build_platform = Utils.unversioned_sys_platform()
25	possible_compiler_list = fc_compiler.get(build_platform, fc_compiler['default'])
26	return ' '.join(possible_compiler_list)
27
28def configure(conf):
29	"""
30	Detects a suitable Fortran compiler
31
32	:raises: :py:class:`waflib.Errors.ConfigurationError` when no suitable compiler is found
33	"""
34	try:
35		test_for_compiler = conf.options.check_fortran_compiler or default_compilers()
36	except AttributeError:
37		conf.fatal("Add options(opt): opt.load('compiler_fc')")
38	for compiler in re.split('[ ,]+', test_for_compiler):
39		conf.env.stash()
40		conf.start_msg('Checking for %r (Fortran compiler)' % compiler)
41		try:
42			conf.load(compiler)
43		except conf.errors.ConfigurationError as e:
44			conf.env.revert()
45			conf.end_msg(False)
46			Logs.debug('compiler_fortran: %r', e)
47		else:
48			if conf.env.FC:
49				conf.end_msg(conf.env.get_flat('FC'))
50				conf.env.COMPILER_FORTRAN = compiler
51				conf.env.commit()
52				break
53			conf.env.revert()
54			conf.end_msg(False)
55	else:
56		conf.fatal('could not configure a Fortran compiler!')
57
58def options(opt):
59	"""
60	This is how to provide compiler preferences on the command-line::
61
62		$ waf configure --check-fortran-compiler=ifort
63	"""
64	test_for_compiler = default_compilers()
65	opt.load_special_tools('fc_*.py')
66	fortran_compiler_opts = opt.add_option_group('Configuration options')
67	fortran_compiler_opts.add_option('--check-fortran-compiler', default=None,
68			help='list of Fortran compiler to try [%s]' % test_for_compiler,
69		dest="check_fortran_compiler")
70
71	for x in test_for_compiler.split():
72		opt.load('%s' % x)
73
74