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