Merge pull request #4655 from bdbaddog/fix_new_ninja_package
[scons.git] / SCons / Tool / gcc.py
blob1ccbefcf9edbfec847a3c39691824cfff1e2d103
1 # MIT License
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 """SCons.Tool.gcc
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()
30 selection method.
32 """
34 from . import cc
35 import re
36 from subprocess import PIPE
38 import SCons.Util
40 compilers = ['gcc', 'cc']
43 def generate(env) -> None:
44 """Add Builders and construction variables for gcc to an Environment."""
46 if 'CC' not in env:
47 env['CC'] = env.Detect(compilers) or compilers[0]
49 cc.generate(env)
51 if env['PLATFORM'] in ['cygwin', 'win32']:
52 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
53 else:
54 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS -fPIC')
55 # determine compiler version
56 version = detect_version(env, env['CC'])
57 if version:
58 env['CCVERSION'] = version
60 env['CCDEPFLAGS'] = '-MMD -MF ${TARGET}.d'
61 env["NINJA_DEPFILE_PARSE_FORMAT"] = 'gcc'
65 def exists(env):
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."""
72 version = None
73 cc = env.subst(cc)
74 if not cc:
75 return version
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
79 # regular expression.
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
84 if cp.returncode:
85 return version
87 # -dumpversion variant:
88 # line = cp.stdout.strip()
89 # --version variant:
90 try:
91 line = SCons.Util.to_str(cp.stdout.splitlines()[0])
92 except IndexError:
93 return version
95 # -dumpversion variant:
96 # version = line
97 # --version variant:
98 match = re.search(r'[0-9]+(\.[0-9]+)+', line)
99 if match:
100 version = match.group(0)
102 return version
104 # Local Variables:
105 # tab-width:4
106 # indent-tabs-mode:nil
107 # End:
108 # vim: set expandtab tabstop=4 shiftwidth=4: