Update Maintainers.pl for encoding::warnings
[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                    ext/ExtUtils-Install/lib ext/ExtUtils-MakeMaker/lib
16                    ext/ExtUtils-Manifest/lib ext/Text-ParseWords/lib);
17
18 # This script acts as a simple interface for building extensions.
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:
24 #
25 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
26 #       @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
27 #
28 # On Windows or VMS,
29 # If '--static' is specified, static extensions will be built.
30 # If '--dynamic' is specified, dynamic extensions will be built.
31 # If '--nonxs' is specified, nonxs extensions will be built.
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
53 # It may be deleted in a later release of perl so try to
54 # avoid using it for other purposes.
55
56 my $is_Win32 = $^O eq 'MSWin32';
57 my $is_VMS = $^O eq 'VMS';
58 my $is_Unix = !$is_Win32 && !$is_VMS;
59
60 require FindExt if $is_Win32;
61
62 my (%excl, %incl, %opts, @extspec, @pass_through);
63
64 foreach (@ARGV) {
65     if (/^!(.*)$/) {
66         $excl{$1} = 1;
67     } elsif (/^\+(.*)$/) {
68         $incl{$1} = 1;
69     } elsif (/^--([\w\-]+)$/) {
70         $opts{$1} = 1;
71     } elsif (/^--([\w\-]+)=(.*)$/) {
72         $opts{$1} = $2;
73     } elsif (/=/) {
74         push @pass_through, $_;
75     } elsif (length) {
76         push @extspec, $_;
77     }
78 }
79
80 my $static = $opts{static} || $opts{all};
81 my $dynamic = $opts{dynamic} || $opts{all};
82 my $nonxs = $opts{nonxs} || $opts{all};
83
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
91 foreach (@extspec) {
92     if (s{^lib/auto/}{}) {
93         # Remove lib/auto prefix and /*.* suffix
94         s{/[^/]+\.[^/]+$}{};
95     } elsif (s{^ext/}{}) {
96         # Remove ext/ prefix and /pm_to_blib suffix
97         s{/pm_to_blib$}{};
98         # Targets are given as files on disk, but the extension spec is still
99         # written using /s for each ::
100         tr!-!/!;
101     } elsif (s{::}{\/}g) {
102         # Convert :: to /
103     } else {
104         s/\..*o//;
105     }
106 }
107
108 my $makecmd  = shift @pass_through; # Should be something like MAKE=make
109 unshift @pass_through, 'PERL_CORE=1';
110
111 my $dir  = $opts{dir} || 'ext';
112 my $target   = $opts{target};
113 $target = 'all' unless defined $target;
114
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.
122 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
123     die "$0:  WARNING:  Please include MAKE=\$(MAKE) in \@ARGV\n";
124 }
125
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
129 my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
130 # Using an array of 0 or 1 elements makes the subsequent code simpler.
131 my @run = $Config{run};
132 @run = () if not defined $run[0] or $run[0] eq '';
133
134
135 if ($target eq '') {
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";
140 }
141
142 if (!@extspec and !$static and !$dynamic and !$nonxs)  {
143     die "$0: no extension specified\n";
144 }
145
146 my $perl;
147 my %extra_passthrough;
148
149 if ($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;
173     push @ext, FindExt::dynamic_ext() if $dynamic;
174     push @ext, FindExt::nonxs_ext() if $nonxs;
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 }
192 elsif ($is_VMS) {
193     $perl = $^X;
194     push @extspec, (split ' ', $Config{static_ext}) if $static;
195     push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
196     push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
197 }
198
199 foreach my $spec (@extspec)  {
200     my $mname = $spec;
201     $mname =~ s!/!::!g;
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     }
212
213     if ($Config{osname} eq 'catamount') {
214         # Snowball's chance of building extensions.
215         die "This is $Config{osname}, not building $mname, sorry.\n";
216     }
217
218     print "\tMaking $mname ($target)\n";
219
220     build_extension($ext_pathname, $perl, $mname,
221                     [@pass_through, @{$extra_passthrough{$spec} || []}]);
222 }
223
224 sub build_extension {
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";
233     $ENV{PERL5LIB}
234         = join $Config{path_sep}, $lib_dir, map {"$up/$_"} @toolchain;
235
236     unless (chdir "$ext_dir") {
237         warn "Cannot cd to $ext_dir: $!";
238         return;
239     }
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     }
251     
252     if (!-f $makefile) {
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
280 use strict;
281 use ExtUtils::MakeMaker;
282
283 WriteMakefile(
284     NAME          => '$mname',
285     VERSION_FROM  => '$fromname',
286     ABSTRACT_FROM => '$fromname',
287     realclean     => {FILES => 'Makefile.PL'},
288 );
289
290 # ex: set ro:
291 EOM
292             close $fh or die "Can't close Makefile.PL: $!";
293         }
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';
304         }
305             
306         my @args = (@cross, 'Makefile.PL');
307         if ($is_VMS) {
308             my $libd = VMS::Filespec::vmspath($lib_dir);
309             push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
310         } else {
311             push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
312                 'INSTALLMAN3DIR=none';
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;
318         warn "$code from $ext_dir\'s Makefile.PL" if $code;
319
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.
331         if ($is_Unix) {
332             my $suffix = '.sh';
333             foreach my $clean_target ('realclean', 'veryclean') {
334                 my $file = "$return_dir/$clean_target$suffix";
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";
340 cd $ext_dir
341 if test ! -f Makefile -a -f Makefile.old; then
342     echo "Note: Using Makefile.old"
343     make -f Makefile.old $clean_target MAKE='@make' @pass_through
344 else
345     if test ! -f Makefile ; then
346         echo "Warning: No Makefile!"
347     fi
348     make $clean_target MAKE='@make' @pass_through
349 fi
350 cd $return_dir
351 EOS
352                 close $fh or die "close $file: $!";
353             }
354         }
355     }
356
357     if (not -f $makefile) {
358         print "Warning: No Makefile!\n";
359     }
360
361     if ($is_VMS) {
362         _macroify_passthrough($pass_through);
363         unshift @$pass_through, "/DESCRIPTION=$makefile";
364     }
365
366     if (!$target or $target !~ /clean$/) {
367         # Give makefile an opportunity to rewrite itself.
368         # reassure users that life goes on...
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";
372     }
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);
377     die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
378
379     chdir $return_dir || die "Cannot cd to $return_dir: $!";
380 }
381
382 sub _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
394 sub _macroify_passthrough {
395     my $passthrough = shift;
396     _quote_args($passthrough);
397     my $macro = '/MACRO=(' . join(',',@$passthrough) . ')';
398     @$passthrough = ();
399     @$passthrough[0] = $macro;  
400 }