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