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