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