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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [tools/] [update/] [ampolish] - Blame information for rev 831

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

Line No. Rev Author Line
1 30 unneback
#!/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
#    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 "\n", @buffer, "\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
 
258
die "Conditional stack corrupted" if ( $#conditions != 0 );
259
 
260
foreach( @vars )
261
{
262
  purge( \@{$var_{"$_"}} );
263
}
264
 
265
# print STDERR "\n", @buffer, "\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 "\n", @buffer, "\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
 
350
exit 0;
351
 
352
# find a relative up-path to a file $file, starting at directory $pre
353
sub find_file
354
{
355
  my $pre = $_[0] ;
356
  my $file= $_[1] ;
357
 
358
  my $top = "." ;
359
  if (not "$pre") { $pre = "." ; }
360
 
361
  for ( my $str = "$pre" . "/" . "$top" ;
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
 
381
sub variable_seen($)
382
{
383
  my $label = "$_[0]" ;
384
  my $res = defined $var_{"$label"};
385
#print STDERR "SEEN: $label ->$res<\n" ;
386
  return $res ;
387
}
388
 
389
sub define_variable($$)
390
{
391
  my ($label,@value) = @_ ;
392
 
393
  if ( not variable_seen("$label") )
394
  {
395
#print STDERR "DEFINING: $label\n" ;
396
    push @vars, "$label" ;
397
  }
398
 
399
  foreach my $i ( @{value} )
400
  {
401
    push @{$var_{"$label"}}, $i ;
402
  }
403
}
404
 
405
# Strip off duplicate entries from a list
406
sub purge($)
407
{
408
  my $list = $_[0] ; # Reference to list !
409
  my (@tmp) = () ;
410
 
411
  foreach my $l ( @{$list} )
412
  {
413
    my $i = 1 ;
414
    foreach my $t (@tmp)
415
    {
416
      if ( $t eq $l )
417
      {
418
        $i = 0 ;
419
        last ;
420
      }
421
    }
422
    push @tmp,$l if ($i) ;
423
  }
424
 
425
  @{$list} = @tmp ;
426
}
427
 
428
#
429
# Break the right hand side of a variable assignment into separate chunks
430
#
431
sub 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++ )
444
  {
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
    }
478
  }
479
 
480
  if ( $state > 0 )
481
  {
482
    push @res, substr($line,$begin,$len-$begin);
483
    $state = 0
484
  }
485
  return @res ;
486
}
487
 
488
sub 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
    }
502
  }
503
  push @{$ibuf}, "$line\n" ;
504
}
505
 
506
sub print_line($$)
507
{
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" ;
524
}
525
 
526
sub print_script($$)
527
{
528
  my ($ibuf,$input) = @_ ;
529
  $input =~ s%§%\\\n%g ;
530
  push @{$ibuf}, $input ;
531
}

powered by: WebSVN 2.1.0

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