1
"""SCons.Tool.FortranCommon
3
Stuff for processing Fortran, common to all fortran dialects.
8
# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 The SCons Foundation
10
# Permission is hereby granted, free of charge, to any person obtaining
11
# a copy of this software and associated documentation files (the
12
# "Software"), to deal in the Software without restriction, including
13
# without limitation the rights to use, copy, modify, merge, publish,
14
# distribute, sublicense, and/or sell copies of the Software, and to
15
# permit persons to whom the Software is furnished to do so, subject to
16
# the following conditions:
18
# The above copyright notice and this permission notice shall be included
19
# in all copies or substantial portions of the Software.
21
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
__revision__ = "src/engine/SCons/Tool/FortranCommon.py 2013/03/03 09:48:35 garyo"
37
import SCons.Scanner.Fortran
41
def isfortran(env, source):
42
"""Return 1 if any of code in source has fortran files in it, 0
45
fsuffixes = env['FORTRANSUFFIXES']
47
# If no FORTRANSUFFIXES, no fortran tool, so there is no need to look
48
# for fortran sources.
52
# Source might be None for unusual cases like SConf.
56
ext = os.path.splitext(str(s.sources[0]))[1]
61
def _fortranEmitter(target, source, env):
62
node = source[0].rfile()
63
if not node.exists() and not node.is_derived():
64
print "Could not locate " + str(node.name)
66
mod_regex = """(?i)^\s*MODULE\s+(?!PROCEDURE)(\w+)"""
67
cre = re.compile(mod_regex,re.M)
68
# Retrieve all USE'd module names
69
modules = cre.findall(node.get_text_contents())
70
# Remove unique items from the list
71
modules = SCons.Util.unique(modules)
72
# Convert module name to a .mod filename
73
suffix = env.subst('$FORTRANMODSUFFIX', target=target, source=source)
74
moddir = env.subst('$FORTRANMODDIR', target=target, source=source)
75
modules = [x.lower() + suffix for x in modules]
77
target.append(env.fs.File(m, moddir))
78
return (target, source)
80
def FortranEmitter(target, source, env):
81
target, source = _fortranEmitter(target, source, env)
82
return SCons.Defaults.StaticObjectEmitter(target, source, env)
84
def ShFortranEmitter(target, source, env):
85
target, source = _fortranEmitter(target, source, env)
86
return SCons.Defaults.SharedObjectEmitter(target, source, env)
88
def ComputeFortranSuffixes(suffixes, ppsuffixes):
89
"""suffixes are fortran source files, and ppsuffixes the ones to be
90
pre-processed. Both should be sequences, not strings."""
91
assert len(suffixes) > 0
94
upper_suffixes = [_.upper() for _ in suffixes]
95
if SCons.Util.case_sensitive_suffixes(s, sup):
96
ppsuffixes.extend(upper_suffixes)
98
suffixes.extend(upper_suffixes)
100
def CreateDialectActions(dialect):
101
"""Create dialect specific actions."""
102
CompAction = SCons.Action.Action('$%sCOM ' % dialect, '$%sCOMSTR' % dialect)
103
CompPPAction = SCons.Action.Action('$%sPPCOM ' % dialect, '$%sPPCOMSTR' % dialect)
104
ShCompAction = SCons.Action.Action('$SH%sCOM ' % dialect, '$SH%sCOMSTR' % dialect)
105
ShCompPPAction = SCons.Action.Action('$SH%sPPCOM ' % dialect, '$SH%sPPCOMSTR' % dialect)
107
return CompAction, CompPPAction, ShCompAction, ShCompPPAction
109
def DialectAddToEnv(env, dialect, suffixes, ppsuffixes, support_module = 0):
110
"""Add dialect specific construction variables."""
111
ComputeFortranSuffixes(suffixes, ppsuffixes)
113
fscan = SCons.Scanner.Fortran.FortranScan("%sPATH" % dialect)
115
for suffix in suffixes + ppsuffixes:
116
SCons.Tool.SourceFileScanner.add_scanner(suffix, fscan)
118
env.AppendUnique(FORTRANSUFFIXES = suffixes + ppsuffixes)
120
compaction, compppaction, shcompaction, shcompppaction = \
121
CreateDialectActions(dialect)
123
static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
125
for suffix in suffixes:
126
static_obj.add_action(suffix, compaction)
127
shared_obj.add_action(suffix, shcompaction)
128
static_obj.add_emitter(suffix, FortranEmitter)
129
shared_obj.add_emitter(suffix, ShFortranEmitter)
131
for suffix in ppsuffixes:
132
static_obj.add_action(suffix, compppaction)
133
shared_obj.add_action(suffix, shcompppaction)
134
static_obj.add_emitter(suffix, FortranEmitter)
135
shared_obj.add_emitter(suffix, ShFortranEmitter)
137
if '%sFLAGS' % dialect not in env:
138
env['%sFLAGS' % dialect] = SCons.Util.CLVar('')
140
if 'SH%sFLAGS' % dialect not in env:
141
env['SH%sFLAGS' % dialect] = SCons.Util.CLVar('$%sFLAGS' % dialect)
143
# If a tool does not define fortran prefix/suffix for include path, use C ones
144
if 'INC%sPREFIX' % dialect not in env:
145
env['INC%sPREFIX' % dialect] = '$INCPREFIX'
147
if 'INC%sSUFFIX' % dialect not in env:
148
env['INC%sSUFFIX' % dialect] = '$INCSUFFIX'
150
env['_%sINCFLAGS' % dialect] = '$( ${_concat(INC%sPREFIX, %sPATH, INC%sSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)' % (dialect, dialect, dialect)
152
if support_module == 1:
153
env['%sCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
154
env['%sPPCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
155
env['SH%sCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
156
env['SH%sPPCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
158
env['%sCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
159
env['%sPPCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
160
env['SH%sCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
161
env['SH%sPPCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
163
def add_fortran_to_env(env):
164
"""Add Builders and construction variables for Fortran to an Environment."""
166
FortranSuffixes = env['FORTRANFILESUFFIXES']
168
FortranSuffixes = ['.f', '.for', '.ftn']
170
#print "Adding %s to fortran suffixes" % FortranSuffixes
172
FortranPPSuffixes = env['FORTRANPPFILESUFFIXES']
174
FortranPPSuffixes = ['.fpp', '.FPP']
176
DialectAddToEnv(env, "FORTRAN", FortranSuffixes,
177
FortranPPSuffixes, support_module = 1)
179
env['FORTRANMODPREFIX'] = '' # like $LIBPREFIX
180
env['FORTRANMODSUFFIX'] = '.mod' # like $LIBSUFFIX
182
env['FORTRANMODDIR'] = '' # where the compiler should place .mod files
183
env['FORTRANMODDIRPREFIX'] = '' # some prefix to $FORTRANMODDIR - similar to $INCPREFIX
184
env['FORTRANMODDIRSUFFIX'] = '' # some suffix to $FORTRANMODDIR - similar to $INCSUFFIX
185
env['_FORTRANMODFLAG'] = '$( ${_concat(FORTRANMODDIRPREFIX, FORTRANMODDIR, FORTRANMODDIRSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
187
def add_f77_to_env(env):
188
"""Add Builders and construction variables for f77 to an Environment."""
190
F77Suffixes = env['F77FILESUFFIXES']
192
F77Suffixes = ['.f77']
194
#print "Adding %s to f77 suffixes" % F77Suffixes
196
F77PPSuffixes = env['F77PPFILESUFFIXES']
200
DialectAddToEnv(env, "F77", F77Suffixes, F77PPSuffixes)
202
def add_f90_to_env(env):
203
"""Add Builders and construction variables for f90 to an Environment."""
205
F90Suffixes = env['F90FILESUFFIXES']
207
F90Suffixes = ['.f90']
209
#print "Adding %s to f90 suffixes" % F90Suffixes
211
F90PPSuffixes = env['F90PPFILESUFFIXES']
215
DialectAddToEnv(env, "F90", F90Suffixes, F90PPSuffixes,
218
def add_f95_to_env(env):
219
"""Add Builders and construction variables for f95 to an Environment."""
221
F95Suffixes = env['F95FILESUFFIXES']
223
F95Suffixes = ['.f95']
225
#print "Adding %s to f95 suffixes" % F95Suffixes
227
F95PPSuffixes = env['F95PPFILESUFFIXES']
231
DialectAddToEnv(env, "F95", F95Suffixes, F95PPSuffixes,
234
def add_f03_to_env(env):
235
"""Add Builders and construction variables for f03 to an Environment."""
237
F03Suffixes = env['F03FILESUFFIXES']
239
F03Suffixes = ['.f03']
241
#print "Adding %s to f95 suffixes" % F95Suffixes
243
F03PPSuffixes = env['F03PPFILESUFFIXES']
247
DialectAddToEnv(env, "F03", F03Suffixes, F03PPSuffixes,
250
def add_all_to_env(env):
251
"""Add builders and construction variables for all supported fortran
253
add_fortran_to_env(env)
261
# indent-tabs-mode:nil
263
# vim: set expandtab tabstop=4 shiftwidth=4: