source: rtems/tools/update/acpolish @ 9e633f5

4.104.114.84.95
Last change on this file since 9e633f5 was df49c60, checked in by Joel Sherrill <joel.sherrill@…>, on 06/12/00 at 15:00:15

Merged from 4.5.0-beta3a

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