7 $Getopt::Std::STANDARD_HELP_VERSION = 1;
9 my $trysource = "try.c";
12 getopts('fF:ekvI:X', \my %opt) or pod2usage();
14 my($expr, @headers) = @ARGV ? splice @ARGV : "-";
16 pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
18 foreach($trysource, $tryout) {
20 die "You already have a $_" if -e $_;
24 warn "reading from stdin...\n";
25 $expr = do { local $/; <> };
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";
31 if (!(@ARGV = @headers)) {
32 open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
34 push @ARGV, $1 if m!^([^/]+\.h)\t!;
36 push @ARGV, 'config.h' if -f 'config.h';
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};
47 $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
52 die "$macro not found\n" unless defined $header;
54 if ($^O =~ /MSWin(32|64)/) {
55 # The Win32 (and Win64) build process expects to be run from
58 or die "Couldn't chdir to win32: $!";
61 open my $out, '>', $trysource or die "Can't open $trysource: $!";
63 my $sentinel = "$macro expands to";
65 # These two are included from perl.h, and perl.h sometimes redefines their
66 # macros. So no need to include them.
67 my %done_header = ('embed.h' => 1, 'embedvar.h' => 1);
71 return if $done_header{$header}++;
72 print $out qq{#include "$header"\n};
75 print $out <<'EOF' if $opt{X};
76 /* Need to do this like this, as cflags.sh sets it for us come what may. */
81 do_header('EXTERN.h');
84 do_header('XSUB.h') if $opt{X};
91 close $out or die "Can't close $trysource: $!";
93 print "doing: $Config{make} $tryout\n" if $opt{v};
94 my $cmd = "$Config{make} $tryout";
96 or die "Couldn't launch [$cmd]: $! / $?";
98 # if user wants 'indent' formatting ..
101 if ($opt{f} || $opt{F}) {
102 # a: indent is a well behaved filter when given 0 arguments, reading from
103 # stdin and writing to stdout
104 # b: all our braces should be balanced, indented back to column 0, in the
105 # headers, hence everything before our #line directive can be ignored
107 # We can take advantage of this to reduce the work to indent.
109 my $indent_command = $opt{f} ? 'indent' : $opt{F};
111 if (defined $opt{I}) {
112 $indent_command .= " $opt{I}";
114 open $out_fh, '|-', $indent_command or die $?;
120 open my $fh, '<', $tryout or die "Can't open $tryout: $!";
123 print $out_fh $_ if /$sentinel/o .. 1;
128 foreach($trysource, $tryout) {
129 die "Can't unlink $_: $!" unless unlink $_;
137 expand-macro.pl - expand C macros using the C preprocessor
141 expand-macro.pl [options]
142 [ < macro-name | macro-expression | - > [headers] ]
145 -f use 'indent' to format output
146 -F <tool> use <tool> to format output (instead of -f)
147 -e erase try.[ic] instead of failing when they're present
149 -k keep them after generating (for handy inspection)
151 -I <indent-opts> passed into indent
152 -X include "XSUB.h" (and undefine PERL_CORE)