6 # This script acts as a simple interface for building extensions.
7 # It primarily used by the perl Makefile:
9 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
10 # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
12 # It may be deleted in a later release of perl so try to
13 # avoid using it for other purposes.
15 my $is_Win32 = $^O eq 'MSWin32';
16 my $is_VMS = $^O eq 'VMS';
17 my $is_Unix = !$is_Win32 && !$is_VMS;
19 my (%excl, %incl, %opts, @extspec, @pass_through);
24 } elsif (/^\+(.*)$/) {
26 } elsif (/^--([\w\-]+)$/) {
28 } elsif (/^--([\w\-]+)=(.*)$/) {
31 push @pass_through, $_;
37 my $makecmd = shift @pass_through; # Should be something like MAKE=make
38 unshift @pass_through, 'PERL_CORE=1';
40 my $target = $opts{target};
41 my $extspec = $extspec[0];
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.
51 if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
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";
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 '';
65 if (!defined($extspec) or $extspec eq '') {
66 print "make_ext: no extension specified\n";
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';
77 elsif ($target eq 'static') {
78 unshift @pass_through, 'LINKTYPE=static', 'CCCDLFLAGS=';
81 elsif ($target eq 'static_pic') {
82 unshift @pass_through, 'LINKTYPE=static';
85 elsif ($target eq 'nonxs') {
88 elsif ($target =~ /clean$/) {
90 elsif ($target eq '') {
91 print "make_ext: no make target specified (eg static or dynamic)\n";
95 # for the time being we are strict about what make_ext is used for
96 print "make_ext: unknown make target '$target'\n";
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)
105 # canonise into X/Y form (pname)
107 my $pname = $extspec;
108 if ($extspec =~ /^lib/) {
109 # Remove lib/auto prefix and /*.* suffix
110 $pname =~ s{^lib/auto/}{};
111 $pname =~ s{[^/]*\.[^/]*$}{};
113 elsif ($extspec =~ /^ext/) {
114 # Remove ext/ prefix and /pm_to_blib suffix
115 $pname =~ s{^ext/}{};
116 $pname =~ s{/pm_to_blib$}{};
118 elsif ($extspec =~ /::/) {
120 $pname =~ s{::}{\/}g;
122 elsif ($extspec =~ /\..*o$/) {
129 $depth =~ s![^/]+!..!g;
130 # Always need one more .. for ext/
131 my $up = "../$depth";
132 my $perl = "$up/miniperl";
134 if (not -d "ext/$pname") {
135 print "\tSkipping $extspec (directory does not exist)\n";
136 exit(0); # not an error ?
139 if ($Config{osname} eq 'catamount') {
140 # Snowball's chance of building extensions.
141 print "This is $Config{osname}, not building $mname, sorry.\n";
145 print "\tMaking $mname ($target)\n";
147 build_extension('ext', "ext/$pname", $up, "$up/lib", \@pass_through);
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: $!";
156 if (!-f 'Makefile') {
157 print "\nRunning Makefile.PL in $ext_dir\n";
159 # Presumably this can be simplified
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
169 my @perl = (@run, $perl, "-I$lib_dir", @cross, 'Makefile.PL',
170 'INSTALLDIRS=perl', 'INSTALLMAN3DIR=none',
172 print join(' ', @perl), "\n";
173 my $code = system @perl;
174 warn "$code from $ext_dir\'s Makefile.PL" if $code;
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
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
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: $!";
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
201 if test ! -f Makefile ; then
202 echo "Warning: No Makefile!"
204 make $clean_target MAKE=$make @pass_through
208 close $fh or die "close $file: $!";
213 if (not -f 'Makefile') {
214 print "Warning: No Makefile!\n";
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";
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;
228 chdir $return_dir || die "Cannot cd to $return_dir: $!";