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