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