7 my @toolchain = qw(ext/constant/lib ext/ExtUtils-Command/lib);
9 # This script acts as a simple interface for building extensions.
11 # It's actually a cut and shut of the Unix version ext/utils/makeext and the
12 # Windows version win32/build_ext.pl hence the two invocation styles.
14 # On Unix, it primarily used by the perl Makefile one extention at a time:
16 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
17 # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
20 # If '--static' is specified, static extensions will be built.
21 # If '--dynamic' is specified, dynamic extensions will be built.
22 # If '--nonxs' is specified, nonxs extensions will be built.
23 # If '--all' is specified, all extensions will be built.
25 # make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
29 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
31 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
33 # make_ext.pl MAKE=dmake --dir=..\ext
35 # make_ext.pl MAKE=dmake --dir=..\ext --target=clean
37 # Will skip building extensions which are marked with an '!' char.
38 # Mostly because they still not ported to specified platform.
40 # If any extensions are listed with a '+' char then only those
41 # extensions will be built, but only if they arent countermanded
42 # by an '!ext' and are appropriate to the type of building being done.
44 # It may be deleted in a later release of perl so try to
45 # avoid using it for other purposes.
47 my $is_Win32 = $^O eq 'MSWin32';
48 my $is_VMS = $^O eq 'VMS';
49 my $is_Unix = !$is_Win32 && !$is_VMS;
51 require FindExt if $is_Win32;
53 my (%excl, %incl, %opts, @extspec, @pass_through);
58 } elsif (/^\+(.*)$/) {
60 } elsif (/^--([\w\-]+)$/) {
62 } elsif (/^--([\w\-]+)=(.*)$/) {
65 push @pass_through, $_;
71 my $static = $opts{static} || $opts{all};
72 my $dynamic = $opts{dynamic} || $opts{all};
73 my $nonxs = $opts{nonxs} || $opts{all};
75 # The Perl Makefile.SH will expand all extensions to
76 # lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
77 # A user wishing to run make_ext might use
78 # X (or X/Y or X::Y if nested)
80 # canonise into X/Y form (pname)
83 if (s{^lib/auto/}{}) {
84 # Remove lib/auto prefix and /*.* suffix
86 } elsif (s{^ext/}{}) {
87 # Remove ext/ prefix and /pm_to_blib suffix
89 # Targets are given as files on disk, but the extension spec is still
90 # written using /s for each ::
92 } elsif (s{::}{\/}g) {
99 my $makecmd = shift @pass_through; # Should be something like MAKE=make
100 unshift @pass_through, 'PERL_CORE=1';
102 my $dir = $opts{dir} || 'ext';
103 my $target = $opts{target};
104 $target = 'all' unless defined $target;
106 # Previously, $make was taken from config.sh. However, the user might
107 # instead be running a possibly incompatible make. This might happen if
108 # the user types "gmake" instead of a plain "make", for example. The
109 # correct current value of MAKE will come through from the main perl
110 # makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
111 # case third party users of this script (are there any?) don't have the
112 # MAKE=$(MAKE) argument, which was added after 5.004_03.
113 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
114 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
117 # This isn't going to cope with anything fancy, such as spaces inside command
118 # names, but neither did what it replaced. Once there is a use case that needs
119 # it, please supply patches. Until then, I'm sticking to KISS
120 my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
121 # Using an array of 0 or 1 elements makes the subsequent code simpler.
122 my @run = $Config{run};
123 @run = () if not defined $run[0] or $run[0] eq '';
127 die "make_ext: no make target specified (eg all or clean)\n";
128 } elsif ($target !~ /(?:^all|clean)$/) {
129 # for the time being we are strict about what make_ext is used for
130 die "$0: unknown make target '$target'\n";
133 if (!@extspec and !$static and !$dynamic and !$nonxs) {
134 die "$0: no extension specified\n";
138 my %extra_passthrough;
141 (my $here = getcwd()) =~ s{/}{\\}g;
143 if ($perl =~ m#^\.\.#) {
144 $perl = "$here\\$perl";
146 (my $topdir = $perl) =~ s/\\[^\\]+$//;
147 # miniperl needs to find perlglob and pl2bat
148 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
149 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
150 unless (-f "$pl2bat.bat") {
151 my @args = ($perl, ("$pl2bat.pl") x 2);
153 system(@args) unless defined $::Cross::platform;
156 print "In ", getcwd();
157 chdir($dir) || die "Cannot cd to $dir\n";
158 (my $ext = getcwd()) =~ s{/}{\\}g;
159 FindExt::scan_ext($ext);
160 FindExt::set_static_extensions(split ' ', $Config{static_ext});
163 push @ext, FindExt::static_ext() if $static;
164 push @ext, FindExt::dynamic_ext() if $dynamic;
165 push @ext, FindExt::nonxs_ext() if $nonxs;
167 foreach (sort @ext) {
168 if (%incl and !exists $incl{$_}) {
169 #warn "Skipping extension $ext\\$_, not in inclusion list\n";
172 if (exists $excl{$_}) {
173 warn "Skipping extension $ext\\$_, not ported to current platform";
177 if(FindExt::is_static($_)) {
178 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
181 chdir '..'; # now in the Perl build directory
185 push @extspec, (split ' ', $Config{static_ext}) if $static;
186 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
187 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
190 foreach my $spec (@extspec) {
194 if (-d "ext/$spec") {
195 # Old style ext/Data/Dumper/
196 $ext_pathname = "ext/$spec";
198 # New style ext/Data-Dumper/
201 $ext_pathname = "ext/$copy";
204 if ($Config{osname} eq 'catamount') {
205 # Snowball's chance of building extensions.
206 die "This is $Config{osname}, not building $mname, sorry.\n";
209 print "\tMaking $mname ($target)\n";
211 build_extension($ext_pathname, $perl, $mname,
212 [@pass_through, @{$extra_passthrough{$spec} || []}]);
215 sub build_extension {
216 my ($ext_dir, $perl, $mname, $pass_through) = @_;
221 $perl ||= "$up/miniperl";
222 my $return_dir = $up;
223 my $lib_dir = "$up/lib";
225 = join $Config{path_sep}, $lib_dir, map {"$up/$_"} @toolchain;
227 unless (chdir "$ext_dir") {
228 warn "Cannot cd to $ext_dir: $!";
233 $makefile = 'descrip.mms';
234 if ($target =~ /clean$/
236 && -f "${makefile}_old") {
237 $makefile = "${makefile}_old";
240 $makefile = 'Makefile';
244 if (!-f 'Makefile.PL') {
245 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
246 # We need to cope well with various possible layouts
247 my @dirs = split /::/, $mname;
248 my $leaf = pop @dirs;
249 my $leafname = "$leaf.pm";
250 my $pathname = join '/', @dirs, $leafname;
251 my @locations = ($leafname, $pathname, "lib/$pathname");
253 foreach (@locations) {
261 die "For $mname tried @locations in in $ext_dir but can't find source";
263 open my $fh, '>', 'Makefile.PL'
264 or die "Can't open Makefile.PL for writing: $!";
266 #-*- buffer-read-only: t -*-
268 # This Makefile.PL was written by $0.
269 # It will be deleted automatically by make realclean
272 use ExtUtils::MakeMaker;
276 VERSION_FROM => '$fromname',
277 ABSTRACT_FROM => '$fromname',
278 realclean => {FILES => 'Makefile.PL'},
283 close $fh or die "Can't close Makefile.PL: $!";
285 print "\nRunning Makefile.PL in $ext_dir\n";
287 # Presumably this can be simplified
289 if (defined $::Cross::platform) {
290 # Inherited from win32/buildext.pl
291 @cross = "-MCross=$::Cross::platform";
292 } elsif ($opts{cross}) {
293 # Inherited from make_ext.pl
297 my @args = (@cross, 'Makefile.PL');
299 my $libd = VMS::Filespec::vmspath($lib_dir);
300 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
302 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
303 'INSTALLMAN3DIR=none';
305 push @args, @$pass_through;
306 _quote_args(\@args) if $is_VMS;
307 print join(' ', @run, $perl, @args), "\n";
308 my $code = system @run, $perl, @args;
309 warn "$code from $ext_dir\'s Makefile.PL" if $code;
311 # Right. The reason for this little hack is that we're sitting inside
312 # a program run by ./miniperl, but there are tasks we need to perform
313 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
314 # Unfortunately, they can be run *after* 'clean', which deletes
316 # So we do our best to leave a set of instructions identical to what
317 # we would do if we are run directly as 'realclean' etc
318 # Whilst we're perfect, unfortunately the targets we call are not, as
319 # some of them rely on a $(PERL) for their own distclean targets.
320 # But this always used to be a problem with the old /bin/sh version of
324 foreach my $clean_target ('realclean', 'veryclean') {
325 my $file = "$return_dir/$clean_target$suffix";
326 open my $fh, '>>', $file or die "open $file: $!";
327 # Quite possible that we're being run in parallel here.
328 # Can't use Fcntl this early to get the LOCK_EX
329 flock $fh, 2 or warn "flock $file: $!";
332 if test ! -f Makefile -a -f Makefile.old; then
333 echo "Note: Using Makefile.old"
334 make -f Makefile.old $clean_target MAKE='@make' @pass_through
336 if test ! -f Makefile ; then
337 echo "Warning: No Makefile!"
339 make $clean_target MAKE='@make' @pass_through
343 close $fh or die "close $file: $!";
348 if (not -f $makefile) {
349 print "Warning: No Makefile!\n";
353 _macroify_passthrough($pass_through);
354 unshift @$pass_through, "/DESCRIPTION=$makefile";
357 if (!$target or $target !~ /clean$/) {
358 # Give makefile an opportunity to rewrite itself.
359 # reassure users that life goes on...
360 my @args = ('config', @$pass_through);
361 _quote_args(\@args) if $is_VMS;
362 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
364 my @targ = ($target, @$pass_through);
365 _quote_args(\@targ) if $is_VMS;
366 print "Making $target in $ext_dir\n@run @make @targ\n";
367 my $code = system(@run, @make, @targ);
368 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
370 chdir $return_dir || die "Cannot cd to $return_dir: $!";
374 my $args = shift; # must be array reference
376 # Do not quote qualifiers that begin with '/'.
378 $_ =~ s/\"/""/g; # escape C<"> by doubling
385 sub _macroify_passthrough {
386 my $passthrough = shift;
387 _quote_args($passthrough);
388 my $macro = '/MACRO=(' . join(',',@$passthrough) . ')';
390 @$passthrough[0] = $macro;