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