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()
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._subproc(env, SCons.Util.CLVar(cc) + ['-dumpversion'],
81 with SCons
.Action
._subproc
(env
, SCons
.Util
.CLVar(cc
) + ['--version'],
84 stdout
=subprocess
.PIPE
) as pipe
:
88 # -dumpversion variant:
89 # line = pipe.stdout.read().strip()
91 line
= SCons
.Util
.to_str(pipe
.stdout
.readline())
92 # Non-GNU compiler's output (like AIX xlc's) may exceed the stdout buffer:
93 # So continue with reading to let the child process actually terminate.
94 # We don't need to know the rest of the data, so don't bother decoding.
95 while pipe
.stdout
.readline():
99 # -dumpversion variant:
103 match
= re
.search(r
'[0-9]+(\.[0-9]+)+', line
)
105 version
= match
.group(0)
111 # indent-tabs-mode:nil
113 # vim: set expandtab tabstop=4 shiftwidth=4: