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