This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make_ext.pl can also handle extensions with a module file at the top level.
[perl5.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19
SM
2use strict;
3use warnings;
b8d39eba 4use Config;
4b226208 5use constant IS_CROSS => defined $Config::Config{usecrosscompile} ? 1 : 0;
75f92628 6
91c30809
NC
7my $is_Win32 = $^O eq 'MSWin32';
8my $is_VMS = $^O eq 'VMS';
9my $is_Unix = !$is_Win32 && !$is_VMS;
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
fc678412 54my (%excl, %incl, %opts, @extspec, @pass_through);
97a26ad9
NC
55
56foreach (@ARGV) {
57 if (/^!(.*)$/) {
58 $excl{$1} = 1;
59 } elsif (/^\+(.*)$/) {
60 $incl{$1} = 1;
61 } elsif (/^--([\w\-]+)$/) {
62 $opts{$1} = 1;
e2fabae1 63 } elsif (/^--([\w\-]+)=(.*)$/) {
7c40aa71 64 push @{$opts{$1}}, $2;
e2fabae1 65 } elsif (/=/) {
fc678412 66 push @pass_through, $_;
ca5de986 67 } elsif (length) {
e2fabae1 68 push @extspec, $_;
97a26ad9
NC
69 }
70}
71
286d62c2
NC
72my $static = $opts{static} || $opts{all};
73my $dynamic = $opts{dynamic} || $opts{all};
a34ce875 74my $nonxs = $opts{nonxs} || $opts{all};
281da5ea 75my $dynaloader = $opts{dynaloader} || $opts{all};
286d62c2 76
ca5de986
NC
77# The Perl Makefile.SH will expand all extensions to
78# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
79# A user wishing to run make_ext might use
80# X (or X/Y or X::Y if nested)
81
82# canonise into X/Y form (pname)
83
84foreach (@extspec) {
7ad017a8 85 if (s{^lib/auto/}{}) {
ca5de986 86 # Remove lib/auto prefix and /*.* suffix
c5aac6ab 87 s{/[^/]+\.[^/]+$}{};
321c3589 88 } elsif (s{^$ext_dirs_re/}{}) {
ca5de986 89 # Remove ext/ prefix and /pm_to_blib suffix
ca5de986 90 s{/pm_to_blib$}{};
57df1c46
NC
91 # Targets are given as files on disk, but the extension spec is still
92 # written using /s for each ::
93 tr!-!/!;
7ad017a8 94 } elsif (s{::}{\/}g) {
ca5de986 95 # Convert :: to /
7ad017a8 96 } else {
ca5de986
NC
97 s/\..*o//;
98 }
99}
100
fc678412
NC
101my $makecmd = shift @pass_through; # Should be something like MAKE=make
102unshift @pass_through, 'PERL_CORE=1';
103
9afc198b 104my @dirs = @{$opts{dir} || \@ext_dirs};
7c40aa71 105my $target = $opts{target}[0];
07f3cc2a 106$target = 'all' unless defined $target;
a0d0e21e 107
fb73857a
PP
108# Previously, $make was taken from config.sh. However, the user might
109# instead be running a possibly incompatible make. This might happen if
110# the user types "gmake" instead of a plain "make", for example. The
111# correct current value of MAKE will come through from the main perl
112# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
113# case third party users of this script (are there any?) don't have the
114# MAKE=$(MAKE) argument, which was added after 5.004_03.
ca5de986
NC
115unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
116 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
a2f19a19
SM
117}
118
eb1f8df7
NC
119# This isn't going to cope with anything fancy, such as spaces inside command
120# names, but neither did what it replaced. Once there is a use case that needs
121# it, please supply patches. Until then, I'm sticking to KISS
122my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
a2f19a19 123
a0d0e21e 124
07f3cc2a 125if ($target eq '') {
ca5de986 126 die "make_ext: no make target specified (eg all or clean)\n";
3fbff3ec 127} elsif ($target !~ /^(?:all|clean|distclean|realclean|veryclean)$/) {
f6dd0e4f
NC
128 # we are strict about what make_ext is used for because we emulate these
129 # targets for simple modules:
ca5de986 130 die "$0: unknown make target '$target'\n";
a2f19a19
SM
131}
132
281da5ea 133if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
286d62c2
NC
134 die "$0: no extension specified\n";
135}
136
137my $perl;
138my %extra_passthrough;
139
140if ($is_Win32) {
14636674
NC
141 require Cwd;
142 require FindExt;
143 my $build = Cwd::getcwd();
286d62c2
NC
144 $perl = $^X;
145 if ($perl =~ m#^\.\.#) {
bc3847b4
NC
146 my $here = $build;
147 $here =~ s{/}{\\}g;
286d62c2
NC
148 $perl = "$here\\$perl";
149 }
150 (my $topdir = $perl) =~ s/\\[^\\]+$//;
151 # miniperl needs to find perlglob and pl2bat
152 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
153 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
154 unless (-f "$pl2bat.bat") {
d7f84378 155 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
286d62c2 156 print "@args\n";
27329181 157 system(@args) unless IS_CROSS;
286d62c2
NC
158 }
159
a2ac9a71 160 print "In $build";
7c40aa71 161 foreach my $dir (@dirs) {
a2ac9a71 162 chdir($dir) or die "Cannot cd to $dir: $!\n";
14636674 163 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
7c40aa71
NC
164 FindExt::scan_ext($ext);
165 FindExt::set_static_extensions(split ' ', $Config{static_ext});
a85e0e8c
SH
166 chdir $build
167 or die "Couldn't chdir to '$build': $!"; # restore our start directory
168 }
7c40aa71 169
a85e0e8c
SH
170 my @ext;
171 push @ext, FindExt::static_ext() if $static;
172 push @ext, FindExt::dynamic_ext() if $dynamic;
173 push @ext, FindExt::nonxs_ext() if $nonxs;
174 push @ext, 'DynaLoader' if $dynaloader;
7c40aa71 175
a85e0e8c
SH
176 foreach (sort @ext) {
177 if (%incl and !exists $incl{$_}) {
178 #warn "Skipping extension $_, not in inclusion list\n";
179 next;
180 }
181 if (exists $excl{$_}) {
182 warn "Skipping extension $_, not ported to current platform";
183 next;
184 }
185 push @extspec, $_;
186 if($_ eq 'DynaLoader' and $target !~ /clean$/) {
187 # No, we don't know why nmake can't work out the dependency chain
188 push @{$extra_passthrough{$_}}, 'DynaLoader.c';
189 } elsif(FindExt::is_static($_)) {
190 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
286d62c2
NC
191 }
192 }
a85e0e8c 193
a2ac9a71 194 chdir '..'
a85e0e8c 195 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
286d62c2 196}
902aaf3e
CB
197elsif ($is_VMS) {
198 $perl = $^X;
199 push @extspec, (split ' ', $Config{static_ext}) if $static;
200 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
a34ce875 201 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
281da5ea 202 push @extspec, 'DynaLoader' if $dynaloader;
902aaf3e 203}
286d62c2 204
dc0655f7
NC
205{
206 # Cwd needs to be built before Encode recurses into subdirectories.
1179df5f 207 # Pod::Simple needs to be built before Pod::Functions
dc0655f7
NC
208 # This seems to be the simplest way to ensure this ordering:
209 my (@first, @other);
210 foreach (@extspec) {
1179df5f 211 if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') {
dc0655f7
NC
212 push @first, $_;
213 } else {
214 push @other, $_;
215 }
216 }
217 @extspec = (@first, @other);
218}
219
91c30809
NC
220if ($Config{osname} eq 'catamount' and @extspec) {
221 # Snowball's chance of building extensions.
222 die "This is $Config{osname}, not building $extspec[0], sorry.\n";
223}
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
ca5de986 255 print "\tMaking $mname ($target)\n";
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";
6c8b0117 275 $ENV{PERL_CORE} = 1;
acb65a20 276
902aaf3e
CB
277 my $makefile;
278 if ($is_VMS) {
279 $makefile = 'descrip.mms';
280 if ($target =~ /clean$/
281 && !-f $makefile
282 && -f "${makefile}_old") {
283 $makefile = "${makefile}_old";
284 }
285 } else {
286 $makefile = 'Makefile';
287 }
fc678412 288
baff067e
FC
289 if (-f $makefile) {
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 }
baff067e
FC
340 }
341
902aaf3e 342 if (!-f $makefile) {
27329181 343 NO_MAKEFILE:
e74f76b2 344 if (!-f 'Makefile.PL') {
1804eb61 345 unless (just_pm_to_blib($target, $ext_dir, $mname)) {
f6dd0e4f
NC
346 # No problems returned, so it has faked everything for us. :-)
347 chdir $return_dir || die "Cannot cd to $return_dir: $!";
348 return;
349 }
350
e74f76b2 351 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
d3bc6aa6
NC
352 my ($fromname, $key, $value);
353 if ($mname eq 'podlators') {
354 # We need to special case this somewhere, and this is fewer
355 # lines of code than a core-only Makefile.PL, and no more
356 # complex
357 $fromname = 'VERSION';
358 $key = 'DISTNAME';
359 $value = 'podlators';
360 $mname = 'Pod';
361 } else {
362 $key = 'ABSTRACT_FROM';
363 # We need to cope well with various possible layouts
364 my @dirs = split /::/, $mname;
365 my $leaf = pop @dirs;
366 my $leafname = "$leaf.pm";
367 my $pathname = join '/', @dirs, $leafname;
368 my @locations = ($leafname, $pathname, "lib/$pathname");
f6d098c6 369 unshift @locations, 'lib/IO/Compress/Base.pm' if $mname eq 'IO::Compress';
d3bc6aa6
NC
370 foreach (@locations) {
371 if (-f $_) {
372 $fromname = $_;
373 last;
374 }
e74f76b2 375 }
e74f76b2 376
d3bc6aa6
NC
377 unless ($fromname) {
378 die "For $mname tried @locations in in $ext_dir but can't find source";
379 }
380 ($value = $fromname) =~ s/\.pm\z/.pod/;
381 $value = $fromname unless -e $value;
e74f76b2
NC
382 }
383 open my $fh, '>', 'Makefile.PL'
384 or die "Can't open Makefile.PL for writing: $!";
d3bc6aa6 385 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
e74f76b2
NC
386#-*- buffer-read-only: t -*-
387
13b5e8d8 388# This Makefile.PL was written by %s.
e74f76b2
NC
389# It will be deleted automatically by make realclean
390
391use strict;
392use ExtUtils::MakeMaker;
393
13b5e8d8
NC
394# This is what the .PL extracts to. Not the ultimate file that is installed.
395# (ie Win32 runs pl2bat after this)
396
397# Doing this here avoids all sort of quoting issues that would come from
398# attempting to write out perl source with literals to generate the arrays and
399# hash.
400my @temps = 'Makefile.PL';
401foreach (glob('scripts/pod*.PL')) {
8abc1060 402 # The various pod*.PL extractors change directory. Doing that with relative
13b5e8d8
NC
403 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
404 # the chdir doing anything, than to attempt to convert lib paths to
405 # absolute, and potentially run into problems with quoting special
406 # characters in the path to our build dir (such as spaces)
407 require File::Copy;
408
409 my $temp = $_;
410 $temp =~ s!scripts/!!;
411 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
412 push @temps, $temp;
413}
414
415my $script_ext = $^O eq 'VMS' ? '.com' : '';
416my %%pod_scripts;
417foreach (glob('pod*.PL')) {
418 my $script = $_;
9ed9b795 419 s/.PL$/$script_ext/i;
13b5e8d8
NC
420 $pod_scripts{$script} = $_;
421}
422my @exe_files = values %%pod_scripts;
423
e74f76b2 424WriteMakefile(
13b5e8d8
NC
425 NAME => '%s',
426 VERSION_FROM => '%s',
d3bc6aa6 427 %-13s => '%s',
13b5e8d8
NC
428 realclean => { FILES => "@temps" },
429 (%%pod_scripts ? (
430 PL_FILES => \%%pod_scripts,
431 EXE_FILES => \@exe_files,
432 clean => { FILES => "@exe_files" },
433 ) : ()),
e74f76b2
NC
434);
435
436# ex: set ro:
437EOM
438 close $fh or die "Can't close Makefile.PL: $!";
43f197b5
NC
439 # As described in commit 23525070d6c0e51f:
440 # Push the atime and mtime of generated Makefile.PLs back 4
441 # seconds. In certain circumstances ( on virtual machines ) the
442 # generated Makefile.PL can produce a Makefile that is older than
443 # the Makefile.PL. Altering the atime and mtime backwards by 4
444 # seconds seems to resolve the issue.
445 eval {
446 my $ftime = time - 4;
447 utime $ftime, $ftime, 'Makefile.PL';
448 };
e74f76b2 449 }
fc678412
NC
450 print "\nRunning Makefile.PL in $ext_dir\n";
451
4b226208 452 my @args = ("-I$lib_dir", 'Makefile.PL');
902aaf3e
CB
453 if ($is_VMS) {
454 my $libd = VMS::Filespec::vmspath($lib_dir);
455 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
456 } else {
a2b175af
NC
457 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
458 'INSTALLMAN3DIR=none';
902aaf3e
CB
459 }
460 push @args, @$pass_through;
461 _quote_args(\@args) if $is_VMS;
d708ee4b
JR
462 print join(' ', $perl, @args), "\n";
463 my $code = system $perl, @args;
fc678412
NC
464 warn "$code from $ext_dir\'s Makefile.PL" if $code;
465
61edc683
NC
466 # Right. The reason for this little hack is that we're sitting inside
467 # a program run by ./miniperl, but there are tasks we need to perform
468 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
469 # Unfortunately, they can be run *after* 'clean', which deletes
470 # ./miniperl
471 # So we do our best to leave a set of instructions identical to what
472 # we would do if we are run directly as 'realclean' etc
473 # Whilst we're perfect, unfortunately the targets we call are not, as
474 # some of them rely on a $(PERL) for their own distclean targets.
475 # But this always used to be a problem with the old /bin/sh version of
476 # this.
e3b84025 477 if ($is_Unix) {
fc678412 478 foreach my $clean_target ('realclean', 'veryclean') {
a5bd6593 479 fallback_cleanup($return_dir, $clean_target, <<"EOS");
fc678412
NC
480cd $ext_dir
481if test ! -f Makefile -a -f Makefile.old; then
61edc683 482 echo "Note: Using Makefile.old"
eb1f8df7 483 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 484else
fc678412 485 if test ! -f Makefile ; then
61edc683
NC
486 echo "Warning: No Makefile!"
487 fi
eb1f8df7 488 make $clean_target MAKE='@make' @pass_through
61edc683 489fi
fc678412 490cd $return_dir
61edc683 491EOS
fc678412 492 }
61edc683 493 }
fc678412 494 }
a2f19a19 495
902aaf3e 496 if (not -f $makefile) {
a2f19a19 497 print "Warning: No Makefile!\n";
fc678412 498 }
a2f19a19 499
902aaf3e 500 if ($is_VMS) {
e0697720
NC
501 _quote_args($pass_through);
502 @$pass_through = (
503 "/DESCRIPTION=$makefile",
504 '/MACRO=(' . join(',',@$pass_through) . ')'
505 );
902aaf3e
CB
506 }
507
fc678412 508 if (!$target or $target !~ /clean$/) {
a2f19a19 509 # Give makefile an opportunity to rewrite itself.
75f92628 510 # reassure users that life goes on...
902aaf3e 511 my @args = ('config', @$pass_through);
d708ee4b 512 system(@make, @args) and print "@make @args failed, continuing anyway...\n";
fc678412 513 }
902aaf3e 514 my @targ = ($target, @$pass_through);
d708ee4b
JR
515 print "Making $target in $ext_dir\n@make @targ\n";
516 my $code = system(@make, @targ);
fc678412 517 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 518
fc678412
NC
519 chdir $return_dir || die "Cannot cd to $return_dir: $!";
520}
902aaf3e
CB
521
522sub _quote_args {
523 my $args = shift; # must be array reference
524
525 # Do not quote qualifiers that begin with '/'.
526 map { if (!/^\//) {
527 $_ =~ s/\"/""/g; # escape C<"> by doubling
528 $_ = q(").$_.q(");
529 }
530 } @{$args}
531 ;
532}
27329181
DD
533
534#guarentee that a file is deleted or die, void _unlink($filename)
535#xxx replace with _unlink_or_rename from EU::Install?
536sub _unlink {
537 1 while unlink $_[0];
538 my $err = $!;
539 die "Can't unlink $_[0]: $err" if -f $_[0];
540}
a5bd6593 541
f6dd0e4f
NC
542# Figure out if this extension is simple enough that it would only use
543# ExtUtils::MakeMaker's pm_to_blib target. If we're confident that it would,
544# then do all the work ourselves (returning an empty list), else return the
545# name of a file that we identified as beyond our ability to handle.
546#
547# While this is clearly quite a bit more work than just letting
548# ExtUtils::MakeMaker do it, and effectively is some code duplication, the time
549# savings are impressive.
550
551sub just_pm_to_blib {
1804eb61 552 my ($target, $ext_dir, $mname) = @_;
f6dd0e4f 553 my $has_lib;
1804eb61
NC
554 my $has_top;
555 my ($last) = $mname =~ /([^:]+)$/;
556
f6dd0e4f
NC
557 foreach my $leaf (<*>) {
558 if (-d $leaf) {
559 next if $leaf =~ /\A(?:\.|\.\.|t|demo)\z/;
560 if ($leaf eq 'lib') {
561 ++$has_lib;
562 next;
563 }
564 }
565 return $leaf
566 unless -f _;
567 # Makefile.PL is "safe" to ignore because we will only be called for
568 # directories that hold a Makefile.PL if they are in the exception list.
569 next
570 if $leaf =~ /\A(ChangeLog
571 |Changes
572 |LICENSE
573 |Makefile\.PL
574 |MANIFEST
575 |META\.yml
576 |pm_to_blib
577 |README
578 |README\.patching
579 |README\.release
580 )\z/xi; # /i to deal with case munging systems.
1804eb61
NC
581 if ($leaf eq "$last.pm") {
582 ++$has_top;
583 next;
584 }
f6dd0e4f
NC
585 return $leaf;
586 }
587 return 'no lib/'
1804eb61 588 unless $has_lib || $has_top;
f6dd0e4f
NC
589
590 print "\nRunning pm_to_blib for $ext_dir directly\n";
591
1804eb61
NC
592 my %pm;
593 if ($has_top) {
594 my $to = $mname =~ s!::!/!gr;
595 $pm{"$last.pm"} = "../../lib/$to.pm";
596 }
597 if ($has_lib) {
598 # strictly ExtUtils::MakeMaker uses the pm_to_blib target to install
599 # .pm, pod and .pl files. We're just going to do it for .pm and .pod
600 # files, to avoid problems on case munging file systems. Specifically,
601 # _pm.PL which ExtUtils::MakeMaker should run munges to _PM.PL, and
602 # looks a lot like a regular foo.pl (ie FOO.PL)
603 my @found;
604 require File::Find;
605 unless (eval {
606 File::Find::find({
607 no_chdir => 1,
608 wanted => sub {
609 return if -d $_;
610 # Bail out immediately with the problem file:
611 die \$_
612 unless -f _;
613 die \$_
614 unless /\A[^.]+\.(?:pm|pod)\z/i;
615 push @found, $_;
f6dd0e4f 616 }
1804eb61
NC
617 }, 'lib');
618 1;
619 }) {
620 # Problem files aren't really errors:
621 return ${$@}
622 if ref $@ eq 'SCALAR';
623 # But anything else is:
624 die $@;
625 }
626 foreach (@found) {
627 $pm{$_} = "../../$_";
628 }
f6dd0e4f
NC
629 }
630 # This is running under miniperl, so no autodie
631 if ($target eq 'all') {
632 require ExtUtils::Install;
1804eb61 633 ExtUtils::Install::pm_to_blib(\%pm, '../../lib/auto');
f6dd0e4f
NC
634 open my $fh, '>', 'pm_to_blib'
635 or die $!;
636 print $fh "$0 has handled pm_to_blib directly\n";
637 close $fh
638 or die $!;
639 } else {
640 # A clean target.
641 # For now, make the targets behave the same way as ExtUtils::MakeMaker
642 # does
643 _unlink('pm_to_blib');
644 unless ($target eq 'clean') {
645 # but cheat a bit, by relying on the top level Makefile clean target
646 # to take out our directory lib/auto/...
647 # (which it has to deal with, as cpan/foo/bar creates
648 # lib/auto/foo/bar, but the EU::MM rule will only
649 # rmdir lib/auto/foo/bar, leaving lib/auto/foo
650 _unlink("../../$_")
1804eb61 651 foreach sort values %pm;
f6dd0e4f
NC
652 }
653 }
654 return;
655}
656
a5bd6593
NC
657sub fallback_cleanup {
658 my ($dir, $clean_target, $contents) = @_;
659 my $file = "$dir/$clean_target.sh";
660 open my $fh, '>>', $file or die "open $file: $!";
661 # Quite possible that we're being run in parallel here.
662 # Can't use Fcntl this early to get the LOCK_EX
663 flock $fh, 2 or warn "flock $file: $!";
664 print $fh $contents or die "print $file: $!";
665 close $fh or die "close $file: $!";
666}