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