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