3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Routines for setting up Fortran, common to all dialects."""
28 from typing
import Tuple
30 import SCons
.Scanner
.Fortran
33 from SCons
.Action
import Action
, ActionBase
36 def isfortran(env
, source
) -> bool:
37 """Returns True if source has any fortran files in it.
39 Only checks based on filename suffixes, does not examine code.
42 fsuffixes
= env
['FORTRANSUFFIXES']
44 # If no FORTRANSUFFIXES, no fortran tool, so there is no need to look
45 # for fortran sources.
49 # Source might be None for unusual cases like SConf.
53 ext
= os
.path
.splitext(str(s
.sources
[0]))[1]
59 def _fortranEmitter(target
, source
, env
) -> Tuple
:
60 """Common code for Fortran emitter.
62 Called by both the static and shared object emitters,
63 mainly to account for generated module files.
66 node
= source
[0].rfile()
67 if not node
.exists() and not node
.is_derived():
68 print("Could not locate " + str(node
.name
))
70 # This has to match the def_regex in the Fortran scanner
71 mod_regex
= r
"""(?i)^\s*MODULE\s+(?!PROCEDURE|SUBROUTINE|FUNCTION|PURE|ELEMENTAL)(\w+)"""
72 cre
= re
.compile(mod_regex
,re
.M
)
73 # Retrieve all USE'd module names
74 modules
= cre
.findall(node
.get_text_contents())
75 # Remove unique items from the list
76 modules
= SCons
.Util
.unique(modules
)
77 # Convert module name to a .mod filename
78 suffix
= env
.subst('$FORTRANMODSUFFIX', target
=target
, source
=source
)
79 moddir
= env
.subst('$FORTRANMODDIR', target
=target
, source
=source
)
80 modules
= [x
.lower() + suffix
for x
in modules
]
82 target
.append(env
.fs
.File(m
, moddir
))
83 return (target
, source
)
86 def FortranEmitter(target
, source
, env
) -> Tuple
:
88 target
, source
= _fortranEmitter(target
, source
, env
)
89 return SCons
.Defaults
.StaticObjectEmitter(target
, source
, env
)
92 def ShFortranEmitter(target
, source
, env
) -> Tuple
:
94 target
, source
= _fortranEmitter(target
, source
, env
)
95 return SCons
.Defaults
.SharedObjectEmitter(target
, source
, env
)
98 def ComputeFortranSuffixes(suffixes
, ppsuffixes
) -> None:
99 """Update the suffix lists to reflect the platform requirements.
101 If upper-cased suffixes can be distinguished from lower, those are
102 added to *ppsuffixes*. If not, they are added to *suffixes*.
105 suffixes (list): indicate regular Fortran source files
106 ppsuffixes (list): indicate Fortran source files that should be
107 be run through the pre-processor
109 assert len(suffixes
) > 0
112 upper_suffixes
= [_
.upper() for _
in suffixes
]
113 if SCons
.Util
.case_sensitive_suffixes(s
, sup
):
114 ppsuffixes
.extend(upper_suffixes
)
116 suffixes
.extend(upper_suffixes
)
118 def CreateDialectActions(
120 ) -> Tuple
[ActionBase
, ActionBase
, ActionBase
, ActionBase
]:
121 """Create dialect specific actions."""
122 CompAction
= Action(f
'${dialect}COM ', cmdstr
=f
'${dialect}COMSTR')
123 CompPPAction
= Action(f
'${dialect}PPCOM ', cmdstr
=f
'${dialect}PPCOMSTR')
124 ShCompAction
= Action(f
'$SH{dialect}COM ', cmdstr
=f
'$SH{dialect}COMSTR')
125 ShCompPPAction
= Action(f
'$SH{dialect}PPCOM ', cmdstr
=f
'$SH{dialect}PPCOMSTR')
126 return CompAction
, CompPPAction
, ShCompAction
, ShCompPPAction
129 def DialectAddToEnv(env
, dialect
, suffixes
, ppsuffixes
, support_mods
: bool=False) -> None:
130 """Add dialect specific construction variables.
133 dialect (str): dialect name
134 suffixes (list): suffixes associated with this dialect
135 ppsuffixes (list): suffixes using cpp associated with this dialect
136 support_mods (bool): whether this dialect supports modules
138 ComputeFortranSuffixes(suffixes
, ppsuffixes
)
140 fscan
= SCons
.Scanner
.Fortran
.FortranScan(f
"{dialect}PATH")
141 for suffix
in suffixes
+ ppsuffixes
:
142 SCons
.Tool
.SourceFileScanner
.add_scanner(suffix
, fscan
)
144 env
.AppendUnique(FORTRANSUFFIXES
=suffixes
+ ppsuffixes
)
146 compaction
, compppaction
, shcompaction
, shcompppaction
= \
147 CreateDialectActions(dialect
)
148 static_obj
, shared_obj
= SCons
.Tool
.createObjBuilders(env
)
150 for suffix
in suffixes
:
151 static_obj
.add_action(suffix
, compaction
)
152 shared_obj
.add_action(suffix
, shcompaction
)
153 static_obj
.add_emitter(suffix
, FortranEmitter
)
154 shared_obj
.add_emitter(suffix
, ShFortranEmitter
)
156 for suffix
in ppsuffixes
:
157 static_obj
.add_action(suffix
, compppaction
)
158 shared_obj
.add_action(suffix
, shcompppaction
)
159 static_obj
.add_emitter(suffix
, FortranEmitter
)
160 shared_obj
.add_emitter(suffix
, ShFortranEmitter
)
162 if f
'{dialect}FLAGS' not in env
:
163 env
[f
'{dialect}FLAGS'] = SCons
.Util
.CLVar('')
164 if f
'SH{dialect}FLAGS' not in env
:
165 env
[f
'SH{dialect}FLAGS'] = SCons
.Util
.CLVar(f
'${dialect}FLAGS')
167 # If a tool does not define fortran prefix/suffix for include path, use C ones
168 if f
'INC{dialect}PREFIX' not in env
:
169 env
[f
'INC{dialect}PREFIX'] = '$INCPREFIX'
170 if f
'INC{dialect}SUFFIX' not in env
:
171 env
[f
'INC{dialect}SUFFIX'] = '$INCSUFFIX'
173 env
[f
'_{dialect}INCFLAGS'] = f
'${{_concat(INC{dialect}PREFIX, {dialect}PATH, INC{dialect}SUFFIX, __env__, RDirs, TARGET, SOURCE, affect_signature=False)}}'
176 env
[f
'{dialect}COM'] = f
'${dialect} -o $TARGET -c $FORTRANCOMMONFLAGS ${dialect}FLAGS $_{dialect}INCFLAGS $_FORTRANMODFLAG $SOURCES'
177 env
[f
'{dialect}PPCOM'] = f
'${dialect} -o $TARGET -c $FORTRANCOMMONFLAGS ${dialect}FLAGS $CPPFLAGS $_CPPDEFFLAGS $_{dialect}INCFLAGS $_FORTRANMODFLAG $SOURCES'
178 env
[f
'SH{dialect}COM'] = f
'$SH{dialect} -o $TARGET -c $FORTRANCOMMONFLAGS $SH{dialect}FLAGS $_{dialect}INCFLAGS $_FORTRANMODFLAG $SOURCES'
179 env
[f
'SH{dialect}PPCOM'] = f
'$SH{dialect} -o $TARGET -c $FORTRANCOMMONFLAGS $SH{dialect}FLAGS $CPPFLAGS $_CPPDEFFLAGS $_{dialect}INCFLAGS $_FORTRANMODFLAG $SOURCES'
181 env
[f
'{dialect}COM'] = f
'${dialect} -o $TARGET -c $FORTRANCOMMONFLAGS ${dialect}FLAGS $_{dialect}INCFLAGS $SOURCES'
182 env
[f
'{dialect}PPCOM'] = f
'${dialect} -o $TARGET -c $FORTRANCOMMONFLAGS ${dialect}FLAGS $CPPFLAGS $_CPPDEFFLAGS $_{dialect}INCFLAGS $SOURCES'
183 env
[f
'SH{dialect}COM'] = f
'$SH{dialect} -o $TARGET -c $FORTRANCOMMONFLAGS $SH{dialect}FLAGS $_{dialect}INCFLAGS $SOURCES'
184 env
[f
'SH{dialect}PPCOM'] = f
'$SH{dialect} -o $TARGET -c $FORTRANCOMMONFLAGS $SH{dialect}FLAGS $CPPFLAGS $_CPPDEFFLAGS $_{dialect}INCFLAGS $SOURCES'
187 def add_fortran_to_env(env
) -> None:
188 """Add Builders and construction variables for Fortran/generic."""
190 FortranSuffixes
= env
['FORTRANFILESUFFIXES']
192 FortranSuffixes
= ['.f', '.for', '.ftn']
195 FortranPPSuffixes
= env
['FORTRANPPFILESUFFIXES']
197 FortranPPSuffixes
= ['.fpp', '.FPP']
199 DialectAddToEnv(env
, "FORTRAN", FortranSuffixes
, FortranPPSuffixes
, support_mods
=True)
202 env
['FORTRANMODPREFIX'] = '' # like $LIBPREFIX
203 env
['FORTRANMODSUFFIX'] = '.mod' # like $LIBSUFFIX
204 env
['FORTRANMODDIR'] = '' # where the compiler should place .mod files
205 env
['FORTRANMODDIRPREFIX'] = '' # some prefix to $FORTRANMODDIR - similar to $INCPREFIX
206 env
['FORTRANMODDIRSUFFIX'] = '' # some suffix to $FORTRANMODDIR - similar to $INCSUFFIX
207 env
['_FORTRANMODFLAG'] = '$( ${_concat(FORTRANMODDIRPREFIX, FORTRANMODDIR, FORTRANMODDIRSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
209 def add_f77_to_env(env
) -> None:
210 """Add Builders and construction variables for f77 dialect."""
212 F77Suffixes
= env
['F77FILESUFFIXES']
214 F77Suffixes
= ['.f77']
217 F77PPSuffixes
= env
['F77PPFILESUFFIXES']
221 DialectAddToEnv(env
, "F77", F77Suffixes
, F77PPSuffixes
)
223 def add_f90_to_env(env
) -> None:
224 """Add Builders and construction variables for f90 dialect."""
226 F90Suffixes
= env
['F90FILESUFFIXES']
228 F90Suffixes
= ['.f90']
231 F90PPSuffixes
= env
['F90PPFILESUFFIXES']
235 DialectAddToEnv(env
, "F90", F90Suffixes
, F90PPSuffixes
, support_mods
=True)
237 def add_f95_to_env(env
) -> None:
238 """Add Builders and construction variables for f95 dialect."""
240 F95Suffixes
= env
['F95FILESUFFIXES']
242 F95Suffixes
= ['.f95']
245 F95PPSuffixes
= env
['F95PPFILESUFFIXES']
249 DialectAddToEnv(env
, "F95", F95Suffixes
, F95PPSuffixes
, support_mods
=True)
251 def add_f03_to_env(env
) -> None:
252 """Add Builders and construction variables for f03 dialect."""
254 F03Suffixes
= env
['F03FILESUFFIXES']
256 F03Suffixes
= ['.f03']
259 F03PPSuffixes
= env
['F03PPFILESUFFIXES']
263 DialectAddToEnv(env
, "F03", F03Suffixes
, F03PPSuffixes
, support_mods
=True)
265 def add_f08_to_env(env
) -> None:
266 """Add Builders and construction variables for f08 dialect."""
268 F08Suffixes
= env
['F08FILESUFFIXES']
270 F08Suffixes
= ['.f08']
273 F08PPSuffixes
= env
['F08PPFILESUFFIXES']
277 DialectAddToEnv(env
, "F08", F08Suffixes
, F08PPSuffixes
, support_mods
=True)
279 def add_all_to_env(env
) -> None:
280 """Add builders and construction variables for all supported dialects."""
281 add_fortran_to_env(env
)
290 # indent-tabs-mode:nil
292 # vim: set expandtab tabstop=4 shiftwidth=4: