werror.py (2599B)
1 #!/usr/bin/env python3 2 3 import os 4 import subprocess 5 6 def main(): 7 cc = os.environ.get('CC', 'cc') 8 sink = open(os.devnull, 'wb') 9 try: 10 cc_is_clang = 'clang' in subprocess.check_output( 11 [cc, '--version'], universal_newlines=True, stderr=sink) 12 except OSError: 13 # We probably just don't have CC/cc. 14 return 15 16 def warning_supported(warning): 17 return subprocess.call([cc, '-x', 'c', '-E', '-Werror', 18 '-W%s' % warning, os.devnull], stdout=sink, stderr=sink) == 0 19 def can_enable(): 20 # This would be a problem 21 if not warning_supported('all'): 22 return False 23 24 # If we aren't clang, make sure we have gcc 4.8 at least 25 if not cc_is_clang: 26 try: 27 v = subprocess.check_output([cc, '-dumpversion'], stderr=sink).decode("utf-8") 28 v = v.strip(' \r\n').split('.') 29 v = list(map(int, v)) 30 if v[0] < 4 or (v[0] == 4 and v[1] < 8): 31 # gcc 4.8 minimum 32 return False 33 except OSError: 34 return False 35 return True 36 37 if not can_enable(): 38 print('-DNSS_NO_GCC48') 39 return 40 41 print('-Werror') 42 print('-Wall') 43 44 def set_warning(warning, contra=''): 45 if warning_supported(warning): 46 print('-W%s%s' % (contra, warning)) 47 48 if cc_is_clang: 49 # clang is unable to handle glib's expansion of strcmp and similar for 50 # optimized builds, so disable the resulting errors. 51 # See https://llvm.org/bugs/show_bug.cgi?id=20144 52 for w in ['array-bounds', 53 'unevaluated-expression', 54 'parentheses-equality', 55 'tautological-type-limit-compare', 56 'sign-compare', 57 'comma', 58 'implicit-fallthrough' 59 ]: 60 set_warning(w, 'no-') 61 for w in ['tautological-constant-in-range-compare', 62 'bitfield-enum-conversion', 63 'empty-body', 64 'format-type-confusion', 65 'ignored-qualifiers', 66 'pointer-arith', 67 'type-limits', 68 'unreachable-code', 69 'unreachable-code-return', 70 'duplicated-cond', 71 'logical-op', 72 'implicit-function-declaration' 73 ]: 74 set_warning(w,'') 75 print('-Qunused-arguments') 76 77 set_warning('shadow') 78 79 if __name__ == '__main__': 80 main()