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