This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make t/pod/pod2usage2.t work on case insensitive file systems.
[perl5.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19
S
2use strict;
3use warnings;
b8d39eba 4use Config;
75f92628 5
a0d0e21e
LW
6# This script acts as a simple interface for building extensions.
7# It primarily used by the perl Makefile:
8#
9# d_dummy $(dynamic_ext): miniperl preplibrary FORCE
e2fabae1 10# @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
a0d0e21e
LW
11#
12# It may be deleted in a later release of perl so try to
13# avoid using it for other purposes.
14
e3b84025
NC
15my $is_Win32 = $^O eq 'MSWin32';
16my $is_VMS = $^O eq 'VMS';
17my $is_Unix = !$is_Win32 && !$is_VMS;
18
fc678412 19my (%excl, %incl, %opts, @extspec, @pass_through);
97a26ad9
NC
20
21foreach (@ARGV) {
22 if (/^!(.*)$/) {
23 $excl{$1} = 1;
24 } elsif (/^\+(.*)$/) {
25 $incl{$1} = 1;
26 } elsif (/^--([\w\-]+)$/) {
27 $opts{$1} = 1;
e2fabae1
NC
28 } elsif (/^--([\w\-]+)=(.*)$/) {
29 $opts{$1} = $2;
e2fabae1 30 } elsif (/=/) {
fc678412 31 push @pass_through, $_;
ca5de986 32 } elsif (length) {
e2fabae1 33 push @extspec, $_;
97a26ad9
NC
34 }
35}
36
ca5de986
NC
37# The Perl Makefile.SH will expand all extensions to
38# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
39# A user wishing to run make_ext might use
40# X (or X/Y or X::Y if nested)
41
42# canonise into X/Y form (pname)
43
44foreach (@extspec) {
45 if (/^lib/) {
46 # Remove lib/auto prefix and /*.* suffix
47 s{^lib/auto/}{};
48 s{[^/]*\.[^/]*$}{};
49 } elsif (/^ext/) {
50 # Remove ext/ prefix and /pm_to_blib suffix
51 s{^ext/}{};
52 s{/pm_to_blib$}{};
53 } elsif (/::/) {
54 # Convert :: to /
55 s{::}{\/}g;
56 } elsif (/\..*o$/) {
57 s/\..*o//;
58 }
59}
60
fc678412
NC
61my $makecmd = shift @pass_through; # Should be something like MAKE=make
62unshift @pass_through, 'PERL_CORE=1';
63
e2fabae1 64my $target = $opts{target};
07f3cc2a 65$target = 'all' unless defined $target;
a0d0e21e 66
fb73857a 67# Previously, $make was taken from config.sh. However, the user might
68# instead be running a possibly incompatible make. This might happen if
69# the user types "gmake" instead of a plain "make", for example. The
70# correct current value of MAKE will come through from the main perl
71# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
72# case third party users of this script (are there any?) don't have the
73# MAKE=$(MAKE) argument, which was added after 5.004_03.
ca5de986
NC
74unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
75 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
a2f19a19
S
76}
77
eb1f8df7
NC
78# This isn't going to cope with anything fancy, such as spaces inside command
79# names, but neither did what it replaced. Once there is a use case that needs
80# it, please supply patches. Until then, I'm sticking to KISS
81my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
ca5de986 82# Using an array of 0 or 1 elements makes the subsequent code simpler.
fc678412
NC
83my @run = $Config{run};
84@run = () if not defined $run[0] or $run[0] eq '';
a2f19a19 85
ca5de986
NC
86if (!@extspec) {
87 die "$0: no extension specified\n";
a2f19a19 88}
a0d0e21e 89
07f3cc2a 90if ($target eq '') {
ca5de986
NC
91 die "make_ext: no make target specified (eg all or clean)\n";
92} elsif ($target !~ /(?:^all|clean)$/) {
93 # for the time being we are strict about what make_ext is used for
94 die "$0: unknown make target '$target'\n";
a2f19a19
S
95}
96
ca5de986
NC
97foreach my $pname (@extspec) {
98 my $mname = $pname;
99 $mname =~ s!/!::!g;
100 my $depth = $pname;
101 $depth =~ s![^/]+!..!g;
102 # Always need one more .. for ext/
103 my $up = "../$depth";
104 my $perl = "$up/miniperl";
a2f19a19 105
ca5de986 106 if ($Config{osname} eq 'catamount') {
a2f19a19 107 # Snowball's chance of building extensions.
ca5de986
NC
108 die "This is $Config{osname}, not building $mname, sorry.\n";
109 }
a2f19a19 110
ca5de986 111 print "\tMaking $mname ($target)\n";
a2f19a19 112
ca5de986
NC
113 build_extension('ext', "ext/$pname", $up, $perl, "$up/lib",
114 \@pass_through);
115}
a0d0e21e 116
fc678412 117sub build_extension {
ca5de986 118 my ($ext, $ext_dir, $return_dir, $perl, $lib_dir, $pass_through) = @_;
fc678412
NC
119 unless (chdir "$ext_dir") {
120 warn "Cannot cd to $ext_dir: $!";
121 return;
122 }
123
124 if (!-f 'Makefile') {
125 print "\nRunning Makefile.PL in $ext_dir\n";
126
127 # Presumably this can be simplified
128 my @cross;
129 if (defined $::Cross::platform) {
130 # Inherited from win32/buildext.pl
131 @cross = "-MCross=$::Cross::platform";
132 } elsif ($opts{cross}) {
133 # Inherited from make_ext.pl
134 @cross = '-MCross';
a2f19a19 135 }
fc678412
NC
136
137 my @perl = (@run, $perl, "-I$lib_dir", @cross, 'Makefile.PL',
138 'INSTALLDIRS=perl', 'INSTALLMAN3DIR=none',
139 @$pass_through);
140 print join(' ', @perl), "\n";
141 my $code = system @perl;
142 warn "$code from $ext_dir\'s Makefile.PL" if $code;
143
61edc683
NC
144 # Right. The reason for this little hack is that we're sitting inside
145 # a program run by ./miniperl, but there are tasks we need to perform
146 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
147 # Unfortunately, they can be run *after* 'clean', which deletes
148 # ./miniperl
149 # So we do our best to leave a set of instructions identical to what
150 # we would do if we are run directly as 'realclean' etc
151 # Whilst we're perfect, unfortunately the targets we call are not, as
152 # some of them rely on a $(PERL) for their own distclean targets.
153 # But this always used to be a problem with the old /bin/sh version of
154 # this.
e3b84025 155 if ($is_Unix) {
fc678412
NC
156 my $suffix = '.sh';
157 foreach my $clean_target ('realclean', 'veryclean') {
ca5de986 158 my $file = "$return_dir/$clean_target$suffix";
fc678412
NC
159 open my $fh, '>>', $file or die "open $file: $!";
160 # Quite possible that we're being run in parallel here.
161 # Can't use Fcntl this early to get the LOCK_EX
162 flock $fh, 2 or warn "flock $file: $!";
163 print $fh <<"EOS";
164cd $ext_dir
165if test ! -f Makefile -a -f Makefile.old; then
61edc683 166 echo "Note: Using Makefile.old"
eb1f8df7 167 make -f Makefile.old $clean_target MAKE='@make' @pass_through
61edc683 168else
fc678412 169 if test ! -f Makefile ; then
61edc683
NC
170 echo "Warning: No Makefile!"
171 fi
eb1f8df7 172 make $clean_target MAKE='@make' @pass_through
61edc683 173fi
fc678412 174cd $return_dir
61edc683 175EOS
fc678412
NC
176 close $fh or die "close $file: $!";
177 }
61edc683 178 }
fc678412 179 }
a2f19a19 180
fc678412 181 if (not -f 'Makefile') {
a2f19a19 182 print "Warning: No Makefile!\n";
fc678412 183 }
a2f19a19 184
fc678412 185 if (!$target or $target !~ /clean$/) {
a2f19a19 186 # Give makefile an opportunity to rewrite itself.
75f92628 187 # reassure users that life goes on...
eb1f8df7 188 my @config = (@run, @make, 'config', @$pass_through);
fc678412
NC
189 system @config and print "@config failed, continuing anyway...\n";
190 }
eb1f8df7 191 my @targ = (@run, @make, $target, @$pass_through);
a79902b1 192 print "Making $target in $ext_dir\n@targ\n";
fc678412
NC
193 my $code = system @targ;
194 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 195
fc678412
NC
196 chdir $return_dir || die "Cannot cd to $return_dir: $!";
197}