5 use 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',
11 my @ext_dirs = qw(cpan dist ext);
12 my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')';
14 # This script acts as a simple interface for building extensions.
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.
19 # On Unix, it primarily used by the perl Makefile one extension at a time:
21 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
22 # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
25 # If '--static' is specified, static extensions will be built.
26 # If '--dynamic' is specified, dynamic extensions will be built.
27 # If '--nonxs' is specified, nonxs extensions will be built.
28 # If '--dynaloader' is specified, DynaLoader will be built.
29 # If '--all' is specified, all extensions will be built.
31 # make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
35 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
37 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
39 # make_ext.pl MAKE=dmake --dir=..\ext
41 # make_ext.pl MAKE=dmake --dir=..\ext --target=clean
43 # Will skip building extensions which are marked with an '!' char.
44 # Mostly because they still not ported to specified platform.
46 # If any extensions are listed with a '+' char then only those
47 # extensions will be built, but only if they aren't countermanded
48 # by an '!ext' and are appropriate to the type of building being done.
49 # An extensions follows the format of Foo/Bar, which would be extension Foo::Bar
51 # To fix dependency ordering, on *nix systems, edit Makefile.SH to create a
52 # rule. That isn't sufficient for other systems; you also have to do
53 # something in this file. See the code at
54 # '# XXX hack for dependency # ordering'
58 # 1) if there's a Makefile.PL in git for the module, use it. and call make
59 # 2) If not, auto-generate one (normally)
60 # 3) unless the auto-generation code figures out that the extension is
61 # *really* simple, in which case don't. This will be for pure perl
62 # modules, and all that is needed to be done is to copy from the source
63 # to the dest directories.
65 # It may be deleted in a later release of perl so try to
66 # avoid using it for other purposes.
68 my (%excl, %incl, %opts, @extspec, @pass_through, $verbose);
73 } elsif (/^\+(.*)$/) {
75 } elsif (/^--verbose$/ or /^-v$/) {
77 } elsif (/^--([\w\-]+)$/) {
79 } elsif (/^--([\w\-]+)=(.*)$/) {
80 push @{$opts{$1}}, $2;
82 push @pass_through, $_;
88 my $static = $opts{static} || $opts{all};
89 my $dynamic = $opts{dynamic} || $opts{all};
90 my $nonxs = $opts{nonxs} || $opts{all};
91 my $dynaloader = $opts{dynaloader} || $opts{all};
93 # The Perl Makefile.SH will expand all extensions to
94 # lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
95 # A user wishing to run make_ext might use
96 # X (or X/Y or X::Y if nested)
98 # canonise into X/Y form (pname)
101 if (s{^lib/auto/}{}) {
102 # Remove lib/auto prefix and /*.* suffix
104 } elsif (s{^$ext_dirs_re/}{}) {
105 # Remove ext/ prefix and /pm_to_blib suffix
107 # Targets are given as files on disk, but the extension spec is still
108 # written using /s for each ::
110 } elsif (s{::}{\/}g) {
117 my $makecmd = shift @pass_through; # Should be something like MAKE=make
118 unshift @pass_through, 'PERL_CORE=1';
120 my @dirs = @{$opts{dir} || \@ext_dirs};
121 my $target = $opts{target}[0];
122 $target = 'all' unless defined $target;
124 # Previously, $make was taken from config.sh. However, the user might
125 # instead be running a possibly incompatible make. This might happen if
126 # the user types "gmake" instead of a plain "make", for example. The
127 # correct current value of MAKE will come through from the main perl
128 # makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
129 # case third party users of this script (are there any?) don't have the
130 # MAKE=$(MAKE) argument, which was added after 5.004_03.
131 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
132 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
135 # This isn't going to cope with anything fancy, such as spaces inside command
136 # names, but neither did what it replaced. Once there is a use case that needs
137 # it, please supply patches. Until then, I'm sticking to KISS
138 my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
142 die "make_ext: no make target specified (eg all or clean)\n";
143 } elsif ($target !~ /^(?:all|clean|distclean|realclean|veryclean)$/) {
144 # we are strict about what make_ext is used for because we emulate these
145 # targets for simple modules:
146 die "$0: unknown make target '$target'\n";
149 if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
150 die "$0: no extension specified\n";
154 my %extra_passthrough;
159 my $build = Cwd::getcwd();
161 if ($perl =~ m#^\.\.#) {
164 $perl = "$here\\$perl";
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") {
171 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
172 print "@args\n" if $verbose;
173 system(@args) unless IS_CROSS;
176 print "In $build" if $verbose;
177 foreach my $dir (@dirs) {
178 chdir($dir) or die "Cannot cd to $dir: $!\n";
179 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
180 FindExt::scan_ext($ext);
181 FindExt::set_static_extensions(split ' ', $Config{static_ext});
183 or die "Couldn't chdir to '$build': $!"; # restore our start directory
187 push @ext, FindExt::static_ext() if $static;
188 push @ext, FindExt::dynamic_ext() if $dynamic;
189 push @ext, FindExt::nonxs_ext() if $nonxs;
190 push @ext, 'DynaLoader' if $dynaloader;
192 foreach (sort @ext) {
193 if (%incl and !exists $incl{$_}) {
194 #warn "Skipping extension $_, not in inclusion list\n";
197 if (exists $excl{$_}) {
198 warn "Skipping extension $_, not ported to current platform";
202 if($_ ne 'DynaLoader' && FindExt::is_static($_)) {
203 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
208 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
212 push @extspec, (split ' ', $Config{static_ext}) if $static;
213 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
214 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
215 push @extspec, 'DynaLoader' if $dynaloader;
218 { # XXX hack for dependency ordering
219 # Cwd needs to be built before Encode recurses into subdirectories.
220 # Pod::Simple needs to be built before Pod::Functions, but after 'if'
221 # lib needs to be built before IO-Compress
222 # This seems to be the simplest way to ensure this ordering:
223 my (@first, @second, @other);
225 if ($_ eq 'Cwd' || $_ eq 'if' || $_ eq 'lib') {
228 elsif ($_ eq 'Pod/Simple') {
234 @extspec = (@first, @second, @other);
237 if ($Config{osname} eq 'catamount' and @extspec) {
238 # Snowball's chance of building extensions.
239 die "This is $Config{osname}, not building $extspec[0], sorry.\n";
243 foreach my $spec (@extspec) {
248 # Try new style ext/Data-Dumper/ first
252 # List/Util.xs lives in Scalar-List-Utils, Cwd.xs lives in PathTools
253 $copy = 'Scalar-List-Utils' if $copy eq 'List-Util';
254 $copy = 'PathTools' if $copy eq 'Cwd';
256 foreach my $dir (@ext_dirs) {
257 if (-d "$dir/$copy") {
258 $ext_pathname = "$dir/$copy";
263 if (!defined $ext_pathname) {
264 if (-d "ext/$spec") {
265 # Old style ext/Data/Dumper/
266 $ext_pathname = "ext/$spec";
268 warn "Can't find extension $spec in any of @ext_dirs";
273 print "\tMaking $mname ($target)\n" if $verbose;
275 build_extension($ext_pathname, $perl, $mname, $target,
276 [@pass_through, @{$extra_passthrough{$spec} || []}]);
279 sub build_extension {
280 my ($ext_dir, $perl, $mname, $target, $pass_through) = @_;
282 unless (chdir "$ext_dir") {
283 warn "Cannot cd to $ext_dir: $!";
290 $perl ||= "$up/miniperl";
291 my $return_dir = $up;
292 my $lib_dir = "$up/lib";
294 my ($makefile, $makefile_no_minus_f);
296 $makefile = 'descrip.mms';
297 if ($target =~ /clean$/
299 && -f "${makefile}_old") {
300 $makefile = "${makefile}_old";
303 $makefile = 'Makefile';
307 $makefile_no_minus_f = 0;
308 open my $mfh, '<', $makefile or die "Cannot open $makefile: $!";
310 # Plagiarised from CPAN::Distribution
311 last if /MakeMaker post_initialize section/;
312 next unless /^#\s+VERSION_FROM\s+=>\s+(.+)/;
316 next unless /^XS_VERSION = (\S+)/;
320 last unless defined $oldv;
321 require ExtUtils::MM_Unix;
322 defined (my $newv = parse_version MM $vmod) or last;
323 if (version->parse($newv) ne $oldv) {
324 close $mfh or die "close $makefile: $!";
327 no warnings 'deprecated';
334 # If we're cross-compiling, it's possible that the host's
335 # Makefiles are around.
336 seek($mfh, 0, 0) or die "Cannot seek $makefile: $!";
340 # XXX This might not be throughout enough.
341 # For example, it's possible to cause a false-positive
342 # if cross compiling on and for the Raspberry Pi,
343 # which is insane but plausible.
344 # False positives are really not troublesome, though;
345 # all they mean is that the module gets rebuilt.
346 if (/^CC = \Q$Config{cc}\E/) {
352 if (!$cross_makefile) {
353 print "Deleting non-Cross makefile\n";
354 close $mfh or die "close $makefile: $!";
359 $makefile_no_minus_f = 1;
362 if ($makefile_no_minus_f || !-f $makefile) {
364 if (!-f 'Makefile.PL') {
365 unless (just_pm_to_blib($target, $ext_dir, $mname, $return_dir)) {
366 # No problems returned, so it has faked everything for us. :-)
367 chdir $return_dir || die "Cannot cd to $return_dir: $!";
371 print "\nCreating Makefile.PL in $ext_dir for $mname\n" if $verbose;
372 my ($fromname, $key, $value);
374 $key = 'ABSTRACT_FROM';
375 # We need to cope well with various possible layouts
376 my @dirs = split /::/, $mname;
377 my $leaf = pop @dirs;
378 my $leafname = "$leaf.pm";
379 my $pathname = join '/', @dirs, $leafname;
380 my @locations = ($leafname, $pathname, "lib/$pathname");
381 foreach (@locations) {
389 die "For $mname tried @locations in $ext_dir but can't find source";
391 ($value = $fromname) =~ s/\.pm\z/.pod/;
392 $value = $fromname unless -e $value;
394 if ($mname eq 'Pod::Checker') {
395 # the abstract in the .pm file is unparseable by MM,
396 # so special-case it. We can't use the package's own
397 # Makefile.PL, as it doesn't handle the executable scripts
400 # this is copied from the CPAN Makefile.PL v 1.171
401 $value = 'Pod::Checker verifies POD documentation contents for compliance with the POD format specifications';
404 open my $fh, '>', 'Makefile.PL'
405 or die "Can't open Makefile.PL for writing: $!";
406 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
407 #-*- buffer-read-only: t -*-
409 # This Makefile.PL was written by %s.
410 # It will be deleted automatically by make realclean
413 use ExtUtils::MakeMaker;
415 # This is what the .PL extracts to. Not the ultimate file that is installed.
416 # (ie Win32 runs pl2bat after this)
418 # Doing this here avoids all sort of quoting issues that would come from
419 # attempting to write out perl source with literals to generate the arrays and
421 my @temps = 'Makefile.PL';
422 foreach (glob('scripts/pod*.PL')) {
423 # The various pod*.PL extractors change directory. Doing that with relative
424 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
425 # the chdir doing anything, than to attempt to convert lib paths to
426 # absolute, and potentially run into problems with quoting special
427 # characters in the path to our build dir (such as spaces)
431 $temp =~ s!scripts/!!;
432 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
436 my $script_ext = $^O eq 'VMS' ? '.com' : '';
438 foreach (glob('pod*.PL')) {
440 s/.PL$/$script_ext/i;
441 $pod_scripts{$script} = $_;
443 my @exe_files = values %%pod_scripts;
447 VERSION_FROM => '%s',
449 realclean => { FILES => "@temps" },
451 PL_FILES => \%%pod_scripts,
452 EXE_FILES => \@exe_files,
453 clean => { FILES => "@exe_files" },
459 close $fh or die "Can't close Makefile.PL: $!";
460 # As described in commit 23525070d6c0e51f:
461 # Push the atime and mtime of generated Makefile.PLs back 4
462 # seconds. In certain circumstances ( on virtual machines ) the
463 # generated Makefile.PL can produce a Makefile that is older than
464 # the Makefile.PL. Altering the atime and mtime backwards by 4
465 # seconds seems to resolve the issue.
467 my $ftime = (stat('Makefile.PL'))[9] - 4;
468 utime $ftime, $ftime, 'Makefile.PL';
470 } elsif ($mname =~ /\A(?:Carp
473 |Search::Dict)\z/x) {
474 # An explicit list of dual-life extensions that have a Makefile.PL
475 # for CPAN, but we have verified can also be built using the fakery.
476 my ($problem) = just_pm_to_blib($target, $ext_dir, $mname, $return_dir);
477 # We really need to sanity test that we can fake it.
478 # Otherwise "skips" will go undetected, and the build slow down for
479 # everyone, defeating the purpose.
480 if (defined $problem) {
481 if (-d "$return_dir/.git") {
482 # Get the list of files that git isn't ignoring:
483 my @files = `git ls-files --cached --others --exclude-standard 2>/dev/null`;
484 # on error (eg no git) we get nothing, but that's not a
485 # problem. The goal is to see if git thinks that the problem
486 # file is interesting, by getting a positive match with
487 # something git told us about, and if so bail out:
490 # We really need to sanity test that we can fake it.
491 # The intent is that this should only fail because
492 # you've just added a file to the dual-life dist that
493 # we can't handle. In which case you should either
494 # 1) remove the dist from the regex a few lines above.
496 # 2) add the file to regex of "safe" filenames earlier
497 # in this function, that starts with ChangeLog
498 die "FATAL - $0 has $mname in the list of simple extensions, but it now contains file '$problem' which we can't handle"
501 # There's an unexpected file, but it seems to be something
502 # that git will ignore. So fall through to the regular
503 # Makefile.PL handling code below, on the assumption that
504 # we won't get here for a clean build.
506 warn "WARNING - $0 is building $mname using EU::MM, as it found file '$problem'";
508 # It faked everything for us.
509 chdir $return_dir || die "Cannot cd to $return_dir: $!";
514 # We are going to have to use Makefile.PL:
515 print "\nRunning Makefile.PL in $ext_dir\n" if $verbose;
517 my @args = ("-I$lib_dir", 'Makefile.PL');
519 my $libd = VMS::Filespec::vmspath($lib_dir);
520 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
522 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
523 'INSTALLMAN3DIR=none';
525 push @args, @$pass_through;
526 _quote_args(\@args) if IS_VMS;
527 print join(' ', $perl, @args), "\n" if $verbose;
529 local $ENV{PERL_MM_USE_DEFAULT} = 1;
533 #make sure next build attempt/run of make_ext.pl doesn't succeed
535 die "Unsuccessful Makefile.PL($ext_dir): code=$code";
538 # Right. The reason for this little hack is that we're sitting inside
539 # a program run by ./miniperl, but there are tasks we need to perform
540 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
541 # Unfortunately, they can be run *after* 'clean', which deletes
543 # So we do our best to leave a set of instructions identical to what
544 # we would do if we are run directly as 'realclean' etc
545 # Whilst we're perfect, unfortunately the targets we call are not, as
546 # some of them rely on a $(PERL) for their own distclean targets.
547 # But this always used to be a problem with the old /bin/sh version of
550 foreach my $clean_target ('realclean', 'veryclean') {
551 fallback_cleanup($return_dir, $clean_target, <<"EOS");
553 if test ! -f Makefile -a -f Makefile.old; then
554 echo "Note: Using Makefile.old"
555 make -f Makefile.old $clean_target MAKE='@make' @pass_through
557 if test ! -f Makefile ; then
558 echo "Warning: No Makefile!"
560 @make $clean_target MAKE='@make' @pass_through
568 if (not -f $makefile) {
569 print "Warning: No Makefile!\n";
573 _quote_args($pass_through);
575 "/DESCRIPTION=$makefile",
576 '/MACRO=(' . join(',',@$pass_through) . ')'
580 my @targ = ($target, @$pass_through);
581 print "Making $target in $ext_dir\n@make @targ\n" if $verbose;
582 local $ENV{PERL_INSTALL_QUIET} = 1;
583 my $code = system(@make, @targ);
584 if($code >> 8 != 0){ # probably cleaned itself, try again once more time
585 $code = system(@make, @targ);
587 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
589 chdir $return_dir || die "Cannot cd to $return_dir: $!";
593 my $args = shift; # must be array reference
595 # Do not quote qualifiers that begin with '/'.
597 $_ =~ s/\"/""/g; # escape C<"> by doubling
604 #guarentee that a file is deleted or die, void _unlink($filename)
605 #xxx replace with _unlink_or_rename from EU::Install?
607 1 while unlink $_[0];
609 die "Can't unlink $_[0]: $err" if -f $_[0];
612 # Figure out if this extension is simple enough that it would only use
613 # ExtUtils::MakeMaker's pm_to_blib target. If we're confident that it would,
614 # then do all the work ourselves (returning an empty list), else return the
615 # name of a file that we identified as beyond our ability to handle.
617 # While this is clearly quite a bit more work than just letting
618 # ExtUtils::MakeMaker do it, and effectively is some code duplication, the time
619 # savings are impressive.
621 sub just_pm_to_blib {
622 my ($target, $ext_dir, $mname, $return_dir) = @_;
623 my ($has_lib, $has_top, $has_topdir);
624 my ($last) = $mname =~ /([^:]+)$/;
625 my ($first) = $mname =~ /^([^:]+)/;
627 my $pm_to_blib = IS_VMS ? 'pm_to_blib.ts' : 'pm_to_blib';
628 my $silent = defined $ENV{MAKEFLAGS} && $ENV{MAKEFLAGS} =~ /\b(s|silent|quiet)\b/;
630 foreach my $leaf (<*>) {
632 $leaf =~ s/\.DIR\z//i
634 next if $leaf =~ /\A(?:\.|\.\.|t|demo)\z/;
635 if ($leaf eq 'lib') {
639 if ($leaf eq $first) {
648 # Makefile.PL is "safe" to ignore because we will only be called for
649 # directories that hold a Makefile.PL if they are in the exception list.
651 if $leaf =~ /\A(ChangeLog
661 )\z/xi; # /i to deal with case munging systems.
662 if ($leaf eq "$last.pm") {
669 unless $has_lib || $has_top;
670 die "Inconsistent module $mname has both lib/ and $first/"
671 if $has_lib && $has_topdir;
673 print "Running pm_to_blib for $ext_dir directly\n"
678 my $to = $mname =~ s!::!/!gr;
679 $pm{"$last.pm"} = "../../lib/$to.pm";
681 if ($has_lib || $has_topdir) {
682 # strictly ExtUtils::MakeMaker uses the pm_to_blib target to install
683 # .pm, pod and .pl files. We're just going to do it for .pm and .pod
684 # files, to avoid problems on case munging file systems. Specifically,
685 # _pm.PL which ExtUtils::MakeMaker should run munges to _PM.PL, and
686 # looks a lot like a regular foo.pl (ie FOO.PL)
694 # Bail out immediately with the problem file:
698 unless /\A[^.]+\.(?:pm|pod)\z/i;
701 }, $has_lib ? 'lib' : $first);
704 # Problem files aren't really errors:
706 if ref $@ eq 'SCALAR';
707 # But anything else is:
714 $pm{$_} = "../../lib/$_"
718 # This is running under miniperl, so no autodie
719 if ($target eq 'all') {
721 if (-f $pm_to_blib) {
722 # avoid touching pm_to_blib unless there's something that
723 # needs updating, see #126710
726 while (my $from = each(%pm)) {
727 if (-M $from < $test_at) {
732 keys %pm; # reset iterator
736 local $ENV{PERL_INSTALL_QUIET} = 1;
737 require ExtUtils::Install;
738 ExtUtils::Install::pm_to_blib(\%pm, '../../lib/auto');
739 open my $fh, '>', $pm_to_blib
740 or die "Can't open '$pm_to_blib': $!";
741 print $fh "$0 has handled pm_to_blib directly\n";
743 or die "Can't close '$pm_to_blib': $!";
745 # Fake the fallback cleanup
747 = join '', map {s!^\.\./\.\./!!; "rm -f $_\n"} sort values %pm;
748 foreach my $clean_target ('realclean', 'veryclean') {
749 fallback_cleanup($return_dir, $clean_target, $fallback);
755 # For now, make the targets behave the same way as ExtUtils::MakeMaker
757 _unlink($pm_to_blib);
758 unless ($target eq 'clean') {
759 # but cheat a bit, by relying on the top level Makefile clean target
760 # to take out our directory lib/auto/...
761 # (which it has to deal with, as cpan/foo/bar creates
762 # lib/auto/foo/bar, but the EU::MM rule will only
763 # rmdir lib/auto/foo/bar, leaving lib/auto/foo
765 foreach sort values %pm;
771 sub fallback_cleanup {
772 my ($dir, $clean_target, $contents) = @_;
773 my $file = "$dir/$clean_target.sh";
774 open my $fh, '>>', $file or die "open $file: $!";
775 # Quite possible that we're being run in parallel here.
776 # Can't use Fcntl this early to get the LOCK_EX
777 flock $fh, 2 or warn "flock $file: $!";
778 print $fh $contents or die "print $file: $!";
779 close $fh or die "close $file: $!";