OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [tools/] [update/] [ampolish] - Blame information for rev 1778

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1026 ivang
#!/usr/bin/perl
2
 
3
package main ;
4
 
5
use strict ;
6
 
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
# /ampolish Makefile.am~
14
# mv Makefile.am~ Makefile.am
15
#
16
 
17
my @vars ;
18
my @conditions = ( "" ) ;
19
my @buffer = ();
20
my %var_ ;
21
 
22
define_variable( "\$(AUTOMAKE_OPTIONS)", ( "foreign",  "1.4" ) );
23
define_variable( "\$(VPATH)", ( "\@srcdir\@" ) );
24
 
25
# find relative up-path to configure.in
26
my $rtems_cfg = find_file(".","configure.in");
27
 
28
# find relative up-path from configure.in to VERSION
29
my $rtems_top = find_file("$rtems_cfg","VERSION");
30
 
31
if ( "$rtems_top" eq "." ) { $rtems_top = "" ; }
32
else { $rtems_top .= "/" ; }
33
 
34
{
35
# PASS1:
36
# read input file and concatenate multiple lines into single lines.
37
 
38
  my @ibuf = () ;
39
 
40
  while(  )
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(  )
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(  )
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 (  ) ;
94
  @buffer = @ibuf ;
95
}
96
 
97
{
98
# PASS2:
99
# fix obsolete constructs
100
  my @ibuf = () ;
101
 
102
  foreach ( @buffer )
103
  {
104
    if ( /^(TMP|PRE)INSTALL_FILES[\s]*=(.*)$/o )
105
    { # force "+="
106
      push @ibuf, "$1INSTALL_FILES +=$2\n" ;
107
    }
108
    elsif ( /^(VPATH|EXTRA_DIST)[\s]*\+=(.*)$/o )
109
    { # force "="
110
      push @ibuf, "$1 = $2\n" ;
111
    }
112
    elsif ( /^[\s]*ACLOCAL[\s]*=[\s]*\@ACLOCAL\@.*$/o )
113
    { # remove the line
114
    }
115
    elsif ( /^[\s]*(ACLOCAL_AMFLAGS)[\s\t]*[\+]*=[\s]*(.*)[\s]*$/o )
116
    { # remove the line
117
    }
118
    elsif ( /^[\s]*(AM_CFLAGS)[\s\t]*[\+]*=[\s]*\$\(CFLAGS_OS_V\)[\s]*$/o )
119
    { # remove the line
120
    }
121
    elsif ( /^[\s]*debug-am:.*$/o )
122
    { # remove the line
123
    }
124
    elsif ( /^[\s]*all(\-am):(.*)$/o )
125
    { # replace the line
126
      push @ibuf, "all-local:$2\n" ;
127
    }
128
    elsif ( /^[\s]*profile-am:.*$/o )
129
    { # remove the line
130
    }
131
    elsif ( /^[\s]*include[\s\t]*\$\(RTEMS_ROOT\)\/make\/lib.cfg[\s]*$/o )
132
    {
133
      push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/lib.am\n" ;
134
    }
135
    elsif ( /^[\s]*include[\s\t]*.*compile.am[\s]*$/o )
136
    {
137
      # remove the line
138
    }
139
    elsif ( /^(.*[^\s])[\s]*$/o )
140
    { # remove trailing spaces
141
      push @ibuf, "$1\n" ;
142
    }
143
    else
144
    {
145
      push @ibuf, "$_" ;
146
    }
147
  }
148
  @buffer = @ibuf ;
149
}
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/compile.am\n" ;
224
        push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/$1.am\n" ;
225
      }
226
      elsif ( "$1" eq "local" )
227
      {
228
        $main::seen_local = 1 ;
229
      }
230
      elsif ( "$1" eq "host" )
231
      {
232
        $main::seen_host = 1 ;
233
      }
234
    }
235
    elsif ( /^[\s]*include[\s\t]*\$\(RTEMS_ROOT\)\/make\/(.*)\.cfg$/o )
236
    {
237
      if ( "$1" eq "leaf" )
238
      {
239
        push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/compile.am\n" ;
240
        push @ibuf, "include \$(RTEMS_ROOT)/make/$1.cfg\n" ;
241
      }
242
      else
243
      {
244
        push @ibuf, "include \$(RTEMS_ROOT)/make/$1.cfg\n" ;
245
      }
246
    }
247
    elsif ( /^[\s]*include[\s\t]*(.*)$/o )
