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