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