From 41c170c42fed338e449fc1e6e49f977a81e1b082 Mon Sep 17 00:00:00 2001 From: Shunqian Zheng Date: Wed, 18 Jul 2018 11:10:26 +0800 Subject: [PATCH] scripts: make gcc-wrapper.py compatible with python 2.7 and 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python 3 requires parentheses in call to 'print', meanwhile the 'line' could be bytes-like, let's decoding to str as utf-8. This makes the gcc-wrapper.py compatible with both 2.7 and 3. For example, a bytes-like string as below, b'kernel/reboot.c:47:13: error: function declaration isn\xe2\x80\x99t a prototype [-Werror=strict-prototypes]\n' b' static void no_use()\n' b' ^~~~~~\n' After decoding, it looks like, kernel/reboot.c:47:13: error: function declaration isn’t a prototype [-Werror=strict-prototypes] static void no_use() ^~~~~~ Change-Id: Icacdbe2ca7b7ab674ab90e54b79d3176e0061ac6 Signed-off-by: Shunqian Zheng Signed-off-by: Tao Huang --- scripts/gcc-wrapper.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/scripts/gcc-wrapper.py b/scripts/gcc-wrapper.py index 672b504b84e0..3a4b766924c4 100755 --- a/scripts/gcc-wrapper.py +++ b/scripts/gcc-wrapper.py @@ -30,6 +30,7 @@ # Invoke gcc, looking for warnings, and causing a failure if there are # non-whitelisted warnings. +from __future__ import print_function import errno import re import os @@ -58,13 +59,15 @@ allowed_warnings = set([ # Capture the name of the object file, can find it. ofile = None +do_exit = False; + warning_re = re.compile(r'''(.*/|)([^/]+\.[a-z]+:\d+):(\d+:)? warning:''') def interpret_warning(line): """Decode the message from gcc. The messages we care about have a filename, and a warning""" line = line.rstrip('\n') m = warning_re.match(line) if m and m.group(2) not in allowed_warnings: - print "error, forbidden warning:", m.group(2) + print ("error, forbidden warning:" + m.group(2)) # If there is a warning, remove any object if it exists. if ofile: @@ -72,7 +75,8 @@ def interpret_warning(line): os.remove(ofile) except OSError: pass - sys.exit(1) + global do_exit + do_exit = True; def run_gcc(): args = sys.argv[1:] @@ -89,17 +93,19 @@ def run_gcc(): try: proc = subprocess.Popen(args, stderr=subprocess.PIPE) for line in proc.stderr: - print line, - interpret_warning(line) + print (line.decode("utf-8"), end="") + interpret_warning(line.decode("utf-8")) + if do_exit: + sys.exit(1) result = proc.wait() except OSError as e: result = e.errno if result == errno.ENOENT: - print args[0] + ':',e.strerror - print 'Is your PATH set correctly?' + print (args[0] + ':' + e.strerror) + print ('Is your PATH set correctly?') else: - print ' '.join(args), str(e) + print (' '.join(args) + str(e)) return result