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