source: rtems/tools/update/acpolish @ 3a8915e

4.104.114.84.95
Last change on this file since 3a8915e was 3a8915e, checked in by Joel Sherrill <joel.sherrill@…>, on 08/06/99 at 17:55:25

Patch rtems-rc-19990709-6-diff from Ralf Corsepius <corsepiu@…>
applied. This modified many Makefiles and custom files and makes many more
settings (network, multiprocessing, etc) gnerated by autoconf.

  • Property mode set to 100755
File size: 14.8 KB
Line 
1#!/usr/bin/perl
2
3# use strict 'subs' ;
4# use strict 'vars' ;
5# use strict 'refs' ;
6
7# use strict ;
8
9#
10# Perl script to beautify and enhance RTEMS autoconf Makefile.ins
11#
12# Reads from stdin and writes to stdout
13#
14# usage:
15# acpolish <Makefile.in >Makefile.in~
16# mv Makefile.in~ Makefile.in
17#
18# Note: This tool is not indented to be exported from the source-tree
19#
20
21if ( -f "Makefile.am" )
22{
23  # Refuse to work on Makefile.ins generated from automake;
24  # redirecting STDOUT to Makefile.in will trash the Makefile.in ;-
25
26  die "acpolish must not be run in automake directories" ;
27}
28
29my $experimental = 0 ; # enable experimental/unsafe features
30my $verbose = 0 ;
31my $build_pgms_seen = "" ;
32my $make_exe_seen = 0 ;
33my $install_seen = 0 ;
34my $top_builddir = "";
35my $subdir = "";
36my @installdirs = () ;
37
38my @pieces = () ;
39my @files = () ;
40my @variants = () ;
41my @vars = () ;
42
43sub print_macro($$)
44{
45  my $line = $_[0] ;
46  my $input  = $_[1] ;
47  $input =~ s/\\\\/ /g;
48  my @l = split(' ',$input);
49
50  foreach (@l) {
51    if ( ( length($line) + length($_) ) < 76 )
52    {
53          $line .= " $_";
54    }
55    else
56    {
57           print "$line \\\n";
58           $line = "    $_" ;
59    }
60  }
61  print "$line\n" ;
62}
63
64# Strip off duplicate entries from a list
65sub purge($)
66{
67  my $list = $_[0] ; # Reference to list !
68  my (@tmp) = () ;
69
70  foreach my $l ( @{$list} )
71  {
72    my $i = 1 ;
73    foreach my $t (@tmp)
74    {
75      if ( $t eq $l )
76      {
77        $i = 0 ;
78        last ;
79      }
80    }
81    push @tmp,$l if ($i) ;
82  }
83
84  @{$list} = @tmp ;
85}
86
87sub find_root
88{
89  $top_builddir = "." ;
90  $subdir="";
91  my $pwd = `pwd`; chomp $pwd;
92  $pwd .= "/" ;
93  my $len ;
94
95  if ( -f "configure.in" )  { return $top_builddir ; }
96  my $i = rindex($pwd,'/');
97
98  $len = $i;
99  $pwd = substr($pwd,0,$len);
100  $i = rindex($pwd,'/');
101  $subdir = substr($pwd,$i+1,$len - 1);
102  $top_builddir = ".." ; 
103
104  while( -d "$top_builddir" )
105  {
106    if ( -f "${top_builddir}/configure.in" ) 
107    {
108      return $top_builddir ;
109    }
110    $len=$i;
111    $pwd = substr($pwd,0,$len);
112    $i = rindex($pwd,'/');
113    $subdir = substr($pwd,$i+1,$len - 1) . "/$subdir";
114    $top_builddir .= "/.." ; 
115  } ;
116  die "Can't find configure.in\n" ;
117}
118
119find_root();
120
121my @buffer = () ;
122
123sub subst_line
124{
125# substitute obsolete variables
126  if ( /^([^\s]*)[\s]+$/o )
127  { # strip trailing spaces
128    $_ = "$1\n";
129  }
130  if ( /^(.*)MKLIB(.*)$/o )
131  {
132    s/MKLIB/RANLIB/g ;
133  }
134  if ( /^(.*)\$\(INSTINCFLAGS\)(.*)$/o )
135  {
136    s/\$\(INSTINCFLAGS\)/-m 644/g ;
137  }
138  if ( /^(.*)ASM(_FILES|_PIECES|_O_FILES)(.*)$/o )
139  {
140    s/ASM_FILES/S_FILES/g ;
141    s/ASM_PIECES/S_PIECES/g ;
142    s/ASM_O_FILES/S_O_FILES/g ;
143    print STDERR "ASM: $_"  if ( $verbose > 0) ;
144  }
145  if ( /^(.*)MP_PIECES(.*)$/o )
146  { # HACK: this is not always correct
147    s/MP_PIECES/MP_C_PIECES/g ;
148    print STDERR "MP_PIECES: $_"  if ( $verbose > 0 );
149  }
150  if ( /^(.*)\$\(RTEMS_BSP\)(.*)$/o )
151  {
152    s/\$\(RTEMS_BSP\)/\@RTEMS_BSP\@/g ;
153  }
154  if ( /^(.*)\$\(RTEMS_ROOT\)\/mkinstalldirs(.*)$/o )
155  {
156    $_ = "$1\$\(mkinstalldirs\)$2\n" ;
157  }
158  if ( /^(.*)\$\{(.*)_FILES\}(.*)$/o )
159  {
160    $_ = "$1\$\($2_FILES\)$3\n" ;
161  }
162  if ( /^(.*)\$\{(.*)_PIECES\}(.*)$/o )
163  {
164    $_ = "$1\$\($2_PIECES\)$3\n" ;
165  }
166  if ( /^(.*)\$\(PROJECT_ROOT\)\/\@RTEMS_BSP\@\/lib\/include(.*)$/o )
167  {
168    $_ = "$1\$\(PROJECT_INCLUDE\)$2\n" ;
169  }
170  if ( /^(.*)\$\{PROJECT_RELEASE\}(.*)$/o )
171  {
172    $_ = "$1\$\(PROJECT_RELEASE\)$2\n" ;
173  }
174  if ( /^(.*\$\(INSTALL_[A-Z]+\))[\s]+-m[\s]+([54])([0-9][0-9].*)$/o )
175  {
176    my $v = $2 + 2 ;
177    $_ = "$1 -m $v$3\n" ;
178  }
179  if ( /^H_FILES[\s]*=[\s]*\$\(wildcard[\s]+\$\(srcdir\)\/\*\.h\)[\s]*$/o )
180  {
181     my $files =`ls *.h 2>/dev/null`;
182     print STDERR "WARNING: Replacing \"\(wildcard... in $_\n" ;
183     $line = "H_FILES =";
184     @l = split(' ',$files) ;
185     foreach(@l) { $line .= " \$\(srcdir\)/$_"; }
186     $line .= "\n" ;
187     $_ = "$line" ;
188  }
189  if ( /^(.*)RTEMS_HAS_([A-Z]+)(.*)$/o )
190  {
191     print STDERR "WARNING: Replacing RTEMS_HAS_$2 with HAS_$2\n" ;
192     $_ = "$1HAS_$2$3\n" ;
193  }
194  if ( /^.*\$\(make\-exe\).*$/o )
195  {
196    $make_exe_seen = 1 ;
197  }
198  if ( /^.*\$\(INSTALL_(DATA|SCRIPT|PROGRAM)\)(.*)$/o )
199  {
200    $install_seen = 1 ;
201  }
202}
203
204{
205# 1st PASS:
206# * Read input file
207# * concatenate multiple lines
208# * Remove obsolete variables
209
210  my @ibuf = () ;
211  my $line = "" ;
212
213  while ( <> )
214  {
215    &subst_line ;
216    if ( /^(#.*)$/o )
217    { # preserve comments
218      $line = "$_" ;
219      push @ibuf, $line ;
220      $line = "" ;
221    }
222    elsif ( /^.*\\$/o )
223    { # multilines
224      chop ;
225      $line .= "$_\\" ;
226    }
227    else
228    {
229      $line .= "$_" ;
230      push @ibuf, $line ;
231      $line = "" ;
232    }
233  }
234  @buffer = @ibuf;
235}
236
237{
238# 2nd PASS:
239# * remove automatically generated lines
240# * process some selected make rules
241  my $line = "" ;
242  my @tbuf = () ;
243  foreach (@buffer)
244  {
245    if ( /^[\t](.*)$/o )
246    { # make rule production
247      my $line = $1 ;
248      tr/\\/ / ;
249      my @l = split(/;/,$_);     
250      foreach (@l)
251      { # try to get installation directories
252        if ( /^.*\$\(mkinstalldirs\).*\s\$\(INSTALLDIRS\)$/o )
253        {
254        }
255        elsif ( /^.*\$\(mkinstalldirs\).*\s([^\s]+)$/o )
256        {
257          push @installdirs, "$1" ;
258        }
259        elsif ( /^.*\$\(INSTALL_CHANGE\).*\s([^\s]+)(\/[^\.\s]+\.[^\s\/]+)$/o )
260        {
261#            print STDERR "WARNING - DIR1: $1 <$2> " ;
262            push @installdirs, "$1" ;
263        }
264        elsif ( /^.*\$\(INSTALL_CHANGE\).*\s([^\s]+)$/o )
265        {
266#          print STDERR "DIR2 $1\n" ;
267          push @installdirs, "$1" ;
268        }
269      }
270      push @tbuf, "§3$line"
271    }
272    elsif ( /^[\s]*\#.*$/o )
273    { # comment
274      push @tbuf, "$_" ;
275    }
276    elsif ( /^[\s]*([A-Z_]*)_FILES[\s]*\=[\s]*(.*)$/o )
277    { # *_FILES = ... Macros
278      if ( /^[\s]*([A-Z_]*_|)(CC|C|EQ|H|I|O|S|X)_FILES[\s]*\=[\s]*(.*)$/o )
279      {
280#        print STDERR "FILES: <$1>--<$2>--<$3>\n" ;
281        my $f = "$1$2_FILES" ;
282        ${"var_$f"}="$3" ;
283        if ( ( $experimental > 0 )
284          and ( ( "$2" eq "C" ) or ( "$2" eq "CC" ) or ( "$2" eq "S" )
285          or ( "$2" eq "I" ) or ( "$2" eq "H" ) ) )
286        {
287          my $p = "$1$2_PIECES" ;
288          if ( not defined ${"var_$p"} )
289          {
290            print STDERR "ADDING $p\n" ;
291            ${"var_$p"} = "" ;
292            push @tbuf, "§4$p\n" ;
293            push @pieces, "$p" ;
294          }
295        }
296        # place a marker
297        push @tbuf, "§4$f\n" ;
298        push @files, "$f" ;
299      }
300      else
301      { # found a bug
302        print STDERR "UNKNOWN _FILES: $_\n" ;
303        my $f = "$1_FILES" ;
304        ${"var_$f"}="$2" ;
305        # place a marker
306        push @tbuf, "§4$f\n" ;
307        push @files, "$f" ;
308      }
309    }
310    elsif ( /^[\s]*([A-Z_]*)_PIECES[\s]*\=[\s]*(.*)$/o )
311    { # *_PIECES = ... Macros
312      if ( /^[\s]*([A-Z][A-Z0-9_]*_|)(CC|C|EQ|H|I|O|S|X|REL)_PIECES[\s]*\=[\s]*(.*)$/o )
313      {
314        my $p = "$1$2_PIECES" ;
315
316        if ( not defined ${"var_$p"} )
317        {
318          ${"var_$p"} = "$3" ;
319          push @tbuf, "§4$p\n" ;
320          push @pieces, "$p" ;
321        }
322        else
323        {
324          ${"var_$p"} .= " $3" ;
325        }
326      }
327      elsif ( /^[\s]*(BSP|CPU|GENERIC)_PIECES[\s]*\=[\s]*(.*)$/o )
328      { # Explicit exceptions from the *_PIECES naming conventions
329        # They should better be replaced in future
330        my $p = "$1_PIECES" ;
331        ${"var_$p"}="$2" ;
332        # place a marker
333        push @tbuf, "§4$p\n" ;
334        push @pieces, "$p" ;
335      }
336      else
337      { # found a bug
338        print STDERR "UNKNOWN _PIECES: $_\n" ;
339        my $p = "$1_PIECES" ;
340        ${"var_$p"}="$2" ;
341        # place a marker
342        push @tbuf, "§4$p\n" ;
343        push @pieces, "$p" ;
344      }
345    }
346    elsif ( /^[\s]*([A-Z_]+)_PIECES_([^\s]+)_V[\s]*\=[\s]*(.*)$/o )
347    { # *_PIECES_.._V = ... Macros
348      if ( /^[\s]*([A-Z][A-Z0-9_]*_|)(CC|C|EQ|H|I|O|S|X|REL)_PIECES_([^\s]+)_V[\s]*\=[\s]*(.*)$/o )
349      {
350        my @l = split(/_/,$3);
351        my $v = "$1$2-$#l" ;
352        if ( not defined @{"variants_$v"} ) { push @variants, "$v" ; }
353
354        my $p = "$1$2_PIECES_$3_V" ;
355        push @{"variants_${v}"}, "$p" ;
356
357        ${"var_$p"}="$4" ;
358        # place a marker
359        push @tbuf, "§4$p\n" ;
360        push @pieces, "$p" ;
361      }
362      else
363      { # found a bug
364        print STDERR "UNKNOWN _PIECES: $_\n" ;
365        my $p = "$1_PIECES" ;
366        ${"var_$p"}="$2" ;
367        # place a marker
368        push @tbuf, "§4$p\n" ;
369        push @pieces, "$p" ;
370      }
371    }
372    elsif ( /^[\s]*([^\s+=]+)[\s]*\=[\s]*(.*)$/o )
373    { # makefile variable
374      if ( ( "$1" eq "subdir" )
375        or ( "$1" eq "top_srcdir" )
376        or ( "$1" eq "top_builddir" )
377        or ( "$1" eq "RTEMS_ROOT" )
378        or ( "$1" eq "PROJECT_ROOT" )
379        or ( "$1" eq "INSTALL" )
380        or ( "$1" eq "PACKHEX" )
381        or ( "$1" eq "INSTALL_CHANGE" )
382        or ( "$1" eq "mkinstalldirs" )
383      )
384      {
385        print STDERR "REMOVE: $1\n" if $verbose ;
386      }
387      elsif ( "$1" eq "srcdir" )
388      { # place marker
389        push @tbuf, "§0\n";
390      }
391      elsif ( "$1" eq "INSTALLDIRS" )
392      { # process the block
393        my $input = $2 ;
394        $input =~ s/\\\\/ /g ;
395        my @l = split(' ',$input);
396        foreach (@l)
397        {
398          if ( /[\s]*([^\s]+)[\s]*$/o )
399          {
400            push @installdirs, "$1" ;
401          }
402        }
403      }
404      else
405      {
406# print STDERR "MACRO: <$1> = <$2>\n";
407        my $p = "$1" ;
408        ${"var_$p"}="$2" ;
409        # place a marker
410        push @tbuf, "§4$p\n" ;
411        push @vars, "$p" ;
412      }
413    }
414    elsif ( /^[\s]*([^\s+=]+)[\s]*\+\=[\s]*(.*)$/o )
415    { # makefile variable addition
416# print STDERR "MACRO: <$1> += <$2>\n";
417        my $p = "$1" ;
418        if ( not defined ${"var_$p+"} )
419        {
420          # place a marker
421          push @tbuf, "§5$p\n" ;
422          push @vars, "$p+" ;
423        }
424        else
425        {
426          print STDERR "WARNING += $_" ;
427        }
428        ${"var_$p+"} .=" $2" ;
429    }
430    elsif ( /^[\s]*(\@[^\s]+\@)$/o )
431    { # autoconf variable
432      if ( "$1" eq "\@SET_MAKE\@" )
433      {
434      }
435      else
436      {
437        push @tbuf, "$1\n" ;
438      }
439    }
440    elsif ( /^[\s]*include[\s]+(.*)$/o )
441    { # include line
442      push @tbuf, "$_" ;
443      if ( /^include[\s\t]*.*(directory|leaf|lib)\.cfg.*$/o )
444      {
445        push @tbuf, "§1\n" ;
446        push @tbuf, "PACKHEX = \@PACKHEX\@\n" if ( $make_exe_seen == 1 ) ;
447        push @tbuf, "§2\n" ;
448      }
449    }
450    elsif ( /^[\s]*(ifeq|ifneq|else|endif)[\s]+(.*)$/o )
451    { # gmake conditionals
452      # Most of them are removed, but we still have some :-
453      push @tbuf, "$1 $2\n" ;
454    }
455    elsif ( /^\@.*_(TRUE|FALSE)\@.*$/o )
456    { # automake conditionals
457      # HACK: Don't know how to handle them, so let's pass them through
458      push @tbuf, "$_" ;
459    }
460    elsif ( /^[\s]*([^:]+)[\s]*(:[:]*)[\s]*(.*)$/o )
461    {
462      if ( "$2" eq "::" )
463      {
464        # Warn about "::"-rules
465        # Will be silently removed below.
466        print STDERR "WARNING: Removing \"::\" in RULE $_\n" ;
467      }
468
469      if ( ( "$1" eq "Makefile" )
470        or ( "$1" eq "\$\(INSTALLDIRS\)" ) )
471      { # delete entry
472        shift @buffer ;
473      }
474      elsif ( ( "$1" eq "all" )
475        or    ( "$1" eq "preinstall" ) )
476      {
477        # Note the dependencies
478        # Not yet exploited, but could be useful for dependency
479        # tracking in future
480        if ( defined ${"var_$1"} )
481          { ${"var_$1"} .= " $3" ; }
482        else
483          { ${"var_$1"} = "$3" ; }
484        push @tbuf, "$1: $3\n" ;
485      }
486      else
487      { # make rule
488        push @tbuf, "$1: $3\n" ;
489      }
490    }
491    elsif ( /^[\s]*$/o )
492    { # empty line
493      push @tbuf, "\n" ;
494    }
495    else
496    {
497      die "PASS 2: Unhandled $_" ;
498    }
499  }
500  @buffer = @tbuf ;
501  @tbuf = @installdirs ;
502  @installdirs = () ;
503  foreach ( @tbuf )
504  {
505    if ( /^([^\s]+)(\/[^\.\s]+\.[^\s\/]+)$/o )
506    {
507       print STDERR "WARNING - stripping of file: $1 <$2> " if ( $verbose > 1 );
508       push @installdirs, "$1" ;
509    }
510    else
511    {
512       push @installdirs, "$_" ;
513    }
514  }
515  purge \@installdirs ;
516  purge \@pieces ;
517}
518
519# A fragment to debug conditionals
520#foreach( @variants )
521#{
522#  my $v = $_ ;
523#  print STDERR "VARIANT: $v\n";
524#  foreach (@{"variants_${v}"})
525#  {
526#    print STDERR "* $_\n;" ;
527#  }
528#}
529
530# sanity check on *_FILES macros
531# too fragile for the time being,
532# therefore disabled by default
533if ( $experimental > 1 )
534{
535  foreach( @files )
536  {
537    my $file = "$_" ;
538    my $line = ${"var_$_"} ;
539    $line =~ tr /\\/ /;
540    my @l = split(' ',$line);
541    my @o = () ;
542    foreach (@l)
543    {
544      if ( /^([^\.]+)\.([a-z]+)$/o )
545      {
546        print STDERR "$file: *.$2 in $_\n" ;
547      }
548      elsif ( /^\$\(.*\)$/o )
549      {
550        print STDERR "$file: REF: $_\n" ;
551      }
552      else
553      {
554        print STDERR "$file: UNHANDLED: $_\n" ;
555      }
556    }
557  }
558}
559
560# print STDERR "PASS 2: @buffer" ;
561
562{
563# PASS 3:
564# * output to new Makefile
565# * prettyprint newlines
566
567  my $nl_seen = 0 ;
568  foreach ( @buffer )
569  {
570    if ( /^$/o )
571    {
572      $nl_seen++ ;
573      print "\n" if ( $nl_seen < 2 );
574    }
575    elsif ( /^\§0$/o )
576    {
577      print "\@SET_MAKE\@\n" ;
578      print "srcdir = \@srcdir\@\n" ;
579      print "top_srcdir = \@top_srcdir\@\n" ;
580      print "top_builddir = $top_builddir\n" ;
581      print "subdir = $subdir\n" if "$subdir" ;
582      print "\nRTEMS_ROOT = \@RTEMS_ROOT\@\n" ;
583      print "PROJECT_ROOT = \@PROJECT_ROOT\@\n\n" ;
584      $nl_seen = 1 ;
585    }
586    elsif ( /^\§1$/o )
587    {
588      print "\n" ;
589      print "INSTALL = \@INSTALL\@\n" if ( $install_seen > 0 );
590      print "INSTALL_CHANGE = \@INSTALL_CHANGE\@\n" ;
591      $nl_seen = 0 ;
592    }
593    elsif ( /^\§2$/o )
594    { # Handle installdirs related items
595      if ( $#installdirs >= 0 )
596      {
597        print "mkinstalldirs = \$(SHELL) \$(top_srcdir)/\@RTEMS_TOPdir\@/mkinstalldirs\n\n" ;
598        my $line = join( ' ',@installdirs );
599        print_macro "INSTALLDIRS =", $line ;
600        print "\n\$(INSTALLDIRS):\n\t\@\$(mkinstalldirs) \$(INSTALLDIRS)\n\n" ;
601        $nl_seen = 1 ;
602      }
603    }
604    elsif ( /^\§3(.*)$/o  )
605    { # pretty print a shell script fragment/make production
606      my @l = split(/\\\\/,$1);
607      if ( $#l >= 0 ) { my $i = shift @l ; print "\t$i"; }
608      foreach( @l ) { print "\\\n$_"; }
609      print "\n" ;
610      $nl_seen = 0 ;
611    }
612    elsif ( /^\§4(.*)$/o  )
613    { # pretty print a make variable
614      print_macro "$1 =", ${"var_$1"} ;
615      $nl_seen = 0 ;
616    }
617    elsif ( /^\§5(.*)$/o  )
618    { # pretty print an addition to a make variable
619      print_macro "$1 +=", ${"var_$1+"} ;
620      $nl_seen = 0 ;
621    }
622    else
623    {
624      $nl_seen = 0 ;
625      print "$_" ;
626    }
627  }
628}
629
630# Add rules for config.status generated files
631if ( "$build_pgms_seen" )
632{
633print "%: \$(srcdir)/%.in \$(top_builddir)/config.status\n" ;
634print " cd \$(top_builddir) \\\n" ;
635print "  && CONFIG_FILES=" ;
636print "\$(subdir)/" if ( "$subdir" );
637print "\$@ CONFIG_HEADERS= \$(SHELL) ./config.status\n";
638}
639else
640{
641print "Makefile: \$(srcdir)/Makefile.in \$(top_builddir)/config.status\n" ;
642print " cd \$(top_builddir) \\\n" ;
643print "  && CONFIG_FILES=" ;
644print "\$(subdir)/" if ( "$subdir" );
645print "\$@ CONFIG_HEADERS= \$(SHELL) ./config.status\n";
646}
647
648;1
Note: See TracBrowser for help on using the repository browser.