Use the build_extension() routine from win32/buildext.pl
[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 $is_Win32 = $^O eq 'MSWin32';
16 my $is_VMS = $^O eq 'VMS';
17 my $is_Unix = !$is_Win32 && !$is_VMS;
18
19 my (%excl, %incl, %opts, @extspec, @pass_through);
20
21 foreach (@ARGV) {
22     if (/^!(.*)$/) {
23         $excl{$1} = 1;
24     } elsif (/^\+(.*)$/) {
25         $incl{$1} = 1;
26     } elsif (/^--([\w\-]+)$/) {
27         $opts{$1} = 1;
28     } elsif (/^--([\w\-]+)=(.*)$/) {
29         $opts{$1} = $2;
30     } elsif (/=/) {
31         push @pass_through, $_;
32     } else {
33         push @extspec, $_;
34     }
35 }
36
37 my $makecmd  = shift @pass_through; # Should be something like MAKE=make
38 unshift @pass_through, 'PERL_CORE=1';
39
40 my $target   = $opts{target};
41 my $extspec  = $extspec[0];
42
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.
50 my $make;
51 if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
52         $make = $1;
53 }
54 else {
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
60 # fallback to config.sh's MAKE
61 $make ||= $Config{make} || $ENV{MAKE};
62 my @run = $Config{run};
63 @run = () if not defined $run[0] or $run[0] eq '';
64
65 if (!defined($extspec) or $extspec eq '')  {
66         print "make_ext: no extension specified\n";
67         exit(1);
68 }
69
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)
73 if ($target eq 'dynamic') {
74         unshift @pass_through, 'LINKTYPE=dynamic';
75         $target   = 'all';
76 }
77 elsif ($target eq 'static') {
78         unshift @pass_through, 'LINKTYPE=static', 'CCCDLFLAGS=';
79         $target   = 'all';
80 }
81 elsif ($target eq 'static_pic') {
82         unshift @pass_through, 'LINKTYPE=static';
83         $target   = 'all';
84 }
85 elsif ($target eq 'nonxs') {
86         $target   = 'all';
87 }
88 elsif ($target =~ /clean$/) {
89 }
90 elsif ($target eq '') {
91         print "make_ext: no make target specified (eg static or dynamic)\n";
92         exit(1);
93 }
94 else {
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
100 # The Perl Makefile.SH will expand all extensions to
101 #       lib/auto/X/X.a  (or lib/auto/X/Y/Y.a if nested)
102 # A user wishing to run make_ext might use
103 #       X (or X/Y or X::Y if nested)
104
105 # canonise into X/Y form (pname)
106
107 my $pname = $extspec;
108 if ($extspec =~ /^lib/) {
109         # Remove lib/auto prefix and /*.* suffix
110         $pname =~ s{^lib/auto/}{};
111         $pname =~ s{[^/]*\.[^/]*$}{};
112 }
113 elsif ($extspec =~ /^ext/) {
114         # Remove ext/ prefix and /pm_to_blib suffix
115         $pname =~ s{^ext/}{};
116         $pname =~ s{/pm_to_blib$}{};
117 }
118 elsif ($extspec =~ /::/) {
119         # Convert :: to /
120         $pname =~ s{::}{\/}g;
121 }
122 elsif ($extspec =~ /\..*o$/) {
123         $pname =~ s/\..*o//;
124 }
125
126 my $mname = $pname;
127 $mname =~ s!/!::!g;
128 my $depth = $pname;
129 $depth =~ s![^/]+!..!g;
130 # Always need one more .. for ext/
131 my $up = "../$depth";
132 my $perl = "$up/miniperl";
133
134 if (not -d "ext/$pname") {
135         print "\tSkipping $extspec (directory does not exist)\n";
136         exit(0); # not an error ?
137 }
138
139 if ($Config{osname} eq 'catamount') {
140         # Snowball's chance of building extensions.
141         print "This is $Config{osname}, not building $mname, sorry.\n";
142         exit(0);
143 }
144
145 print "\tMaking $mname ($target)\n";
146
147 build_extension('ext', "ext/$pname", $up, "$up/lib", \@pass_through);
148
149 sub 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';
167         }
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
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.
187         if ($is_Unix) {
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";
196 cd $ext_dir
197 if test ! -f Makefile -a -f Makefile.old; then
198     echo "Note: Using Makefile.old"
199     make -f Makefile.old $clean_target MAKE=$make @pass_through
200 else
201     if test ! -f Makefile ; then
202         echo "Warning: No Makefile!"
203     fi
204     make $clean_target MAKE=$make @pass_through
205 fi
206 cd $return_dir
207 EOS
208                 close $fh or die "close $file: $!";
209             }
210         }
211     }
212
213     if (not -f 'Makefile') {
214         print "Warning: No Makefile!\n";
215     }
216
217     if (!$target or $target !~ /clean$/) {
218         # Give makefile an opportunity to rewrite itself.
219         # reassure users that life goes on...
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;
227
228     chdir $return_dir || die "Cannot cd to $return_dir: $!";
229 }