This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use the build_extension() routine from win32/buildext.pl
[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, $_;
97a26ad9 32 } else {
e2fabae1 33 push @extspec, $_;
97a26ad9
NC
34 }
35}
36
fc678412
NC
37my $makecmd = shift @pass_through; # Should be something like MAKE=make
38unshift @pass_through, 'PERL_CORE=1';
39
e2fabae1
NC
40my $target = $opts{target};
41my $extspec = $extspec[0];
a0d0e21e 42
fb73857a 43# Previously, $make was taken from config.sh. However, the user might
44# instead be running a possibly incompatible make. This might happen if
45# the user types "gmake" instead of a plain "make", for example. The
46# correct current value of MAKE will come through from the main perl
47# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
48# case third party users of this script (are there any?) don't have the
49# MAKE=$(MAKE) argument, which was added after 5.004_03.
a2f19a19
S
50my $make;
51if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
52 $make = $1;
53}
54else {
55 print "ext/util/make_ext: WARNING: Please include MAKE=\$(MAKE)\n";
56 print "\tin your call to make_ext. See ext/util/make_ext for details.\n";
57 exit(1);
58}
59
a2f19a19 60# fallback to config.sh's MAKE
b8d39eba 61$make ||= $Config{make} || $ENV{MAKE};
fc678412
NC
62my @run = $Config{run};
63@run = () if not defined $run[0] or $run[0] eq '';
a2f19a19
S
64
65if (!defined($extspec) or $extspec eq '') {
66 print "make_ext: no extension specified\n";
67 exit(1);
68}
a0d0e21e 69
c337d41a
NC
70# check link type and do any preliminaries. Valid link types are
71# 'dynamic', 'static', and 'static_pic' (the last one respects
72# CCCDLFLAGS such as -fPIC -- see static_target in the main Makefile.SH)
73if ($target eq 'dynamic') {
fc678412 74 unshift @pass_through, 'LINKTYPE=dynamic';
c337d41a
NC
75 $target = 'all';
76}
77elsif ($target eq 'static') {
fc678412 78 unshift @pass_through, 'LINKTYPE=static', 'CCCDLFLAGS=';
c337d41a
NC
79 $target = 'all';
80}
81elsif ($target eq 'static_pic') {
fc678412 82 unshift @pass_through, 'LINKTYPE=static';
c337d41a
NC
83 $target = 'all';
84}
85elsif ($target eq 'nonxs') {
86 $target = 'all';
87}
88elsif ($target =~ /clean$/) {
89}
90elsif ($target eq '') {
91 print "make_ext: no make target specified (eg static or dynamic)\n";
92 exit(1);
93}
94else {
95 # for the time being we are strict about what make_ext is used for
96 print "make_ext: unknown make target '$target'\n";
97 exit(1);
98}
99
75f92628 100# The Perl Makefile.SH will expand all extensions to
2698564b 101# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
75f92628 102# A user wishing to run make_ext might use
2698564b 103# X (or X/Y or X::Y if nested)
75f92628
AD
104
105# canonise into X/Y form (pname)
a2f19a19
S
106
107my $pname = $extspec;
108if ($extspec =~ /^lib/) {
109 # Remove lib/auto prefix and /*.* suffix
110 $pname =~ s{^lib/auto/}{};
111 $pname =~ s{[^/]*\.[^/]*$}{};
112}
113elsif ($extspec =~ /^ext/) {
114 # Remove ext/ prefix and /pm_to_blib suffix
115 $pname =~ s{^ext/}{};
116 $pname =~ s{/pm_to_blib$}{};
117}
118elsif ($extspec =~ /::/) {
119 # Convert :: to /
120 $pname =~ s{::}{\/}g;
121}
122elsif ($extspec =~ /\..*o$/) {
123 $pname =~ s/\..*o//;
124}
125
126my $mname = $pname;
127$mname =~ s!/!::!g;
128my $depth = $pname;
129$depth =~ s![^/]+!..!g;
fc678412
NC
130# Always need one more .. for ext/
131my $up = "../$depth";
132my $perl = "$up/miniperl";
a2f19a19
S
133
134if (not -d "ext/$pname") {
135 print "\tSkipping $extspec (directory does not exist)\n";
136 exit(0); # not an error ?
137}
138
b8d39eba 139if ($Config{osname} eq 'catamount') {
a2f19a19 140 # Snowball's chance of building extensions.
b8d39eba 141 print "This is $Config{osname}, not building $mname, sorry.\n";
a2f19a19
S
142 exit(0);
143}
144
145print "\tMaking $mname ($target)\n";
146
fc678412 147build_extension('ext', "ext/$pname", $up, "$up/lib", \@pass_through);
a0d0e21e 148
fc678412
NC
149sub build_extension {
150 my ($ext, $ext_dir, $return_dir, $lib_dir, $pass_through) = @_;
151 unless (chdir "$ext_dir") {
152 warn "Cannot cd to $ext_dir: $!";
153 return;
154 }
155
156 if (!-f 'Makefile') {
157 print "\nRunning Makefile.PL in $ext_dir\n";
158
159 # Presumably this can be simplified
160 my @cross;
161 if (defined $::Cross::platform) {
162 # Inherited from win32/buildext.pl
163 @cross = "-MCross=$::Cross::platform";
164 } elsif ($opts{cross}) {
165 # Inherited from make_ext.pl
166 @cross = '-MCross';
a2f19a19 167 }
fc678412
NC
168
169 my @perl = (@run, $perl, "-I$lib_dir", @cross, 'Makefile.PL',
170 'INSTALLDIRS=perl', 'INSTALLMAN3DIR=none',
171 @$pass_through);
172 print join(' ', @perl), "\n";
173 my $code = system @perl;
174 warn "$code from $ext_dir\'s Makefile.PL" if $code;
175
61edc683
NC
176 # Right. The reason for this little hack is that we're sitting inside
177 # a program run by ./miniperl, but there are tasks we need to perform
178 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
179 # Unfortunately, they can be run *after* 'clean', which deletes
180 # ./miniperl
181 # So we do our best to leave a set of instructions identical to what
182 # we would do if we are run directly as 'realclean' etc
183 # Whilst we're perfect, unfortunately the targets we call are not, as
184 # some of them rely on a $(PERL) for their own distclean targets.
185 # But this always used to be a problem with the old /bin/sh version of
186 # this.
e3b84025 187 if ($is_Unix) {
fc678412
NC
188 my $suffix = '.sh';
189 foreach my $clean_target ('realclean', 'veryclean') {
190 my $file = "../$depth/$clean_target$suffix";
191 open my $fh, '>>', $file or die "open $file: $!";
192 # Quite possible that we're being run in parallel here.
193 # Can't use Fcntl this early to get the LOCK_EX
194 flock $fh, 2 or warn "flock $file: $!";
195 print $fh <<"EOS";
196cd $ext_dir
197if test ! -f Makefile -a -f Makefile.old; then
61edc683 198 echo "Note: Using Makefile.old"
fc678412 199 make -f Makefile.old $clean_target MAKE=$make @pass_through
61edc683 200else
fc678412 201 if test ! -f Makefile ; then
61edc683
NC
202 echo "Warning: No Makefile!"
203 fi
fc678412 204 make $clean_target MAKE=$make @pass_through
61edc683 205fi
fc678412 206cd $return_dir
61edc683 207EOS
fc678412
NC
208 close $fh or die "close $file: $!";
209 }
61edc683 210 }
fc678412 211 }
a2f19a19 212
fc678412 213 if (not -f 'Makefile') {
a2f19a19 214 print "Warning: No Makefile!\n";
fc678412 215 }
a2f19a19 216
fc678412 217 if (!$target or $target !~ /clean$/) {
a2f19a19 218 # Give makefile an opportunity to rewrite itself.
75f92628 219 # reassure users that life goes on...
fc678412
NC
220 my @config = (@run, $make, 'config', @$pass_through);
221 system @config and print "@config failed, continuing anyway...\n";
222 }
223 my @targ = (@run, $make, $target, @$pass_through);
224 print "Making $target in $ext_dir\n$@targ\n";
225 my $code = system @targ;
226 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
a2f19a19 227
fc678412
NC
228 chdir $return_dir || die "Cannot cd to $return_dir: $!";
229}