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