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