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