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