This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Makefile.SH: Remove remnants of the old cross-compilation model
[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};
a2f19a19 123
a0d0e21e 124
07f3cc2a 125if ($target eq '') {
ca5de986
NC
126 die "make_ext: no make target specified (eg all or clean)\n";
127} elsif ($target !~ /(?:^all|clean)$/) {
128 # for the time being we are strict about what make_ext is used for
129 die "$0: unknown make target '$target'\n";
a2f19a19
S
130}
131
281da5ea 132if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
286d62c2
NC
133 die "$0: no extension specified\n";
134}
135
136my $perl;
137my %extra_passthrough;
138
139if ($is_Win32) {
14636674
NC
140 require Cwd;
141 require FindExt;
142 my $build = Cwd::getcwd();
286d62c2
NC
143 $perl = $^X;
144 if ($perl =~ m#^\.\.#) {
bc3847b4
NC
145 my $here = $build;
146 $here =~ s{/}{\\}g;
286d62c2
NC
147 $perl = "$here\\$perl";
148 }
149 (my $topdir = $perl) =~ s/\\[^\\]+$//;
150 # miniperl needs to find perlglob and pl2bat
151 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
152 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
153 unless (-f "$pl2bat.bat") {
d7f84378 154 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
286d62c2 155 print "@args\n";
27329181 156 system(@args) unless IS_CROSS;
286d62c2
NC
157 }
158
a2ac9a71 159 print "In $build";
7c40aa71 160 foreach my $dir (@dirs) {
a2ac9a71 161 chdir($dir) or die "Cannot cd to $dir: $!\n";
14636674 162 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
7c40aa71
NC
163 FindExt::scan_ext($ext);
164 FindExt::set_static_extensions(split ' ', $Config{static_ext});
a85e0e8c
SH
165 chdir $build
166 or die "Couldn't chdir to '$build': $!"; # restore our start directory
167 }
7c40aa71 168
a85e0e8c
SH
169 my @ext;
170 push @ext, FindExt::static_ext() if $static;
171 push @ext, FindExt::dynamic_ext() if $dynamic;
172 push @ext, FindExt::nonxs_ext() if $nonxs;
173 push @ext, 'DynaLoader' if $dynaloader;
7c40aa71 174
a85e0e8c
SH
175 foreach (sort @ext) {
176 if (%incl and !exists $incl{$_}) {
177 #warn "Skipping extension $_, not in inclusion list\n";
178 next;
179 }
180 if (exists $excl{$_}) {
181 warn "Skipping extension $_, not ported to current platform";
182 next;
183 }
184 push @extspec, $_;
185 if($_ eq 'DynaLoader' and $target !~ /clean$/) {
186 # No, we don't know why nmake can't work out the dependency chain
187 push @{$extra_passthrough{$_}}, 'DynaLoader.c';
188 } elsif(FindExt::is_static($_)) {
189 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
286d62c2
NC
190 }
191 }
a85e0e8c 192
a2ac9a71 193 chdir '..'
a85e0e8c 194 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
286d62c2 195}
902aaf3e
CB
196elsif ($is_VMS) {
197 $perl = $^X;
198 push @extspec, (split ' ', $Config{static_ext}) if $static;
199 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
a34ce875 200 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
281da5ea 201 push @extspec, 'DynaLoader' if $dynaloader;
902aaf3e 202}
286d62c2 203
dc0655f7
NC
204{
205 # Cwd needs to be built before Encode recurses into subdirectories.
1179df5f 206 # Pod::Simple needs to be built before Pod::Functions
dc0655f7
NC
207 # This seems to be the simplest way to ensure this ordering:
208 my (@first, @other);
209 foreach (@extspec) {
1179df5f 210 if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') {
dc0655f7
NC
211 push @first, $_;
212 } else {
213 push @other, $_;
214 }
215 }
216 @extspec = (@first, @other);
217}
218
91c30809
NC
219if ($Config{osname} eq 'catamount' and @extspec) {
220 # Snowball's chance of building extensions.
221 die "This is $Config{osname}, not building $extspec[0], sorry.\n";
222}
223
e08c66ce
NC
224foreach my $spec (@extspec) {
225 my $mname = $spec;
ca5de986 226 $mname =~ s!/!::!g;
238a6851 227 my $ext_pathname;
a28b98e8
NC
228
229 # Try new style ext/Data-Dumper/ first
230 my $copy = $spec;
231 $copy =~ tr!/!-!;
cb8c8458
SH
232
233 # List/Util.xs lives in Scalar-List-Utils, Cwd.xs lives in PathTools
234 $copy = 'Scalar-List-Utils' if $copy eq 'List-Util';
235 $copy = 'PathTools' if $copy eq 'Cwd';
236
a28b98e8
NC
237 foreach my $dir (@ext_dirs) {
238 if (-d "$dir/$copy") {
239 $ext_pathname = "$dir/$copy";
240 last;
8a992763 241 }
a28b98e8
NC
242 }
243
244 if (!defined $ext_pathname) {
245 if (-d "ext/$spec") {
246 # Old style ext/Data/Dumper/
247 $ext_pathname = "ext/$spec";
248 } else {
8a992763
NC
249 warn "Can't find extension $spec in any of @ext_dirs";
250 next;
251 }
238a6851 252 }
a2f19a19 253
ca5de986 254 print "\tMaking $mname ($target)\n";
a2f19a19 255
acb65a20 256 build_extension($ext_pathname, $perl, $mname,
e08c66ce 257 [@pass_through, @{$extra_passthrough{$spec} || []}]);
ca5de986 258}
a0d0e21e 259
fc678412 260sub build_extension {
acb65a20
NC
261 my ($ext_dir, $perl, $mname, $pass_through) = @_;
262
a85e0e8c
SH
263 unless (chdir "$ext_dir") {
264 warn "Cannot cd to $ext_dir: $!";
265 return;
266 }
267
acb65a20
NC
268 my $up = $ext_dir;
269 $up =~ s![^/]+!..!g;
270
271 $perl ||= "$up/miniperl";
272 my $return_dir = $up;
273 my $lib_dir = "$up/lib";
6c8b0117 274 $ENV{PERL_CORE} = 1;
acb65a20 275
902aaf3e
CB
276 my $makefile;
277 if ($is_VMS) {
278 $makefile = 'descrip.mms';
279 if ($target =~ /clean$/
280 && !-f $makefile
281 && -f "${makefile}_old") {
282 $makefile = "${makefile}_old";
283 }
284 } else {
285 $makefile = 'Makefile';
286 }
fc678412 287
baff067e
FC
288 if (-f $makefile) {
289 open my $mfh, $makefile or die "Cannot open $makefile: $!";
290 while (<$mfh>) {
291 # Plagiarised from CPAN::Distribution
292 last if /MakeMaker post_initialize section/;
293 next unless /^#\s+VERSION_FROM\s+=>\s+(.+)/;
294 my $vmod = eval $1;
295 my $oldv;
296 while (<$mfh>) {
297 next unless /^XS_VERSION = (\S+)/;
298 $oldv = $1;
299 last;
300 }
301 last unless defined $oldv;
302 require ExtUtils::MM_Unix;
303 defined (my $newv = parse_version MM $vmod) or last;
304 if ($newv ne $oldv) {
27329181
DD
305 close $mfh or die "close $makefile: $!";
306 _unlink($makefile);
307 {
308 no warnings 'deprecated';
309 goto NO_MAKEFILE;
310 }
baff067e
FC
311 }
312 }
27329181
DD
313 if(IS_CROSS){
314 seek($mfh, 0, 0) or die "Cannot seek $makefile: $!";
315 while (<$mfh>) {
316 #this is used to stop the while loop early for efficiency when
317 #the line is reached, and possibly match a cross build
318 my $header = quotemeta '# These definitions are from config.sh (via ';
319 if(/^$header.+?
320 (xlib[\/\\]
321 $::Cross::platform\Q\/Config.pm\E)?\)\./x) {
322 unless (defined $1){
323 print "Deleting non-Cross makefile\n";
324 close $mfh or die "close $makefile: $!";
325 _unlink($makefile);
326 {
327 no warnings 'deprecated';
328 goto NO_MAKEFILE;
329 }
330 } else { #have a cross makefile
331 goto CROSS_OK_MF;
332 }
333 }
334 } #catch breakage from future changes
335 die "non-standard makefile found in $mname";
336 CROSS_OK_MF:
337 }
baff067e
FC
338 }
339
902aaf3e 340 if (!-f $makefile) {
27329181 341 NO_MAKEFILE:
e74f76b2
NC
342 if (!-f 'Makefile.PL') {
343 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
d3bc6aa6
NC
344 my ($fromname, $key, $value);
345 if ($mname eq 'podlators') {
346 # We need to special case this somewhere, and this is fewer
347 # lines of code than a core-only Makefile.PL, and no more
348 # complex
349 $fromname = 'VERSION';
350 $key = 'DISTNAME';
351 $value = 'podlators';
352 $mname = 'Pod';
353 } else {
354 $key = 'ABSTRACT_FROM';
355 # We need to cope well with various possible layouts
356 my @dirs = split /::/, $mname;
357 my $leaf = pop @dirs;
358 my $leafname = "$leaf.pm";
359 my $pathname = join '/', @dirs, $leafname;
360 my @locations = ($leafname, $pathname, "lib/$pathname");
f6d098c6 361 unshift @locations, 'lib/IO/Compress/Base.pm' if $mname eq 'IO::Compress';
d3bc6aa6
NC
362 foreach (@locations) {
363 if (-f $_) {
364 $fromname = $_;
365 last;
366 }
e74f76b2 367 }
e74f76b2 368
d3bc6aa6
NC
369 unless ($fromname) {
370 die "For $mname tried @locations in in $ext_dir but can't find source";
371 }
372 ($value = $fromname) =~ s/\.pm\z/.pod/;
373 $value = $fromname unless -e $value;
e74f76b2
NC
374 }
375 open my $fh, '>', 'Makefile.PL'
376 or die "Can't open Makefile.PL for writing: $!";
d3bc6aa6 377 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
e74f76b2
NC
378#-*- buffer-read-only: t -*-
379
13b5e8d8 380# This Makefile.PL was written by %s.
e74f76b2
NC
381# It will be deleted automatically by make realclean
382
383use strict;
384use ExtUtils::MakeMaker;
385
13b5e8d8
NC
386# This is what the .PL extracts to. Not the ultimate file that is installed.
387# (ie Win32 runs pl2bat after this)
388
389# Doing this here avoids all sort of quoting issues that would come from
390# attempting to write out perl source with literals to generate the arrays and
391# hash.
392my @temps = 'Makefile.PL';
393foreach (glob('scripts/pod*.PL')) {
8abc1060 394 # The various pod*.PL extractors change directory. Doing that with relative
13b5e8d8
NC
395 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
396 # the chdir doing anything, than to attempt to convert lib paths to
397 # absolute, and potentially run into problems with quoting special
398 # characters in the path to our build dir (such as spaces)
399 require File::Copy;
400
401 my $temp = $_;
402 $temp =~ s!scripts/!!;
403 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
404 push @temps, $temp;
405}
406
407my $script_ext = $^O eq 'VMS' ? '.com' : '';
408my %%pod_scripts;
409foreach (glob('pod*.PL')) {
410 my $script = $_;
9ed9b795 411 s/.PL$/$script_ext/i;
13b5e8d8
NC
412 $pod_scripts{$script} = $_;
413}
414my @exe_files = values %%pod_scripts;
415
e74f76b2 416WriteMakefile(
13b5e8d8
NC
417 NAME => '%s',
418 VERSION_FROM => '%s',
d3bc6aa6 419 %-13s => '%s',
13b5e8d8
NC
420 realclean => { FILES => "@temps" },
421 (%%pod_scripts ? (
422 PL_FILES => \%%pod_scripts,
423 EXE_FILES => \@exe_files,
424 clean => { FILES => "@exe_files" },
425 ) : ()),
e74f76b2
NC
426);
427
428# ex: set ro:
429EOM
430 close $fh or die "Can't close Makefile.PL: $!";
43f197b5
NC
431 # As described in commit 23525070d6c0e51f:
432 # Push the atime and mtime of generated Makefile.PLs back 4
433 # seconds. In certain circumstances ( on virtual machines ) the
434 # generated Makefile.PL can produce a Makefile that is older than
435 # the Makefile.PL. Altering the atime and mtime backwards by 4
436 # seconds seems to resolve the issue.
437 eval {
438 my $ftime = time - 4;
439 utime $ftime, $ftime, 'Makefile.PL';
440 };
e74f76b2 441 }
fc678412
NC
442 print "\nRunning Makefile.PL in $ext_dir\n";
443
444 # Presumably this can be simplified
445 my @cross;
27329181 446 if (IS_CROSS) {
fc678412
NC
447 # Inherited from win32/buildext.pl
448 @cross = "-MCross=$::Cross::platform";
449 } elsif ($opts{cross}) {
450 # Inherited from make_ext.pl
451 @cross = '-MCross';
a2f19a19 452 }
23525070 453
5e4c4c91 454 my @args = ("-I$lib_dir", @cross, 'Makefile.PL');
902aaf3e
CB
455 if ($is_VMS) {
456 my $libd = VMS::Filespec::vmspath($lib_dir);
457 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
458 } else {
a2b175af
NC
459 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
460 'INSTALLMAN3DIR=none';
902aaf3e
CB
461 }
462 push @args, @$pass_through;
463 _quote_args(\@args) if $is_VMS;
d708ee4b
JR
464 print join(' ', $perl, @args), "\n";
465 my $code = system $perl, @args;
fc678412
NC
466 warn "$code from $ext_dir\'s Makefile.PL" if $code;
467
61edc683
NC
468 # Right. The reason for this little hack is that we're sitting inside
469 # a program run by ./miniperl, but there are tasks we need to perform
470 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
471 # Unfortunately, they can be run *after* 'clean', which deletes
472 # ./miniperl
473 # So we do our best to leave a set of instructions identical to what
474 # we would do if we are run directly as 'realclean' etc
475 # Whilst we're perfect, unfortunately the targets we call are not, as
476 # some of them rely on a $(PERL) for their own distclean targets.
477 # But this always used to be a problem with the old /bin/sh version of
478 # this.
e3b84025 479 if ($is_Unix) {
fc678412
NC
480 my $suffix = '.sh';
481 foreach my $clean_target ('realclean', 'veryclean') {
ca5de986 482 my $file = "$return_dir/$clean_target$suffix";
fc678412
NC
483 open my $fh, '>>', $file or die "open $file: $!";
484 # Quite possible that we're being run in parallel here.
485 # Can't use Fcntl this early to get the LOCK_EX
486 flock $fh, 2 or warn "flock $file: $!";
487 print $fh <<"EOS";
488cd $ext_dir
489if test ! -f Makefile -a -f Makefile.old; then
61edc683 490 echo "Note: Using Makefile.old"
eb1f8df7 491 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 492else
fc678412 493 if test ! -f Makefile ; then
61edc683
NC
494 echo "Warning: No Makefile!"
495 fi
eb1f8df7 496 make $clean_target MAKE='@make' @pass_through
61edc683 497fi
fc678412 498cd $return_dir
61edc683 499EOS
fc678412
NC
500 close $fh or die "close $file: $!";
501 }
61edc683 502 }
fc678412 503 }
a2f19a19 504
902aaf3e 505 if (not -f $makefile) {
a2f19a19 506 print "Warning: No Makefile!\n";
fc678412 507 }
a2f19a19 508
902aaf3e 509 if ($is_VMS) {
e0697720
NC
510 _quote_args($pass_through);
511 @$pass_through = (
512 "/DESCRIPTION=$makefile",
513 '/MACRO=(' . join(',',@$pass_through) . ')'
514 );
902aaf3e
CB
515 }
516
fc678412 517 if (!$target or $target !~ /clean$/) {
a2f19a19 518 # Give makefile an opportunity to rewrite itself.
75f92628 519 # reassure users that life goes on...
902aaf3e 520 my @args = ('config', @$pass_through);
d708ee4b 521 system(@make, @args) and print "@make @args failed, continuing anyway...\n";
fc678412 522 }
902aaf3e 523 my @targ = ($target, @$pass_through);
d708ee4b
JR
524 print "Making $target in $ext_dir\n@make @targ\n";
525 my $code = system(@make, @targ);
fc678412 526 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 527
fc678412
NC
528 chdir $return_dir || die "Cannot cd to $return_dir: $!";
529}
902aaf3e
CB
530
531sub _quote_args {
532 my $args = shift; # must be array reference
533
534 # Do not quote qualifiers that begin with '/'.
535 map { if (!/^\//) {
536 $_ =~ s/\"/""/g; # escape C<"> by doubling
537 $_ = q(").$_.q(");
538 }
539 } @{$args}
540 ;
541}
27329181
DD
542
543#guarentee that a file is deleted or die, void _unlink($filename)
544#xxx replace with _unlink_or_rename from EU::Install?
545sub _unlink {
546 1 while unlink $_[0];
547 my $err = $!;
548 die "Can't unlink $_[0]: $err" if -f $_[0];
549}