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