This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add comment explaining where terrible code comes from
[perl5.git] / Porting / expand-macro.pl
1 #!perl -w
2 use strict;
3
4 use Pod::Usage;
5 use Getopt::Std;
6 use Config;
7 $Getopt::Std::STANDARD_HELP_VERSION = 1;
8
9 my $trysource = "try.c";
10 my $tryout = "try.i";
11
12 getopts('fF:ekvI:X', \my %opt) or pod2usage();
13
14 my($expr, @headers) = @ARGV ? splice @ARGV : "-";
15
16 pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
17
18 foreach($trysource, $tryout) {
19     unlink $_ if $opt{e};
20     die "You already have a $_" if -e $_;
21 }
22
23 if ($expr eq '-') {
24     warn "reading from stdin...\n";
25     $expr = do { local $/; <> };
26 }
27
28 my($macro, $args) = $expr =~ /^\s*(\w+)((?:\s*\(.*\))?)\s*;?\s*$/s
29     or pod2usage "$expr doesn't look like a macro-name or macro-expression to me";
30
31 if (!(@ARGV = @headers)) {
32     open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
33     while (<$fh>) {
34         push @ARGV, $1 if m!^([^/]+\.h)\t!;
35     }
36     push @ARGV, 'config.h' if -f 'config.h';
37 }
38
39 my $header;
40 while (<>) {
41     next unless /^#\s*define\s+$macro\b/;
42     my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
43     if (defined $def_args && !$args) {
44         my @args = split ',', $def_args;
45         print "# macro: $macro args: @args in $_\n" if $opt{v};
46         my $argname = "A0";
47         $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
48     }
49     $header = $ARGV;
50     last;
51 }
52 die "$macro not found\n" unless defined $header;
53
54 if ($^O =~ /MSWin(32|64)/) {
55     # The Win32 (and Win64) build process expects to be run from
56     # bleadperl/Win32
57     chdir "Win32"
58         or die "Couldn't chdir to win32: $!";
59 };
60
61 open my $out, '>', $trysource or die "Can't open $trysource: $!";
62
63 my $sentinel = "$macro expands to";
64
65 my %done_header;
66
67 sub do_header {
68     my $header = shift;
69     return if $done_header{$header}++;
70     print $out qq{#include "$header"\n};
71 }
72
73 print $out <<'EOF' if $opt{X};
74 /* Need to do this like this, as cflags.sh sets it for us come what may.  */
75 #undef PERL_CORE
76
77 EOF
78
79 do_header('EXTERN.h');
80 do_header('perl.h');
81 do_header($header);
82 do_header('XSUB.h') if $opt{X};
83
84 print $out <<"EOF";
85 #line 4 "$sentinel"
86 $macro$args
87 EOF
88
89 close $out or die "Can't close $trysource: $!";
90
91 print "doing: $Config{make} $tryout\n" if $opt{v};
92 my $cmd = "$Config{make} $tryout";
93 system( $cmd ) == 0
94     or die "Couldn't launch [$cmd]: $! / $?";
95
96 # if user wants 'indent' formatting ..
97 my $out_fh;
98
99 if ($opt{f} || $opt{F}) {
100     # a: indent is a well behaved filter when given 0 arguments, reading from
101     #    stdin and writing to stdout
102     # b: all our braces should be balanced, indented back to column 0, in the
103     #    headers, hence everything before our #line directive can be ignored
104     #
105     # We can take advantage of this to reduce the work to indent.
106
107     my $indent_command = $opt{f} ? 'indent' : $opt{F};
108
109     if (defined $opt{I}) {
110         $indent_command .= " $opt{I}";
111     }
112     open $out_fh, '|-', $indent_command or die $?;
113 } else {
114     $out_fh = \*STDOUT;
115 }
116
117 {
118     open my $fh, '<', $tryout or die "Can't open $tryout: $!";
119
120     while (<$fh>) {
121         print $out_fh $_ if /$sentinel/o .. 1;
122     }
123 };
124
125 unless ($opt{k}) {
126     foreach($trysource, $tryout) {
127         die "Can't unlink $_: $!" unless unlink $_;
128     }
129 }
130
131 __END__
132
133 =head1 NAME
134
135 expand-macro.pl - expand C macros using the C preprocessor
136
137 =head1 SYNOPSIS
138
139   expand-macro.pl [options] [ < macro-name | macro-expression | - > [headers] ]
140
141   options:
142     -f          use 'indent' to format output
143     -F  <tool>  use <tool> to format output  (instead of -f)
144     -e          erase try.[ic] instead of failing when they're present (errdetect)
145     -k          keep them after generating (for handy inspection)
146     -v          verbose
147     -I <indent-opts>    passed into indent
148     -X          include "XSUB.h" (and undefine PERL_CORE)
149
150 =cut