5 # This script acts as a simple interface for building extensions.
6 # It primarily used by the perl Makefile:
8 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
9 # @$(RUN) ./miniperl ext/util/make_ext.pl dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
11 # It may be deleted in a later release of perl so try to
12 # avoid using it for other purposes.
14 my (%excl, %incl, %opts, @argv);
19 } elsif (/^\+(.*)$/) {
21 } elsif (/^--([\w\-]+)$/) {
28 my $target = shift @argv;
29 my $extspec = shift @argv;
30 my $makecmd = shift @argv; # Should be something like MAKE=make
31 my $passthru = join ' ', @argv; # allow extra macro=value to be passed through
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.
42 if (defined($makecmd) and $makecmd =~ /^MAKE=(.*)$/) {
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";
51 # search config.sh for inclusion
52 $ENV{CONFIG} = '' if not defined $ENV{CONFIG};
53 if ($ENV{CONFIG} eq '') {
55 foreach my $depth (0..4) {
56 my $file = ('../' x $depth) . 'config.sh';
57 $config = $file, last if -f $file;
59 print("Can't find config.sh generated by Configure"), exit(1)
60 unless defined $config;
62 load_config_sh($config);
65 # fallback to config.sh's MAKE
66 $make ||= $ENV{make} || $ENV{MAKE};
68 $run = '' if not defined $run;
69 $run .= ' ' if $run ne '';;
71 if (!defined($extspec) or $extspec eq '') {
72 print "make_ext: no extension specified\n";
76 # The Perl Makefile.SH will expand all extensions to
77 # lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
78 # A user wishing to run make_ext might use
79 # X (or X/Y or X::Y if nested)
81 # canonise into X/Y form (pname)
84 if ($extspec =~ /^lib/) {
85 # Remove lib/auto prefix and /*.* suffix
86 $pname =~ s{^lib/auto/}{};
87 $pname =~ s{[^/]*\.[^/]*$}{};
89 elsif ($extspec =~ /^ext/) {
90 # Remove ext/ prefix and /pm_to_blib suffix
92 $pname =~ s{/pm_to_blib$}{};
94 elsif ($extspec =~ /::/) {
98 elsif ($extspec =~ /\..*o$/) {
105 $depth =~ s![^/]+!..!g;
106 my $makefile = "Makefile";
110 if (not -d "ext/$pname") {
111 print "\tSkipping $extspec (directory does not exist)\n";
112 exit(0); # not an error ?
115 if ($ENV{osname} eq 'catamount') {
116 # Snowball's chance of building extensions.
117 print "This is $ENV{osname}, not building $mname, sorry.\n";
121 print "\tMaking $mname ($target)\n";
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)
128 if ($target eq 'dynamic') {
129 $makeargs = "LINKTYPE=dynamic";
132 elsif ($target eq 'static') {
133 $makeargs = "LINKTYPE=static CCCDLFLAGS=";
136 elsif ($target eq 'static_pic') {
137 $makeargs = "LINKTYPE=static";
140 elsif ($target eq 'nonxs') {
144 elsif ($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";
153 elsif ($target eq '') {
154 print "make_ext: no make target specified (eg static or dynamic)\n";
158 # for the time being we are strict about what make_ext is used for
159 print "make_ext: unknown make target '$target'\n";
164 if (not -f $makefile) {
165 if (-f "Makefile.PL") {
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");
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
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
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: $!";
189 } elsif ($^O eq 'MSWin32') {
190 # Might not need anything here.
194 if test ! -f $makefile -a -f Makefile.old; then
195 echo "Note: Using Makefile.old"
196 make -f Makefile.old $clean_target MAKE=$make $passthru
198 if test ! -f $makefile ; then
199 echo "Warning: No Makefile!"
201 make $clean_target MAKE=$make $passthru
206 close $fh or die "close $file: $!";
210 if (not -f $makefile) {
211 print "Warning: No Makefile!\n";
214 if ($target eq 'clean') {
216 elsif ($target eq 'realclean') {
219 # Give makefile an opportunity to rewrite itself.
220 # reassure users that life goes on...
221 system( "$run$make config MAKE=$make $passthru" )
222 and print "$make config failed, continuing anyway...\n";
226 "$run$make $target MAKE=$make $makeargs $passthru"
231 # read config.sh and add its keys to our %ENV
234 open my $fh, '<', $file or die "Could not open file '$file' as a 'config.sh': $!";
238 $ENV{$1} = $3 if /^(?!:)([^\s=]+)=('?)(.*?)\2$/;