blob: 40dc9fb9335ea52d740dd2e2cda75fdcf1a06700 [file] [log] [blame]
Rishabh Bhatnagarf6619422018-04-05 14:19:20 -07001#! /usr/bin/env python2
Rishabh Bhatnagare9a05bb2018-12-10 11:09:45 -08002# SPDX-License-Identifier: GPL-2.0-only
Rishabh Bhatnagarf6619422018-04-05 14:19:20 -07003# 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
10import errno
11import re
12import os
13import sys
14import 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
19allowed_warnings = set([
Channagoud Kadabi4b619332018-09-14 15:58:41 -070020 "umid.c:138",
21 "umid.c:213",
22 "umid.c:388",
Rishabh Bhatnagarf6619422018-04-05 14:19:20 -070023 ])
24
25# Capture the name of the object file, can find it.
26ofile = None
27
28warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? warning:''')
29def 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
44def 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
73if __name__ == '__main__':
74 status = run_gcc()
75 sys.exit(status)