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