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