This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make sure expand-macro.pl also works for macros in headers
[perl5.git] / Porting / expand-macro.pl
1 #!perl -w
2 use strict;
3
4 use Getopt::Std;
5
6 use vars qw($trysource $tryout $sentinel);
7 $trysource = "try.c";
8 $tryout = "try.i";
9
10 getopts('fF:ekvI:', \my %opt) or usage();
11
12 sub usage {
13     die<<EO_HELP;
14 @_;
15 usage: $0 [options] <macro-name> [headers]
16 options:
17     -f          use 'indent' to format output
18     -F  <tool>  use <tool> to format output  (instead of -f)
19     -e          erase try.[ic] instead of failing when theyre present (errdetect)
20     -k          keep them after generating (for handy inspection)
21     -v          verbose
22     -I <indent-opts>    passed into indent
23 EO_HELP
24 }
25
26 my $macro = shift;
27 usage "missing <macro-name>" unless defined $macro;
28
29 $sentinel = "$macro expands to";
30
31 usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
32
33 foreach($trysource, $tryout) {
34     unlink $_ if $opt{e};
35     die "You already have a $_" if -e $_;
36 }
37
38 if (!@ARGV) {
39     open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
40     while (<$fh>) {
41         push @ARGV, $1 if m!^([^/]+\.h)\t!;
42     }
43 }
44
45 my $args = '';
46
47 my $header;
48 while (<>) {
49     next unless /^#\s*define\s+$macro\b/;
50     my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
51     if (defined $def_args) {
52         my @args = split ',', $def_args;
53         print "# macro: $macro args: @args in $_\n" if $opt{v};
54         my $argname = "A0";
55         $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
56     }
57     $header = $ARGV;
58     last;
59 }
60 die "$macro not found\n" unless defined $header;
61
62 open my $out, '>', $trysource or die "Can't open $trysource: $!";
63
64 print $out <<"EOF";
65 #include "EXTERN.h"
66 #include "perl.h"
67 #include "$header"
68 #line 4 "$sentinel"
69 $macro$args
70 EOF
71
72 close $out or die "Can't close $trysource: $!";
73
74 print "doing: make $tryout\n" if $opt{v};
75 system "make $tryout" and die;
76
77 # if user wants 'indent' formatting ..
78 my $out_fh;
79
80 if ($opt{f} || $opt{F}) {
81     # a: indent is a well behaved filter when given 0 arguments, reading from
82     #    stdin and writing to stdout
83     # b: all our braces should be balanced, indented back to column 0, in the
84     #    headers, hence everything before our #line directive can be ignored
85     #
86     # We can take advantage of this to reduce the work to indent.
87
88     my $indent_command = $opt{f} ? 'indent' : $opt{F};
89
90     if (defined $opt{I}) {
91         $indent_command .= " $opt{I}";
92     }
93     open $out_fh, '|-', $indent_command or die $?;
94 } else {
95     $out_fh = \*STDOUT;
96 }
97
98 open my $fh, '<', $tryout or die "Can't open $tryout: $!";
99
100 while (<$fh>) {
101     print $out_fh $_ if /$sentinel/o .. 1;
102 }
103
104 unless ($opt{k}) {
105     foreach($trysource, $tryout) {
106         die "Can't unlink $_" unless unlink $_;
107     }
108 }