This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
vmspath without thread context.
[perl5.git] / 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
97a26ad9
NC
14my (%excl, %incl, %opts, @argv);
15
16foreach (@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
28my $target = shift @argv;
29my $extspec = shift @argv;
30my $makecmd = shift @argv; # Should be something like MAKE=make
31my $passthru = join ' ', @argv; # allow extra macro=value to be passed through
a2f19a19 32print "\n";
a0d0e21e 33
fb73857a
PP
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.
a2f19a19
SM
41my $make;
42if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
43 $make = $1;
44}
45else {
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};
53if ($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};
67my $run = $ENV{run};
68$run = '' if not defined $run;
69$run .= ' ' if $run ne '';;
70
71if (!defined($extspec) or $extspec eq '') {
72 print "make_ext: no extension specified\n";
73 exit(1);
74}
a0d0e21e 75
75f92628 76# The Perl Makefile.SH will expand all extensions to
2698564b 77# lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
75f92628 78# A user wishing to run make_ext might use
2698564b 79# X (or X/Y or X::Y if nested)
75f92628
AD
80
81# canonise into X/Y form (pname)
a2f19a19
SM
82
83my $pname = $extspec;
84if ($extspec =~ /^lib/) {
85 # Remove lib/auto prefix and /*.* suffix
86 $pname =~ s{^lib/auto/}{};
87 $pname =~ s{[^/]*\.[^/]*$}{};
88}
89elsif ($extspec =~ /^ext/) {
90 # Remove ext/ prefix and /pm_to_blib suffix
91 $pname =~ s{^ext/}{};
92 $pname =~ s{/pm_to_blib$}{};
93}
94elsif ($extspec =~ /::/) {
95 # Convert :: to /
96 $pname =~ s{::}{\/}g;
97}
98elsif ($extspec =~ /\..*o$/) {
99 $pname =~ s/\..*o//;
100}
101
102my $mname = $pname;
103$mname =~ s!/!::!g;
104my $depth = $pname;
105$depth =~ s![^/]+!..!g;
106my $makefile = "Makefile";
107my $makeargs = '';
108my $makeopts = '';
109
110if (not -d "ext/$pname") {
111 print "\tSkipping $extspec (directory does not exist)\n";
112 exit(0); # not an error ?
113}
114
115if ($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
121print "\tMaking $mname ($target)\n";
122
123chdir("ext/$pname");
a0d0e21e 124
94c41b70
AD
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)
a2f19a19
SM
128if ($target eq 'dynamic') {
129 $makeargs = "LINKTYPE=dynamic";
130 $target = 'all';
131}
132elsif ($target eq 'static') {
133 $makeargs = "LINKTYPE=static CCCDLFLAGS=";
134 $target = 'all';
135}
136elsif ($target eq 'static_pic') {
137 $makeargs = "LINKTYPE=static";
138 $target = 'all';
139}
140elsif ($target eq 'nonxs') {
141 $makeargs = "";
142 $target = 'all';
143}
144elsif ($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}
153elsif ($target eq '') {
154 print "make_ext: no make target specified (eg static or dynamic)\n";
155 exit(1);
156}
157else {
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
164if (not -f $makefile) {
165 if (-f "Makefile.PL") {
97a26ad9
NC
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");
a2f19a19 168 }
61edc683
NC
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";
193chdir ext/$pname
194if test ! -f $makefile -a -f Makefile.old; then
195 echo "Note: Using Makefile.old"
196 make -f Makefile.old $clean_target MAKE=$make $passthru
197else
198 if test ! -f $makefile ; then
199 echo "Warning: No Makefile!"
200 fi
201 make $clean_target MAKE=$make $passthru
202fi
203chdir ../$depth
204EOS
205 }
206 close $fh or die "close $file: $!";
207 }
a2f19a19
SM
208}
209
210if (not -f $makefile) {
211 print "Warning: No Makefile!\n";
212}
213
214if ($target eq 'clean') {
215}
216elsif ($target eq 'realclean') {
217}
218else {
219 # Give makefile an opportunity to rewrite itself.
75f92628 220 # reassure users that life goes on...
a2f19a19
SM
221 system( "$run$make config MAKE=$make $passthru" )
222 and print "$make config failed, continuing anyway...\n";
223}
224
225system(
226 "$run$make $target MAKE=$make $makeargs $passthru"
227) or exit();
228
229exit($?);
230
231# read config.sh and add its keys to our %ENV
232sub 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}