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