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