source: rtems/tools/update/ampolish @ cadbf44a

4.104.114.84.95
Last change on this file since cadbf44a was cadbf44a, checked in by Joel Sherrill <joel.sherrill@…>, on 08/01/00 at 14:16:16

Patch rtems-rc-20000731-1-cvs.diff from Ralf Corsepius <corsepiu@…>
that is a cosmetic patch to ampolish.

  • Property mode set to 100755
File size: 10.1 KB
RevLine 
[9b8baa1]1#!/usr/bin/perl
2
[df49c60]3package main ;
4
5use strict ;
6
[9b8baa1]7#
8# Perl script to beautify and enhance RTEMS automake Makefile.ams
9#
10# Reads from stdin and writes to stdout
11#
12# usage:
13# <path-to>/ampolish <Makefile.am >Makefile.am~
14# mv Makefile.am~ Makefile.am
15#
16
[df49c60]17my @vars ;
18my @conditions = ( "" ) ;
19my @buffer = ();
20my %var_ ;
21
22define_variable( "\$(AUTOMAKE_OPTIONS)", ( "foreign",  "1.4" ) );
23define_variable( "\$(VPATH)", ( "\@srcdir\@" ) );
24
25# find relative up-path to configure.in
26my $rtems_cfg = find_file(".","configure.in");
27
28# find relative up-path from configure.in to VERSION
29my $rtems_top = find_file("$rtems_cfg","VERSION");
30
31if ( "$rtems_top" eq "." ) { $rtems_top = "" ; }
32else { $rtems_top .= "/" ; } 
33
34{
35# PASS1:
36# read input file and concatenate multiple lines into single lines.
37
38  my @ibuf = () ;
39
40  while( <STDIN> )
41  { # consume header
42    last if ( /^[^#].*$/ ) ;
43    push @ibuf, "$_" ;
44  }
45
46  push @ibuf, "§header\n" ;
47
48  do
49  {
50    if ( /^(#.*)$/o )
51    { # preserve comments
52        push @ibuf, "$_" ;
53    }
54    elsif ( /^(\t.*)\\[\s]*$/o )
55      { # multilines for scripts
56        my $line = "$1§" ;
57        while( <STDIN> )
58        {
59          if ( /^(.*)\\[\s]*$/o )
60          {
61            $line .= "$1§" ;
62          }
63          else
64          {
65            $line .= "$_" ;
66            push @ibuf, $line ;
67            last ;
68          }
69        }
70      }
71    elsif ( /^(.*)\\[\s]*$/o )
72      { # multilines
73        my $line = "$1" ;
74        while( <STDIN> )
75        {
76          if ( /^(.*)\\[\s]*$/o )
77          {
78            $line .= "$1" ;
79          }
80          else
81          {
82            $line .= "$_" ;
83            $line =~ s%[\s]+% %g ;
84            push @ibuf, "$line\n" ;
85            last ;
86          }
87        }
88      }
89    else
90      {
91        push @ibuf, "$_" ;
92      }
93  } while ( <STDIN> ) ;
94  @buffer = @ibuf ;
95}
96
97{
98# PASS2:
99# fix obsolete constructs
100  my @ibuf = () ;
101
102  foreach ( @buffer )
103  {
104#    tr /\{\}/\(\)/ ;
105
106    if ( /^(TMP|PRE)INSTALL_FILES[\s]*=(.*)$/o )
107    { # force "+="
108      push @ibuf, "$1INSTALL_FILES +=$2\n" ;
109    }
110    elsif ( /^(VPATH|EXTRA_DIST)[\s]*\+=(.*)$/o )
111    { # force "="
112      push @ibuf, "$1 = $2\n" ;
113    }
114    elsif ( /^[\s]*ACLOCAL[\s]*=[\s]*\@ACLOCAL\@.*$/o )
115    { # remove the line
116    }
117    elsif ( /^[\s]*(ACLOCAL_AMFLAGS)[\s\t]*[\+]*=[\s]*(.*)[\s]*$/o )
118    { # remove the line
119    }
120    elsif ( /^[\s]*(AM_CFLAGS)[\s\t]*[\+]*=[\s]*\$\(CFLAGS_OS_V\)[\s]*$/o )
121    { # remove the line
122    }
123    elsif ( /^[\s]*debug-am:.*$/o )
124    { # remove the line
125    }
126    elsif ( /^[\s]*all(\-am):(.*)$/o )
127    { # replace the line
128      push @ibuf, "all-local:$2\n" ;
129    }
130    elsif ( /^[\s]*profile-am:.*$/o )
131    { # remove the line
132    }
133    elsif ( /^[\s]*include[\s\t]*\$\(RTEMS_ROOT\)\/make\/lib.cfg[\s]*$/o )
134    {
135      push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/lib.am\n" ;
136    }
137    elsif ( /^(.*[^\s])[\s]*$/o )
138    { # remove trailing spaces
139      push @ibuf, "$1\n" ;
140    }
141    else
142    {
143      push @ibuf, "$_" ;
144    }
145  }
146  @buffer = @ibuf ;
147}
148
149# print STDERR "<tmp>\n", @buffer, "</tmp>\n" ;
150
151{
152  my @ibuf = () ;
153  foreach ( @buffer )
154  {
155    if ( /^#(.*)$/o )
156    {
157      push @ibuf, "#$1\n" ;
158    }
159    elsif ( /^[\s]*if[\s\t]+([a-zA-Z0-9_]+)[\s\t]*$/o )
160    {
161      push @conditions, "\@" . $1 . "_TRUE\@" ;
162      push @ibuf, "if $1\n" ;
163    }
164    elsif ( /^[\s]*else[\s\t]*$/o )
165    {
166      @conditions[$#conditions] =~ s/_TRUE\@$/_FALSE\@/;
167      push @ibuf, "else\n" ;
168    }
169    elsif ( /^[\s]*endif[\s\t]*$/o )
170    {
171      pop @conditions ;
172      push @ibuf, "endif\n" ;
173    }
174    elsif ( /^§.*$/o )
175    {
176      push @ibuf, "$_" ;
177    }
178    elsif ( /^[\s]*(VPATH)[\s\t]*([\+]*)=[\s]*(.*)[\s]*$/o )
179    {
180      my $lh = "\$($1)" ;
181      my @rh = split( /:/,"$3");
182      if ( $#conditions  > 0 )
183      {
184        print STDERR "WARNING: $1 must not be set inside of conditionals!\n"
185      }
186      define_variable( "$lh", @rh );
187
188    }
189    elsif ( /^[\s]*(AUTOMAKE_OPTIONS)[\s\t]*([\+]*)=[\s]*(.*)$/o )
190    {
191      my $lh = "\$($1)" ;
192      my @rh = &split_vars("$3");
193
194      if ( $#conditions > 0 )
195      {
196        print STDERR "WARNING: $1 must not be set inside of conditionals!\n"
197      }
198
199      define_variable( "$lh", @rh );
200    }
201    elsif ( /^[\s]*([a-zA-Z0-9_]+)[\s\t]*([\+]*)=[\s]*(.*)$/o )
202    {
203      my $lh = join ('',@conditions) . "\$($1)" ;
204      my @rh = &split_vars("$3");
205      my $seen = variable_seen( "$lh" ) ;
206
207      if ( $#conditions > 0 )
208      {
209        define_variable( "\$($1)", () );
210      }
211
212      define_variable( "$lh", @rh );
213
214      if ( not $seen )
215      {
216        push @ibuf, "§$2var_$lh\n" ;
217      }
218    }
219    elsif ( /^[\s]*include[\s\t]*\$\(top_srcdir\)[\.\/]*automake\/(.*)\.am$/o )
220    {
221      if ( "$1" eq "lib" )
222      {
223        push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/$1.am\n" ;
224      }
225      elsif ( "$1" eq "local" )
226      {
227        $main::seen_local = 1 ;
228      }
229      elsif ( "$1" eq "host" )
230      {
231        $main::seen_host = 1 ;
232      }
233    }
234    elsif ( /^[\s]*include[\s\t]*(.*)$/o )
235    {
236      push @ibuf, "include $1\n" ;
237    }
238    elsif ( /^\t(.*)$/o )
239    {
240      push @ibuf, "\t$1\n" ;
241    }
242    elsif ( /^(.*)\:(.*)$/o )
243    {
244      push @ibuf, "$1:$2\n" ;
245    }
246    elsif ( /^[\s]*$/o )
247    {
248      push @ibuf, "\n" ;
249    }
250    else
251    {
252      die "ERROR: Don't know how to handle <$_>" ;
253    }
254  } # for
255  @buffer = @ibuf ;
256} # while
257
258die "Conditional stack corrupted" if ( $#conditions != 0 );
259
260foreach( @vars )
261{
262  purge( \@{$var_{"$_"}} );
263}
264
265# print STDERR "<tmp>\n", @buffer, "</tmp>\n" ;
266
267
268{
269  my @ibuf = () ;
270  foreach( @buffer )
271  {
272    if ( /^#.*$/o )
273    {
274      push @ibuf, "$_" ;
275    }
276    elsif( /^§header$/o )
277    {
278      my $l = $var_{"\$(AUTOMAKE_OPTIONS)"} ;
279      push @ibuf, "\nAUTOMAKE_OPTIONS = @{$l}\n" ;
280      if ( "$rtems_cfg" eq "." )
281      {
282        push @ibuf, "ACLOCAL_AMFLAGS = -I \$(RTEMS_TOPdir)/aclocal\n" ;
283      }
284      if ( defined( @{$var_{"\$(VPATH)"}} ) )
285      {
286        if ( $#{$var_{"\$(VPATH)"}} > 0 )
287        {
288          my $l = join (':',@{$var_{"\$(VPATH)"}}) ;
289          push @ibuf, "\nVPATH = $l\n" ;
290        }
291      }
292      push @ibuf, "\n" ;
293    }
294    elsif ( /^§(\+|)var_(.*)\$\((.*)\)$/o )
295    {
296      print_var(\@ibuf, "$3 $1=", $var_{"$2\$($3)"}) ;
297    }
298    elsif ( /^\t.*$/o )
299    {
300      &print_script(\@ibuf, "$_");
301    }
302    elsif ( /^[\s]*if[\s]+([a-zA-Z0-9_]+)[\s\t]*$/o )
303    {
304      push @conditions, "\@$1_TRUE\@" ;
305      push @ibuf, "if $1\n" ;
306    }
307    elsif ( /^[\s]*else[\s]*$/o )
308    {
309      @conditions[$#conditions] =~ s/_TRUE\@$/_FALSE\@/;
310      push @ibuf, "else\n" ;
311    }
312    elsif ( /^[\s]*endif[\s]*$/o )
313    {
314      pop @conditions ;
315      push @ibuf, "endif\n" ;
316    }
317    else
318    {
319      print_line(\@ibuf,$_);
320    }
321  }
322
323  if ( variable_seen("\$(SUBDIRS)") )
324  {
325    push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/subdirs.am\n" ;
326  }
327
328  if ( defined( $main::seen_host ) )
329  {
330    push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/host.am\n" ;
331  }
332  else
333  {
334    push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/local.am\n" ;
335  }
336
337  @buffer = @ibuf ;
338}
339
340#print STDERR "<tmp>\n", @buffer, "</tmp>\n" ;
341
342{ ## pretty print
343  my $out = join ('',@buffer) ;
344  $out =~ s/\s\#\n(\#\n)+/\n/g ;
345  $out =~ s/\n\n\#\n\n/\n/g ;
346  $out =~ s/\n\n[\n]*/\n\n/g ;
347  print $out ;
348}
349
350exit 0;
[9b8baa1]351
352# find a relative up-path to a file $file, starting at directory $pre
[cadbf44a]353sub find_file($$)
[9b8baa1]354{
355  my $pre = $_[0] ;
356  my $file= $_[1] ;
357
358  my $top = "." ;
359  if (not "$pre") { $pre = "." ; }
360
[df49c60]361  for ( my $str = "$pre" . "/" . "$top" ;
[9b8baa1]362    ( -d "$str" ) ;
363    $str = "$pre" . "/" . "$top" )
364  {
365    if ( -f "${str}/${file}" ) 
366    {
367      return $top ;
368    }
369    if ( "$top" eq "." )
370    {
371      $top = ".." ;
372    }
373    else
374    {
375      $top .= "/.." ;
376    }
377  } ;
378  die "Can't find file ${file}\n" ;
379}
380
[df49c60]381sub variable_seen($)
382{
383  my $label = "$_[0]" ;
384  my $res = defined $var_{"$label"};
385#print STDERR "SEEN: $label ->$res<\n" ;
386  return $res ;
[9b8baa1]387}
388
[df49c60]389sub define_variable($$)
[9b8baa1]390{
[df49c60]391  my ($label,@value) = @_ ;
[9b8baa1]392
[df49c60]393  if ( not variable_seen("$label") )
[9b8baa1]394  {
[df49c60]395#print STDERR "DEFINING: $label\n" ;
396    push @vars, "$label" ;
[9b8baa1]397  }
398
[df49c60]399  foreach my $i ( @{value} )
400  {
401    push @{$var_{"$label"}}, $i ;
[9b8baa1]402  }
[df49c60]403}
404
405# Strip off duplicate entries from a list
406sub purge($)
407{
408  my $list = $_[0] ; # Reference to list !
409  my (@tmp) = () ;
410
411  foreach my $l ( @{$list} )
[9b8baa1]412  {
[df49c60]413    my $i = 1 ;
414    foreach my $t (@tmp)
[9f4868c]415    {
[df49c60]416      if ( $t eq $l )
417      {
418        $i = 0 ;
419        last ;
420      }
[9f4868c]421    }
[df49c60]422    push @tmp,$l if ($i) ;
[9f4868c]423  }
[df49c60]424
425  @{$list} = @tmp ;
426}
427
428#
429# Break the right hand side of a variable assignment into separate chunks
430#
431sub split_vars($)
432{
433  my $line = $_[0] ;
434  my (@buf) = split(//,"$line") ;
435
436  my $begin = 0 ;
437  my @res = () ;
438
439  my $depth = 0 ;
440  my $state = 0 ;
441
442  my $len = $#buf + 1 ;
443  for ( my $i = 0 ; $i < $len ; $i++ )
[9b8baa1]444  {
[df49c60]445    my $c = @buf[$i] ;
446    if ( $state == 0 )
447    {
448      if ( "$c" ne " " )
449      { # token
450        $begin = $i ;
451        $state++ ;
452      }
453      if ( "$c" eq "\$" )
454      { # variable
455        $depth++ ;
456      }
457    }
458    elsif ( $state == 1 )
459    {
460      if ( ( "$c" eq "\)" ) or ( "$c" eq "\}" ) )
461      { # variable
462        $depth-- ;
463      }
464      elsif ( ("$c" eq " " ) and ( $depth == 0 ) )
465      {
466        push @res, substr($line,$begin,$i-$begin);
467        $state-- ;
468      }
469      elsif ( "$c" eq "\$" )
470      { # variable
471        $depth++ ;
472      }
473    }
474    else
475    {
476      die "split_vars: unknown mode\n" ;
477    }
[9b8baa1]478  }
[df49c60]479
480  if ( $state > 0 )
[9b8baa1]481  {
[df49c60]482    push @res, substr($line,$begin,$len-$begin);
483    $state = 0
[9b8baa1]484  }
[df49c60]485  return @res ;
486}
487
488sub print_var($$$)
489{
490  my ($ibuf,$line,$l) = @_ ; # $l .. reference to list
491
492  foreach (@{$l}) {
493    if ( ( length($line) + length($_) ) < 76 )
494    {
495          $line .= " $_";
496    }
497    else
498    {
499           push @{$ibuf}, "$line \\\n";
500           $line = "    $_" ;
501    }
[9b8baa1]502  }
[df49c60]503  push @{$ibuf}, "$line\n" ;
504}
[9b8baa1]505
[df49c60]506sub print_line($$)
[9b8baa1]507{
[df49c60]508  my ($ibuf,$input) = @_ ;
509  my @l = split( / /, $input );
510  my $line = shift @l ;
511
512  foreach my $i (@l) {
513    if ( ( length($line) + length($i) ) < 76 )
514    {
515          $line .= " $i";
516    }
517    else
518    {
519           push @{$ibuf}, "$line \\\n";
520           $line = "    $i" ;
521    }
522  }
523  push @{$ibuf}, "$line" ;
[9b8baa1]524}
525
[df49c60]526sub print_script($$)
527{
528  my ($ibuf,$input) = @_ ;
529  $input =~ s%§%\\\n%g ;
530  push @{$ibuf}, $input ;
531}
Note: See TracBrowser for help on using the repository browser.