Move Archive-Extract from ext/ to cpan/
[p5sagit/p5-mst-13.2.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19 2use strict;
3use warnings;
b8d39eba 4use Config;
c3ef65fb 5BEGIN {
9ddff148 6 unshift @INC, $^O eq 'MSWin32' ? '../ext/Cwd' : 'ext/Cwd';
c3ef65fb 7}
286d62c2 8use Cwd;
75f92628 9
392ef80c 10# To clarify, this isn't the entire suite of modules considered "toolchain"
11# It's not even all modules needed to build ext/
12# It's just the source paths of the (minimum complete set of) modules in ext/
13# needed to build the nonxs modules
14# After which, all nonxs modules are in lib, which was always sufficient to
15# allow miniperl to build everything else.
16
83f40c94 17my @toolchain = qw(ext/constant/lib ext/Cwd ext/Cwd/lib ext/ExtUtils-Command/lib
b78fd716 18 ext/ExtUtils-Install/lib ext/ExtUtils-MakeMaker/lib
1a7ec96d 19 ext/ExtUtils-Manifest/lib ext/Text-ParseWords/lib
4677aef7 20 ext/File-Path/lib ext/AutoLoader/lib);
f345288b 21
8a992763 22my @ext_dirs = qw(ext cpan);
321c3589 23my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')';
8a992763 24
a0d0e21e 25# This script acts as a simple interface for building extensions.
286d62c2 26
27# It's actually a cut and shut of the Unix version ext/utils/makeext and the
28# Windows version win32/build_ext.pl hence the two invocation styles.
29
30# On Unix, it primarily used by the perl Makefile one extention at a time:
a0d0e21e 31#
32# d_dummy $(dynamic_ext): miniperl preplibrary FORCE
e2fabae1 33# @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
a0d0e21e 34#
902aaf3e 35# On Windows or VMS,
286d62c2 36# If '--static' is specified, static extensions will be built.
a34ce875 37# If '--dynamic' is specified, dynamic extensions will be built.
38# If '--nonxs' is specified, nonxs extensions will be built.
281da5ea 39# If '--dynaloader' is specificied, DynaLoader will be built.
286d62c2 40# If '--all' is specified, all extensions will be built.
41#
42# make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
43#
44# E.g.
45#
46# make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
47#
48# make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
49#
50# make_ext.pl MAKE=dmake --dir=..\ext
51#
52# make_ext.pl MAKE=dmake --dir=..\ext --target=clean
53#
54# Will skip building extensions which are marked with an '!' char.
55# Mostly because they still not ported to specified platform.
56#
57# If any extensions are listed with a '+' char then only those
58# extensions will be built, but only if they arent countermanded
59# by an '!ext' and are appropriate to the type of building being done.
60
a0d0e21e 61# It may be deleted in a later release of perl so try to
62# avoid using it for other purposes.
63
e3b84025 64my $is_Win32 = $^O eq 'MSWin32';
65my $is_VMS = $^O eq 'VMS';
66my $is_Unix = !$is_Win32 && !$is_VMS;
67
286d62c2 68require FindExt if $is_Win32;
69
fc678412 70my (%excl, %incl, %opts, @extspec, @pass_through);
97a26ad9 71
72foreach (@ARGV) {
73 if (/^!(.*)$/) {
74 $excl{$1} = 1;
75 } elsif (/^\+(.*)$/) {
76 $incl{$1} = 1;
77 } elsif (/^--([\w\-]+)$/) {
78 $opts{$1} = 1;
e2fabae1 79 } elsif (/^--([\w\-]+)=(.*)$/) {
7c40aa71 80 push @{$opts{$1}}, $2;
e2fabae1 81 } elsif (/=/) {
fc678412 82 push @pass_through, $_;
ca5de986 83 } elsif (length) {
e2fabae1 84 push @extspec, $_;
97a26ad9 85 }
86}
87
286d62c2 88my $static = $opts{static} || $opts{all};
89my $dynamic = $opts{dynamic} || $opts{all};
a34ce875 90my $nonxs = $opts{nonxs} || $opts{all};
281da5ea 91my $dynaloader = $opts{dynaloader} || $opts{all};
286d62c2 92
ca5de986 93# The Perl Makefile.SH will expand all extensions to
94# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
95# A user wishing to run make_ext might use
96# X (or X/Y or X::Y if nested)
97
98# canonise into X/Y form (pname)
99
100foreach (@extspec) {
7ad017a8 101 if (s{^lib/auto/}{}) {
ca5de986 102 # Remove lib/auto prefix and /*.* suffix
c5aac6ab 103 s{/[^/]+\.[^/]+$}{};
321c3589 104 } elsif (s{^$ext_dirs_re/}{}) {
ca5de986 105 # Remove ext/ prefix and /pm_to_blib suffix
ca5de986 106 s{/pm_to_blib$}{};
57df1c46 107 # Targets are given as files on disk, but the extension spec is still
108 # written using /s for each ::
109 tr!-!/!;
7ad017a8 110 } elsif (s{::}{\/}g) {
ca5de986 111 # Convert :: to /
7ad017a8 112 } else {
ca5de986 113 s/\..*o//;
114 }
115}
116
fc678412 117my $makecmd = shift @pass_through; # Should be something like MAKE=make
118unshift @pass_through, 'PERL_CORE=1';
119
ebbb719b 120my @dirs = @{$opts{dir} || ['ext', 'cpan']};
7c40aa71 121my $target = $opts{target}[0];
07f3cc2a 122$target = 'all' unless defined $target;
a0d0e21e 123
fb73857a 124# Previously, $make was taken from config.sh. However, the user might
125# instead be running a possibly incompatible make. This might happen if
126# the user types "gmake" instead of a plain "make", for example. The
127# correct current value of MAKE will come through from the main perl
128# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
129# case third party users of this script (are there any?) don't have the
130# MAKE=$(MAKE) argument, which was added after 5.004_03.
ca5de986 131unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
132 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
a2f19a19 133}
134
eb1f8df7 135# This isn't going to cope with anything fancy, such as spaces inside command
136# names, but neither did what it replaced. Once there is a use case that needs
137# it, please supply patches. Until then, I'm sticking to KISS
138my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
ca5de986 139# Using an array of 0 or 1 elements makes the subsequent code simpler.
fc678412 140my @run = $Config{run};
141@run = () if not defined $run[0] or $run[0] eq '';
a2f19a19 142
a0d0e21e 143
07f3cc2a 144if ($target eq '') {
ca5de986 145 die "make_ext: no make target specified (eg all or clean)\n";
146} elsif ($target !~ /(?:^all|clean)$/) {
147 # for the time being we are strict about what make_ext is used for
148 die "$0: unknown make target '$target'\n";
a2f19a19 149}
150
281da5ea 151if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
286d62c2 152 die "$0: no extension specified\n";
153}
154
155my $perl;
156my %extra_passthrough;
157
158if ($is_Win32) {
159 (my $here = getcwd()) =~ s{/}{\\}g;
160 $perl = $^X;
161 if ($perl =~ m#^\.\.#) {
162 $perl = "$here\\$perl";
163 }
164 (my $topdir = $perl) =~ s/\\[^\\]+$//;
165 # miniperl needs to find perlglob and pl2bat
166 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
167 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
168 unless (-f "$pl2bat.bat") {
d7f84378 169 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
286d62c2 170 print "@args\n";
171 system(@args) unless defined $::Cross::platform;
172 }
173
a2ac9a71 174 my $build = getcwd();
175 print "In $build";
7c40aa71 176 foreach my $dir (@dirs) {
a2ac9a71 177 chdir($dir) or die "Cannot cd to $dir: $!\n";
7c40aa71 178 (my $ext = getcwd()) =~ s{/}{\\}g;
179 FindExt::scan_ext($ext);
180 FindExt::set_static_extensions(split ' ', $Config{static_ext});
181
182 my @ext;
183 push @ext, FindExt::static_ext() if $static;
184 push @ext, FindExt::dynamic_ext() if $dynamic;
185 push @ext, FindExt::nonxs_ext() if $nonxs;
186 push @ext, 'DynaLoader' if $dynaloader;
187
188 foreach (sort @ext) {
189 if (%incl and !exists $incl{$_}) {
190 #warn "Skipping extension $ext\\$_, not in inclusion list\n";
191 next;
192 }
193 if (exists $excl{$_}) {
194 warn "Skipping extension $ext\\$_, not ported to current platform";
195 next;
196 }
197 push @extspec, $_;
198 if($_ eq 'DynaLoader') {
199 # No, we don't know why nmake can't work out the dependency chain
200 push @{$extra_passthrough{$_}}, 'DynaLoader.c';
201 } elsif(FindExt::is_static($_)) {
202 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
203 }
286d62c2 204 }
a2ac9a71 205 chdir $build
206 or die "Couldn't chdir to '$build': $!"; # restore our start directory
286d62c2 207 }
a2ac9a71 208 chdir '..'
209 or die "Couldn't chdir to build directory: $!"; # now in the Perl build directory
286d62c2 210}
902aaf3e 211elsif ($is_VMS) {
212 $perl = $^X;
213 push @extspec, (split ' ', $Config{static_ext}) if $static;
214 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
a34ce875 215 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
281da5ea 216 push @extspec, 'DynaLoader' if $dynaloader;
902aaf3e 217}
286d62c2 218
dc0655f7 219{
220 # Cwd needs to be built before Encode recurses into subdirectories.
221 # This seems to be the simplest way to ensure this ordering:
222 my (@first, @other);
223 foreach (@extspec) {
224 if ($_ eq 'Cwd') {
225 push @first, $_;
226 } else {
227 push @other, $_;
228 }
229 }
230 @extspec = (@first, @other);
231}
232
e08c66ce 233foreach my $spec (@extspec) {
234 my $mname = $spec;
ca5de986 235 $mname =~ s!/!::!g;
238a6851 236 my $ext_pathname;
237 if (-d "ext/$spec") {
238 # Old style ext/Data/Dumper/
239 $ext_pathname = "ext/$spec";
240 } else {
241 # New style ext/Data-Dumper/
242 my $copy = $spec;
243 $copy =~ tr!/!-!;
8a992763 244 foreach my $dir (@ext_dirs) {
245 if (-d "$dir/$copy") {
246 $ext_pathname = "$dir/$copy";
247 last;
248 }
249 }
250 if (!defined $ext_pathname) {
251 warn "Can't find extension $spec in any of @ext_dirs";
252 next;
253 }
238a6851 254 }
a2f19a19 255
ca5de986 256 if ($Config{osname} eq 'catamount') {
a2f19a19 257 # Snowball's chance of building extensions.
ca5de986 258 die "This is $Config{osname}, not building $mname, sorry.\n";
259 }
a2f19a19 260
ca5de986 261 print "\tMaking $mname ($target)\n";
a2f19a19 262
acb65a20 263 build_extension($ext_pathname, $perl, $mname,
e08c66ce 264 [@pass_through, @{$extra_passthrough{$spec} || []}]);
ca5de986 265}
a0d0e21e 266
fc678412 267sub build_extension {
acb65a20 268 my ($ext_dir, $perl, $mname, $pass_through) = @_;
269
270 my $up = $ext_dir;
271 $up =~ s![^/]+!..!g;
272
273 $perl ||= "$up/miniperl";
274 my $return_dir = $up;
275 my $lib_dir = "$up/lib";
bc72ff49 276 # $lib_dir must be last, as we're copying files into it, and in a parallel
277 # make there's a race condition if one process tries to open a module that
278 # another process has half-written.
f345288b 279 $ENV{PERL5LIB}
bc72ff49 280 = join $Config{path_sep}, (map {"$up/$_"} @toolchain), $lib_dir;
6c8b0117 281 $ENV{PERL_CORE} = 1;
acb65a20 282
fc678412 283 unless (chdir "$ext_dir") {
284 warn "Cannot cd to $ext_dir: $!";
285 return;
286 }
902aaf3e 287 my $makefile;
288 if ($is_VMS) {
289 $makefile = 'descrip.mms';
290 if ($target =~ /clean$/
291 && !-f $makefile
292 && -f "${makefile}_old") {
293 $makefile = "${makefile}_old";
294 }
295 } else {
296 $makefile = 'Makefile';
297 }
fc678412 298
902aaf3e 299 if (!-f $makefile) {
e74f76b2 300 if (!-f 'Makefile.PL') {
301 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
302 # We need to cope well with various possible layouts
303 my @dirs = split /::/, $mname;
304 my $leaf = pop @dirs;
305 my $leafname = "$leaf.pm";
306 my $pathname = join '/', @dirs, $leafname;
307 my @locations = ($leafname, $pathname, "lib/$pathname");
308 my $fromname;
309 foreach (@locations) {
310 if (-f $_) {
311 $fromname = $_;
312 last;
313 }
314 }
315
316 unless ($fromname) {
317 die "For $mname tried @locations in in $ext_dir but can't find source";
318 }
319 open my $fh, '>', 'Makefile.PL'
320 or die "Can't open Makefile.PL for writing: $!";
321 print $fh <<"EOM";
322#-*- buffer-read-only: t -*-
323
324# This Makefile.PL was written by $0.
325# It will be deleted automatically by make realclean
326
327use strict;
328use ExtUtils::MakeMaker;
329
330WriteMakefile(
331 NAME => '$mname',
332 VERSION_FROM => '$fromname',
333 ABSTRACT_FROM => '$fromname',
334 realclean => {FILES => 'Makefile.PL'},
335);
336
337# ex: set ro:
338EOM
339 close $fh or die "Can't close Makefile.PL: $!";
340 }
fc678412 341 print "\nRunning Makefile.PL in $ext_dir\n";
342
343 # Presumably this can be simplified
344 my @cross;
345 if (defined $::Cross::platform) {
346 # Inherited from win32/buildext.pl
347 @cross = "-MCross=$::Cross::platform";
348 } elsif ($opts{cross}) {
349 # Inherited from make_ext.pl
350 @cross = '-MCross';
a2f19a19 351 }
fc678412 352
73402eba 353 my @args = (@cross, 'Makefile.PL');
902aaf3e 354 if ($is_VMS) {
355 my $libd = VMS::Filespec::vmspath($lib_dir);
356 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
357 } else {
a2b175af 358 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
359 'INSTALLMAN3DIR=none';
902aaf3e 360 }
361 push @args, @$pass_through;
362 _quote_args(\@args) if $is_VMS;
363 print join(' ', @run, $perl, @args), "\n";
364 my $code = system @run, $perl, @args;
fc678412 365 warn "$code from $ext_dir\'s Makefile.PL" if $code;
366
61edc683 367 # Right. The reason for this little hack is that we're sitting inside
368 # a program run by ./miniperl, but there are tasks we need to perform
369 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
370 # Unfortunately, they can be run *after* 'clean', which deletes
371 # ./miniperl
372 # So we do our best to leave a set of instructions identical to what
373 # we would do if we are run directly as 'realclean' etc
374 # Whilst we're perfect, unfortunately the targets we call are not, as
375 # some of them rely on a $(PERL) for their own distclean targets.
376 # But this always used to be a problem with the old /bin/sh version of
377 # this.
e3b84025 378 if ($is_Unix) {
fc678412 379 my $suffix = '.sh';
380 foreach my $clean_target ('realclean', 'veryclean') {
ca5de986 381 my $file = "$return_dir/$clean_target$suffix";
fc678412 382 open my $fh, '>>', $file or die "open $file: $!";
383 # Quite possible that we're being run in parallel here.
384 # Can't use Fcntl this early to get the LOCK_EX
385 flock $fh, 2 or warn "flock $file: $!";
386 print $fh <<"EOS";
387cd $ext_dir
388if test ! -f Makefile -a -f Makefile.old; then
61edc683 389 echo "Note: Using Makefile.old"
eb1f8df7 390 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 391else
fc678412 392 if test ! -f Makefile ; then
61edc683 393 echo "Warning: No Makefile!"
394 fi
eb1f8df7 395 make $clean_target MAKE='@make' @pass_through
61edc683 396fi
fc678412 397cd $return_dir
61edc683 398EOS
fc678412 399 close $fh or die "close $file: $!";
400 }
61edc683 401 }
fc678412 402 }
a2f19a19 403
902aaf3e 404 if (not -f $makefile) {
a2f19a19 405 print "Warning: No Makefile!\n";
fc678412 406 }
a2f19a19 407
902aaf3e 408 if ($is_VMS) {
409 _macroify_passthrough($pass_through);
410 unshift @$pass_through, "/DESCRIPTION=$makefile";
411 }
412
fc678412 413 if (!$target or $target !~ /clean$/) {
a2f19a19 414 # Give makefile an opportunity to rewrite itself.
75f92628 415 # reassure users that life goes on...
902aaf3e 416 my @args = ('config', @$pass_through);
417 _quote_args(\@args) if $is_VMS;
418 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
fc678412 419 }
902aaf3e 420 my @targ = ($target, @$pass_through);
421 _quote_args(\@targ) if $is_VMS;
422 print "Making $target in $ext_dir\n@run @make @targ\n";
423 my $code = system(@run, @make, @targ);
fc678412 424 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 425
fc678412 426 chdir $return_dir || die "Cannot cd to $return_dir: $!";
427}
902aaf3e 428
429sub _quote_args {
430 my $args = shift; # must be array reference
431
432 # Do not quote qualifiers that begin with '/'.
433 map { if (!/^\//) {
434 $_ =~ s/\"/""/g; # escape C<"> by doubling
435 $_ = q(").$_.q(");
436 }
437 } @{$args}
438 ;
439}
440
441sub _macroify_passthrough {
442 my $passthrough = shift;
443 _quote_args($passthrough);
444 my $macro = '/MACRO=(' . join(',',@$passthrough) . ')';
445 @$passthrough = ();
446 @$passthrough[0] = $macro;
447}