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