[Mesa-dev] [PATCH 1/3] scons: Enable building through Clang Static Analyzer.

2014-04-16 Thread jfonseca
From: José Fonseca jfons...@vmware.com

Same intent as commit a45a50a4828e1357e9555474bc127c5585b3a420,
but this the C compiler is detected via C-preprocessor macros,
similar to how autotools do it, as that seems to be the most
reliable method.
---
 scons/gallium.py | 38 +++---
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/scons/gallium.py b/scons/gallium.py
index bd71e51..d13d0e6 100755
--- a/scons/gallium.py
+++ b/scons/gallium.py
@@ -36,6 +36,8 @@ import os.path
 import re
 import subprocess
 import platform as _platform
+import sys
+import tempfile
 
 import SCons.Action
 import SCons.Builder
@@ -104,6 +106,28 @@ def num_jobs():
 return 1
 
 
+def check_cc(env, cc, expr, cpp_opt = '-E'):
+# Invoke C-preprocessor to determine whether the specified expression is
+# true or not.
+
+sys.stdout.write('Checking for %s ... ' % cc)
+
+source = tempfile.NamedTemporaryFile(suffix='.c', delete=False)
+source.write('#if !(%s)\n#error\n#endif\n' % expr)
+source.close()
+
+pipe = SCons.Action._subproc(env, [env['CC'], cpp_opt, source.name],
+ stdin = 'devnull',
+ stderr = 'devnull',
+ stdout = 'devnull')
+result = pipe.wait() == 0
+
+os.unlink(source.name)
+
+sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
+return result
+
+
 def generate(env):
 Common environment generation code
 
@@ -137,10 +161,18 @@ 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('-')
-env['msvc'] = env['CC'] == 'cl'
+# Detect gcc/clang not by executable name, but through pre-defined macros
+# as autoconf does, to avoid drawing wrong conclusions when using tools
+# that overrice CC/CXX like scan-build.
+env['gcc'] = 0
+env['clang'] = 0
+env['msvc'] = 0
+if _platform.system() == 'Windows':
+env['msvc'] = check_cc(env, 'MSVC', 'defined(_MSC_VER)', '/E')
+if not env['msvc']:
+env['gcc'] = check_cc(env, 'GCC', 'defined(__GNUC__)  
!defined(__clang__)')
+env['clang'] = check_cc(env, 'Clang', '__clang__')
 env['suncc'] = env['platform'] == 'sunos' and os.path.basename(env['CC']) 
== 'cc'
-env['clang'] = env['CC'] == 'clang'
 env['icc'] = 'icc' == os.path.basename(env['CC'])
 
 if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 
'x86_64':
-- 
1.8.3.2

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 1/3] scons: Enable building through Clang Static Analyzer.

2014-04-16 Thread Brian Paul

On 04/16/2014 10:46 AM, jfons...@vmware.com wrote:

From: José Fonseca jfons...@vmware.com

Same intent as commit a45a50a4828e1357e9555474bc127c5585b3a420,
but this the C compiler is detected via C-preprocessor macros,
similar to how autotools do it, as that seems to be the most
reliable method.
---
  scons/gallium.py | 38 +++---
  1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/scons/gallium.py b/scons/gallium.py
index bd71e51..d13d0e6 100755
--- a/scons/gallium.py
+++ b/scons/gallium.py
@@ -36,6 +36,8 @@ import os.path
  import re
  import subprocess
  import platform as _platform
+import sys
+import tempfile

  import SCons.Action
  import SCons.Builder
@@ -104,6 +106,28 @@ def num_jobs():
  return 1


+def check_cc(env, cc, expr, cpp_opt = '-E'):
+# Invoke C-preprocessor to determine whether the specified expression is
+# true or not.
+
+sys.stdout.write('Checking for %s ... ' % cc)
+
+source = tempfile.NamedTemporaryFile(suffix='.c', delete=False)
+source.write('#if !(%s)\n#error\n#endif\n' % expr)
+source.close()
+
+pipe = SCons.Action._subproc(env, [env['CC'], cpp_opt, source.name],
+ stdin = 'devnull',
+ stderr = 'devnull',
+ stdout = 'devnull')
+result = pipe.wait() == 0
+
+os.unlink(source.name)
+
+sys.stdout.write(' %s\n' % ['no', 'yes'][int(bool(result))])
+return result
+
+
  def generate(env):
  Common environment generation code

@@ -137,10 +161,18 @@ 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('-')
-env['msvc'] = env['CC'] == 'cl'
+# Detect gcc/clang not by executable name, but through pre-defined macros
+# as autoconf does, to avoid drawing wrong conclusions when using tools
+# that overrice CC/CXX like scan-build.
+env['gcc'] = 0
+env['clang'] = 0
+env['msvc'] = 0
+if _platform.system() == 'Windows':
+env['msvc'] = check_cc(env, 'MSVC', 'defined(_MSC_VER)', '/E')
+if not env['msvc']:
+env['gcc'] = check_cc(env, 'GCC', 'defined(__GNUC__)  
!defined(__clang__)')
+env['clang'] = check_cc(env, 'Clang', '__clang__')
  env['suncc'] = env['platform'] == 'sunos' and os.path.basename(env['CC']) 
== 'cc'
-env['clang'] = env['CC'] == 'clang'
  env['icc'] = 'icc' == os.path.basename(env['CC'])

  if env['msvc'] and env['toolchain'] == 'default' and env['machine'] == 
'x86_64':




Series looks OK to me.

Reviewed-by: Brian Paul bri...@vmware.com

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev


[Mesa-dev] [PATCH 1/3] scons: Enable building through Clang Static Analyzer.

2014-04-14 Thread jfonseca
From: José Fonseca jfons...@vmware.com

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
---
 scons/gallium.py | 31 +++
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/scons/gallium.py b/scons/gallium.py
index e11d4db..42e8f7c 100755
--- a/scons/gallium.py
+++ b/scons/gallium.py
@@ -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':
-- 
1.8.3.2

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 1/3] scons: Enable building through Clang Static Analyzer.

2014-04-14 Thread Brian Paul

On 04/14/2014 07:32 AM, jfons...@vmware.com wrote:

From: José Fonseca jfons...@vmware.com

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
---
  scons/gallium.py | 31 +++
  1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/scons/gallium.py b/scons/gallium.py
index e11d4db..42e8f7c 100755
--- a/scons/gallium.py
+++ b/scons/gallium.py
@@ -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':



Series looks good to me.

Reviewed-by: Brian Paul bri...@vmware.com

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/mesa-dev