Merge zizzer.eecs.umich.edu:/bk/m5
[gem5.git] / util / sort-includes
1 #! /usr/bin/env perl
2 # Copyright (c) 2003 The Regents of The University of Michigan
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #
28 # Authors: Steve Reinhardt
29
30 open (FOO, "<$ARGV[0]") or die;
31
32 # Suck in everything before the first include
33 # (line-by-line into @before list).
34 while (($_ = <FOO>) && !/^#include/) {
35 push @before, $_;
36 }
37
38 #print join("", @before);
39 #print "##########################\n";
40
41 # Suck in include lines into @includes list.
42 # Skip blank lines (keep processing, but don't put in @includes).
43 # End on first non-blank, non-include line.
44 # Note that this means that files with comments or #ifdefs
45 # interspersed among their #includes will only get the initial
46 # set of #includes sorted.
47 do {
48 push @includes, $_ unless /^\s*$/;
49 } while (($_ = <FOO>) && /^#include|^\s*$/);
50
51 # Now sort the includes. This simple ordering function
52 # puts system includes first, followed by non-system includes.
53 # Within each group the sort is alphabetical.
54 # We may want something a little more sophisticated.
55 # Personally, I'd like to see something like:
56 # <sys/*.h> - header files from sys subdir
57 # <*.h> - other system headers
58 # <*> - STL headers
59 # "base/*" - M5 base headers
60 # "sim/*" - M5 sim headers
61 # "*" - other M5 headers
62 # ...but I didn't have the energy to code that up.
63 sub sortorder {
64 my $sysa = ($a =~ /<.*>/);
65 my $sysb = ($b =~ /<.*>/);
66 return -1 if ($sysa && !$sysb);
67 return 1 if ($sysb && !$sysa);
68 return $a cmp $b;
69 }
70
71 @includes = sort sortorder @includes;
72 #print join("", @includes);
73 #print "##########################\n";
74
75 # Put everything after the includes in the @after list.
76 do {
77 push @after, $_;
78 if (/^#include/) {
79 print "$ARGV[0]: ";
80 print $after[0];
81 exit 0;
82 }
83 } while ($_ = <FOO>);
84
85 #print join("", @after);
86 #print "##########################\n";
87
88 # Print out the file with sorted includes.
89
90 print join("", @before, @includes, @after);
91