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