Create build_extension() to encapsulate the per-extension logic.
[p5sagit/p5-mst-13.2.git] / make_ext.pl
CommitLineData
61edc683 1#!./miniperl
a2f19a19 2use strict;
3use warnings;
b8d39eba 4use Config;
75f92628 5
a0d0e21e 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 11#
12# It may be deleted in a later release of perl so try to
13# avoid using it for other purposes.
14
e3b84025 15my $is_Win32 = $^O eq 'MSWin32';
16my $is_VMS = $^O eq 'VMS';
17my $is_Unix = !$is_Win32 && !$is_VMS;
18
e2fabae1 19my (%excl, %incl, %opts, @extspec, @passthrough);
97a26ad9 20
21foreach (@ARGV) {
22 if (/^!(.*)$/) {
23 $excl{$1} = 1;
24 } elsif (/^\+(.*)$/) {
25 $incl{$1} = 1;
26 } elsif (/^--([\w\-]+)$/) {
27 $opts{$1} = 1;
e2fabae1 28 } elsif (/^--([\w\-]+)=(.*)$/) {
29 $opts{$1} = $2;
30 } elsif (/^--([\w\-]+)=(.*)$/) {
31 $opts{$1} = $2;
32 } elsif (/=/) {
33 push @passthrough, $_;
97a26ad9 34 } else {
e2fabae1 35 push @extspec, $_;
97a26ad9 36 }
37}
38
e2fabae1 39my $target = $opts{target};
40my $extspec = $extspec[0];
41my $makecmd = shift @passthrough; # Should be something like MAKE=make
42my $passthru = join ' ', @passthrough; # allow extra macro=value to be passed through
a2f19a19 43print "\n";
a0d0e21e 44
fb73857a 45# Previously, $make was taken from config.sh. However, the user might
46# instead be running a possibly incompatible make. This might happen if
47# the user types "gmake" instead of a plain "make", for example. The
48# correct current value of MAKE will come through from the main perl
49# makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
50# case third party users of this script (are there any?) don't have the
51# MAKE=$(MAKE) argument, which was added after 5.004_03.
a2f19a19 52my $make;
53if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
54 $make = $1;
55}
56else {
57 print "ext/util/make_ext: WARNING: Please include MAKE=\$(MAKE)\n";
58 print "\tin your call to make_ext. See ext/util/make_ext for details.\n";
59 exit(1);
60}
61
a2f19a19 62# fallback to config.sh's MAKE
b8d39eba 63$make ||= $Config{make} || $ENV{MAKE};
64my $run = $Config{run};
a2f19a19 65$run = '' if not defined $run;
66$run .= ' ' if $run ne '';;
67
68if (!defined($extspec) or $extspec eq '') {
69 print "make_ext: no extension specified\n";
70 exit(1);
71}
a0d0e21e 72
c337d41a 73# check link type and do any preliminaries. Valid link types are
74# 'dynamic', 'static', and 'static_pic' (the last one respects
75# CCCDLFLAGS such as -fPIC -- see static_target in the main Makefile.SH)
76if ($target eq 'dynamic') {
77 $passthru = "LINKTYPE=dynamic $passthru";
78 $target = 'all';
79}
80elsif ($target eq 'static') {
81 $passthru = "LINKTYPE=static CCCDLFLAGS= $passthru";
82 $target = 'all';
83}
84elsif ($target eq 'static_pic') {
85 $passthru = "LINKTYPE=static $passthru";
86 $target = 'all';
87}
88elsif ($target eq 'nonxs') {
89 $target = 'all';
90}
91elsif ($target =~ /clean$/) {
92}
93elsif ($target eq '') {
94 print "make_ext: no make target specified (eg static or dynamic)\n";
95 exit(1);
96}
97else {
98 # for the time being we are strict about what make_ext is used for
99 print "make_ext: unknown make target '$target'\n";
100 exit(1);
101}
102
75f92628 103# The Perl Makefile.SH will expand all extensions to
2698564b 104# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
75f92628 105# A user wishing to run make_ext might use
2698564b 106# X (or X/Y or X::Y if nested)
75f92628 107
108# canonise into X/Y form (pname)
a2f19a19 109
110my $pname = $extspec;
111if ($extspec =~ /^lib/) {
112 # Remove lib/auto prefix and /*.* suffix
113 $pname =~ s{^lib/auto/}{};
114 $pname =~ s{[^/]*\.[^/]*$}{};
115}
116elsif ($extspec =~ /^ext/) {
117 # Remove ext/ prefix and /pm_to_blib suffix
118 $pname =~ s{^ext/}{};
119 $pname =~ s{/pm_to_blib$}{};
120}
121elsif ($extspec =~ /::/) {
122 # Convert :: to /
123 $pname =~ s{::}{\/}g;
124}
125elsif ($extspec =~ /\..*o$/) {
126 $pname =~ s/\..*o//;
127}
128
129my $mname = $pname;
130$mname =~ s!/!::!g;
131my $depth = $pname;
132$depth =~ s![^/]+!..!g;
133my $makefile = "Makefile";
a2f19a19 134
135if (not -d "ext/$pname") {
136 print "\tSkipping $extspec (directory does not exist)\n";
137 exit(0); # not an error ?
138}
139
b8d39eba 140if ($Config{osname} eq 'catamount') {
a2f19a19 141 # Snowball's chance of building extensions.
b8d39eba 142 print "This is $Config{osname}, not building $mname, sorry.\n";
a2f19a19 143 exit(0);
144}
145
146print "\tMaking $mname ($target)\n";
147
148chdir("ext/$pname");
a0d0e21e 149
a2f19a19 150if (not -f $makefile) {
151 if (-f "Makefile.PL") {
97a26ad9 152 my $cross = $opts{cross} ? ' -MCross' : '';
153 system("${run}../$depth/miniperl -I../$depth/lib$cross Makefile.PL INSTALLDIRS=perl INSTALLMAN3DIR=none PERL_CORE=1 $passthru");
a2f19a19 154 }
61edc683 155 # Right. The reason for this little hack is that we're sitting inside
156 # a program run by ./miniperl, but there are tasks we need to perform
157 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
158 # Unfortunately, they can be run *after* 'clean', which deletes
159 # ./miniperl
160 # So we do our best to leave a set of instructions identical to what
161 # we would do if we are run directly as 'realclean' etc
162 # Whilst we're perfect, unfortunately the targets we call are not, as
163 # some of them rely on a $(PERL) for their own distclean targets.
164 # But this always used to be a problem with the old /bin/sh version of
165 # this.
e3b84025 166 if ($is_Unix) {
167 my $suffix = '.sh';
168 foreach my $clean_target ('realclean', 'veryclean') {
169 my $file = "../$depth/$clean_target$suffix";
170 open my $fh, '>>', $file or die "open $file: $!";
171 # Quite possible that we're being run in parallel here.
172 # Can't use Fcntl this early to get the LOCK_EX
173 flock $fh, 2 or warn "flock $file: $!";
61edc683 174 print $fh <<"EOS";
175chdir ext/$pname
176if test ! -f $makefile -a -f Makefile.old; then
177 echo "Note: Using Makefile.old"
178 make -f Makefile.old $clean_target MAKE=$make $passthru
179else
180 if test ! -f $makefile ; then
181 echo "Warning: No Makefile!"
182 fi
183 make $clean_target MAKE=$make $passthru
184fi
185chdir ../$depth
186EOS
e3b84025 187 close $fh or die "close $file: $!";
61edc683 188 }
61edc683 189 }
a2f19a19 190}
191
192if (not -f $makefile) {
193 print "Warning: No Makefile!\n";
194}
195
196if ($target eq 'clean') {
197}
198elsif ($target eq 'realclean') {
199}
200else {
201 # Give makefile an opportunity to rewrite itself.
75f92628 202 # reassure users that life goes on...
a2f19a19 203 system( "$run$make config MAKE=$make $passthru" )
204 and print "$make config failed, continuing anyway...\n";
205}
206
9822e6e0 207system "$run$make $target MAKE=$make $passthru" and exit $?;