2014-09-26 06:12:33 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import select
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Wrapping this rather than os.devnull since os.devnull does not seem to implement 'write'.
|
2014-10-02 22:07:46 +08:00
|
|
|
class NullWriter(object):
|
|
|
|
def write (self, *args):
|
|
|
|
pass
|
|
|
|
def writelines (self, *args):
|
|
|
|
pass
|
|
|
|
def close (self, *args):
|
|
|
|
pass
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Find all "Makefile"-s in the current directory.
|
2014-09-26 06:12:33 +08:00
|
|
|
def find_makefile_dirs():
|
|
|
|
makefile_dirs = []
|
|
|
|
for root, dirs, files in os.walk("."):
|
|
|
|
for file in files:
|
|
|
|
if file == "Makefile":
|
|
|
|
makefile_dirs.append(root)
|
|
|
|
return makefile_dirs
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Test if line starts with <file name>:<line number> pattern
|
2014-09-26 06:12:33 +08:00
|
|
|
_TESTDIR_RELATIVE_REGEX = re.compile(r"^([^/:]+:\d+:)")
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Test if the line starts with the string "[ FAILED ]" (whitespace
|
|
|
|
# independent) what displayed on the end of a failed test case
|
|
|
|
_COMBINER_TERMINATION_REGEX = re.compile(r"^\[ *FAILED *\]")
|
2014-09-26 20:58:25 +08:00
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Prepends directory before each file name in line matching the regular
|
|
|
|
# expression "_TESTDIR_RELATIVE_REGEX" and returns the new value of line and the
|
|
|
|
# number of file names modified as a tuple.
|
|
|
|
def expand_file_name(directory, line):
|
|
|
|
return _TESTDIR_RELATIVE_REGEX.subn(directory + r"/\1", line)
|
|
|
|
|
|
|
|
# Combine the failure report information from the output of gtest into a
|
|
|
|
# single line for better displaying inside IDEs
|
2014-09-26 20:58:25 +08:00
|
|
|
def line_combine_printer(file, previous_data, new_line_subn_result):
|
|
|
|
(incoming_line, sub_match_count) = new_line_subn_result
|
|
|
|
|
|
|
|
if sub_match_count > 0:
|
2015-01-23 19:02:28 +08:00
|
|
|
# New line was a match for a file name. It means is the first line of
|
|
|
|
# a failure report. Don't print yet, start an accumulation for more
|
|
|
|
# info about the failure.
|
|
|
|
if len(previous_data) > 0:
|
|
|
|
# Flush anything previously accumulated (most likely part of the
|
|
|
|
# previous failure report).
|
|
|
|
print(previous_data, file=file)
|
|
|
|
return incoming_line + ": "
|
2014-09-26 20:58:25 +08:00
|
|
|
else:
|
2015-01-23 19:02:28 +08:00
|
|
|
# If we're combining and incoming is a "[ FAILED ]" line then we have
|
|
|
|
# to stop combining now.
|
|
|
|
if (len(previous_data) > 0) and _COMBINER_TERMINATION_REGEX.match(incoming_line):
|
|
|
|
# Stop the combine and print out its data and the FAIL line also.
|
|
|
|
print(previous_data, file=file)
|
2014-09-26 20:58:25 +08:00
|
|
|
print(incoming_line, file=file)
|
2015-01-23 19:02:28 +08:00
|
|
|
return ""
|
2014-09-26 20:58:25 +08:00
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
if len(previous_data) > 0:
|
|
|
|
# Previous data is available what means we are currently
|
|
|
|
# accumulating a failure report. Append this line to it.
|
|
|
|
if len(previous_data) >= 2 and previous_data[-2:] != ": ":
|
|
|
|
return previous_data + ", " + incoming_line
|
2014-09-27 03:08:00 +08:00
|
|
|
else:
|
2015-01-23 19:02:28 +08:00
|
|
|
return previous_data + incoming_line
|
2014-09-26 20:58:25 +08:00
|
|
|
else:
|
2015-01-23 19:02:28 +08:00
|
|
|
# No previous data and don't have to start new accumulation. Just
|
|
|
|
# print the incoming line if it is not empty.
|
|
|
|
if len(incoming_line) > 0:
|
|
|
|
print(incoming_line, file=file)
|
|
|
|
return ""
|
2014-09-26 20:58:25 +08:00
|
|
|
|
2014-10-02 22:07:46 +08:00
|
|
|
def call_make(makefile_dir, extra_args=None, stdout=sys.stdout, stderr=sys.stderr):
|
2014-09-26 06:12:33 +08:00
|
|
|
command = ["make", "-C", makefile_dir]
|
|
|
|
if extra_args:
|
|
|
|
command.extend(extra_args)
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Execute make as a new subprocess
|
2014-09-26 06:12:33 +08:00
|
|
|
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
stdout_data, stderr_data = "", ""
|
2014-09-26 20:58:25 +08:00
|
|
|
|
2014-09-26 06:12:33 +08:00
|
|
|
while True:
|
|
|
|
reads = [proc.stdout.fileno(), proc.stderr.fileno()]
|
|
|
|
select_result = select.select(reads, [], [])
|
|
|
|
|
2015-01-23 19:02:28 +08:00
|
|
|
# Copy the currently available output from make to the standard output
|
|
|
|
# streams (stdout or stderr)
|
2014-09-26 06:12:33 +08:00
|
|
|
for fd in select_result[0]:
|
|
|
|
if fd == proc.stdout.fileno():
|
|
|
|
line = proc.stdout.readline()
|
2015-01-23 19:02:28 +08:00
|
|
|
stdout_data = line_combine_printer(stdout, stdout_data, expand_file_name(makefile_dir, line.rstrip()))
|
2014-09-26 06:12:33 +08:00
|
|
|
elif fd == proc.stderr.fileno():
|
|
|
|
line = proc.stderr.readline()
|
2015-01-23 19:02:28 +08:00
|
|
|
stderr_data = line_combine_printer(stderr, stderr_data, expand_file_name(makefile_dir, line.rstrip()))
|
2014-09-26 06:12:33 +08:00
|
|
|
|
|
|
|
proc_retval = proc.poll()
|
|
|
|
if proc_retval != None:
|
2015-01-23 19:02:28 +08:00
|
|
|
# Process stopped. Drain output before finishing up.
|
2014-09-26 06:12:33 +08:00
|
|
|
|
|
|
|
# Drain stdout.
|
|
|
|
while True:
|
2015-01-20 02:18:24 +08:00
|
|
|
line = proc.stdout.readline()
|
2014-09-26 20:58:25 +08:00
|
|
|
if line and len(line) > 0:
|
2015-01-23 19:02:28 +08:00
|
|
|
stdout_data = line_combine_printer(stdout, stdout_data, expand_file_name(makefile_dir, line.rstrip()))
|
2014-09-26 06:12:33 +08:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Drain stderr.
|
|
|
|
while True:
|
2015-01-20 02:18:24 +08:00
|
|
|
line = proc.stderr.readline()
|
2014-09-26 20:58:25 +08:00
|
|
|
if line and len(line) > 0:
|
2015-01-23 19:02:28 +08:00
|
|
|
stderr_data = line_combine_printer(stderr, stderr_data, expand_file_name(makefile_dir, line.rstrip()))
|
2014-09-26 06:12:33 +08:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
return proc_retval
|
|
|
|
|
|
|
|
|
|
|
|
global_retval = 0
|
|
|
|
|
2014-10-02 22:07:46 +08:00
|
|
|
do_clean_only = ('clean' in sys.argv)
|
2014-09-26 06:12:33 +08:00
|
|
|
|
|
|
|
for makefile_dir in find_makefile_dirs():
|
2014-10-02 22:07:46 +08:00
|
|
|
# If we're not only cleaning, we do the normal build.
|
|
|
|
if not do_clean_only:
|
|
|
|
print("making: {}".format(makefile_dir))
|
|
|
|
retval = call_make(makefile_dir)
|
|
|
|
# Remember any errors that happen here.
|
|
|
|
if retval != 0:
|
2015-01-23 19:02:28 +08:00
|
|
|
print()
|
|
|
|
print("-" * 80)
|
|
|
|
print("Tests failed for Makefile in directory: %s" % makefile_dir)
|
|
|
|
print("-" * 80)
|
|
|
|
print()
|
2014-10-02 22:07:46 +08:00
|
|
|
global_retval = retval
|
|
|
|
|
|
|
|
# Now clean
|
|
|
|
call_make(makefile_dir, ['clean'], stdout=NullWriter(), stderr=NullWriter())
|
2015-01-23 19:02:28 +08:00
|
|
|
|
|
|
|
if global_retval == 0:
|
|
|
|
print()
|
|
|
|
print("========================")
|
|
|
|
print("| All tests are PASSED |")
|
|
|
|
print("========================")
|
|
|
|
else:
|
|
|
|
print()
|
|
|
|
print("=========================================================")
|
|
|
|
print("| Some of the test cases are FAILED with return code: %d |" % global_retval)
|
|
|
|
print("=========================================================")
|
2014-09-26 06:12:33 +08:00
|
|
|
sys.exit(global_retval)
|