1#!/usr/bin/perl
2#
3#  mms2make.pl - convert Descrip.MMS file to Makefile
4#  Version 2.2 29-Jan-1996
5#  David Denholm <denholm@conmat.phys.soton.ac.uk>
6#
7#  1.0  06-Aug-1994  Charles Bailey  bailey@newman.upenn.edu
8#    - original version
9#  2.0  29-Sep-1994  David Denholm <denholm@conmat.phys.soton.ac.uk>
10#    - take action based on MMS .if / .else / .endif
11#      any command line options after filenames are set in an assoc array %macros
12#      maintain "@condition as a stack of current conditions
13#      we unshift a 0 or 1 to front of @conditions at an .ifdef
14#      we invert top of stack at a .else
15#      we pop at a .endif
16#      we deselect any other line if $conditions[0] is 0
17#      I'm being very lazy - push a 1 at start, then don't need to check for
18#      an empty @conditions [assume nesting in descrip.mms is correct]
19#  2.1  26-Feb-1995  Charles Bailey  bailey@newman.upenn.edu
20#    - handle MMS macros generated by MakeMaker
21#  2.2  29-Jan-1996  Charles Bailey  bailey@newman.upenn.edu
22#    - Fix output file name to work under Unix
23
24if ($#ARGV > -1 && $ARGV[0] =~ /^[\-\/]trim/i) {
25  $do_trim = 1;
26  shift @ARGV;
27}
28$infile  = $#ARGV > -1 ? shift(@ARGV) : "Descrip.MMS";
29$outfile = $#ARGV > -1 ? shift(@ARGV) : "Makefile";
30
31# set any other args in %macros - set VAXC by default
32foreach (@ARGV) { $macros{"\U$_"}=1 }
33
34# consistency check
35$macros{"DECC"} = 1 if $macros{"__AXP__"};
36
37# set conditions as if there was a .if 1  around whole file
38# [lazy - saves having to check for empty array - just test [0]==1]
39@conditions = (1);
40
41open(INFIL,'<',$infile) || die "Can't open $infile: $!\n";
42open(OUTFIL,'>',$outfile) || die "Can't open $outfile: $!\n";
43
44print OUTFIL "#> This file produced from $infile by $0\n";
45print OUTFIL "#> Lines beginning with \"#>\" were commented out during the\n";
46print OUTFIL "#> conversion process.  For more information, see $0\n";
47print OUTFIL "#>\n";
48
49while (<INFIL>) {
50  s/$infile/$outfile/eoi;
51  if (/^\#/) {
52    if (!/^\#\:/) {print OUTFIL;}
53    next;
54  }
55
56# look for ".ifdef macro" and push 1 or 0 to head of @conditions
57# push 0 if we are in false branch of another if
58  if (/^\.ifdef\s*(.+)/i)
59  {
60     print OUTFIL "#> ",$_ unless $do_trim;
61     unshift @conditions, ($macros{"\U$1"} ? $conditions[0] : 0);
62     next;
63  }
64
65# reverse $conditions[0] for .else provided surrounding if is active
66  if (/^\.else/i)
67  {
68      print OUTFIL "#> ",$_ unless $do_trim;
69      $conditions[0] = $conditions[1] && !$conditions[0];
70      next;
71  }
72
73# pop top condition for .endif
74  if (/^\.endif/i)
75  {
76     print OUTFIL "#> ",$_ unless $do_trim;
77     shift @conditions;
78     next;
79  }
80
81  next if ($do_trim && !$conditions[0]);
82
83# spot new rule and pick up first source file, since some versions of
84# Make don't provide a macro for this
85  if (/[^#!]*:\s+/) {
86    if (/:\s+([^\s,]+)/) { $firstsrc = $1 }
87    else { $firstsrc = "\$<" }
88  }
89
90#convert macros we expect to see in MakeMaker-generated Descrip.MMSs
91  s#/Descrip=\s*\n#-f \nMMS = make\n#;
92  s#/Macro=\(# #;
93  s#MACROEND = \)#MACROEND = #;
94  if (m#\$\(USEMACROS\)(.*)(\$\(MACROEND\))?#) {
95    while (1) {
96      my($macros,$end) = ($1,$2);
97      $macros =~ s/,/ /g;  # We're hosed if there're commas within a macro -
98                           # someday, check for "" and skip contents
99      last if $end;
100      print OUTFIL $conditions[0] ? "#> " : "",$_;
101      $_ = <INFIL>;
102      m#(.*)(\$\(MACROEND\))?#;
103    }
104  }
105
106  s/^ +/\t/;
107  s/^\.first/\.first:/i;
108  s/^\.suffixes/\.suffixes:/i;
109  s/\@\[\.vms\]/\$\$\@\[\.vms\]/;
110  s/f\$/f\$\$/goi;
111  s/\$\(mms\$source\)/$firstsrc/i;
112  s/\$\(mms\$target\)/\$\@/i;
113  s/\$\(mms\$target_name\)\$\(O\)/\$\@/i;
114  s/\$\(mms\$target_name\)/\$\*/i;
115  s/sys\$([^\(])/sys\$\$$1/gi;
116  print OUTFIL "#> " unless $conditions[0];
117  print OUTFIL $_;
118}
119
120close INFIL;
121close OUTFIL;
122
123