This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19
S
2use strict;
3use warnings;
b8d39eba 4use Config;
0e150199
DD
5use constant{IS_CROSS => defined $Config::Config{usecrosscompile} ? 1 : 0,
6 IS_WIN32 => $^O eq 'MSWin32',
7 IS_VMS => $^O eq 'VMS',
8 IS_UNIX => $^O ne 'MSWin32' && $^O ne 'VMS',
9};
91c30809 10
a193a2db 11my @ext_dirs = qw(cpan dist ext);
321c3589 12my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')';
8a992763 13
a0d0e21e 14# This script acts as a simple interface for building extensions.
286d62c2
NC
15
16# It's actually a cut and shut of the Unix version ext/utils/makeext and the
17# Windows version win32/build_ext.pl hence the two invocation styles.
18
8abc1060 19# On Unix, it primarily used by the perl Makefile one extension at a time:
a0d0e21e
LW
20#
21# d_dummy $(dynamic_ext): miniperl preplibrary FORCE
e2fabae1 22# @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
a0d0e21e 23#
902aaf3e 24# On Windows or VMS,
286d62c2 25# If '--static' is specified, static extensions will be built.
a34ce875
NC
26# If '--dynamic' is specified, dynamic extensions will be built.
27# If '--nonxs' is specified, nonxs extensions will be built.
8abc1060 28# If '--dynaloader' is specified, DynaLoader will be built.
286d62c2
NC
29# If '--all' is specified, all extensions will be built.
30#
31# make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
32#
33# E.g.
34#
35# make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
36#
37# make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
286d62c2
NC
38#
39# Will skip building extensions which are marked with an '!' char.
40# Mostly because they still not ported to specified platform.
41#
42# If any extensions are listed with a '+' char then only those
2effe01f 43# extensions will be built, but only if they aren't countermanded
286d62c2 44# by an '!ext' and are appropriate to the type of building being done.
58ff4bd0 45# An extensions follows the format of Foo/Bar, which would be extension Foo::Bar
286d62c2 46
2fecb8b2
KW
47# To fix dependency ordering, on *nix systems, edit Makefile.SH to create a
48# rule. That isn't sufficient for other systems; you also have to do
49# something in this file. See the code at
50# '# XXX hack for dependency # ordering'
51# below.
52#
b83fde97
KW
53# The basic logic is:
54# 1) if there's a Makefile.PL in git for the module, use it. and call make
55# 2) If not, auto-generate one (normally)
56# 3) unless the auto-generation code figures out that the extension is
57# *really* simple, in which case don't. This will be for pure perl
58# modules, and all that is needed to be done is to copy from the source
59# to the dest directories.
60#
a0d0e21e
LW
61# It may be deleted in a later release of perl so try to
62# avoid using it for other purposes.
63
63d47850 64my (%excl, %incl, %opts, @extspec, @pass_through, $verbose);
97a26ad9
NC
65
66foreach (@ARGV) {
67 if (/^!(.*)$/) {
68 $excl{$1} = 1;
69 } elsif (/^\+(.*)$/) {
70 $incl{$1} = 1;
63d47850
RU
71 } elsif (/^--verbose$/ or /^-v$/) {
72 $verbose = 1;
97a26ad9
NC
73 } elsif (/^--([\w\-]+)$/) {
74 $opts{$1} = 1;
e2fabae1 75 } elsif (/^--([\w\-]+)=(.*)$/) {
7c40aa71 76 push @{$opts{$1}}, $2;
e2fabae1 77 } elsif (/=/) {
fc678412 78 push @pass_through, $_;
ca5de986 79 } elsif (length) {
e2fabae1 80 push @extspec, $_;
97a26ad9
NC
81 }
82}
83
286d62c2
NC
84my $static = $opts{static} || $opts{all};
85my $dynamic = $opts{dynamic} || $opts{all};
a34ce875 86my $nonxs = $opts{nonxs} || $opts{all};
281da5ea 87my $dynaloader = $opts{dynaloader} || $opts{all};
286d62c2 88
ca5de986
NC
89# The Perl Makefile.SH will expand all extensions to
90# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
91# A user wishing to run make_ext might use
92# X (or X/Y or X::Y if nested)
93
94# canonise into X/Y form (pname)
95
96foreach (@extspec) {
7ad017a8 97 if (s{^lib/auto/}{}) {
ca5de986 98 # Remove lib/auto prefix and /*.* suffix
c5aac6ab 99 s{/[^/]+\.[^/]+$}{};
321c3589 100 } elsif (s{^$ext_dirs_re/}{}) {
ca5de986 101 # Remove ext/ prefix and /pm_to_blib suffix
ca5de986 102 s{/pm_to_blib$}{};
57df1c46
NC
103 # Targets are given as files on disk, but the extension spec is still
104 # written using /s for each ::
105 tr!-!/!;
7ad017a8 106 } elsif (s{::}{\/}g) {
ca5de986 107 # Convert :: to /
7ad017a8 108 } else {
ca5de986
NC
109 s/\..*o//;
110 }
111}
112
fc678412
NC
113my $makecmd = shift @pass_through; # Should be something like MAKE=make
114unshift @pass_through, 'PERL_CORE=1';
115
9afc198b 116my @dirs = @{$opts{dir} || \@ext_dirs};
7c40aa71 117my $target = $opts{target}[0];
07f3cc2a 118$target = 'all' unless defined $target;
a0d0e21e 119
fb73857a 120# Previously, $make was taken from config.sh. However, the user might
121# instead be running a possibly incompatible make. This might happen if
122# the user types "gmake" instead of a plain "make", for example. The
123# correct current value of MAKE will come through from the main perl
124# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
125# case third party users of this script (are there any?) don't have the
126# MAKE=$(MAKE) argument, which was added after 5.004_03.
ca5de986
NC
127unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
128 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
a2f19a19
S
129}
130
eb1f8df7
NC
131# This isn't going to cope with anything fancy, such as spaces inside command
132# names, but neither did what it replaced. Once there is a use case that needs
133# it, please supply patches. Until then, I'm sticking to KISS
134my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
a2f19a19 135
a0d0e21e 136
07f3cc2a 137if ($target eq '') {
ca5de986 138 die "make_ext: no make target specified (eg all or clean)\n";
3fbff3ec 139} elsif ($target !~ /^(?:all|clean|distclean|realclean|veryclean)$/) {
f6dd0e4f
NC
140 # we are strict about what make_ext is used for because we emulate these
141 # targets for simple modules:
ca5de986 142 die "$0: unknown make target '$target'\n";
a2f19a19
S
143}
144
281da5ea 145if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
286d62c2
NC
146 die "$0: no extension specified\n";
147}
148
149my $perl;
150my %extra_passthrough;
151
0e150199 152if (IS_WIN32) {
14636674
NC
153 require Cwd;
154 require FindExt;
155 my $build = Cwd::getcwd();
286d62c2
NC
156 $perl = $^X;
157 if ($perl =~ m#^\.\.#) {
bc3847b4
NC
158 my $here = $build;
159 $here =~ s{/}{\\}g;
286d62c2
NC
160 $perl = "$here\\$perl";
161 }
162 (my $topdir = $perl) =~ s/\\[^\\]+$//;
163 # miniperl needs to find perlglob and pl2bat
164 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
165 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
166 unless (-f "$pl2bat.bat") {
88e7da03 167 my @args = ($perl, "-I$topdir\\lib", "-I$topdir\\cpan\\ExtUtils-PL2Bat\\lib", ("$pl2bat.pl") x 2);
63d47850 168 print "@args\n" if $verbose;
27329181 169 system(@args) unless IS_CROSS;
286d62c2
NC
170 }
171
63d47850 172 print "In $build" if $verbose;
7c40aa71 173 foreach my $dir (@dirs) {
a2ac9a71 174 chdir($dir) or die "Cannot cd to $dir: $!\n";
14636674 175 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
7c40aa71
NC
176 FindExt::scan_ext($ext);
177 FindExt::set_static_extensions(split ' ', $Config{static_ext});
a85e0e8c
SH
178 chdir $build
179 or die "Couldn't chdir to '$build': $!"; # restore our start directory
180 }
7c40aa71 181
a85e0e8c
SH
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;
7c40aa71 187
a85e0e8c
SH
188 foreach (sort @ext) {
189 if (%incl and !exists $incl{$_}) {
190 #warn "Skipping extension $_, not in inclusion list\n";
191 next;
192 }
193 if (exists $excl{$_}) {
194 warn "Skipping extension $_, not ported to current platform";
195 next;
196 }
197 push @extspec, $_;
1ea1c22c 198 if($_ ne 'DynaLoader' && FindExt::is_static($_)) {
a85e0e8c 199 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
286d62c2
NC
200 }
201 }
a85e0e8c 202
a2ac9a71 203 chdir '..'
a85e0e8c 204 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
286d62c2 205}
0e150199 206elsif (IS_VMS) {
902aaf3e
CB
207 $perl = $^X;
208 push @extspec, (split ' ', $Config{static_ext}) if $static;
209 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
a34ce875 210 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
281da5ea 211 push @extspec, 'DynaLoader' if $dynaloader;
902aaf3e 212}
286d62c2 213
2fecb8b2 214{ # XXX hack for dependency ordering
dc0655f7 215 # Cwd needs to be built before Encode recurses into subdirectories.
926760a6 216 # Pod::Simple needs to be built before Pod::Functions, but after 'if'
dd3e0aed 217 # lib needs to be built before IO-Compress
dc0655f7 218 # This seems to be the simplest way to ensure this ordering:
926760a6 219 my (@first, @second, @other);
dc0655f7 220 foreach (@extspec) {
926760a6 221 if ($_ eq 'Cwd' || $_ eq 'if' || $_ eq 'lib') {
dc0655f7 222 push @first, $_;
926760a6
KW
223 }
224 elsif ($_ eq 'Pod/Simple') {
225 push @second, $_;
dc0655f7
NC
226 } else {
227 push @other, $_;
228 }
229 }
926760a6 230 @extspec = (@first, @second, @other);
dc0655f7
NC
231}
232
91c30809
NC
233if ($Config{osname} eq 'catamount' and @extspec) {
234 # Snowball's chance of building extensions.
235 die "This is $Config{osname}, not building $extspec[0], sorry.\n";
236}
31221351 237$ENV{PERL_CORE} = 1;
91c30809 238
e08c66ce
NC
239foreach my $spec (@extspec) {
240 my $mname = $spec;
ca5de986 241 $mname =~ s!/!::!g;
238a6851 242 my $ext_pathname;
a28b98e8
NC
243
244 # Try new style ext/Data-Dumper/ first
245 my $copy = $spec;
246 $copy =~ tr!/!-!;
cb8c8458
SH
247
248 # List/Util.xs lives in Scalar-List-Utils, Cwd.xs lives in PathTools
249 $copy = 'Scalar-List-Utils' if $copy eq 'List-Util';
250 $copy = 'PathTools' if $copy eq 'Cwd';
251
a28b98e8
NC
252 foreach my $dir (@ext_dirs) {
253 if (-d "$dir/$copy") {
254 $ext_pathname = "$dir/$copy";
255 last;
8a992763 256 }
a28b98e8
NC
257 }
258
259 if (!defined $ext_pathname) {
260 if (-d "ext/$spec") {
261 # Old style ext/Data/Dumper/
262 $ext_pathname = "ext/$spec";
263 } else {
8a992763
NC
264 warn "Can't find extension $spec in any of @ext_dirs";
265 next;
266 }
238a6851 267 }
a2f19a19 268
63d47850 269 print "\tMaking $mname ($target)\n" if $verbose;
a2f19a19 270
f6dd0e4f 271 build_extension($ext_pathname, $perl, $mname, $target,
e08c66ce 272 [@pass_through, @{$extra_passthrough{$spec} || []}]);
ca5de986 273}
a0d0e21e 274
fc678412 275sub build_extension {
f6dd0e4f 276 my ($ext_dir, $perl, $mname, $target, $pass_through) = @_;
acb65a20 277
a85e0e8c
SH
278 unless (chdir "$ext_dir") {
279 warn "Cannot cd to $ext_dir: $!";
280 return;
281 }
282
acb65a20
NC
283 my $up = $ext_dir;
284 $up =~ s![^/]+!..!g;
285
286 $perl ||= "$up/miniperl";
287 my $return_dir = $up;
288 my $lib_dir = "$up/lib";
289
0e150199
DD
290 my ($makefile, $makefile_no_minus_f);
291 if (IS_VMS) {
902aaf3e
CB
292 $makefile = 'descrip.mms';
293 if ($target =~ /clean$/
294 && !-f $makefile
295 && -f "${makefile}_old") {
296 $makefile = "${makefile}_old";
297 }
298 } else {
299 $makefile = 'Makefile';
300 }
fc678412 301
baff067e 302 if (-f $makefile) {
0e150199 303 $makefile_no_minus_f = 0;
1ae6ead9 304 open my $mfh, '<', $makefile or die "Cannot open $makefile: $!";
baff067e
FC
305 while (<$mfh>) {
306 # Plagiarised from CPAN::Distribution
307 last if /MakeMaker post_initialize section/;
308 next unless /^#\s+VERSION_FROM\s+=>\s+(.+)/;
309 my $vmod = eval $1;
310 my $oldv;
311 while (<$mfh>) {
312 next unless /^XS_VERSION = (\S+)/;
313 $oldv = $1;
314 last;
315 }
316 last unless defined $oldv;
317 require ExtUtils::MM_Unix;
318 defined (my $newv = parse_version MM $vmod) or last;
301bcfda 319 if (version->parse($newv) ne $oldv) {
27329181
DD
320 close $mfh or die "close $makefile: $!";
321 _unlink($makefile);
322 {
323 no warnings 'deprecated';
324 goto NO_MAKEFILE;
325 }
baff067e
FC
326 }
327 }
4b226208
BF
328
329 if (IS_CROSS) {
330 # If we're cross-compiling, it's possible that the host's
331 # Makefiles are around.
332 seek($mfh, 0, 0) or die "Cannot seek $makefile: $!";
333
334 my $cross_makefile;
335 while (<$mfh>) {
336 # XXX This might not be throughout enough.
337 # For example, it's possible to cause a false-positive
338 # if cross compiling on and for the Raspberry Pi,
339 # which is insane but plausible.
340 # False positives are really not troublesome, though;
341 # all they mean is that the module gets rebuilt.
342 if (/^CC = \Q$Config{cc}\E/) {
343 $cross_makefile = 1;
344 last;
345 }
346 }
347
348 if (!$cross_makefile) {
349 print "Deleting non-Cross makefile\n";
350 close $mfh or die "close $makefile: $!";
351 _unlink($makefile);
352 }
353 }
0e150199
DD
354 } else {
355 $makefile_no_minus_f = 1;
baff067e
FC
356 }
357
0e150199 358 if ($makefile_no_minus_f || !-f $makefile) {
27329181 359 NO_MAKEFILE:
e74f76b2 360 if (!-f 'Makefile.PL') {
d527ad5d 361 unless (just_pm_to_blib($target, $ext_dir, $mname, $return_dir)) {
f6dd0e4f
NC
362 # No problems returned, so it has faked everything for us. :-)
363 chdir $return_dir || die "Cannot cd to $return_dir: $!";
364 return;
365 }
366
63d47850 367 print "\nCreating Makefile.PL in $ext_dir for $mname\n" if $verbose;
d3bc6aa6 368 my ($fromname, $key, $value);
e74f76b2 369
d42e1eb7
CB
370 $key = 'ABSTRACT_FROM';
371 # We need to cope well with various possible layouts
372 my @dirs = split /::/, $mname;
373 my $leaf = pop @dirs;
374 my $leafname = "$leaf.pm";
375 my $pathname = join '/', @dirs, $leafname;
376 my @locations = ($leafname, $pathname, "lib/$pathname");
377 foreach (@locations) {
378 if (-f $_) {
379 $fromname = $_;
380 last;
d3bc6aa6 381 }
d42e1eb7
CB
382 }
383
384 unless ($fromname) {
385 die "For $mname tried @locations in $ext_dir but can't find source";
386 }
387 ($value = $fromname) =~ s/\.pm\z/.pod/;
388 $value = $fromname unless -e $value;
f689ad37
DM
389
390 if ($mname eq 'Pod::Checker') {
391 # the abstract in the .pm file is unparseable by MM,
392 # so special-case it. We can't use the package's own
393 # Makefile.PL, as it doesn't handle the executable scripts
394 # right.
395 $key = 'ABSTRACT';
396 # this is copied from the CPAN Makefile.PL v 1.171
397 $value = 'Pod::Checker verifies POD documentation contents for compliance with the POD format specifications';
398 }
399
e74f76b2
NC
400 open my $fh, '>', 'Makefile.PL'
401 or die "Can't open Makefile.PL for writing: $!";
d3bc6aa6 402 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
e74f76b2
NC
403#-*- buffer-read-only: t -*-
404
13b5e8d8 405# This Makefile.PL was written by %s.
e74f76b2
NC
406# It will be deleted automatically by make realclean
407
408use strict;
409use ExtUtils::MakeMaker;
410
13b5e8d8
NC
411# This is what the .PL extracts to. Not the ultimate file that is installed.
412# (ie Win32 runs pl2bat after this)
413
414# Doing this here avoids all sort of quoting issues that would come from
415# attempting to write out perl source with literals to generate the arrays and
416# hash.
417my @temps = 'Makefile.PL';
418foreach (glob('scripts/pod*.PL')) {
8abc1060 419 # The various pod*.PL extractors change directory. Doing that with relative
13b5e8d8
NC
420 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
421 # the chdir doing anything, than to attempt to convert lib paths to
422 # absolute, and potentially run into problems with quoting special
423 # characters in the path to our build dir (such as spaces)
424 require File::Copy;
425
426 my $temp = $_;
427 $temp =~ s!scripts/!!;
428 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
429 push @temps, $temp;
430}
431
432my $script_ext = $^O eq 'VMS' ? '.com' : '';
433my %%pod_scripts;
434foreach (glob('pod*.PL')) {
435 my $script = $_;
9ed9b795 436 s/.PL$/$script_ext/i;
13b5e8d8
NC
437 $pod_scripts{$script} = $_;
438}
439my @exe_files = values %%pod_scripts;
440
e74f76b2 441WriteMakefile(
13b5e8d8
NC
442 NAME => '%s',
443 VERSION_FROM => '%s',
d3bc6aa6 444 %-13s => '%s',
13b5e8d8
NC
445 realclean => { FILES => "@temps" },
446 (%%pod_scripts ? (
447 PL_FILES => \%%pod_scripts,
448 EXE_FILES => \@exe_files,
449 clean => { FILES => "@exe_files" },
450 ) : ()),
e74f76b2
NC
451);
452
453# ex: set ro:
454EOM
455 close $fh or die "Can't close Makefile.PL: $!";
43f197b5
NC
456 # As described in commit 23525070d6c0e51f:
457 # Push the atime and mtime of generated Makefile.PLs back 4
458 # seconds. In certain circumstances ( on virtual machines ) the
459 # generated Makefile.PL can produce a Makefile that is older than
460 # the Makefile.PL. Altering the atime and mtime backwards by 4
461 # seconds seems to resolve the issue.
462 eval {
648eb45f
AH
463 my $ftime = (stat('Makefile.PL'))[9] - 4;
464 utime $ftime, $ftime, 'Makefile.PL';
43f197b5 465 };
21fb4fa0
NC
466 } elsif ($mname =~ /\A(?:Carp
467 |ExtUtils::CBuilder
468 |Safe
469 |Search::Dict)\z/x) {
470 # An explicit list of dual-life extensions that have a Makefile.PL
471 # for CPAN, but we have verified can also be built using the fakery.
d527ad5d 472 my ($problem) = just_pm_to_blib($target, $ext_dir, $mname, $return_dir);
21fb4fa0
NC
473 # We really need to sanity test that we can fake it.
474 # Otherwise "skips" will go undetected, and the build slow down for
475 # everyone, defeating the purpose.
476 if (defined $problem) {
477 if (-d "$return_dir/.git") {
478 # Get the list of files that git isn't ignoring:
479 my @files = `git ls-files --cached --others --exclude-standard 2>/dev/null`;
480 # on error (eg no git) we get nothing, but that's not a
481 # problem. The goal is to see if git thinks that the problem
482 # file is interesting, by getting a positive match with
483 # something git told us about, and if so bail out:
484 foreach (@files) {
485 chomp;
486 # We really need to sanity test that we can fake it.
487 # The intent is that this should only fail because
488 # you've just added a file to the dual-life dist that
489 # we can't handle. In which case you should either
490 # 1) remove the dist from the regex a few lines above.
491 # or
492 # 2) add the file to regex of "safe" filenames earlier
493 # in this function, that starts with ChangeLog
494 die "FATAL - $0 has $mname in the list of simple extensions, but it now contains file '$problem' which we can't handle"
495 if $problem eq $_;
496 }
497 # There's an unexpected file, but it seems to be something
498 # that git will ignore. So fall through to the regular
499 # Makefile.PL handling code below, on the assumption that
500 # we won't get here for a clean build.
501 }
502 warn "WARNING - $0 is building $mname using EU::MM, as it found file '$problem'";
503 } else {
504 # It faked everything for us.
505 chdir $return_dir || die "Cannot cd to $return_dir: $!";
506 return;
507 }
e74f76b2 508 }
21fb4fa0
NC
509
510 # We are going to have to use Makefile.PL:
63d47850 511 print "\nRunning Makefile.PL in $ext_dir\n" if $verbose;
fc678412 512
4b226208 513 my @args = ("-I$lib_dir", 'Makefile.PL');
0e150199 514 if (IS_VMS) {
902aaf3e
CB
515 my $libd = VMS::Filespec::vmspath($lib_dir);
516 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
517 } else {
a2b175af
NC
518 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
519 'INSTALLMAN3DIR=none';
902aaf3e
CB
520 }
521 push @args, @$pass_through;
85e384a6 522 push @args, 'PERL=' . $perl if $perl; # use miniperl to run the Makefile later
0e150199 523 _quote_args(\@args) if IS_VMS;
63d47850 524 print join(' ', $perl, @args), "\n" if $verbose;
cbf3d0f6
FC
525 my $code = do {
526 local $ENV{PERL_MM_USE_DEFAULT} = 1;
527 system $perl, @args;
528 };
b5f7915c
DD
529 if($code != 0){
530 #make sure next build attempt/run of make_ext.pl doesn't succeed
531 _unlink($makefile);
532 die "Unsuccessful Makefile.PL($ext_dir): code=$code";
533 }
fc678412 534
61edc683
NC
535 # Right. The reason for this little hack is that we're sitting inside
536 # a program run by ./miniperl, but there are tasks we need to perform
537 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
538 # Unfortunately, they can be run *after* 'clean', which deletes
539 # ./miniperl
540 # So we do our best to leave a set of instructions identical to what
541 # we would do if we are run directly as 'realclean' etc
542 # Whilst we're perfect, unfortunately the targets we call are not, as
543 # some of them rely on a $(PERL) for their own distclean targets.
544 # But this always used to be a problem with the old /bin/sh version of
545 # this.
0e150199 546 if (IS_UNIX) {
fc678412 547 foreach my $clean_target ('realclean', 'veryclean') {
a5bd6593 548 fallback_cleanup($return_dir, $clean_target, <<"EOS");
fc678412
NC
549cd $ext_dir
550if test ! -f Makefile -a -f Makefile.old; then
61edc683 551 echo "Note: Using Makefile.old"
eb1f8df7 552 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 553else
fc678412 554 if test ! -f Makefile ; then
61edc683
NC
555 echo "Warning: No Makefile!"
556 fi
62fdd72a 557 @make $clean_target MAKE='@make' @pass_through
61edc683 558fi
fc678412 559cd $return_dir
61edc683 560EOS
fc678412 561 }
61edc683 562 }
fc678412 563 }
a2f19a19 564
902aaf3e 565 if (not -f $makefile) {
a2f19a19 566 print "Warning: No Makefile!\n";
fc678412 567 }
a2f19a19 568
0e150199 569 if (IS_VMS) {
e0697720
NC
570 _quote_args($pass_through);
571 @$pass_through = (
572 "/DESCRIPTION=$makefile",
573 '/MACRO=(' . join(',',@$pass_through) . ')'
574 );
902aaf3e
CB
575 }
576
902aaf3e 577 my @targ = ($target, @$pass_through);
63d47850 578 print "Making $target in $ext_dir\n@make @targ\n" if $verbose;
d316fef4 579 local $ENV{PERL_INSTALL_QUIET} = 1;
d708ee4b 580 my $code = system(@make, @targ);
056c263b
DD
581 if($code >> 8 != 0){ # probably cleaned itself, try again once more time
582 $code = system(@make, @targ);
583 }
fc678412 584 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 585
fc678412
NC
586 chdir $return_dir || die "Cannot cd to $return_dir: $!";
587}
902aaf3e
CB
588
589sub _quote_args {
590 my $args = shift; # must be array reference
591
592 # Do not quote qualifiers that begin with '/'.
593 map { if (!/^\//) {
594 $_ =~ s/\"/""/g; # escape C<"> by doubling
595 $_ = q(").$_.q(");
596 }
597 } @{$args}
598 ;
599}
27329181
DD
600
601#guarentee that a file is deleted or die, void _unlink($filename)
602#xxx replace with _unlink_or_rename from EU::Install?
603sub _unlink {
604 1 while unlink $_[0];
605 my $err = $!;
606 die "Can't unlink $_[0]: $err" if -f $_[0];
607}
a5bd6593 608
f6dd0e4f
NC
609# Figure out if this extension is simple enough that it would only use
610# ExtUtils::MakeMaker's pm_to_blib target. If we're confident that it would,
611# then do all the work ourselves (returning an empty list), else return the
612# name of a file that we identified as beyond our ability to handle.
613#
614# While this is clearly quite a bit more work than just letting
615# ExtUtils::MakeMaker do it, and effectively is some code duplication, the time
616# savings are impressive.
617
618sub just_pm_to_blib {
d527ad5d 619 my ($target, $ext_dir, $mname, $return_dir) = @_;
2ce0aa69 620 my ($has_lib, $has_top, $has_topdir);
1804eb61 621 my ($last) = $mname =~ /([^:]+)$/;
2ce0aa69 622 my ($first) = $mname =~ /^([^:]+)/;
1804eb61 623
0e150199 624 my $pm_to_blib = IS_VMS ? 'pm_to_blib.ts' : 'pm_to_blib';
8f8d807b 625 my $silent = defined $ENV{MAKEFLAGS} && $ENV{MAKEFLAGS} =~ /\b(s|silent|quiet)\b/;
ad124136 626
f6dd0e4f
NC
627 foreach my $leaf (<*>) {
628 if (-d $leaf) {
ad124136 629 $leaf =~ s/\.DIR\z//i
0e150199 630 if IS_VMS;
f6dd0e4f
NC
631 next if $leaf =~ /\A(?:\.|\.\.|t|demo)\z/;
632 if ($leaf eq 'lib') {
633 ++$has_lib;
634 next;
635 }
2ce0aa69
NC
636 if ($leaf eq $first) {
637 ++$has_topdir;
638 next;
639 }
f6dd0e4f
NC
640 }
641 return $leaf
642 unless -f _;
ad124136 643 $leaf =~ s/\.\z//
0e150199 644 if IS_VMS;
f6dd0e4f
NC
645 # Makefile.PL is "safe" to ignore because we will only be called for
646 # directories that hold a Makefile.PL if they are in the exception list.
647 next
648 if $leaf =~ /\A(ChangeLog
649 |Changes
650 |LICENSE
651 |Makefile\.PL
652 |MANIFEST
653 |META\.yml
ad124136 654 |\Q$pm_to_blib\E
f6dd0e4f
NC
655 |README
656 |README\.patching
657 |README\.release
4a573798 658 |\.gitignore
f6dd0e4f 659 )\z/xi; # /i to deal with case munging systems.
1804eb61
NC
660 if ($leaf eq "$last.pm") {
661 ++$has_top;
662 next;
663 }
f6dd0e4f
NC
664 return $leaf;
665 }
666 return 'no lib/'
1804eb61 667 unless $has_lib || $has_top;
2ce0aa69
NC
668 die "Inconsistent module $mname has both lib/ and $first/"
669 if $has_lib && $has_topdir;
f6dd0e4f 670
5db46185 671 print "Running pm_to_blib for $ext_dir directly\n"
b4c079ca 672 unless $silent;
f6dd0e4f 673
1804eb61
NC
674 my %pm;
675 if ($has_top) {
676 my $to = $mname =~ s!::!/!gr;
677 $pm{"$last.pm"} = "../../lib/$to.pm";
678 }
2ce0aa69 679 if ($has_lib || $has_topdir) {
1804eb61
NC
680 # strictly ExtUtils::MakeMaker uses the pm_to_blib target to install
681 # .pm, pod and .pl files. We're just going to do it for .pm and .pod
682 # files, to avoid problems on case munging file systems. Specifically,
683 # _pm.PL which ExtUtils::MakeMaker should run munges to _PM.PL, and
684 # looks a lot like a regular foo.pl (ie FOO.PL)
685 my @found;
686 require File::Find;
687 unless (eval {
688 File::Find::find({
689 no_chdir => 1,
690 wanted => sub {
691 return if -d $_;
692 # Bail out immediately with the problem file:
693 die \$_
694 unless -f _;
695 die \$_
696 unless /\A[^.]+\.(?:pm|pod)\z/i;
697 push @found, $_;
f6dd0e4f 698 }
2ce0aa69 699 }, $has_lib ? 'lib' : $first);
1804eb61
NC
700 1;
701 }) {
702 # Problem files aren't really errors:
703 return ${$@}
704 if ref $@ eq 'SCALAR';
705 # But anything else is:
706 die $@;
707 }
2ce0aa69
NC
708 if ($has_lib) {
709 $pm{$_} = "../../$_"
710 foreach @found;
711 } else {
712 $pm{$_} = "../../lib/$_"
713 foreach @found;
1804eb61 714 }
f6dd0e4f
NC
715 }
716 # This is running under miniperl, so no autodie
717 if ($target eq 'all') {
31b6f23e
TC
718 my $need_update = 1;
719 if (-f $pm_to_blib) {
720 # avoid touching pm_to_blib unless there's something that
721 # needs updating, see #126710
722 $need_update = 0;
723 my $test_at = -M _;
724 while (my $from = each(%pm)) {
725 if (-M $from < $test_at) {
726 ++$need_update;
727 last;
728 }
729 }
730 keys %pm; # reset iterator
731 }
732
733 if ($need_update) {
734 local $ENV{PERL_INSTALL_QUIET} = 1;
735 require ExtUtils::Install;
736 ExtUtils::Install::pm_to_blib(\%pm, '../../lib/auto');
737 open my $fh, '>', $pm_to_blib
738 or die "Can't open '$pm_to_blib': $!";
739 print $fh "$0 has handled pm_to_blib directly\n";
740 close $fh
741 or die "Can't close '$pm_to_blib': $!";
742 if (IS_UNIX) {
743 # Fake the fallback cleanup
744 my $fallback
745 = join '', map {s!^\.\./\.\./!!; "rm -f $_\n"} sort values %pm;
746 foreach my $clean_target ('realclean', 'veryclean') {
747 fallback_cleanup($return_dir, $clean_target, $fallback);
748 }
d527ad5d
NC
749 }
750 }
f6dd0e4f
NC
751 } else {
752 # A clean target.
753 # For now, make the targets behave the same way as ExtUtils::MakeMaker
754 # does
ad124136 755 _unlink($pm_to_blib);
f6dd0e4f
NC
756 unless ($target eq 'clean') {
757 # but cheat a bit, by relying on the top level Makefile clean target
758 # to take out our directory lib/auto/...
759 # (which it has to deal with, as cpan/foo/bar creates
760 # lib/auto/foo/bar, but the EU::MM rule will only
761 # rmdir lib/auto/foo/bar, leaving lib/auto/foo
9fa5aa91 762 _unlink($_)
1804eb61 763 foreach sort values %pm;
f6dd0e4f
NC
764 }
765 }
766 return;
767}
768
a5bd6593
NC
769sub fallback_cleanup {
770 my ($dir, $clean_target, $contents) = @_;
771 my $file = "$dir/$clean_target.sh";
772 open my $fh, '>>', $file or die "open $file: $!";
773 # Quite possible that we're being run in parallel here.
774 # Can't use Fcntl this early to get the LOCK_EX
387eee9b 775 flock $fh, 2 or warn "flock $file: $!";
a5bd6593
NC
776 print $fh $contents or die "print $file: $!";
777 close $fh or die "close $file: $!";
778}