diff --git a/Makefile b/Makefile index 5155fb034..79f5bcb90 100644 --- a/Makefile +++ b/Makefile @@ -199,7 +199,7 @@ CPP_GEN = ${SOURCE_ROOT}/tools/cpp_gen.sh SYSCALL_ID_GEN_PATH = ${SOURCE_ROOT}/tools/syscall_header_gen.py INVOCATION_ID_GEN_PATH = ${SOURCE_ROOT}/tools/invocation_header_gen.py XMLLINT = xmllint.sh -CIRCULAR_INCLUDES = ${SOURCE_ROOT}/tools/circular_includes.pl +CIRCULAR_INCLUDES = ${SOURCE_ROOT}/tools/circular_includes.py ######################################## ## Check tools diff --git a/tools/circular_includes.pl b/tools/circular_includes.pl deleted file mode 100755 index 3a37a4381..000000000 --- a/tools/circular_includes.pl +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/perl -w - -@file_stack=(); - -while (<>) { - $input=$_; - if ($input =~ /^# 1 "kernel_all.c"/) { - } elsif ($input =~ /^# 1 "(.*\..)"/) { - # Found a new header - $header = $1; - foreach $item (@file_stack) { - if ($item eq $header) { - print "Circular includes found:\n"; - map { print "$_\n" } @file_stack; - print "$header\n"; - exit -1; - } - } - push @file_stack, $header; - } elsif ($input =~ /^# \d+ "(.*\..)"/) { - # Have popped back up to an earlier header - $header = $1; - while ($file_stack[$#file_stack] ne $header) { - pop @file_stack; - } - } -} -exit 0; diff --git a/tools/circular_includes.py b/tools/circular_includes.py new file mode 100755 index 000000000..691310fe3 --- /dev/null +++ b/tools/circular_includes.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +""" +Script for reporting circular #includes in pre-processed sel4 source. +Exits with a status of 0 if no circular dependencies are found, otherwise +prints circular dependency and exits with a status of -1. +""" + +import sys +import re + +def main(): + """ + Reads pre-processed sel4 source from standard input. + If a circular dependency is found, the chain of includes + resulting in the loop is printed out. + """ + + kernel_all_re = re.compile(r'^# 1 "kernel_all\.c"') + header_re = re.compile(r'^# (\d+) "(.*\..)"') + + file_stack = [] + + for line in sys.stdin: + + if kernel_all_re.match(line): + continue + + match = header_re.match(line) + + if match is None: + continue + + depth = int(match.group(1)) + header = match.group(2) + + if depth == 1: + # found a new header + if header in file_stack: + print("Circular includes found:") + print("\n".join(file_stack)) + print(header) + return -1 + else: + file_stack.append(header) + else: + # popped back up to an earlier header + while file_stack[-1] != header: + file_stack.pop() + + return 0 + +if __name__ == "__main__": + + if len(sys.argv) != 1: + print("Usage: %s < path/to/kernel_all.c_pp" % sys.argv[0]) + print(__doc__) + sys.exit(-1) + + sys.exit(main())