Loop over @extspec, calling build_extension() on each.
[p5sagit/p5-mst-13.2.git] / make_ext.pl
1 #!./miniperl
2 use strict;
3 use warnings;
4 use Config;
5
6 # This script acts as a simple interface for building extensions.
7 # It primarily used by the perl Makefile:
8 #
9 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
10 #       @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
11 #
12 # It may be deleted in a later release of perl so try to
13 # avoid using it for other purposes.
14
15 my $is_Win32 = $^O eq 'MSWin32';
16 my $is_VMS = $^O eq 'VMS';
17 my $is_Unix = !$is_Win32 && !$is_VMS;
18
19 my (%excl, %incl, %opts, @extspec, @pass_through);
20
21 foreach (@ARGV) {
22     if (/^!(.*)$/) {
23         $excl{$1} = 1;
24     } elsif (/^\+(.*)$/) {
25         $incl{$1} = 1;
26     } elsif (/^--([\w\-]+)$/) {
27         $opts{$1} = 1;
28     } elsif (/^--([\w\-]+)=(.*)$/) {
29         $opts{$1} = $2;
30     } elsif (/=/) {
31         push @pass_through, $_;
32     } elsif (length) {
33         push @extspec, $_;
34     }
35 }
36
37 # The Perl Makefile.SH will expand all extensions to
38 #       lib/auto/X/X.a  (or lib/auto/X/Y/Y.a if nested)
39 # A user wishing to run make_ext might use
40 #       X (or X/Y or X::Y if nested)
41
42 # canonise into X/Y form (pname)
43
44 foreach (@extspec) {
45     if (/^lib/) {
46         # Remove lib/auto prefix and /*.* suffix
47         s{^lib/auto/}{};
48         s{[^/]*\.[^/]*$}{};
49     } elsif (/^ext/) {
50         # Remove ext/ prefix and /pm_to_blib suffix
51         s{^ext/}{};
52         s{/pm_to_blib$}{};
53     } elsif (/::/) {
54         # Convert :: to /
55         s{::}{\/}g;
56     } elsif (/\..*o$/) {
57         s/\..*o//;
58     }
59 }
60
61 my $makecmd  = shift @pass_through; # Should be something like MAKE=make
62 unshift @pass_through, 'PERL_CORE=1';
63
64 my $target   = $opts{target};
65 $target = 'all' unless defined $target;
66
67 # Previously, $make was taken from config.sh.  However, the user might
68 # instead be running a possibly incompatible make.  This might happen if
69 # the user types "gmake" instead of a plain "make", for example.  The
70 # correct current value of MAKE will come through from the main perl
71 # makefile as MAKE=/whatever/make in $makecmd.  We'll be cautious in
72 # case third party users of this script (are there any?) don't have the
73 # MAKE=$(MAKE) argument, which was added after 5.004_03.
74 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
75     die "$0:  WARNING:  Please include MAKE=\$(MAKE) in \@ARGV\n";
76 }
77
78 my $make = $1 || $Config{make} || $ENV{MAKE};
79 # Using an array of 0 or 1 elements makes the subsequent code simpler.
80 my @run = $Config{run};
81 @run = () if not defined $run[0] or $run[0] eq '';
82
83 if (!@extspec)  {
84     die "$0: no extension specified\n";
85 }
86
87 if ($target eq '') {
88     die "make_ext: no make target specified (eg all or clean)\n";
89 } elsif ($target !~ /(?:^all|clean)$/) {
90     # for the time being we are strict about what make_ext is used for
91     die "$0: unknown make target '$target'\n";
92 }
93
94 foreach my $pname (@extspec)  {
95     my $mname = $pname;
96     $mname =~ s!/!::!g;
97     my $depth = $pname;
98     $depth =~ s![^/]+!..!g;
99     # Always need one more .. for ext/
100     my $up = "../$depth";
101     my $perl = "$up/miniperl";
102
103     if ($Config{osname} eq 'catamount') {
104         # Snowball's chance of building extensions.
105         die "This is $Config{osname}, not building $mname, sorry.\n";
106     }
107
108     print "\tMaking $mname ($target)\n";
109
110     build_extension('ext', "ext/$pname", $up, $perl, "$up/lib",
111                     \@pass_through);
112 }
113
114 sub build_extension {
115     my ($ext, $ext_dir, $return_dir, $perl, $lib_dir, $pass_through) = @_;
116     unless (chdir "$ext_dir") {
117         warn "Cannot cd to $ext_dir: $!";
118         return;
119     }
120     
121     if (!-f 'Makefile') {
122         print "\nRunning Makefile.PL in $ext_dir\n";
123
124         # Presumably this can be simplified
125         my @cross;
126         if (defined $::Cross::platform) {
127             # Inherited from win32/buildext.pl
128             @cross = "-MCross=$::Cross::platform";
129         } elsif ($opts{cross}) {
130             # Inherited from make_ext.pl
131             @cross = '-MCross';
132         }
133             
134         my @perl = (@run, $perl, "-I$lib_dir", @cross, 'Makefile.PL',
135                     'INSTALLDIRS=perl', 'INSTALLMAN3DIR=none',
136                     @$pass_through);
137         print join(' ', @perl), "\n";
138         my $code = system @perl;
139         warn "$code from $ext_dir\'s Makefile.PL" if $code;
140
141         # Right. The reason for this little hack is that we're sitting inside
142         # a program run by ./miniperl, but there are tasks we need to perform
143         # when the 'realclean', 'distclean' or 'veryclean' targets are run.
144         # Unfortunately, they can be run *after* 'clean', which deletes
145         # ./miniperl
146         # So we do our best to leave a set of instructions identical to what
147         # we would do if we are run directly as 'realclean' etc
148         # Whilst we're perfect, unfortunately the targets we call are not, as
149         # some of them rely on a $(PERL) for their own distclean targets.
150         # But this always used to be a problem with the old /bin/sh version of
151         # this.
152         if ($is_Unix) {
153             my $suffix = '.sh';
154             foreach my $clean_target ('realclean', 'veryclean') {
155                 my $file = "$return_dir/$clean_target$suffix";
156                 open my $fh, '>>', $file or die "open $file: $!";
157                 # Quite possible that we're being run in parallel here.
158                 # Can't use Fcntl this early to get the LOCK_EX
159                 flock $fh, 2 or warn "flock $file: $!";
160                 print $fh <<"EOS";
161 cd $ext_dir
162 if test ! -f Makefile -a -f Makefile.old; then
163     echo "Note: Using Makefile.old"
164     make -f Makefile.old $clean_target MAKE=$make @pass_through
165 else
166     if test ! -f Makefile ; then
167         echo "Warning: No Makefile!"
168     fi
169     make $clean_target MAKE=$make @pass_through
170 fi
171 cd $return_dir
172 EOS
173                 close $fh or die "close $file: $!";
174             }
175         }
176     }
177
178     if (not -f 'Makefile') {
179         print "Warning: No Makefile!\n";
180     }
181
182     if (!$target or $target !~ /clean$/) {
183         # Give makefile an opportunity to rewrite itself.
184         # reassure users that life goes on...
185         my @config = (@run, $make, 'config', @$pass_through);
186         system @config and print "@config failed, continuing anyway...\n";
187     }
188     my @targ = (@run, $make, $target, @$pass_through);
189     print "Making $target in $ext_dir\n@targ\n";
190     my $code = system @targ;
191     die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
192
193     chdir $return_dir || die "Cannot cd to $return_dir: $!";
194 }