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