This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for 4748e0020f
[perl5.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19
S
2use strict;
3use warnings;
27329181 4use constant IS_CROSS => defined $::Cross::platform ? 1 : 0;
b8d39eba 5use Config;
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 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
S
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};
ca5de986 123# Using an array of 0 or 1 elements makes the subsequent code simpler.
fc678412
NC
124my @run = $Config{run};
125@run = () if not defined $run[0] or $run[0] eq '';
a2f19a19 126
a0d0e21e 127
07f3cc2a 128if ($target eq '') {
ca5de986
NC
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";
a2f19a19
S
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
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);
286d62c2 158 print "@args\n";
27329181 159 system(@args) unless IS_CROSS;
286d62c2
NC
160 }
161
a2ac9a71 162 print "In $build";
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, $_;
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';
286d62c2
NC
193 }
194 }
a85e0e8c 195
a2ac9a71 196 chdir '..'
a85e0e8c 197 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
286d62c2 198}
902aaf3e
CB
199elsif ($is_VMS) {
200 $perl = $^X;
201 push @extspec, (split ' ', $Config{static_ext}) if $static;
202 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
a34ce875 203 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
281da5ea 204 push @extspec, 'DynaLoader' if $dynaloader;
902aaf3e 205}
286d62c2 206
dc0655f7
NC
207{
208 # Cwd needs to be built before Encode recurses into subdirectories.
1179df5f 209 # Pod::Simple needs to be built before Pod::Functions
dc0655f7
NC
210 # This seems to be the simplest way to ensure this ordering:
211 my (@first, @other);
212 foreach (@extspec) {
1179df5f 213 if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') {
dc0655f7
NC
214 push @first, $_;
215 } else {
216 push @other, $_;
217 }
218 }
219 @extspec = (@first, @other);
220}
221
91c30809
NC
222if ($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";
225}
226
e08c66ce
NC
227foreach my $spec (@extspec) {
228 my $mname = $spec;
ca5de986 229 $mname =~ s!/!::!g;
238a6851 230 my $ext_pathname;
a28b98e8
NC
231
232 # Try new style ext/Data-Dumper/ first
233 my $copy = $spec;
234 $copy =~ tr!/!-!;
cb8c8458
SH
235
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';
239
a28b98e8
NC
240 foreach my $dir (@ext_dirs) {
241 if (-d "$dir/$copy") {
242 $ext_pathname = "$dir/$copy";
243 last;
8a992763 244 }
a28b98e8
NC
245 }
246
247 if (!defined $ext_pathname) {
248 if (-d "ext/$spec") {
249 # Old style ext/Data/Dumper/
250 $ext_pathname = "ext/$spec";
251 } else {
8a992763
NC
252 warn "Can't find extension $spec in any of @ext_dirs";
253 next;
254 }
238a6851 255 }
a2f19a19 256
ca5de986 257 print "\tMaking $mname ($target)\n";
a2f19a19 258
acb65a20 259 build_extension($ext_pathname, $perl, $mname,
e08c66ce 260 [@pass_through, @{$extra_passthrough{$spec} || []}]);
ca5de986 261}
a0d0e21e 262
fc678412 263sub build_extension {
acb65a20
NC
264 my ($ext_dir, $perl, $mname, $pass_through) = @_;
265
a85e0e8c
SH
266 unless (chdir "$ext_dir") {
267 warn "Cannot cd to $ext_dir: $!";
268 return;
269 }
270
acb65a20
NC
271 my $up = $ext_dir;
272 $up =~ s![^/]+!..!g;
273
274 $perl ||= "$up/miniperl";
275 my $return_dir = $up;
276 my $lib_dir = "$up/lib";
6c8b0117 277 $ENV{PERL_CORE} = 1;
acb65a20 278
902aaf3e
CB
279 my $makefile;
280 if ($is_VMS) {
281 $makefile = 'descrip.mms';
282 if ($target =~ /clean$/
283 && !-f $makefile
284 && -f "${makefile}_old") {
285 $makefile = "${makefile}_old";
286 }
287 } else {
288 $makefile = 'Makefile';
289 }
fc678412 290
baff067e
FC
291 if (-f $makefile) {
292 open my $mfh, $makefile or die "Cannot open $makefile: $!";
293 while (<$mfh>) {
294 # Plagiarised from CPAN::Distribution
295 last if /MakeMaker post_initialize section/;
296 next unless /^#\s+VERSION_FROM\s+=>\s+(.+)/;
297 my $vmod = eval $1;
298 my $oldv;
299 while (<$mfh>) {
300 next unless /^XS_VERSION = (\S+)/;
301 $oldv = $1;
302 last;
303 }
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) {
27329181
DD
308 close $mfh or die "close $makefile: $!";
309 _unlink($makefile);
310 {
311 no warnings 'deprecated';
312 goto NO_MAKEFILE;
313 }
baff067e
FC
314 }
315 }
27329181
DD
316 if(IS_CROSS){
317 seek($mfh, 0, 0) or die "Cannot seek $makefile: $!";
318 while (<$mfh>) {
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 ';
322 if(/^$header.+?
323 (xlib[\/\\]
324 $::Cross::platform\Q\/Config.pm\E)?\)\./x) {
325 unless (defined $1){
326 print "Deleting non-Cross makefile\n";
327 close $mfh or die "close $makefile: $!";
328 _unlink($makefile);
329 {
330 no warnings 'deprecated';
331 goto NO_MAKEFILE;
332 }
333 } else { #have a cross makefile
334 goto CROSS_OK_MF;
335 }
336 }
337 } #catch breakage from future changes
338 die "non-standard makefile found in $mname";
339 CROSS_OK_MF:
340 }
baff067e
FC
341 }
342
902aaf3e 343 if (!-f $makefile) {
27329181 344 NO_MAKEFILE:
e74f76b2
NC
345 if (!-f 'Makefile.PL') {
346 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
d3bc6aa6
NC
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
351 # complex
352 $fromname = 'VERSION';
353 $key = 'DISTNAME';
354 $value = 'podlators';
355 $mname = 'Pod';
356 } else {
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");
f6d098c6 364 unshift @locations, 'lib/IO/Compress/Base.pm' if $mname eq 'IO::Compress';
d3bc6aa6
NC
365 foreach (@locations) {
366 if (-f $_) {
367 $fromname = $_;
368 last;
369 }
e74f76b2 370 }
e74f76b2 371
d3bc6aa6
NC
372 unless ($fromname) {
373 die "For $mname tried @locations in in $ext_dir but can't find source";
374 }
375 ($value = $fromname) =~ s/\.pm\z/.pod/;
376 $value = $fromname unless -e $value;
e74f76b2
NC
377 }
378 open my $fh, '>', 'Makefile.PL'
379 or die "Can't open Makefile.PL for writing: $!";
d3bc6aa6 380 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
e74f76b2
NC
381#-*- buffer-read-only: t -*-
382
13b5e8d8 383# This Makefile.PL was written by %s.
e74f76b2
NC
384# It will be deleted automatically by make realclean
385
386use strict;
387use ExtUtils::MakeMaker;
388
13b5e8d8
NC
389# This is what the .PL extracts to. Not the ultimate file that is installed.
390# (ie Win32 runs pl2bat after this)
391
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
394# hash.
395my @temps = 'Makefile.PL';
396foreach (glob('scripts/pod*.PL')) {
8abc1060 397 # The various pod*.PL extractors change directory. Doing that with relative
13b5e8d8
NC
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)
402 require File::Copy;
403
404 my $temp = $_;
405 $temp =~ s!scripts/!!;
406 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
407 push @temps, $temp;
408}
409
410my $script_ext = $^O eq 'VMS' ? '.com' : '';
411my %%pod_scripts;
412foreach (glob('pod*.PL')) {
413 my $script = $_;
9ed9b795 414 s/.PL$/$script_ext/i;
13b5e8d8
NC
415 $pod_scripts{$script} = $_;
416}
417my @exe_files = values %%pod_scripts;
418
e74f76b2 419WriteMakefile(
13b5e8d8
NC
420 NAME => '%s',
421 VERSION_FROM => '%s',
d3bc6aa6 422 %-13s => '%s',
13b5e8d8
NC
423 realclean => { FILES => "@temps" },
424 (%%pod_scripts ? (
425 PL_FILES => \%%pod_scripts,
426 EXE_FILES => \@exe_files,
427 clean => { FILES => "@exe_files" },
428 ) : ()),
e74f76b2
NC
429);
430
431# ex: set ro:
432EOM
433 close $fh or die "Can't close Makefile.PL: $!";
43f197b5
NC
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.
440 eval {
441 my $ftime = time - 4;
442 utime $ftime, $ftime, 'Makefile.PL';
443 };
e74f76b2 444 }
fc678412
NC
445 print "\nRunning Makefile.PL in $ext_dir\n";
446
447 # Presumably this can be simplified
448 my @cross;
27329181 449 if (IS_CROSS) {
fc678412
NC
450 # Inherited from win32/buildext.pl
451 @cross = "-MCross=$::Cross::platform";
452 } elsif ($opts{cross}) {
453 # Inherited from make_ext.pl
454 @cross = '-MCross';
a2f19a19 455 }
23525070 456
5e4c4c91 457 my @args = ("-I$lib_dir", @cross, 'Makefile.PL');
902aaf3e
CB
458 if ($is_VMS) {
459 my $libd = VMS::Filespec::vmspath($lib_dir);
460 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
461 } else {
a2b175af
NC
462 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
463 'INSTALLMAN3DIR=none';
902aaf3e
CB
464 }
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;
fc678412
NC
469 warn "$code from $ext_dir\'s Makefile.PL" if $code;
470
61edc683
NC
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
475 # ./miniperl
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
481 # this.
e3b84025 482 if ($is_Unix) {
fc678412
NC
483 my $suffix = '.sh';
484 foreach my $clean_target ('realclean', 'veryclean') {
ca5de986 485 my $file = "$return_dir/$clean_target$suffix";
fc678412
NC
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: $!";
490 print $fh <<"EOS";
491cd $ext_dir
492if test ! -f Makefile -a -f Makefile.old; then
61edc683 493 echo "Note: Using Makefile.old"
eb1f8df7 494 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 495else
fc678412 496 if test ! -f Makefile ; then
61edc683
NC
497 echo "Warning: No Makefile!"
498 fi
eb1f8df7 499 make $clean_target MAKE='@make' @pass_through
61edc683 500fi
fc678412 501cd $return_dir
61edc683 502EOS
fc678412
NC
503 close $fh or die "close $file: $!";
504 }
61edc683 505 }
fc678412 506 }
a2f19a19 507
902aaf3e 508 if (not -f $makefile) {
a2f19a19 509 print "Warning: No Makefile!\n";
fc678412 510 }
a2f19a19 511
902aaf3e 512 if ($is_VMS) {
e0697720
NC
513 _quote_args($pass_through);
514 @$pass_through = (
515 "/DESCRIPTION=$makefile",
516 '/MACRO=(' . join(',',@$pass_through) . ')'
517 );
902aaf3e
CB
518 }
519
fc678412 520 if (!$target or $target !~ /clean$/) {
a2f19a19 521 # Give makefile an opportunity to rewrite itself.
75f92628 522 # reassure users that life goes on...
902aaf3e 523 my @args = ('config', @$pass_through);
902aaf3e 524 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
fc678412 525 }
902aaf3e 526 my @targ = ($target, @$pass_through);
902aaf3e
CB
527 print "Making $target in $ext_dir\n@run @make @targ\n";
528 my $code = system(@run, @make, @targ);
fc678412 529 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 530
fc678412
NC
531 chdir $return_dir || die "Cannot cd to $return_dir: $!";
532}
902aaf3e
CB
533
534sub _quote_args {
535 my $args = shift; # must be array reference
536
537 # Do not quote qualifiers that begin with '/'.
538 map { if (!/^\//) {
539 $_ =~ s/\"/""/g; # escape C<"> by doubling
540 $_ = q(").$_.q(");
541 }
542 } @{$args}
543 ;
544}
27329181
DD
545
546#guarentee that a file is deleted or die, void _unlink($filename)
547#xxx replace with _unlink_or_rename from EU::Install?
548sub _unlink {
549 1 while unlink $_[0];
550 my $err = $!;
551 die "Can't unlink $_[0]: $err" if -f $_[0];
552}