Make expand-macro.pl scan config.h, if it exists.
[p5sagit/p5-mst-13.2.git] / Porting / expand-macro.pl
CommitLineData
d1f3479f 1#!perl -w
2use strict;
3
d5f33267 4use Pod::Usage;
a93e78e3 5use Getopt::Std;
d5f33267 6$Getopt::Std::STANDARD_HELP_VERSION = 1;
a93e78e3 7
d5f33267 8my $trysource = "try.c";
9my $tryout = "try.i";
d1f3479f 10
d5f33267 11getopts('fF:ekvI:', \my %opt) or pod2usage();
a93e78e3 12
d5f33267 13my($expr, @headers) = @ARGV ? splice @ARGV : "-";
d1f3479f 14
d5f33267 15pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
a93e78e3 16
d1f3479f 17foreach($trysource, $tryout) {
a93e78e3 18 unlink $_ if $opt{e};
d1f3479f 19 die "You already have a $_" if -e $_;
20}
21
d5f33267 22if ($expr eq '-') {
23 warn "reading from stdin...\n";
24 $expr = do { local $/; <> };
25}
26
27my($macro, $args) = $expr =~ /^\s*(\w+)((?:\s*\(.*\))?)\s*;?\s*$/s
28 or pod2usage "$expr doesn't look like a macro-name or macro-expression to me";
29
30if (!(@ARGV = @headers)) {
d1f3479f 31 open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
32 while (<$fh>) {
33 push @ARGV, $1 if m!^([^/]+\.h)\t!;
34 }
ed690650 35 push @ARGV, 'config.h' if -f 'config.h';
d1f3479f 36}
37
be4f373d 38my $header;
d1f3479f 39while (<>) {
e25a7dc2 40 next unless /^#\s*define\s+$macro\b/;
d1f3479f 41 my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
d5f33267 42 if (defined $def_args && !$args) {
d1f3479f 43 my @args = split ',', $def_args;
a93e78e3 44 print "# macro: $macro args: @args in $_\n" if $opt{v};
d1f3479f 45 my $argname = "A0";
46 $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
47 }
be4f373d 48 $header = $ARGV;
d1f3479f 49 last;
50}
be4f373d 51die "$macro not found\n" unless defined $header;
d1f3479f 52
53open my $out, '>', $trysource or die "Can't open $trysource: $!";
54
d5f33267 55my $sentinel = "$macro expands to";
56
d1f3479f 57print $out <<"EOF";
58#include "EXTERN.h"
59#include "perl.h"
be4f373d 60#include "$header"
61#line 4 "$sentinel"
d1f3479f 62$macro$args
63EOF
64
65close $out or die "Can't close $trysource: $!";
66
a93e78e3 67print "doing: make $tryout\n" if $opt{v};
d1f3479f 68system "make $tryout" and die;
69
a93e78e3 70# if user wants 'indent' formatting ..
27c6397c 71my $out_fh;
72
73if ($opt{f} || $opt{F}) {
74 # a: indent is a well behaved filter when given 0 arguments, reading from
75 # stdin and writing to stdout
76 # b: all our braces should be balanced, indented back to column 0, in the
77 # headers, hence everything before our #line directive can be ignored
78 #
79 # We can take advantage of this to reduce the work to indent.
80
81 my $indent_command = $opt{f} ? 'indent' : $opt{F};
82
83 if (defined $opt{I}) {
84 $indent_command .= " $opt{I}";
85 }
86 open $out_fh, '|-', $indent_command or die $?;
87} else {
88 $out_fh = \*STDOUT;
89}
a93e78e3 90
d1f3479f 91open my $fh, '<', $tryout or die "Can't open $tryout: $!";
92
93while (<$fh>) {
27c6397c 94 print $out_fh $_ if /$sentinel/o .. 1;
d1f3479f 95}
96
a93e78e3 97unless ($opt{k}) {
98 foreach($trysource, $tryout) {
99 die "Can't unlink $_" unless unlink $_;
100 }
d1f3479f 101}
d5f33267 102
103__END__
104
105=head1 NAME
106
107expand-macro.pl - expand C macros using the C preprocessor
108
109=head1 SYNOPSIS
110
111 expand-macro.pl [options] [ < macro-name | macro-expression | - > [headers] ]
112
113 options:
114 -f use 'indent' to format output
115 -F <tool> use <tool> to format output (instead of -f)
116 -e erase try.[ic] instead of failing when they're present (errdetect)
117 -k keep them after generating (for handy inspection)
118 -v verbose
119 -I <indent-opts> passed into indent
120
121=cut