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