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