For 5.12: saner behaviour for `length`
[p5sagit/p5-mst-13.2.git] / Porting / expand-macro.pl
1 #!perl -w
2 use strict;
3
4 use vars qw($trysource $tryout $sentinel);
5 $trysource = "try.c";
6 $tryout = "try.i";
7
8 my $macro = shift;
9 die "$0 macro [headers]" unless defined $macro;
10
11 $sentinel = "$macro expands to";
12
13 foreach($trysource, $tryout) {
14     die "You already have a $_" if -e $_;
15 }
16
17 if (!@ARGV) {
18     open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
19     while (<$fh>) {
20         push @ARGV, $1 if m!^([^/]+\.h)\t!;
21     }
22 }
23
24 my $args = '';
25
26 while (<>) {
27     next unless /^#\s*define\s+$macro/;
28     my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
29     if (defined $def_args) {
30         my @args = split ',', $def_args;
31         my $argname = "A0";
32         $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
33     }
34     last;
35 }
36
37 open my $out, '>', $trysource or die "Can't open $trysource: $!";
38
39 print $out <<"EOF";
40 #include "EXTERN.h"
41 #include "perl.h"
42 #line 3 "$sentinel"
43 $macro$args
44 EOF
45
46 close $out or die "Can't close $trysource: $!";
47
48 system "make $tryout" and die;
49
50 open my $fh, '<', $tryout or die "Can't open $tryout: $!";
51
52 while (<$fh>) {
53     print if /$sentinel/o .. 1;
54 }
55
56 foreach($trysource, $tryout) {
57     die "Can't unlink $_" unless unlink $_;
58 }