scons: Enable building through Clang Static Analyzer.
authorJosé Fonseca <jfonseca@vmware.com>
Mon, 14 Apr 2014 11:10:25 +0000 (12:10 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Wed, 16 Apr 2014 10:44:06 +0000 (11:44 +0100)
By accurately detecting gcc/clang through --version option instead
of executable name.

Clang Static Analyzer reports many issues, most false positives, but it
found at least one real and subtle use-after-free issue
in st_texture_get_sampler_view():

  http://people.freedesktop.org/~jrfonseca/scan-build-2014-04-14-1/report-869047.html#EndPath

Reviewed-by: Brian Paul <brianp@vmware.com>
scons/gallium.py

index e11d4dba312e9a5f9e1aa82b773de9ceabf13d19..42e8f7c54767c77b82259833afc2d4a1ea738a85 100755 (executable)
@@ -104,6 +104,19 @@ def num_jobs():
     return 1
 
 
+def get_cc_version(env):
+    # Get the first line of `$CC --version`
+    pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
+                                 stdin = 'devnull',
+                                 stderr = 'devnull',
+                                 stdout = subprocess.PIPE)
+    if pipe.wait() != 0:
+        return ''
+
+    line = pipe.stdout.readline()
+    return line
+
+
 def generate(env):
     """Common environment generation code"""
 
@@ -119,12 +132,8 @@ def generate(env):
     if os.environ.has_key('CC'):
         env['CC'] = os.environ['CC']
         # Update CCVERSION to match
-        pipe = SCons.Action._subproc(env, [env['CC'], '--version'],
-                                     stdin = 'devnull',
-                                     stderr = 'devnull',
-                                     stdout = subprocess.PIPE)
-        if pipe.wait() == 0:
-            line = pipe.stdout.readline()
+        line = get_cc_version(env)
+        if line:
             match = re.search(r'[0-9]+(\.[0-9]+)+', line)
             if match:
                 env['CCVERSION'] = match.group(0)
@@ -137,10 +146,16 @@ def generate(env):
     if os.environ.has_key('LDFLAGS'):
         env['LINKFLAGS'] += SCons.Util.CLVar(os.environ['LDFLAGS'])
 
-    env['gcc'] = 'gcc' in os.path.basename(env['CC']).split('-')
+    # Detect gcc/clang not by executable name, but through `--version` option,
+    # to avoid drawing wrong conclusions when using tools that overrice CC/CXX
+    # like scan-build.
+    cc_version = get_cc_version(env)
+    cc_version_words = cc_version.split()
+
+    env['gcc'] = 'gcc' in cc_version_words
     env['msvc'] = env['CC'] == 'cl'
     env['suncc'] = env['platform'] == 'sunos' and os.path.basename(env['CC']) == 'cc'
-    env['clang'] = env['CC'] == 'clang'
+    env['clang'] = 'clang' in cc_version_words
     env['icc'] = 'icc' == os.path.basename(env['CC'])
 
     if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 'x86_64':