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