This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add slab allocation diagnostics (under perl -DS)
[perl5.git] / make_ext.pl
... / ...
CommitLineData
1#!./miniperl
2use strict;
3use warnings;
4use Config;
5
6my $is_Win32 = $^O eq 'MSWin32';
7my $is_VMS = $^O eq 'VMS';
8my $is_Unix = !$is_Win32 && !$is_VMS;
9
10my @ext_dirs = qw(cpan dist ext);
11my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')';
12
13# This script acts as a simple interface for building extensions.
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
18# On Unix, it primarily used by the perl Makefile one extension at a time:
19#
20# d_dummy $(dynamic_ext): miniperl preplibrary FORCE
21# @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
22#
23# On Windows or VMS,
24# If '--static' is specified, static extensions will be built.
25# If '--dynamic' is specified, dynamic extensions will be built.
26# If '--nonxs' is specified, nonxs extensions will be built.
27# If '--dynaloader' is specified, DynaLoader will be built.
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
46# extensions will be built, but only if they arent countermanded
47# by an '!ext' and are appropriate to the type of building being done.
48
49# It may be deleted in a later release of perl so try to
50# avoid using it for other purposes.
51
52my (%excl, %incl, %opts, @extspec, @pass_through);
53
54foreach (@ARGV) {
55 if (/^!(.*)$/) {
56 $excl{$1} = 1;
57 } elsif (/^\+(.*)$/) {
58 $incl{$1} = 1;
59 } elsif (/^--([\w\-]+)$/) {
60 $opts{$1} = 1;
61 } elsif (/^--([\w\-]+)=(.*)$/) {
62 push @{$opts{$1}}, $2;
63 } elsif (/=/) {
64 push @pass_through, $_;
65 } elsif (length) {
66 push @extspec, $_;
67 }
68}
69
70my $static = $opts{static} || $opts{all};
71my $dynamic = $opts{dynamic} || $opts{all};
72my $nonxs = $opts{nonxs} || $opts{all};
73my $dynaloader = $opts{dynaloader} || $opts{all};
74
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) {
83 if (s{^lib/auto/}{}) {
84 # Remove lib/auto prefix and /*.* suffix
85 s{/[^/]+\.[^/]+$}{};
86 } elsif (s{^$ext_dirs_re/}{}) {
87 # Remove ext/ prefix and /pm_to_blib suffix
88 s{/pm_to_blib$}{};
89 # Targets are given as files on disk, but the extension spec is still
90 # written using /s for each ::
91 tr!-!/!;
92 } elsif (s{::}{\/}g) {
93 # Convert :: to /
94 } else {
95 s/\..*o//;
96 }
97}
98
99my $makecmd = shift @pass_through; # Should be something like MAKE=make
100unshift @pass_through, 'PERL_CORE=1';
101
102my @dirs = @{$opts{dir} || \@ext_dirs};
103my $target = $opts{target}[0];
104$target = 'all' unless defined $target;
105
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.
113unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
114 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
115}
116
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};
121# Using an array of 0 or 1 elements makes the subsequent code simpler.
122my @run = $Config{run};
123@run = () if not defined $run[0] or $run[0] eq '';
124
125
126if ($target eq '') {
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";
131}
132
133if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
134 die "$0: no extension specified\n";
135}
136
137my $perl;
138my %extra_passthrough;
139
140if ($is_Win32) {
141 require Cwd;
142 require FindExt;
143 my $build = Cwd::getcwd();
144 $perl = $^X;
145 if ($perl =~ m#^\.\.#) {
146 my $here = $build;
147 $here =~ s{/}{\\}g;
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") {
155 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
156 print "@args\n";
157 system(@args) unless defined $::Cross::platform;
158 }
159
160 print "In $build";
161 foreach my $dir (@dirs) {
162 chdir($dir) or die "Cannot cd to $dir: $!\n";
163 (my $ext = Cwd::getcwd()) =~ s{/}{\\}g;
164 FindExt::scan_ext($ext);
165 FindExt::set_static_extensions(split ' ', $Config{static_ext});
166 chdir $build
167 or die "Couldn't chdir to '$build': $!"; # restore our start directory
168 }
169
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;
175
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';
191 }
192 }
193
194 chdir '..'
195 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
196}
197elsif ($is_VMS) {
198 $perl = $^X;
199 push @extspec, (split ' ', $Config{static_ext}) if $static;
200 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
201 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
202 push @extspec, 'DynaLoader' if $dynaloader;
203}
204
205{
206 # Cwd needs to be built before Encode recurses into subdirectories.
207 # Pod::Simple needs to be built before Pod::Functions
208 # This seems to be the simplest way to ensure this ordering:
209 my (@first, @other);
210 foreach (@extspec) {
211 if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') {
212 push @first, $_;
213 } else {
214 push @other, $_;
215 }
216 }
217 @extspec = (@first, @other);
218}
219
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
225foreach my $spec (@extspec) {
226 my $mname = $spec;
227 $mname =~ s!/!::!g;
228 my $ext_pathname;
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;
237 }
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 {
245 warn "Can't find extension $spec in any of @ext_dirs";
246 next;
247 }
248 }
249
250 print "\tMaking $mname ($target)\n";
251
252 build_extension($ext_pathname, $perl, $mname,
253 [@pass_through, @{$extra_passthrough{$spec} || []}]);
254}
255
256sub build_extension {
257 my ($ext_dir, $perl, $mname, $pass_through) = @_;
258
259 unless (chdir "$ext_dir") {
260 warn "Cannot cd to $ext_dir: $!";
261 return;
262 }
263
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";
270 $ENV{PERL_CORE} = 1;
271
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 }
283
284 if (!-f $makefile) {
285 if (!-f 'Makefile.PL') {
286 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
287 my ($fromname, $key, $value);
288 if ($mname eq 'podlators') {
289 # We need to special case this somewhere, and this is fewer
290 # lines of code than a core-only Makefile.PL, and no more
291 # complex
292 $fromname = 'VERSION';
293 $key = 'DISTNAME';
294 $value = 'podlators';
295 $mname = 'Pod';
296 } else {
297 $key = 'ABSTRACT_FROM';
298 # We need to cope well with various possible layouts
299 my @dirs = split /::/, $mname;
300 my $leaf = pop @dirs;
301 my $leafname = "$leaf.pm";
302 my $pathname = join '/', @dirs, $leafname;
303 my @locations = ($leafname, $pathname, "lib/$pathname");
304 foreach (@locations) {
305 if (-f $_) {
306 $fromname = $_;
307 last;
308 }
309 }
310
311 unless ($fromname) {
312 die "For $mname tried @locations in in $ext_dir but can't find source";
313 }
314 ($value = $fromname) =~ s/\.pm\z/.pod/;
315 $value = $fromname unless -e $value;
316 }
317 open my $fh, '>', 'Makefile.PL'
318 or die "Can't open Makefile.PL for writing: $!";
319 printf $fh <<'EOM', $0, $mname, $fromname, $key, $value;
320#-*- buffer-read-only: t -*-
321
322# This Makefile.PL was written by %s.
323# It will be deleted automatically by make realclean
324
325use strict;
326use ExtUtils::MakeMaker;
327
328# This is what the .PL extracts to. Not the ultimate file that is installed.
329# (ie Win32 runs pl2bat after this)
330
331# Doing this here avoids all sort of quoting issues that would come from
332# attempting to write out perl source with literals to generate the arrays and
333# hash.
334my @temps = 'Makefile.PL';
335foreach (glob('scripts/pod*.PL')) {
336 # The various pod*.PL extractors change directory. Doing that with relative
337 # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid)
338 # the chdir doing anything, than to attempt to convert lib paths to
339 # absolute, and potentially run into problems with quoting special
340 # characters in the path to our build dir (such as spaces)
341 require File::Copy;
342
343 my $temp = $_;
344 $temp =~ s!scripts/!!;
345 File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!";
346 push @temps, $temp;
347}
348
349my $script_ext = $^O eq 'VMS' ? '.com' : '';
350my %%pod_scripts;
351foreach (glob('pod*.PL')) {
352 my $script = $_;
353 s/.PL$/$script_ext/i;
354 $pod_scripts{$script} = $_;
355}
356my @exe_files = values %%pod_scripts;
357
358WriteMakefile(
359 NAME => '%s',
360 VERSION_FROM => '%s',
361 %-13s => '%s',
362 realclean => { FILES => "@temps" },
363 (%%pod_scripts ? (
364 PL_FILES => \%%pod_scripts,
365 EXE_FILES => \@exe_files,
366 clean => { FILES => "@exe_files" },
367 ) : ()),
368);
369
370# ex: set ro:
371EOM
372 close $fh or die "Can't close Makefile.PL: $!";
373 # As described in commit 23525070d6c0e51f:
374 # Push the atime and mtime of generated Makefile.PLs back 4
375 # seconds. In certain circumstances ( on virtual machines ) the
376 # generated Makefile.PL can produce a Makefile that is older than
377 # the Makefile.PL. Altering the atime and mtime backwards by 4
378 # seconds seems to resolve the issue.
379 eval {
380 my $ftime = time - 4;
381 utime $ftime, $ftime, 'Makefile.PL';
382 };
383 }
384 print "\nRunning Makefile.PL in $ext_dir\n";
385
386 # Presumably this can be simplified
387 my @cross;
388 if (defined $::Cross::platform) {
389 # Inherited from win32/buildext.pl
390 @cross = "-MCross=$::Cross::platform";
391 } elsif ($opts{cross}) {
392 # Inherited from make_ext.pl
393 @cross = '-MCross';
394 }
395
396 my @args = ("-I$lib_dir", @cross, 'Makefile.PL');
397 if ($is_VMS) {
398 my $libd = VMS::Filespec::vmspath($lib_dir);
399 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
400 } else {
401 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
402 'INSTALLMAN3DIR=none';
403 }
404 push @args, @$pass_through;
405 _quote_args(\@args) if $is_VMS;
406 print join(' ', @run, $perl, @args), "\n";
407 my $code = system @run, $perl, @args;
408 warn "$code from $ext_dir\'s Makefile.PL" if $code;
409
410 # Right. The reason for this little hack is that we're sitting inside
411 # a program run by ./miniperl, but there are tasks we need to perform
412 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
413 # Unfortunately, they can be run *after* 'clean', which deletes
414 # ./miniperl
415 # So we do our best to leave a set of instructions identical to what
416 # we would do if we are run directly as 'realclean' etc
417 # Whilst we're perfect, unfortunately the targets we call are not, as
418 # some of them rely on a $(PERL) for their own distclean targets.
419 # But this always used to be a problem with the old /bin/sh version of
420 # this.
421 if ($is_Unix) {
422 my $suffix = '.sh';
423 foreach my $clean_target ('realclean', 'veryclean') {
424 my $file = "$return_dir/$clean_target$suffix";
425 open my $fh, '>>', $file or die "open $file: $!";
426 # Quite possible that we're being run in parallel here.
427 # Can't use Fcntl this early to get the LOCK_EX
428 flock $fh, 2 or warn "flock $file: $!";
429 print $fh <<"EOS";
430cd $ext_dir
431if test ! -f Makefile -a -f Makefile.old; then
432 echo "Note: Using Makefile.old"
433 make -f Makefile.old $clean_target MAKE='@make' @pass_through
434else
435 if test ! -f Makefile ; then
436 echo "Warning: No Makefile!"
437 fi
438 make $clean_target MAKE='@make' @pass_through
439fi
440cd $return_dir
441EOS
442 close $fh or die "close $file: $!";
443 }
444 }
445 }
446
447 if (not -f $makefile) {
448 print "Warning: No Makefile!\n";
449 }
450
451 if ($is_VMS) {
452 _quote_args($pass_through);
453 @$pass_through = (
454 "/DESCRIPTION=$makefile",
455 '/MACRO=(' . join(',',@$pass_through) . ')'
456 );
457 }
458
459 if (!$target or $target !~ /clean$/) {
460 # Give makefile an opportunity to rewrite itself.
461 # reassure users that life goes on...
462 my @args = ('config', @$pass_through);
463 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
464 }
465 my @targ = ($target, @$pass_through);
466 print "Making $target in $ext_dir\n@run @make @targ\n";
467 my $code = system(@run, @make, @targ);
468 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
469
470 chdir $return_dir || die "Cannot cd to $return_dir: $!";
471}
472
473sub _quote_args {
474 my $args = shift; # must be array reference
475
476 # Do not quote qualifiers that begin with '/'.
477 map { if (!/^\//) {
478 $_ =~ s/\"/""/g; # escape C<"> by doubling
479 $_ = q(").$_.q(");
480 }
481 } @{$args}
482 ;
483}