1 | #!/usr/bin/perl -w |
---|
2 | # |
---|
3 | # This script parses and fixes dependency lines in Makefiles: |
---|
4 | # 1. Searches for a line containing '# DO NOT DELETE' |
---|
5 | # 2. Lines after that point are modified like follows: |
---|
6 | # a. hardcoded directory path to $ARBHOME (environment variable) |
---|
7 | # is replaced by '$(ARBHOME)' |
---|
8 | # b. split lines with multiple dependencies |
---|
9 | # c. sort lines |
---|
10 | # |
---|
11 | # Goal of this script is to unify the result of 'make depends' |
---|
12 | # to avoid CVS/SVN changes caused by formatting. |
---|
13 | |
---|
14 | my $arbhome = qr/$ENV{ARBHOME}/; |
---|
15 | my $makedependlineseen = 0; |
---|
16 | my @depends; |
---|
17 | |
---|
18 | if (0) { |
---|
19 | foreach (<>) { |
---|
20 | print "ALL: $_"; |
---|
21 | } |
---|
22 | |
---|
23 | die "done"; |
---|
24 | } |
---|
25 | |
---|
26 | sub fix_name($) { |
---|
27 | my ($name) = @_; |
---|
28 | $name =~ s/^$arbhome/\$\(ARBHOME\)/ig; # translate $ARBHOME |
---|
29 | $name =~ s/^.\///ig; # remove './' at start |
---|
30 | |
---|
31 | # ensure there's a / behind '$(ARBHOME)' |
---|
32 | if ($name =~ /\$\(ARBHOME\)[^\/]/) { |
---|
33 | $name =~ s/\$\(ARBHOME\)/\$\(ARBHOME\)\//ig; |
---|
34 | } |
---|
35 | |
---|
36 | $name; |
---|
37 | } |
---|
38 | |
---|
39 | # read input stream |
---|
40 | foreach (<>) { |
---|
41 | if ($makedependlineseen==0) { # simply forward lines before 'DO NOT DELETE' |
---|
42 | print "$_"; |
---|
43 | if (/^\# DO NOT DELETE/) { $makedependlineseen = 1; } |
---|
44 | } |
---|
45 | else { # put lines behind into '@depends' |
---|
46 | chomp; |
---|
47 | if (/^ *[\/\$a-z]/i) { |
---|
48 | if (/^([^:]*): *(.*)$/) { |
---|
49 | my $file = $1; |
---|
50 | my $depends_on = $2; |
---|
51 | $file = fix_name($file); |
---|
52 | |
---|
53 | while ($depends_on =~ / /) { # split lines with multiple dependencies |
---|
54 | my $name = $`; |
---|
55 | my $rest = $'; |
---|
56 | $name = fix_name($name); |
---|
57 | push @depends, "$file: $name"; |
---|
58 | $depends_on = $rest; |
---|
59 | } |
---|
60 | $depends_on = fix_name($depends_on); |
---|
61 | $_ = "$file: $depends_on"; |
---|
62 | } |
---|
63 | push @depends,$_; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | print "\n# Do not add dependencies manually - use 'make depend' in \$ARBHOME\n"; |
---|
69 | print "# For formatting issues see SOURCE_TOOLS/fix_depends.pl\n"; |
---|
70 | |
---|
71 | # sort dependency lines |
---|
72 | |
---|
73 | sub beautiful($$) { |
---|
74 | # sorts files alphabethically (ign. case) |
---|
75 | # sorts local dependencies first (for each file) |
---|
76 | my ($a,$b) = @_; |
---|
77 | my ($ap,$bp) = ('',''); |
---|
78 | ($a,$b) = (lc($a),lc($b)); |
---|
79 | |
---|
80 | if ($a =~ /^[^:]*:/) { $ap = $&; } |
---|
81 | if ($b =~ /^[^:]*:/) { $bp = $&; } |
---|
82 | |
---|
83 | my $res = $ap cmp $bp; |
---|
84 | |
---|
85 | if ($res == 0) { |
---|
86 | if ($a =~ /\$/) { |
---|
87 | if ($b =~ /\$/) { $a cmp $b; } |
---|
88 | else { 1; } |
---|
89 | } |
---|
90 | else { |
---|
91 | if ($b =~ /\$/) { -1; } |
---|
92 | else { $a cmp $b; } |
---|
93 | } |
---|
94 | } |
---|
95 | else { |
---|
96 | $res; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | @depends = sort beautiful @depends; |
---|
101 | |
---|
102 | # print dependency lines |
---|
103 | |
---|
104 | my $prefix = ''; |
---|
105 | foreach (@depends) { |
---|
106 | my $tprefix = ''; |
---|
107 | if (/^([^:]*):/) { $tprefix = $1; } |
---|
108 | if ($tprefix ne $prefix) { |
---|
109 | print "\n"; # empty line between different files |
---|
110 | $prefix = $tprefix; |
---|
111 | } |
---|
112 | print "$_\n"; |
---|
113 | } |
---|