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