4 use constant IS_CROSS => defined $::Cross::platform ? 1 : 0;
7 my $is_Win32 = $^O eq 'MSWin32';
8 my $is_VMS = $^O eq 'VMS';
9 my $is_Unix = !$is_Win32 && !$is_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 # It may be deleted in a later release of perl so try to
52 # avoid using it for other purposes.
54 my (%excl, %incl, %opts, @extspec, @pass_through);
59 } elsif (/^\+(.*)$/) {
61 } elsif (/^--([\w\-]+)$/) {
63 } elsif (/^--([\w\-]+)=(.*)$/) {
64 push @{$opts{$1}}, $2;
66 push @pass_through, $_;
72 my $static = $opts{static} || $opts{all};
73 my $dynamic = $opts{dynamic} || $opts{all};
74 my $nonxs = $opts{nonxs} || $opts{all};
75 my $dynaloader = $opts{dynaloader} || $opts{all};
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)
82 # canonise into X/Y form (pname)
85 if (s{^lib/auto/}{}) {
86 # Remove lib/auto prefix and /*.* suffix
88 } elsif (s{^$ext_dirs_re/}{}) {
89 # Remove ext/ prefix and /pm_to_blib suffix
91 # Targets are given as files on disk, but the extension spec is still
92 # written using /s for each ::
94 } elsif (s{::}{\/}g) {
101 my $makecmd = shift @pass_through; # Should be something like MAKE=make
102 unshift @pass_through, 'PERL_CORE=1';
104 my @dirs = @{$opts{dir} || \@ext_dirs};
105 my $target = $opts{target}[0];
106 $target = 'all' unless defined $target;
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.
115 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
116 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
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
122 my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
123 # Using an array of 0 or 1 elements makes the subsequent code simpler.
124 my @run = $Config{run};
125 @run = () if not defined $run[0] or $run[0] eq '';
129 die "make_ext: no make target specified (eg all or clean)\n";
130 } elsif ($target !~ /(?:^all|clean)$/) {
131 # for the time being we are strict about what make_ext is used for
132 die "$0: unknown make target '$target'\n";
135 if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
136 die "$0: no extension specified\n";
140 my %extra_passthrough;
145 my $build = Cwd::getcwd();
147 if ($perl =~ m#^\.\.#) {
150 $perl = "$here\\$perl";
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") {
157 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
159 system(@args) unless IS_CROSS;
163 foreach my $dir (@dirs) {
164 chdir($dir) or die "Cannot cd to $dir: $!\n";
165 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
166 FindExt::scan_ext($ext);
167 FindExt::set_static_extensions(split ' ', $Config{static_ext});
169 or die "Couldn't chdir to '$build': $!"; # restore our start directory
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;
178 foreach (sort @ext) {
179 if (%incl and !exists $incl{$_}) {
180 #warn "Skipping extension $_, not in inclusion list\n";
183 if (exists $excl{$_}) {
184 warn "Skipping extension $_, not ported to current platform";
188 if($_ eq 'DynaLoader' and $target !~ /clean$/) {
189 # No, we don't know why nmake can't work out the dependency chain
190 push @{$extra_passthrough{$_}}, 'DynaLoader.c';
191 } elsif(FindExt::is_static($_)) {
192 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
197 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
201 push @extspec, (split ' ', $Config{static_ext}) if $static;
202 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
203 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
204 push @extspec, 'DynaLoader' if $dynaloader;
208 # Cwd needs to be built before Encode recurses into subdirectories.
209 # Pod::Simple needs to be built before Pod::Functions
210 # This seems to be the simplest way to ensure this ordering:
213 if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') {
219 @extspec = (@first, @other);
222 if ($Config{osname} eq 'catamount' and @extspec) {
223 # Snowball's chance of building extensions.
224 die "This is $Config{osname}, not building $extspec[0], sorry.\n";
227 foreach my $spec (@extspec) {
232 # Try new style ext/Data-Dumper/ first
236 # List/Util.xs lives in Scalar-List-Utils, Cwd.xs lives in PathTools
237 $copy = 'Scalar-List-Utils' if $copy eq 'List-Util';
238 $copy = 'PathTools' if $copy eq 'Cwd';
240 foreach my $dir (@ext_dirs) {
241 if (-d "$dir/$copy") {
242 $ext_pathname = "$dir/$copy";
247 if (!defined $ext_pathname) {
248 if (-d "ext/$spec") {
249 # Old style ext/Data/Dumper/
250 $ext_pathname = "ext/$spec";
252 warn "Can't find extension $spec in any of @ext_dirs";
257 print "\tMaking $mname ($target)\n";
259 build_extension($ext_pathname, $perl, $mname,
260 [@pass_through, @{$extra_passthrough{$spec} || []}]);
263 sub build_extension {
264 my ($ext_dir, $perl, $mname, $pass_through) = @_;
266 unless (chdir "$ext_dir") {
267 warn "Cannot cd to $ext_dir: $!";
274 $perl ||= "$up/miniperl";
275 my $return_dir = $up;
276 my $lib_dir = "$up/lib";
281 $makefile = 'descrip.mms';
282 if ($target =~ /clean$/
284 && -f "${makefile}_old") {
285 $makefile = "${makefile}_old";
288 $makefile = 'Makefile';
292 open my $mfh, $makefile or die "Cannot open $makefile: $!";
294 # Plagiarised from CPAN::Distribution
295 last if /MakeMaker post_initialize section/;
296 next unless /^#\s+VERSION_FROM\s+=>\s+(.+)/;
300 next unless /^XS_VERSION = (\S+)/;
304 last unless defined $oldv;
305 require ExtUtils::MM_Unix;
306 defined (my $newv = parse_version MM $vmod) or last;
307 if ($newv ne $oldv) {
308 close $mfh or die "close $makefile: $!";
311 no warnings 'deprecated';
317 seek($mfh, 0, 0) or die "Cannot seek $makefile: $!";
319 #this is used to stop the while loop early for efficiency when
320 #the line is reached, and possibly match a cross build
321 my $header = quotemeta '# These definitions are from config.sh (via ';
324 $::Cross::platform\Q\/Config.pm\E)?\)\./x) {
326 print "Deleting non-Cross makefile\n";
327 close $mfh or die "close $makefile: $!";
330 no warnings 'deprecated';
333 } else { #have a cross makefile
337 } #catch breakage from future changes
338 die "non-standard makefile found in $mname";
345 if (!-f 'Makefile.PL') {
346 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
347 my ($fromname, $key, $value);
348 if ($mname eq 'podlators') {
349 # We need to special case this somewhere, and this is fewer
350 # lines of code than a core-only Makefile.PL, and no more
352 $fromname = 'VERSION';
354 $value = 'podlators';
357 $key = 'ABSTRACT_FROM';
358 # We need to cope well with various possible layouts
359 my @dirs = split /::/, $mname;
360 my $leaf = pop @dirs;
361 my $leafname = "$leaf.pm";
362 my $pathname = join '/', @dirs, $leafname;
363 my @locations = ($leafname, $pathname, "lib/$pathname");
364 unshift @locations, 'lib/IO/Compress/Base.pm' if $mname eq 'IO::Compress';
365 foreach (@locations) {
373 die "For $mname tried @locations in in $ext_dir but can't find source";
375 ($value = $fromname) =~ s/\.pm\z/.pod/;
376 $value = $fromname unless -e $value;
378 open my $fh, '>', 'Makefile.PL'
379 or die "Can't open Makefile.PL for writing: $!";
380 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
381 #-*- buffer-read-only: t -*-
383 # This Makefile.PL was written by %s.
384 # It will be deleted automatically by make realclean
387 use ExtUtils::MakeMaker;
389 # This is what the .PL extracts to. Not the ultimate file that is installed.
390 # (ie Win32 runs pl2bat after this)
392 # Doing this here avoids all sort of quoting issues that would come from
393 # attempting to write out perl source with literals to generate the arrays and
395 my @temps = 'Makefile.PL';
396 foreach (glob('scripts/pod*.PL')) {
397 # The various pod*.PL extractors change directory. Doing that with relative
398 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
399 # the chdir doing anything, than to attempt to convert lib paths to
400 # absolute, and potentially run into problems with quoting special
401 # characters in the path to our build dir (such as spaces)
405 $temp =~ s!scripts/!!;
406 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
410 my $script_ext = $^O eq 'VMS' ? '.com' : '';
412 foreach (glob('pod*.PL')) {
414 s/.PL$/$script_ext/i;
415 $pod_scripts{$script} = $_;
417 my @exe_files = values %%pod_scripts;
421 VERSION_FROM => '%s',
423 realclean => { FILES => "@temps" },
425 PL_FILES => \%%pod_scripts,
426 EXE_FILES => \@exe_files,
427 clean => { FILES => "@exe_files" },
433 close $fh or die "Can't close Makefile.PL: $!";
434 # As described in commit 23525070d6c0e51f:
435 # Push the atime and mtime of generated Makefile.PLs back 4
436 # seconds. In certain circumstances ( on virtual machines ) the
437 # generated Makefile.PL can produce a Makefile that is older than
438 # the Makefile.PL. Altering the atime and mtime backwards by 4
439 # seconds seems to resolve the issue.
441 my $ftime = time - 4;
442 utime $ftime, $ftime, 'Makefile.PL';
445 print "\nRunning Makefile.PL in $ext_dir\n";
447 # Presumably this can be simplified
450 # Inherited from win32/buildext.pl
451 @cross = "-MCross=$::Cross::platform";
452 } elsif ($opts{cross}) {
453 # Inherited from make_ext.pl
457 my @args = ("-I$lib_dir", @cross, 'Makefile.PL');
459 my $libd = VMS::Filespec::vmspath($lib_dir);
460 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
462 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
463 'INSTALLMAN3DIR=none';
465 push @args, @$pass_through;
466 _quote_args(\@args) if $is_VMS;
467 print join(' ', @run, $perl, @args), "\n";
468 my $code = system @run, $perl, @args;
469 warn "$code from $ext_dir\'s Makefile.PL" if $code;
471 # Right. The reason for this little hack is that we're sitting inside
472 # a program run by ./miniperl, but there are tasks we need to perform
473 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
474 # Unfortunately, they can be run *after* 'clean', which deletes
476 # So we do our best to leave a set of instructions identical to what
477 # we would do if we are run directly as 'realclean' etc
478 # Whilst we're perfect, unfortunately the targets we call are not, as
479 # some of them rely on a $(PERL) for their own distclean targets.
480 # But this always used to be a problem with the old /bin/sh version of
484 foreach my $clean_target ('realclean', 'veryclean') {
485 my $file = "$return_dir/$clean_target$suffix";
486 open my $fh, '>>', $file or die "open $file: $!";
487 # Quite possible that we're being run in parallel here.
488 # Can't use Fcntl this early to get the LOCK_EX
489 flock $fh, 2 or warn "flock $file: $!";
492 if test ! -f Makefile -a -f Makefile.old; then
493 echo "Note: Using Makefile.old"
494 make -f Makefile.old $clean_target MAKE='@make' @pass_through
496 if test ! -f Makefile ; then
497 echo "Warning: No Makefile!"
499 make $clean_target MAKE='@make' @pass_through
503 close $fh or die "close $file: $!";
508 if (not -f $makefile) {
509 print "Warning: No Makefile!\n";
513 _quote_args($pass_through);
515 "/DESCRIPTION=$makefile",
516 '/MACRO=(' . join(',',@$pass_through) . ')'
520 if (!$target or $target !~ /clean$/) {
521 # Give makefile an opportunity to rewrite itself.
522 # reassure users that life goes on...
523 my @args = ('config', @$pass_through);
524 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
526 my @targ = ($target, @$pass_through);
527 print "Making $target in $ext_dir\n@run @make @targ\n";
528 my $code = system(@run, @make, @targ);
529 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
531 chdir $return_dir || die "Cannot cd to $return_dir: $!";
535 my $args = shift; # must be array reference
537 # Do not quote qualifiers that begin with '/'.
539 $_ =~ s/\"/""/g; # escape C<"> by doubling
546 #guarentee that a file is deleted or die, void _unlink($filename)
547 #xxx replace with _unlink_or_rename from EU::Install?
549 1 while unlink $_[0];
551 die "Can't unlink $_[0]: $err" if -f $_[0];