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