248
    {
249
      push @ibuf, "include $1\n" ;
250
    }
251
    elsif ( /^\t(.*)$/o )
252
    {
253
      push @ibuf, "\t$1\n" ;
254
    }
255
    elsif ( /^(.*)\:(.*)$/o )
256
    {
257
      push @ibuf, "$1:$2\n" ;
258
    }
259
    elsif ( /^[\s]*$/o )
260
    {
261
      push @ibuf, "\n" ;
262
    }
263
    else
264
    {
265
      die "ERROR: Don't know how to handle <$_>" ;
266
    }
267
  } # for
268
  @buffer = @ibuf ;
269
} # while
270
 
271
die "Conditional stack corrupted" if ( $#conditions != 0 );
272
 
273
foreach( @vars )
274
{
275
  purge( \@{$var_{"$_"}} );
276
}
277
 
278
# print STDERR "\n", @buffer, "\n" ;
279
 
280
 
281
{
282
  my @ibuf = () ;
283
  foreach( @buffer )
284
  {
285
    if ( /^#.*$/o )
286
    {
287
      push @ibuf, "$_" ;
288
    }
289
    elsif( /^§header$/o )
290
    {
291
      my $l = $var_{"\$(AUTOMAKE_OPTIONS)"} ;
292
      push @ibuf, "\nAUTOMAKE_OPTIONS = @{$l}\n" ;
293
      if ( "$rtems_cfg" eq "." )
294
      {
295
        push @ibuf, "ACLOCAL_AMFLAGS = -I ${rtems_top}aclocal\n" ;
296
      }
297
      if ( defined( @{$var_{"\$(VPATH)"}} ) )
298
      {
299
        if ( $#{$var_{"\$(VPATH)"}} > 0 )
300
        {
301
          my $l = join (':',@{$var_{"\$(VPATH)"}}) ;
302
          push @ibuf, "\nVPATH = $l\n" ;
303
        }
304
      }
305
      push @ibuf, "\n" ;
306
    }
307
    elsif ( /^§(\+|)var_(.*)\$\((.*)\)$/o )
308
    {
309
      print_var(\@ibuf, "$3 $1=", $var_{"$2\$($3)"}) ;
310
    }
311
    elsif ( /^\t.*$/o )
312
    {
313
      &print_script(\@ibuf, "$_");
314
    }
315
    elsif ( /^[\s]*if[\s]+([a-zA-Z0-9_]+)[\s\t]*$/o )
316
    {
317
      push @conditions, "\@$1_TRUE\@" ;
318
      push @ibuf, "if $1\n" ;
319
    }
320
    elsif ( /^[\s]*else[\s]*$/o )
321
    {
322
      @conditions[$#conditions] =~ s/_TRUE\@$/_FALSE\@/;
323
      push @ibuf, "else\n" ;
324
    }
325
    elsif ( /^[\s]*endif[\s]*$/o )
326
    {
327
      pop @conditions ;
328
      push @ibuf, "endif\n" ;
329
    }
330
    else
331
    {
332
      print_line(\@ibuf,$_);
333
    }
334
  }
335
 
336
  if ( variable_seen("\$(SUBDIRS)") )
337
  {
338
    push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/subdirs.am\n" ;
339
  }
340
 
341
  if ( defined( $main::seen_host ) )
342
  {
343
    push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/host.am\n" ;
344
  }
345
  else
346
  {
347
    push @ibuf, "include \$(top_srcdir)/${rtems_top}automake/local.am\n" ;
348
  }
349
 
350
  @buffer = @ibuf ;
351
}
352
 
353
#print STDERR "\n", @buffer, "\n" ;
354
 
355
{ ## pretty print
356
  my $out = join ('',@buffer) ;
357
  $out =~ s/\s\#\n(\#\n)+/\n/g ;
358
  $out =~ s/\n\n\#\n\n/\n/g ;
359
  $out =~ s/\n\n[\n]*/\n\n/g ;
360
  print $out ;
361
}
362
 
363
exit 0;
364
 
365
# find a relative up-path to a file $file, starting at directory $pre
366
sub find_file($$)
367
{
368
  my $pre = $_[0] ;
369
  my $file= $_[1] ;
370
 
371
  my $top = "." ;
372
  if (not "$pre") { $pre = "." ; }
373
 
374
  for ( my $str = "$pre" . "/" . "$top" ;
375
    ( -d "$str" ) ;
376
    $str = "$pre" . "/" . "$top" )
377
  {
378
    if ( -f "${str}/${file}" )
379
    {
380
      return $top ;
381
    }
382
    if ( "$top" eq "." )
383
    {
384
      $top = ".." ;
385
    }
386
    else
387
    {
388
      $top .= "/.." ;
389
    }
390
  } ;
391
  die "Can't find file ${file}\n" ;
392
}
393
 
394
sub variable_seen($)
395
{
396
  my $label = "$_[0]" ;
397
  my $res = defined $var_{"$label"};
398
#print STDERR "SEEN: $label ->$res<\n" ;
399
  return $res ;
400
}
401
 
402
sub define_variable($$)
403
{
404
  my ($label,@value) = @_ ;
405
 
406
  if ( not variable_seen("$label") )
407
  {
408
#print STDERR "DEFINING: $label\n" ;
409
    push @vars, "$label" ;
410
  }
411
 
412
  foreach my $i ( @{value} )
413
  {
414
    push @{$var_{"$label"}}, $i ;
415
  }
416
}
417
 
418
# Strip off duplicate entries from a list
419
sub purge($)
420
{
421
  my $list = $_[0] ; # Reference to list !
422
  my (@tmp) = () ;
423
 
424
  foreach my $l ( @{$list} )
425
  {
426
    my $i = 1 ;
427
    foreach my $t (@tmp)
428
    {
429
      if ( $t eq $l )
430
      {
431
        $i = 0 ;
432
        last ;
433
      }
434
    }
435
    push @tmp,$l if ($i) ;
436
  }
437
 
438
  @{$list} = @tmp ;
439
}
440
 
441
#
442
# Break the right hand side of a variable assignment into separate chunks
443
#
444
sub split_vars($)
445
{
446
  my $line = $_[0] ;
447
  my (@buf) = split(//,"$line") ;
448
 
449
  my $begin = 0 ;
450
  my @res = () ;
451
 
452
  my $depth = 0 ;
453
  my $state = 0 ;
454
 
455
  my $len = $#buf + 1 ;
456
  for ( my $i = 0 ; $i < $len ; $i++ )
457
  {
458
    my $c = @buf[$i] ;
459
    if ( $state == 0 )
460
    {
461
      if ( "$c" ne " " )
462
      { # token
463
        $begin = $i ;
464
        $state++ ;
465
      }
466
      if ( "$c" eq "\$" )
467
      { # variable
468
        $depth++ ;
469
      }
470
    }
471
    elsif ( $state == 1 )
472
    {
473
      if ( ( "$c" eq "\)" ) or ( "$c" eq "\}" ) )
474
      { # variable
475
        $depth-- ;
476
      }
477
      elsif ( ("$c" eq " " ) and ( $depth == 0 ) )
478
      {
479
        push @res, substr($line,$begin,$i-$begin);
480
        $state-- ;
481
      }
482
      elsif ( "$c" eq "\$" )
483
      { # variable
484
        $depth++ ;
485
      }
486
    }
487
    else
488
    {
489
      die "split_vars: unknown mode\n" ;
490
    }
491
  }
492
 
493
  if ( $state > 0 )
494
  {
495
    push @res, substr($line,$begin,$len-$begin);
496
    $state = 0
497
  }
498
  return @res ;
499
}
500
 
501
sub print_var($$$)
502
{
503
  my ($ibuf,$line,$l) = @_ ; # $l .. reference to list
504
 
505
  foreach (@{$l}) {
506
    if ( ( length($line) + length($_) ) < 76 )
507
    {
508
          $line .= " $_";
509
    }
510
    else
511
    {
512
           push @{$ibuf}, "$line \\\n";
513
           $line = "    $_" ;
514
    }
515
  }
516
  push @{$ibuf}, "$line\n" ;
517
}
518
 
519
sub print_line($$)
520
{
521
  my ($ibuf,$input) = @_ ;
522
  my @l = split( / /, $input );
523
  my $line = shift @l ;
524
 
525
  foreach my $i (@l) {
526
    if ( ( length($line) + length($i) ) < 76 )
527
    {
528
          $line .= " $i";
529
    }
530
    else
531
    {
532
           push @{$ibuf}, "$line \\\n";
533
           $line = "    $i" ;
534
    }
535
  }
536
  push @{$ibuf}, "$line" ;
537
}
538
 
539
sub print_script($$)
540
{
541
  my ($ibuf,$input) = @_ ;
542
  $input =~ s%§%\\\n%g ;
543
  push @{$ibuf}, $input ;
544
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.