1#! /usr/bin/env python
2# encoding: utf-8
3# DC 2008
4# Thomas Nagy 2016-2018 (ita)
5
6import re
7from waflib import Utils
8from waflib.Tools import fc, fc_config, fc_scan, ar
9from waflib.Configure import conf
10
11@conf
12def find_gfortran(conf):
13	"""Find the gfortran program (will look in the environment variable 'FC')"""
14	fc = conf.find_program(['gfortran','g77'], var='FC')
15	# (fallback to g77 for systems, where no gfortran is available)
16	conf.get_gfortran_version(fc)
17	conf.env.FC_NAME = 'GFORTRAN'
18
19@conf
20def gfortran_flags(conf):
21	v = conf.env
22	v.FCFLAGS_fcshlib = ['-fPIC']
23	v.FORTRANMODFLAG = ['-J', ''] # template for module path
24	v.FCFLAGS_DEBUG = ['-Werror'] # why not
25
26@conf
27def gfortran_modifier_win32(conf):
28	fc_config.fortran_modifier_win32(conf)
29
30@conf
31def gfortran_modifier_cygwin(conf):
32	fc_config.fortran_modifier_cygwin(conf)
33
34@conf
35def gfortran_modifier_darwin(conf):
36	fc_config.fortran_modifier_darwin(conf)
37
38@conf
39def gfortran_modifier_platform(conf):
40	dest_os = conf.env.DEST_OS or Utils.unversioned_sys_platform()
41	gfortran_modifier_func = getattr(conf, 'gfortran_modifier_' + dest_os, None)
42	if gfortran_modifier_func:
43		gfortran_modifier_func()
44
45@conf
46def get_gfortran_version(conf, fc):
47	"""Get the compiler version"""
48
49	# ensure this is actually gfortran, not an imposter.
50	version_re = re.compile(r"GNU\s*Fortran", re.I).search
51	cmd = fc + ['--version']
52	out, err = fc_config.getoutput(conf, cmd, stdin=False)
53	if out:
54		match = version_re(out)
55	else:
56		match = version_re(err)
57	if not match:
58		conf.fatal('Could not determine the compiler type')
59
60	# --- now get more detailed info -- see c_config.get_cc_version
61	cmd = fc + ['-dM', '-E', '-']
62	out, err = fc_config.getoutput(conf, cmd, stdin=True)
63
64	if out.find('__GNUC__') < 0:
65		conf.fatal('Could not determine the compiler type')
66
67	k = {}
68	out = out.splitlines()
69	import shlex
70
71	for line in out:
72		lst = shlex.split(line)
73		if len(lst)>2:
74			key = lst[1]
75			val = lst[2]
76			k[key] = val
77
78	def isD(var):
79		return var in k
80
81	def isT(var):
82		return var in k and k[var] != '0'
83
84	conf.env.FC_VERSION = (k['__GNUC__'], k['__GNUC_MINOR__'], k['__GNUC_PATCHLEVEL__'])
85
86def configure(conf):
87	conf.find_gfortran()
88	conf.find_ar()
89	conf.fc_flags()
90	conf.fc_add_flags()
91	conf.gfortran_flags()
92	conf.gfortran_modifier_platform()
93	conf.check_gfortran_o_space()
94