6 if ($^O eq 'MSWin32') {
7 unshift @INC, ('../cpan/Cwd', '../cpan/Cwd/lib');
8 require File::Spec::Functions;
11 unshift @INC, 'cpan/Cwd';
16 # To clarify, this isn't the entire suite of modules considered "toolchain"
17 # It's not even all modules needed to build ext/
18 # It's just the source paths of the (minimum complete set of) modules in ext/
19 # needed to build the nonxs modules
20 # After which, all nonxs modules are in lib, which was always sufficient to
21 # allow miniperl to build everything else.
23 my @toolchain = qw(ext/constant/lib cpan/Cwd cpan/Cwd/lib
24 ext/ExtUtils-Command/lib
25 dist/ExtUtils-Install/lib ext/ExtUtils-MakeMaker/lib
26 ext/ExtUtils-Manifest/lib ext/Text-ParseWords/lib
27 cpan/File-Path/lib cpan/AutoLoader/lib);
29 my @ext_dirs = qw(cpan dist ext);
30 my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')';
32 # This script acts as a simple interface for building extensions.
34 # It's actually a cut and shut of the Unix version ext/utils/makeext and the
35 # Windows version win32/build_ext.pl hence the two invocation styles.
37 # On Unix, it primarily used by the perl Makefile one extention at a time:
39 # d_dummy $(dynamic_ext): miniperl preplibrary FORCE
40 # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
43 # If '--static' is specified, static extensions will be built.
44 # If '--dynamic' is specified, dynamic extensions will be built.
45 # If '--nonxs' is specified, nonxs extensions will be built.
46 # If '--dynaloader' is specificied, DynaLoader will be built.
47 # If '--all' is specified, all extensions will be built.
49 # make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1
53 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext
55 # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean
57 # make_ext.pl MAKE=dmake --dir=..\ext
59 # make_ext.pl MAKE=dmake --dir=..\ext --target=clean
61 # Will skip building extensions which are marked with an '!' char.
62 # Mostly because they still not ported to specified platform.
64 # If any extensions are listed with a '+' char then only those
65 # extensions will be built, but only if they arent countermanded
66 # by an '!ext' and are appropriate to the type of building being done.
68 # It may be deleted in a later release of perl so try to
69 # avoid using it for other purposes.
71 my $is_Win32 = $^O eq 'MSWin32';
72 my $is_VMS = $^O eq 'VMS';
73 my $is_Unix = !$is_Win32 && !$is_VMS;
75 require FindExt if $is_Win32;
77 my (%excl, %incl, %opts, @extspec, @pass_through);
82 } elsif (/^\+(.*)$/) {
84 } elsif (/^--([\w\-]+)$/) {
86 } elsif (/^--([\w\-]+)=(.*)$/) {
87 push @{$opts{$1}}, $2;
89 push @pass_through, $_;
95 my $static = $opts{static} || $opts{all};
96 my $dynamic = $opts{dynamic} || $opts{all};
97 my $nonxs = $opts{nonxs} || $opts{all};
98 my $dynaloader = $opts{dynaloader} || $opts{all};
100 # The Perl Makefile.SH will expand all extensions to
101 # lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested)
102 # A user wishing to run make_ext might use
103 # X (or X/Y or X::Y if nested)
105 # canonise into X/Y form (pname)
108 if (s{^lib/auto/}{}) {
109 # Remove lib/auto prefix and /*.* suffix
111 } elsif (s{^$ext_dirs_re/}{}) {
112 # Remove ext/ prefix and /pm_to_blib suffix
114 # Targets are given as files on disk, but the extension spec is still
115 # written using /s for each ::
117 } elsif (s{::}{\/}g) {
124 my $makecmd = shift @pass_through; # Should be something like MAKE=make
125 unshift @pass_through, 'PERL_CORE=1';
127 my @dirs = @{$opts{dir} || \@ext_dirs};
128 my $target = $opts{target}[0];
129 $target = 'all' unless defined $target;
131 # Previously, $make was taken from config.sh. However, the user might
132 # instead be running a possibly incompatible make. This might happen if
133 # the user types "gmake" instead of a plain "make", for example. The
134 # correct current value of MAKE will come through from the main perl
135 # makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in
136 # case third party users of this script (are there any?) don't have the
137 # MAKE=$(MAKE) argument, which was added after 5.004_03.
138 unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) {
139 die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n";
142 # This isn't going to cope with anything fancy, such as spaces inside command
143 # names, but neither did what it replaced. Once there is a use case that needs
144 # it, please supply patches. Until then, I'm sticking to KISS
145 my @make = split ' ', $1 || $Config{make} || $ENV{MAKE};
146 # Using an array of 0 or 1 elements makes the subsequent code simpler.
147 my @run = $Config{run};
148 @run = () if not defined $run[0] or $run[0] eq '';
152 die "make_ext: no make target specified (eg all or clean)\n";
153 } elsif ($target !~ /(?:^all|clean)$/) {
154 # for the time being we are strict about what make_ext is used for
155 die "$0: unknown make target '$target'\n";
158 if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) {
159 die "$0: no extension specified\n";
163 my %extra_passthrough;
166 (my $here = getcwd()) =~ s{/}{\\}g;
168 if ($perl =~ m#^\.\.#) {
169 $perl = "$here\\$perl";
171 (my $topdir = $perl) =~ s/\\[^\\]+$//;
172 # miniperl needs to find perlglob and pl2bat
173 $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}";
174 my $pl2bat = "$topdir\\win32\\bin\\pl2bat";
175 unless (-f "$pl2bat.bat") {
176 my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2);
178 system(@args) unless defined $::Cross::platform;
181 my $build = getcwd();
183 foreach my $dir (@dirs) {
184 chdir($dir) or die "Cannot cd to $dir: $!\n";
185 (my $ext = getcwd()) =~ s{/}{\\}g;
186 FindExt::scan_ext($ext);
187 FindExt::set_static_extensions(split ' ', $Config{static_ext});
189 or die "Couldn't chdir to '$build': $!"; # restore our start directory
193 push @ext, FindExt::static_ext() if $static;
194 push @ext, FindExt::dynamic_ext() if $dynamic;
195 push @ext, FindExt::nonxs_ext() if $nonxs;
196 push @ext, 'DynaLoader' if $dynaloader;
198 foreach (sort @ext) {
199 if (%incl and !exists $incl{$_}) {
200 #warn "Skipping extension $_, not in inclusion list\n";
203 if (exists $excl{$_}) {
204 warn "Skipping extension $_, not ported to current platform";
208 if($_ eq 'DynaLoader' and $target !~ /clean$/) {
209 # No, we don't know why nmake can't work out the dependency chain
210 push @{$extra_passthrough{$_}}, 'DynaLoader.c';
211 } elsif(FindExt::is_static($_)) {
212 push @{$extra_passthrough{$_}}, 'LINKTYPE=static';
217 or die "Couldn't chdir to build directory: $!"; # now in the Perl build
221 push @extspec, (split ' ', $Config{static_ext}) if $static;
222 push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic;
223 push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs;
224 push @extspec, 'DynaLoader' if $dynaloader;
228 # Cwd needs to be built before Encode recurses into subdirectories.
229 # This seems to be the simplest way to ensure this ordering:
238 @extspec = (@first, @other);
241 foreach my $spec (@extspec) {
246 # Temporary hack to cope with smokers that are not clearing directories:
249 # Old style ext/Data/Dumper/
250 $ext_pathname = "ext/$spec";
252 # New style ext/Data-Dumper/
255 foreach my $dir (@ext_dirs) {
256 if (-d "$dir/$copy") {
257 $ext_pathname = "$dir/$copy";
261 if (!defined $ext_pathname) {
262 warn "Can't find extension $spec in any of @ext_dirs";
267 if ($Config{osname} eq 'catamount') {
268 # Snowball's chance of building extensions.
269 die "This is $Config{osname}, not building $mname, sorry.\n";
272 print "\tMaking $mname ($target)\n";
274 build_extension($ext_pathname, $perl, $mname,
275 [@pass_through, @{$extra_passthrough{$spec} || []}]);
278 sub build_extension {
279 my ($ext_dir, $perl, $mname, $pass_through) = @_;
281 unless (chdir "$ext_dir") {
282 warn "Cannot cd to $ext_dir: $!";
289 $perl ||= "$up/miniperl";
290 my $return_dir = $up;
291 my $lib_dir = "$up/lib";
292 # $lib_dir must be last, as we're copying files into it, and in a parallel
293 # make there's a race condition if one process tries to open a module that
294 # another process has half-written.
295 my @new_inc = ((map {"$up/$_"} @toolchain), $lib_dir);
297 @new_inc = map {File::Spec::Functions::rel2abs($_)} @new_inc;
299 $ENV{PERL5LIB} = join $Config{path_sep}, @new_inc;
304 $makefile = 'descrip.mms';
305 if ($target =~ /clean$/
307 && -f "${makefile}_old") {
308 $makefile = "${makefile}_old";
311 $makefile = 'Makefile';
315 if (!-f 'Makefile.PL') {
316 print "\nCreating Makefile.PL in $ext_dir for $mname\n";
317 # We need to cope well with various possible layouts
318 my @dirs = split /::/, $mname;
319 my $leaf = pop @dirs;
320 my $leafname = "$leaf.pm";
321 my $pathname = join '/', @dirs, $leafname;
322 my @locations = ($leafname, $pathname, "lib/$pathname");
324 foreach (@locations) {
332 die "For $mname tried @locations in in $ext_dir but can't find source";
335 ($pod_name = $fromname) =~ s/\.pm\z/.pod/;
336 $pod_name = $fromname unless -e $pod_name;
337 open my $fh, '>', 'Makefile.PL'
338 or die "Can't open Makefile.PL for writing: $!";
340 #-*- buffer-read-only: t -*-
342 # This Makefile.PL was written by $0.
343 # It will be deleted automatically by make realclean
346 use ExtUtils::MakeMaker;
350 VERSION_FROM => '$fromname',
351 ABSTRACT_FROM => '$pod_name',
352 realclean => {FILES => 'Makefile.PL'},
357 close $fh or die "Can't close Makefile.PL: $!";
359 print "\nRunning Makefile.PL in $ext_dir\n";
361 # Presumably this can be simplified
363 if (defined $::Cross::platform) {
364 # Inherited from win32/buildext.pl
365 @cross = "-MCross=$::Cross::platform";
366 } elsif ($opts{cross}) {
367 # Inherited from make_ext.pl
371 my @args = (@cross, 'Makefile.PL');
373 my $libd = VMS::Filespec::vmspath($lib_dir);
374 push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd";
376 push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none',
377 'INSTALLMAN3DIR=none';
379 push @args, @$pass_through;
380 _quote_args(\@args) if $is_VMS;
381 print join(' ', @run, $perl, @args), "\n";
382 my $code = system @run, $perl, @args;
383 warn "$code from $ext_dir\'s Makefile.PL" if $code;
385 # Right. The reason for this little hack is that we're sitting inside
386 # a program run by ./miniperl, but there are tasks we need to perform
387 # when the 'realclean', 'distclean' or 'veryclean' targets are run.
388 # Unfortunately, they can be run *after* 'clean', which deletes
390 # So we do our best to leave a set of instructions identical to what
391 # we would do if we are run directly as 'realclean' etc
392 # Whilst we're perfect, unfortunately the targets we call are not, as
393 # some of them rely on a $(PERL) for their own distclean targets.
394 # But this always used to be a problem with the old /bin/sh version of
398 foreach my $clean_target ('realclean', 'veryclean') {
399 my $file = "$return_dir/$clean_target$suffix";
400 open my $fh, '>>', $file or die "open $file: $!";
401 # Quite possible that we're being run in parallel here.
402 # Can't use Fcntl this early to get the LOCK_EX
403 flock $fh, 2 or warn "flock $file: $!";
406 if test ! -f Makefile -a -f Makefile.old; then
407 echo "Note: Using Makefile.old"
408 make -f Makefile.old $clean_target MAKE='@make' @pass_through
410 if test ! -f Makefile ; then
411 echo "Warning: No Makefile!"
413 make $clean_target MAKE='@make' @pass_through
417 close $fh or die "close $file: $!";
422 if (not -f $makefile) {
423 print "Warning: No Makefile!\n";
427 _macroify_passthrough($pass_through);
428 unshift @$pass_through, "/DESCRIPTION=$makefile";
431 if (!$target or $target !~ /clean$/) {
432 # Give makefile an opportunity to rewrite itself.
433 # reassure users that life goes on...
434 my @args = ('config', @$pass_through);
435 _quote_args(\@args) if $is_VMS;
436 system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n";
438 my @targ = ($target, @$pass_through);
439 _quote_args(\@targ) if $is_VMS;
440 print "Making $target in $ext_dir\n@run @make @targ\n";
441 my $code = system(@run, @make, @targ);
442 die "Unsuccessful make($ext_dir): code=$code" if $code != 0;
444 chdir $return_dir || die "Cannot cd to $return_dir: $!";
448 my $args = shift; # must be array reference
450 # Do not quote qualifiers that begin with '/'.
452 $_ =~ s/\"/""/g; # escape C<"> by doubling
459 sub _macroify_passthrough {
460 my $passthrough = shift;
461 _quote_args($passthrough);
462 my $macro = '/MACRO=(' . join(',',@$passthrough) . ')';
464 @$passthrough[0] = $macro;