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.
26 Tool-specific initialization for gcc.
28 There normally shouldn't be any need to import this module directly.
29 It will usually be imported through the generic SCons.Tool.Tool()
36 from subprocess
import PIPE
40 compilers
= ['gcc', 'cc']
43 def generate(env
) -> None:
44 """Add Builders and construction variables for gcc to an Environment."""
47 env
['CC'] = env
.Detect(compilers
) or compilers
[0]
51 if env
['PLATFORM'] in ['cygwin', 'win32']:
52 env
['SHCCFLAGS'] = SCons
.Util
.CLVar('$CCFLAGS')
54 env
['SHCCFLAGS'] = SCons
.Util
.CLVar('$CCFLAGS -fPIC')
55 # determine compiler version
56 version
= detect_version(env
, env
['CC'])
58 env
['CCVERSION'] = version
60 env
['CCDEPFLAGS'] = '-MMD -MF ${TARGET}.d'
61 env
["NINJA_DEPFILE_PARSE_FORMAT"] = 'gcc'
66 # is executable, and is a GNU compiler (or accepts '--version' at least)
67 return detect_version(env
, env
.Detect(env
.get('CC', compilers
)))
70 def detect_version(env
, cc
):
71 """Return the version of the GNU compiler, or None if it is not a GNU compiler."""
77 # -dumpversion was added in GCC 3.0. As long as we're supporting
78 # GCC versions older than that, we should use --version and a
80 # pipe = SCons.Action.scons_subproc_run(env, SCons.Util.CLVar(cc) + ['-dumpversion'],
81 cp
= SCons
.Action
.scons_subproc_run(
82 env
, SCons
.Util
.CLVar(cc
) + ['--version'], stdout
=PIPE
87 # -dumpversion variant:
88 # line = cp.stdout.strip()
91 line
= SCons
.Util
.to_str(cp
.stdout
.splitlines()[0])
95 # -dumpversion variant:
98 match
= re
.search(r
'[0-9]+(\.[0-9]+)+', line
)
100 version
= match
.group(0)
106 # indent-tabs-mode:nil
108 # vim: set expandtab tabstop=4 shiftwidth=4: