gcc-wrapper.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #! /usr/bin/env python2
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # Copyright (c) 2011-2017, 2018 The Linux Foundation. All rights reserved.
  4. # -*- coding: utf-8 -*-
  5. # Invoke gcc, looking for warnings, and causing a failure if there are
  6. # non-whitelisted warnings.
  7. import errno
  8. import re
  9. import os
  10. import sys
  11. import subprocess
  12. # Note that gcc uses unicode, which may depend on the locale. TODO:
  13. # force LANG to be set to en_US.UTF-8 to get consistent warnings.
  14. allowed_warnings = set([
  15. "umid.c:138",
  16. "umid.c:213",
  17. "umid.c:388",
  18. "coresight-catu.h:116",
  19. "mprotect.c:42",
  20. "signal.c:93",
  21. "signal.c:51",
  22. ])
  23. # Capture the name of the object file, can find it.
  24. ofile = None
  25. warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? warning:''')
  26. def interpret_warning(line):
  27. """Decode the message from gcc. The messages we care about have a filename, and a warning"""
  28. line = line.rstrip('\n')
  29. m = warning_re.match(line)
  30. if m and m.group(2) not in allowed_warnings:
  31. print "error, forbidden warning:", m.group(2)
  32. # If there is a warning, remove any object if it exists.
  33. if ofile:
  34. try:
  35. os.remove(ofile)
  36. except OSError:
  37. pass
  38. sys.exit(1)
  39. def run_gcc():
  40. args = sys.argv[1:]
  41. # Look for -o
  42. try:
  43. i = args.index('-o')
  44. global ofile
  45. ofile = args[i+1]
  46. except (ValueError, IndexError):
  47. pass
  48. compiler = sys.argv[0]
  49. try:
  50. proc = subprocess.Popen(args, stderr=subprocess.PIPE)
  51. for line in proc.stderr:
  52. print line,
  53. interpret_warning(line)
  54. result = proc.wait()
  55. except OSError as e:
  56. result = e.errno
  57. if result == errno.ENOENT:
  58. print args[0] + ':',e.strerror
  59. print 'Is your PATH set correctly?'
  60. else:
  61. print ' '.join(args), str(e)
  62. return result
  63. if __name__ == '__main__':
  64. status = run_gcc()
  65. sys.exit(status)