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 | use strict; |
---|
15 | use warnings; |
---|
16 | |
---|
17 | my $arbhome_quoted = quotemeta($ENV{ARBHOME}); |
---|
18 | my $arbhome = qr/$arbhome_quoted/; |
---|
19 | |
---|
20 | sub fix_name($) { |
---|
21 | my ($name) = @_; |
---|
22 | $name =~ s/^$arbhome/\$\(ARBHOME\)/ig; # translate $ARBHOME |
---|
23 | $name =~ s/^.\///ig; # remove './' at start |
---|
24 | |
---|
25 | # ensure there's a / behind '$(ARBHOME)' |
---|
26 | if ($name =~ /\$\(ARBHOME\)[^\/]/) { |
---|
27 | $name =~ s/\$\(ARBHOME\)/\$\(ARBHOME\)\//ig; |
---|
28 | } |
---|
29 | |
---|
30 | $name; |
---|
31 | } |
---|
32 | |
---|
33 | sub arb_dependency_order($$) { |
---|
34 | # sorts files alphabetically (ign. case) |
---|
35 | # sorts local dependencies first (for each file) |
---|
36 | my ($a,$b) = @_; |
---|
37 | my ($ap,$bp) = ('',''); |
---|
38 | ($a,$b) = (lc($a),lc($b)); |
---|
39 | |
---|
40 | if ($a =~ /^[^:]*:/) { $ap = $&; } |
---|
41 | if ($b =~ /^[^:]*:/) { $bp = $&; } |
---|
42 | |
---|
43 | my $res = $ap cmp $bp; |
---|
44 | |
---|
45 | if ($res == 0) { |
---|
46 | if ($a =~ /\$/) { |
---|
47 | if ($b =~ /\$/) { $a cmp $b; } |
---|
48 | else { 1; } |
---|
49 | } |
---|
50 | else { |
---|
51 | if ($b =~ /\$/) { -1; } |
---|
52 | else { $a cmp $b; } |
---|
53 | } |
---|
54 | } |
---|
55 | else { |
---|
56 | $res; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | sub add_dependency(\@$$) { |
---|
61 | my ($depends_r,$file,$depends_on) = @_; |
---|
62 | $depends_on = fix_name($depends_on); |
---|
63 | |
---|
64 | # skip outside dependencies |
---|
65 | if (not $depends_on =~ m@/usr/(include|lib)/@) { |
---|
66 | push @$depends_r, "$file: $depends_on"; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | sub read_input_stream(\@) { |
---|
71 | my ($depends_r) = @_; |
---|
72 | my $makedependlineseen = 0; |
---|
73 | |
---|
74 | foreach (<STDIN>) { |
---|
75 | if ($makedependlineseen==0) { # simply forward lines until 'DO NOT DELETE' |
---|
76 | print "$_"; |
---|
77 | if (/^\# DO NOT DELETE/) { $makedependlineseen = 1; } |
---|
78 | } |
---|
79 | else { # put lines behind into '@depends' |
---|
80 | chomp; |
---|
81 | if (/^ *[\/\$a-z]/i) { # skip empty lines |
---|
82 | if (/^([^:]*): *(.*)$/) { |
---|
83 | my $file = $1; |
---|
84 | my $depends_on = $2; |
---|
85 | $file = fix_name($file); |
---|
86 | |
---|
87 | while ($depends_on =~ / /) { # split lines with multiple dependencies |
---|
88 | my $name = $`; |
---|
89 | my $rest = $'; |
---|
90 | |
---|
91 | add_dependency(@$depends_r,$file,$name); |
---|
92 | $depends_on = $rest; |
---|
93 | } |
---|
94 | add_dependency(@$depends_r,$file,$depends_on); |
---|
95 | } |
---|
96 | else { |
---|
97 | push @$depends_r,$_; # what lines go here? |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | sub main() { |
---|
105 | my $comment = join(' ',@ARGV); |
---|
106 | if ($comment ne '') { $comment = ' '.$comment } |
---|
107 | @ARGV = (); |
---|
108 | |
---|
109 | my @depends=(); |
---|
110 | read_input_stream(@depends); |
---|
111 | |
---|
112 | @depends = sort arb_dependency_order @depends; |
---|
113 | |
---|
114 | print "\n# Do not add dependencies manually - use 'make depend' in \$ARBHOME\n"; |
---|
115 | print "# For formatting issues see SOURCE_TOOLS/fix_depends.pl$comment\n"; |
---|
116 | |
---|
117 | # print dependency lines |
---|
118 | my $prefix = ''; |
---|
119 | foreach (@depends) { |
---|
120 | my $tprefix = ''; |
---|
121 | if (/^([^:]*):/) { $tprefix = $1; } |
---|
122 | if ($tprefix ne $prefix) { |
---|
123 | print "\n"; # empty line between different files |
---|
124 | $prefix = $tprefix; |
---|
125 | } |
---|
126 | print "$_\n"; |
---|
127 | } |
---|
128 | } |
---|
129 | main(); |
---|