This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make sv_2bool_flags() non-recursive on overload
[perl5.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19
SM
2use strict;
3use warnings;
b8d39eba 4use Config;
75f92628 5
91c30809
NC
6my $is_Win32 = $^O eq 'MSWin32';
7my $is_VMS = $^O eq 'VMS';
8my $is_Unix = !$is_Win32 && !$is_VMS;
9
a193a2db 10my @ext_dirs = qw(cpan dist ext);
321c3589 11my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')';
8a992763 12
a0d0e21e 13# This script acts as a simple interface for building extensions.
286d62c2
NC
14
15# It's actually a cut and shut of the Unix version ext/utils/makeext and the
16# Windows version win32/build_ext.pl hence the two invocation styles.
17
8abc1060 18# On Unix, it primarily used by the perl Makefile one extension at a time:
a0d0e21e
LW
19#
20# d_dummy $(dynamic_ext): miniperl preplibrary FORCE
e2fabae1 21# @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
a0d0e21e 22#
902aaf3e 23# On Windows or VMS,
286d62c2 24# If '--static' is specified, static extensions will be built.
a34ce875
NC
25# If '--dynamic' is specified, dynamic extensions will be built.
26# If '--nonxs' is specified, nonxs extensions will be built.
8abc1060 27# If '--dynaloader' is specified, DynaLoader will be built.
286d62c2
NC
28# If '--all' is specified, all extensions will be built.
29#
30# make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
31#
32# E.g.
33#
34# make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
35#
36# make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
37#
38# make_ext.pl MAKE=dmake --dir=..\ext
39#
40# make_ext.pl MAKE=dmake --dir=..\ext --target=clean
41#
42# Will skip building extensions which are marked with an '!' char.
43# Mostly because they still not ported to specified platform.
44#
45# If any extensions are listed with a '+' char then only those
2effe01f 46# extensions will be built, but only if they aren't countermanded
286d62c2 47# by an '!ext' and are appropriate to the type of building being done.
58ff4bd0 48# An extensions follows the format of Foo/Bar, which would be extension Foo::Bar
286d62c2 49
a0d0e21e
LW
50# It may be deleted in a later release of perl so try to
51# avoid using it for other purposes.
52
fc678412 53my (%excl, %incl, %opts, @extspec, @pass_through);
97a26ad9
NC
54
55foreach (@ARGV) {
56 if (/^!(.*)$/) {
57 $excl{$1} = 1;
58 } elsif (/^\+(.*)$/) {
59 $incl{$1} = 1;
60 } elsif (/^--([\w\-]+)$/) {
61 $opts{$1} = 1;
e2fabae1 62 } elsif (/^--([\w\-]+)=(.*)$/) {
7c40aa71 63 push @{$opts{$1}}, $2;
e2fabae1 64 } elsif (/=/) {
fc678412 65 push @pass_through, $_;
ca5de986 66 } elsif (length) {
e2fabae1 67 push @extspec, $_;
97a26ad9
NC
68 }
69}
70
286d62c2
NC
71my $static = $opts{static} || $opts{all};
72my $dynamic = $opts{dynamic} || $opts{all};
a34ce875 73my $nonxs = $opts{nonxs} || $opts{all};
281da5ea 74my $dynaloader = $opts{dynaloader} || $opts{all};
286d62c2 75
ca5de986
NC
76# The Perl Makefile.SH will expand all extensions to
77# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
78# A user wishing to run make_ext might use
79# X (or X/Y or X::Y if nested)
80
81# canonise into X/Y form (pname)
82
83foreach (@extspec) {
7ad017a8 84 if (s{^lib/auto/}{}) {
ca5de986 85 # Remove lib/auto prefix and /*.* suffix
c5aac6ab 86 s{/[^/]+\.[^/]+$}{};
321c3589 87 } elsif (s{^$ext_dirs_re/}{}) {
ca5de986 88 # Remove ext/ prefix and /pm_to_blib suffix
ca5de986 89 s{/pm_to_blib$}{};
57df1c46
NC
90 # Targets are given as files on disk, but the extension spec is still
91 # written using /s for each ::
92 tr!-!/!;
7ad017a8 93 } elsif (s{::}{\/}g) {
ca5de986 94 # Convert :: to /
7ad017a8 95 } else {
ca5de986
NC
96 s/\..*o//;
97 }
98}
99
fc678412
NC
100my $makecmd = shift @pass_through; # Should be something like MAKE=make
101unshift @pass_through, 'PERL_CORE=1';
102
9afc198b 103my @dirs = @{$opts{dir} || \@ext_dirs};
7c40aa71 104my $target = $opts{target}[0];
07f3cc2a 105$target = 'all' unless defined $target;
a0d0e21e 106
fb73857a
PP
107# Previously, $make was taken from config.sh. However, the user might
108# instead be running a possibly incompatible make. This might happen if
109# the user types "gmake" instead of a plain "make", for example. The
110# correct current value of MAKE will come through from the main perl
111# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
112# case third party users of this script (are there any?) don't have the
113# MAKE=$(MAKE) argument, which was added after 5.004_03.
ca5de986
NC
114unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
115 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
a2f19a19
SM
116}
117
eb1f8df7
NC
118# This isn't going to cope with anything fancy, such as spaces inside command
119# names, but neither did what it replaced. Once there is a use case that needs
120# it, please supply patches. Until then, I'm sticking to KISS
121my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
ca5de986 122# Using an array of 0 or 1 elements makes the subsequent code simpler.
fc678412
NC
123my @run = $Config{run};
124@run = () if not defined $run[0] or $run[0] eq '';
a2f19a19 125
a0d0e21e 126
07f3cc2a 127if ($target eq '') {
ca5de986
NC
128 die "make_ext: no make target specified (eg all or clean)\n";
129} elsif ($target !~ /(?:^all|clean)$/) {
130 # for the time being we are strict about what make_ext is used for
131 die "$0: unknown make target '$target'\n";
a2f19a19
SM
132}
133
281da5ea 134if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
286d62c2
NC
135 die "$0: no extension specified\n";
136}
137
138my $perl;
139my %extra_passthrough;
140
141if ($is_Win32) {
14636674
NC
142 require Cwd;
143 require FindExt;
144 my $build = Cwd::getcwd();
286d62c2
NC
145 $perl = $^X;
146 if ($perl =~ m#^\.\.#) {
bc3847b4
NC
147 my $here = $build;
148 $here =~ s{/}{\\}g;
286d62c2
NC
149 $perl = "$here\\$perl";
150 }
151 (my $topdir = $perl) =~ s/\\[^\\]+$//;
152 # miniperl needs to find perlglob and pl2bat
153 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
154 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
155 unless (-f "$pl2bat.bat") {
d7f84378 156 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
286d62c2
NC
157 print "@args\n";
158 system(@args) unless defined $::Cross::platform;
159 }
160
a2ac9a71 161 print "In $build";
7c40aa71 162 foreach my $dir (@dirs) {
a2ac9a71 163 chdir($dir) or die "Cannot cd to $dir: $!\n";
14636674 164 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
7c40aa71
NC
165 FindExt::scan_ext($ext);
166 FindExt::set_static_extensions(split ' ', $Config{static_ext});
a85e0e8c
SH
167 chdir $build
168 or die "Couldn't chdir to '$build': $!"; # restore our start directory
169 }
7c40aa71 170
a85e0e8c
SH
171 my @ext;
172 push @ext, FindExt::static_ext() if $static;
173 push @ext, FindExt::dynamic_ext() if $dynamic;
174 push @ext, FindExt::nonxs_ext() if $nonxs;
175 push @ext, 'DynaLoader' if $dynaloader;
7c40aa71 176
a85e0e8c
SH
177 foreach (sort @ext) {
178 if (%incl and !exists $incl{$_}) {
179 #warn "Skipping extension $_, not in inclusion list\n";
180 next;
181 }
182 if (exists $excl{$_}) {
183 warn "Skipping extension $_, not ported to current platform";
184 next;
185 }
186 push @extspec, $_;
187 if($_ eq 'DynaLoader' and $target !~ /clean$/) {
188 # No, we don't know why nmake can't work out the dependency chain
189 push @{$extra_passthrough{$_}}, 'DynaLoader.c';
190 } elsif(FindExt::is_static($_)) {
191 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
286d62c2
NC
192 }
193 }
a85e0e8c 194
a2ac9a71 195 chdir '..'
a85e0e8c 196 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
286d62c2 197}
902aaf3e
CB
198elsif ($is_VMS) {
199 $perl = $^X;
200 push @extspec, (split ' ', $Config{static_ext}) if $static;
201 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
a34ce875 202 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
281da5ea 203 push @extspec, 'DynaLoader' if $dynaloader;
902aaf3e 204}
286d62c2 205
dc0655f7
NC
206{
207 # Cwd needs to be built before Encode recurses into subdirectories.
1179df5f 208 # Pod::Simple needs to be built before Pod::Functions
dc0655f7
NC
209 # This seems to be the simplest way to ensure this ordering:
210 my (@first, @other);
211 foreach (@extspec) {
1179df5f 212 if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') {
dc0655f7
NC
213 push @first, $_;
214 } else {
215 push @other, $_;
216 }
217 }
218 @extspec = (@first, @other);
219}
220
91c30809
NC
221if ($Config{osname} eq 'catamount' and @extspec) {
222 # Snowball's chance of building extensions.
223 die "This is $Config{osname}, not building $extspec[0], sorry.\n";
224}
225
e08c66ce
NC
226foreach my $spec (@extspec) {
227 my $mname = $spec;
ca5de986 228 $mname =~ s!/!::!g;
238a6851 229 my $ext_pathname;
a28b98e8
NC
230
231 # Try new style ext/Data-Dumper/ first
232 my $copy = $spec;
233 $copy =~ tr!/!-!;
234 foreach my $dir (@ext_dirs) {
235 if (-d "$dir/$copy") {
236 $ext_pathname = "$dir/$copy";
237 last;
8a992763 238 }
a28b98e8
NC
239 }
240
241 if (!defined $ext_pathname) {
242 if (-d "ext/$spec") {
243 # Old style ext/Data/Dumper/
244 $ext_pathname = "ext/$spec";
245 } else {
8a992763
NC
246 warn "Can't find extension $spec in any of @ext_dirs";
247 next;
248 }
238a6851 249 }
a2f19a19 250
ca5de986 251 print "\tMaking $mname ($target)\n";
a2f19a19 252
acb65a20 253 build_extension($ext_pathname, $perl, $mname,
e08c66ce 254 [@pass_through, @{$extra_passthrough{$spec} || []}]);
ca5de986 255}
a0d0e21e 256
fc678412 257sub build_extension {
acb65a20
NC
258 my ($ext_dir, $perl, $mname, $pass_through) = @_;
259
a85e0e8c
SH
260 unless (chdir "$ext_dir") {
261 warn "Cannot cd to $ext_dir: $!";
262 return;
263 }
264
acb65a20
NC
265 my $up = $ext_dir;
266 $up =~ s![^/]+!..!g;
267
268 $perl ||= "$up/miniperl";
269 my $return_dir = $up;
270 my $lib_dir = "$up/lib";
6c8b0117 271 $ENV{PERL_CORE} = 1;
acb65a20 272
902aaf3e
CB
273 my $makefile;
274 if ($is_VMS) {
275 $makefile = 'descrip.mms';
276 if ($target =~ /clean$/
277 && !-f $makefile
278 && -f "${makefile}_old") {
279 $makefile = "${makefile}_old";
280 }
281 } else {
282 $makefile = 'Makefile';
283 }
fc678412 284
baff067e
FC
285 if (-f $makefile) {
286 open my $mfh, $makefile or die "Cannot open $makefile: $!";
287 while (<$mfh>) {
288 # Plagiarised from CPAN::Distribution
289 last if /MakeMaker post_initialize section/;
290 next unless /^#\s+VERSION_FROM\s+=>\s+(.+)/;
291 my $vmod = eval $1;
292 my $oldv;
293 while (<$mfh>) {
294 next unless /^XS_VERSION = (\S+)/;
295 $oldv = $1;
296 last;
297 }
298 last unless defined $oldv;
299 require ExtUtils::MM_Unix;
300 defined (my $newv = parse_version MM $vmod) or last;
301 if ($newv ne $oldv) {
302 1 while unlink $makefile
303 }
304 }
305 }
306
902aaf3e 307 if (!-f $makefile) {
e74f76b2
NC
308 if (!-f 'Makefile.PL') {
309 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
d3bc6aa6
NC
310 my ($fromname, $key, $value);
311 if ($mname eq 'podlators') {
312 # We need to special case this somewhere, and this is fewer
313 # lines of code than a core-only Makefile.PL, and no more
314 # complex
315 $fromname = 'VERSION';
316 $key = 'DISTNAME';
317 $value = 'podlators';
318 $mname = 'Pod';
319 } else {
320 $key = 'ABSTRACT_FROM';
321 # We need to cope well with various possible layouts
322 my @dirs = split /::/, $mname;
323 my $leaf = pop @dirs;
324 my $leafname = "$leaf.pm";
325 my $pathname = join '/', @dirs, $leafname;
326 my @locations = ($leafname, $pathname, "lib/$pathname");
327 foreach (@locations) {
328 if (-f $_) {
329 $fromname = $_;
330 last;
331 }
e74f76b2 332 }
e74f76b2 333
d3bc6aa6
NC
334 unless ($fromname) {
335 die "For $mname tried @locations in in $ext_dir but can't find source";
336 }
337 ($value = $fromname) =~ s/\.pm\z/.pod/;
338 $value = $fromname unless -e $value;
e74f76b2
NC
339 }
340 open my $fh, '>', 'Makefile.PL'
341 or die "Can't open Makefile.PL for writing: $!";
d3bc6aa6 342 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
e74f76b2
NC
343#-*- buffer-read-only: t -*-
344
13b5e8d8 345# This Makefile.PL was written by %s.
e74f76b2
NC
346# It will be deleted automatically by make realclean
347
348use strict;
349use ExtUtils::MakeMaker;
350
13b5e8d8
NC
351# This is what the .PL extracts to. Not the ultimate file that is installed.
352# (ie Win32 runs pl2bat after this)
353
354# Doing this here avoids all sort of quoting issues that would come from
355# attempting to write out perl source with literals to generate the arrays and
356# hash.
357my @temps = 'Makefile.PL';
358foreach (glob('scripts/pod*.PL')) {
8abc1060 359 # The various pod*.PL extractors change directory. Doing that with relative
13b5e8d8
NC
360 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
361 # the chdir doing anything, than to attempt to convert lib paths to
362 # absolute, and potentially run into problems with quoting special
363 # characters in the path to our build dir (such as spaces)
364 require File::Copy;
365
366 my $temp = $_;
367 $temp =~ s!scripts/!!;
368 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
369 push @temps, $temp;
370}
371
372my $script_ext = $^O eq 'VMS' ? '.com' : '';
373my %%pod_scripts;
374foreach (glob('pod*.PL')) {
375 my $script = $_;
9ed9b795 376 s/.PL$/$script_ext/i;
13b5e8d8
NC
377 $pod_scripts{$script} = $_;
378}
379my @exe_files = values %%pod_scripts;
380
e74f76b2 381WriteMakefile(
13b5e8d8
NC
382 NAME => '%s',
383 VERSION_FROM => '%s',
d3bc6aa6 384 %-13s => '%s',
13b5e8d8
NC
385 realclean => { FILES => "@temps" },
386 (%%pod_scripts ? (
387 PL_FILES => \%%pod_scripts,
388 EXE_FILES => \@exe_files,
389 clean => { FILES => "@exe_files" },
390 ) : ()),
e74f76b2
NC
391);
392
393# ex: set ro:
394EOM
395 close $fh or die "Can't close Makefile.PL: $!";
43f197b5
NC
396 # As described in commit 23525070d6c0e51f:
397 # Push the atime and mtime of generated Makefile.PLs back 4
398 # seconds. In certain circumstances ( on virtual machines ) the
399 # generated Makefile.PL can produce a Makefile that is older than
400 # the Makefile.PL. Altering the atime and mtime backwards by 4
401 # seconds seems to resolve the issue.
402 eval {
403 my $ftime = time - 4;
404 utime $ftime, $ftime, 'Makefile.PL';
405 };
e74f76b2 406 }
fc678412
NC
407 print "\nRunning Makefile.PL in $ext_dir\n";
408
409 # Presumably this can be simplified
410 my @cross;
411 if (defined $::Cross::platform) {
412 # Inherited from win32/buildext.pl
413 @cross = "-MCross=$::Cross::platform";
414 } elsif ($opts{cross}) {
415 # Inherited from make_ext.pl
416 @cross = '-MCross';
a2f19a19 417 }
23525070 418
5e4c4c91 419 my @args = ("-I$lib_dir", @cross, 'Makefile.PL');
902aaf3e
CB
420 if ($is_VMS) {
421 my $libd = VMS::Filespec::vmspath($lib_dir);
422 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
423 } else {
a2b175af
NC
424 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
425 'INSTALLMAN3DIR=none';
902aaf3e
CB
426 }
427 push @args, @$pass_through;
428 _quote_args(\@args) if $is_VMS;
429 print join(' ', @run, $perl, @args), "\n";
430 my $code = system @run, $perl, @args;
fc678412
NC
431 warn "$code from $ext_dir\'s Makefile.PL" if $code;
432
61edc683
NC
433 # Right. The reason for this little hack is that we're sitting inside
434 # a program run by ./miniperl, but there are tasks we need to perform
435 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
436 # Unfortunately, they can be run *after* 'clean', which deletes
437 # ./miniperl
438 # So we do our best to leave a set of instructions identical to what
439 # we would do if we are run directly as 'realclean' etc
440 # Whilst we're perfect, unfortunately the targets we call are not, as
441 # some of them rely on a $(PERL) for their own distclean targets.
442 # But this always used to be a problem with the old /bin/sh version of
443 # this.
e3b84025 444 if ($is_Unix) {
fc678412
NC
445 my $suffix = '.sh';
446 foreach my $clean_target ('realclean', 'veryclean') {
ca5de986 447 my $file = "$return_dir/$clean_target$suffix";
fc678412
NC
448 open my $fh, '>>', $file or die "open $file: $!";
449 # Quite possible that we're being run in parallel here.
450 # Can't use Fcntl this early to get the LOCK_EX
451 flock $fh, 2 or warn "flock $file: $!";
452 print $fh <<"EOS";
453cd $ext_dir
454if test ! -f Makefile -a -f Makefile.old; then
61edc683 455 echo "Note: Using Makefile.old"
eb1f8df7 456 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 457else
fc678412 458 if test ! -f Makefile ; then
61edc683
NC
459 echo "Warning: No Makefile!"
460 fi
eb1f8df7 461 make $clean_target MAKE='@make' @pass_through
61edc683 462fi
fc678412 463cd $return_dir
61edc683 464EOS
fc678412
NC
465 close $fh or die "close $file: $!";
466 }
61edc683 467 }
fc678412 468 }
a2f19a19 469
902aaf3e 470 if (not -f $makefile) {
a2f19a19 471 print "Warning: No Makefile!\n";
fc678412 472 }
a2f19a19 473
902aaf3e 474 if ($is_VMS) {
e0697720
NC
475 _quote_args($pass_through);
476 @$pass_through = (
477 "/DESCRIPTION=$makefile",
478 '/MACRO=(' . join(',',@$pass_through) . ')'
479 );
902aaf3e
CB
480 }
481
fc678412 482 if (!$target or $target !~ /clean$/) {
a2f19a19 483 # Give makefile an opportunity to rewrite itself.
75f92628 484 # reassure users that life goes on...
902aaf3e 485 my @args = ('config', @$pass_through);
902aaf3e 486 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
fc678412 487 }
902aaf3e 488 my @targ = ($target, @$pass_through);
902aaf3e
CB
489 print "Making $target in $ext_dir\n@run @make @targ\n";
490 my $code = system(@run, @make, @targ);
fc678412 491 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 492
fc678412
NC
493 chdir $return_dir || die "Cannot cd to $return_dir: $!";
494}
902aaf3e
CB
495
496sub _quote_args {
497 my $args = shift; # must be array reference
498
499 # Do not quote qualifiers that begin with '/'.
500 map { if (!/^\//) {
501 $_ =~ s/\"/""/g; # escape C<"> by doubling
502 $_ = q(").$_.q(");
503 }
504 } @{$args}
505 ;
506}