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 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 3842 2008/12/20 22:59:52 scons"
38 import SCons.Scanner.Fortran
42 def isfortran(env, source):
43 """Return 1 if any of code in source has fortran files in it, 0
46 fsuffixes = env['FORTRANSUFFIXES']
48 # If no FORTRANSUFFIXES, no fortran tool, so there is no need to look
49 # for fortran sources.
53 # Source might be None for unusual cases like SConf.
57 ext = os.path.splitext(str(s.sources[0]))[1]
62 def _fortranEmitter(target, source, env):
63 node = source[0].rfile()
64 if not node.exists() and not node.is_derived():
65 print "Could not locate " + str(node.name)
67 mod_regex = """(?i)^\s*MODULE\s+(?!PROCEDURE)(\w+)"""
68 cre = re.compile(mod_regex,re.M)
69 # Retrieve all USE'd module names
70 modules = cre.findall(node.get_contents())
71 # Remove unique items from the list
72 modules = SCons.Util.unique(modules)
73 # Convert module name to a .mod filename
74 suffix = env.subst('$FORTRANMODSUFFIX', target=target, source=source)
75 moddir = env.subst('$FORTRANMODDIR', target=target, source=source)
76 modules = map(lambda x, s=suffix: string.lower(x) + s, modules)
78 target.append(env.fs.File(m, moddir))
79 return (target, source)
81 def FortranEmitter(target, source, env):
82 target, source = _fortranEmitter(target, source, env)
83 return SCons.Defaults.StaticObjectEmitter(target, source, env)
85 def ShFortranEmitter(target, source, env):
86 target, source = _fortranEmitter(target, source, env)
87 return SCons.Defaults.SharedObjectEmitter(target, source, env)
89 def ComputeFortranSuffixes(suffixes, ppsuffixes):
90 """suffixes are fortran source files, and ppsuffixes the ones to be
91 pre-processed. Both should be sequences, not strings."""
92 assert len(suffixes) > 0
95 upper_suffixes = map(string.upper, suffixes)
96 if SCons.Util.case_sensitive_suffixes(s, sup):
97 ppsuffixes.extend(upper_suffixes)
99 suffixes.extend(upper_suffixes)
101 def CreateDialectActions(dialect):
102 """Create dialect specific actions."""
103 CompAction = SCons.Action.Action('$%sCOM ' % dialect, '$%sCOMSTR' % dialect)
104 CompPPAction = SCons.Action.Action('$%sPPCOM ' % dialect, '$%sPPCOMSTR' % dialect)
105 ShCompAction = SCons.Action.Action('$SH%sCOM ' % dialect, '$SH%sCOMSTR' % dialect)
106 ShCompPPAction = SCons.Action.Action('$SH%sPPCOM ' % dialect, '$SH%sPPCOMSTR' % dialect)
108 return CompAction, CompPPAction, ShCompAction, ShCompPPAction
110 def DialectAddToEnv(env, dialect, suffixes, ppsuffixes, support_module = 0):
111 """Add dialect specific construction variables."""
112 ComputeFortranSuffixes(suffixes, ppsuffixes)
114 fscan = SCons.Scanner.Fortran.FortranScan("%sPATH" % dialect)
116 for suffix in suffixes + ppsuffixes:
117 SCons.Tool.SourceFileScanner.add_scanner(suffix, fscan)
119 env.AppendUnique(FORTRANSUFFIXES = suffixes + ppsuffixes)
121 compaction, compppaction, shcompaction, shcompppaction = \
122 CreateDialectActions(dialect)
124 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
126 for suffix in suffixes:
127 static_obj.add_action(suffix, compaction)
128 shared_obj.add_action(suffix, shcompaction)
129 static_obj.add_emitter(suffix, FortranEmitter)
130 shared_obj.add_emitter(suffix, ShFortranEmitter)
132 for suffix in ppsuffixes:
133 static_obj.add_action(suffix, compppaction)
134 shared_obj.add_action(suffix, shcompppaction)
135 static_obj.add_emitter(suffix, FortranEmitter)
136 shared_obj.add_emitter(suffix, ShFortranEmitter)
138 if not env.has_key('%sFLAGS' % dialect):
139 env['%sFLAGS' % dialect] = SCons.Util.CLVar('')
141 if not env.has_key('SH%sFLAGS' % dialect):
142 env['SH%sFLAGS' % dialect] = SCons.Util.CLVar('$%sFLAGS' % dialect)
144 # If a tool does not define fortran prefix/suffix for include path, use C ones
145 if not env.has_key('INC%sPREFIX' % dialect):
146 env['INC%sPREFIX' % dialect] = '$INCPREFIX'
148 if not env.has_key('INC%sSUFFIX' % dialect):
149 env['INC%sSUFFIX' % dialect] = '$INCSUFFIX'
151 env['_%sINCFLAGS' % dialect] = '$( ${_concat(INC%sPREFIX, %sPATH, INC%sSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)' % (dialect, dialect, dialect)
153 if support_module == 1:
154 env['%sCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
155 env['%sPPCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
156 env['SH%sCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
157 env['SH%sPPCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $_FORTRANMODFLAG $SOURCES' % (dialect, dialect, dialect)
159 env['%sCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
160 env['%sPPCOM' % dialect] = '$%s -o $TARGET -c $%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
161 env['SH%sCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
162 env['SH%sPPCOM' % dialect] = '$SH%s -o $TARGET -c $SH%sFLAGS $CPPFLAGS $_CPPDEFFLAGS $_%sINCFLAGS $SOURCES' % (dialect, dialect, dialect)
164 def add_fortran_to_env(env):
165 """Add Builders and construction variables for Fortran to an Environment."""
167 FortranSuffixes = env['FORTRANFILESUFFIXES']
169 FortranSuffixes = ['.f', '.for', '.ftn']
171 #print "Adding %s to fortran suffixes" % FortranSuffixes
173 FortranPPSuffixes = env['FORTRANPPFILESUFFIXES']
175 FortranPPSuffixes = ['.fpp', '.FPP']
177 DialectAddToEnv(env, "FORTRAN", FortranSuffixes,
178 FortranPPSuffixes, support_module = 1)
180 env['FORTRANMODPREFIX'] = '' # like $LIBPREFIX
181 env['FORTRANMODSUFFIX'] = '.mod' # like $LIBSUFFIX
183 env['FORTRANMODDIR'] = '' # where the compiler should place .mod files
184 env['FORTRANMODDIRPREFIX'] = '' # some prefix to $FORTRANMODDIR - similar to $INCPREFIX
185 env['FORTRANMODDIRSUFFIX'] = '' # some suffix to $FORTRANMODDIR - similar to $INCSUFFIX
186 env['_FORTRANMODFLAG'] = '$( ${_concat(FORTRANMODDIRPREFIX, FORTRANMODDIR, FORTRANMODDIRSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
188 def add_f77_to_env(env):
189 """Add Builders and construction variables for f77 to an Environment."""
191 F77Suffixes = env['F77FILESUFFIXES']
193 F77Suffixes = ['.f77']
195 #print "Adding %s to f77 suffixes" % F77Suffixes
197 F77PPSuffixes = env['F77PPFILESUFFIXES']
201 DialectAddToEnv(env, "F77", F77Suffixes, F77PPSuffixes)
203 def add_f90_to_env(env):
204 """Add Builders and construction variables for f90 to an Environment."""
206 F90Suffixes = env['F90FILESUFFIXES']
208 F90Suffixes = ['.f90']
210 #print "Adding %s to f90 suffixes" % F90Suffixes
212 F90PPSuffixes = env['F90PPFILESUFFIXES']
216 DialectAddToEnv(env, "F90", F90Suffixes, F90PPSuffixes,
219 def add_f95_to_env(env):
220 """Add Builders and construction variables for f95 to an Environment."""
222 F95Suffixes = env['F95FILESUFFIXES']
224 F95Suffixes = ['.f95']
226 #print "Adding %s to f95 suffixes" % F95Suffixes
228 F95PPSuffixes = env['F95PPFILESUFFIXES']
232 DialectAddToEnv(env, "F95", F95Suffixes, F95PPSuffixes,
235 def add_all_to_env(env):
236 """Add builders and construction variables for all supported fortran
238 add_fortran_to_env(env)