| 1 | #!./miniperl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use Config; |
| 5 | BEGIN { |
| 6 | if ($^O eq 'MSWin32') { |
| 7 | unshift @INC, '../dist/Cwd'; |
| 8 | require FindExt; |
| 9 | } else { |
| 10 | unshift @INC, 'dist/Cwd'; |
| 11 | } |
| 12 | } |
| 13 | use Cwd; |
| 14 | |
| 15 | my $is_Win32 = $^O eq 'MSWin32'; |
| 16 | my $is_VMS = $^O eq 'VMS'; |
| 17 | my $is_Unix = !$is_Win32 && !$is_VMS; |
| 18 | |
| 19 | my @ext_dirs = qw(cpan dist ext); |
| 20 | my $ext_dirs_re = '(?:' . join('|', @ext_dirs) . ')'; |
| 21 | |
| 22 | # This script acts as a simple interface for building extensions. |
| 23 | |
| 24 | # It's actually a cut and shut of the Unix version ext/utils/makeext and the |
| 25 | # Windows version win32/build_ext.pl hence the two invocation styles. |
| 26 | |
| 27 | # On Unix, it primarily used by the perl Makefile one extension at a time: |
| 28 | # |
| 29 | # d_dummy $(dynamic_ext): miniperl preplibrary FORCE |
| 30 | # @$(RUN) ./miniperl make_ext.pl --target=dynamic $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL) |
| 31 | # |
| 32 | # On Windows or VMS, |
| 33 | # If '--static' is specified, static extensions will be built. |
| 34 | # If '--dynamic' is specified, dynamic extensions will be built. |
| 35 | # If '--nonxs' is specified, nonxs extensions will be built. |
| 36 | # If '--dynaloader' is specified, DynaLoader will be built. |
| 37 | # If '--all' is specified, all extensions will be built. |
| 38 | # |
| 39 | # make_ext.pl "MAKE=make [-make_opts]" --dir=directory [--target=target] [--static|--dynamic|--all] +ext2 !ext1 |
| 40 | # |
| 41 | # E.g. |
| 42 | # |
| 43 | # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext |
| 44 | # |
| 45 | # make_ext.pl "MAKE=nmake -nologo" --dir=..\ext --target=clean |
| 46 | # |
| 47 | # make_ext.pl MAKE=dmake --dir=..\ext |
| 48 | # |
| 49 | # make_ext.pl MAKE=dmake --dir=..\ext --target=clean |
| 50 | # |
| 51 | # Will skip building extensions which are marked with an '!' char. |
| 52 | # Mostly because they still not ported to specified platform. |
| 53 | # |
| 54 | # If any extensions are listed with a '+' char then only those |
| 55 | # extensions will be built, but only if they arent countermanded |
| 56 | # by an '!ext' and are appropriate to the type of building being done. |
| 57 | |
| 58 | # It may be deleted in a later release of perl so try to |
| 59 | # avoid using it for other purposes. |
| 60 | |
| 61 | my (%excl, %incl, %opts, @extspec, @pass_through); |
| 62 | |
| 63 | foreach (@ARGV) { |
| 64 | if (/^!(.*)$/) { |
| 65 | $excl{$1} = 1; |
| 66 | } elsif (/^\+(.*)$/) { |
| 67 | $incl{$1} = 1; |
| 68 | } elsif (/^--([\w\-]+)$/) { |
| 69 | $opts{$1} = 1; |
| 70 | } elsif (/^--([\w\-]+)=(.*)$/) { |
| 71 | push @{$opts{$1}}, $2; |
| 72 | } elsif (/=/) { |
| 73 | push @pass_through, $_; |
| 74 | } elsif (length) { |
| 75 | push @extspec, $_; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | my $static = $opts{static} || $opts{all}; |
| 80 | my $dynamic = $opts{dynamic} || $opts{all}; |
| 81 | my $nonxs = $opts{nonxs} || $opts{all}; |
| 82 | my $dynaloader = $opts{dynaloader} || $opts{all}; |
| 83 | |
| 84 | # The Perl Makefile.SH will expand all extensions to |
| 85 | # lib/auto/X/X.a (or lib/auto/X/Y/Y.a if nested) |
| 86 | # A user wishing to run make_ext might use |
| 87 | # X (or X/Y or X::Y if nested) |
| 88 | |
| 89 | # canonise into X/Y form (pname) |
| 90 | |
| 91 | foreach (@extspec) { |
| 92 | if (s{^lib/auto/}{}) { |
| 93 | # Remove lib/auto prefix and /*.* suffix |
| 94 | s{/[^/]+\.[^/]+$}{}; |
| 95 | } elsif (s{^$ext_dirs_re/}{}) { |
| 96 | # Remove ext/ prefix and /pm_to_blib suffix |
| 97 | s{/pm_to_blib$}{}; |
| 98 | # Targets are given as files on disk, but the extension spec is still |
| 99 | # written using /s for each :: |
| 100 | tr!-!/!; |
| 101 | } elsif (s{::}{\/}g) { |
| 102 | # Convert :: to / |
| 103 | } else { |
| 104 | s/\..*o//; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | my $makecmd = shift @pass_through; # Should be something like MAKE=make |
| 109 | unshift @pass_through, 'PERL_CORE=1'; |
| 110 | |
| 111 | my @dirs = @{$opts{dir} || \@ext_dirs}; |
| 112 | my $target = $opts{target}[0]; |
| 113 | $target = 'all' unless defined $target; |
| 114 | |
| 115 | # Previously, $make was taken from config.sh. However, the user might |
| 116 | # instead be running a possibly incompatible make. This might happen if |
| 117 | # the user types "gmake" instead of a plain "make", for example. The |
| 118 | # correct current value of MAKE will come through from the main perl |
| 119 | # makefile as MAKE=/whatever/make in $makecmd. We'll be cautious in |
| 120 | # case third party users of this script (are there any?) don't have the |
| 121 | # MAKE=$(MAKE) argument, which was added after 5.004_03. |
| 122 | unless(defined $makecmd and $makecmd =~ /^MAKE=(.*)$/) { |
| 123 | die "$0: WARNING: Please include MAKE=\$(MAKE) in \@ARGV\n"; |
| 124 | } |
| 125 | |
| 126 | # This isn't going to cope with anything fancy, such as spaces inside command |
| 127 | # names, but neither did what it replaced. Once there is a use case that needs |
| 128 | # it, please supply patches. Until then, I'm sticking to KISS |
| 129 | my @make = split ' ', $1 || $Config{make} || $ENV{MAKE}; |
| 130 | # Using an array of 0 or 1 elements makes the subsequent code simpler. |
| 131 | my @run = $Config{run}; |
| 132 | @run = () if not defined $run[0] or $run[0] eq ''; |
| 133 | |
| 134 | |
| 135 | if ($target eq '') { |
| 136 | die "make_ext: no make target specified (eg all or clean)\n"; |
| 137 | } elsif ($target !~ /(?:^all|clean)$/) { |
| 138 | # for the time being we are strict about what make_ext is used for |
| 139 | die "$0: unknown make target '$target'\n"; |
| 140 | } |
| 141 | |
| 142 | if (!@extspec and !$static and !$dynamic and !$nonxs and !$dynaloader) { |
| 143 | die "$0: no extension specified\n"; |
| 144 | } |
| 145 | |
| 146 | my $perl; |
| 147 | my %extra_passthrough; |
| 148 | |
| 149 | if ($is_Win32) { |
| 150 | my $build = getcwd(); |
| 151 | $perl = $^X; |
| 152 | if ($perl =~ m#^\.\.#) { |
| 153 | my $here = $build; |
| 154 | $here =~ s{/}{\\}g; |
| 155 | $perl = "$here\\$perl"; |
| 156 | } |
| 157 | (my $topdir = $perl) =~ s/\\[^\\]+$//; |
| 158 | # miniperl needs to find perlglob and pl2bat |
| 159 | $ENV{PATH} = "$topdir;$topdir\\win32\\bin;$ENV{PATH}"; |
| 160 | my $pl2bat = "$topdir\\win32\\bin\\pl2bat"; |
| 161 | unless (-f "$pl2bat.bat") { |
| 162 | my @args = ($perl, "-I$topdir\\lib", ("$pl2bat.pl") x 2); |
| 163 | print "@args\n"; |
| 164 | system(@args) unless defined $::Cross::platform; |
| 165 | } |
| 166 | |
| 167 | print "In $build"; |
| 168 | foreach my $dir (@dirs) { |
| 169 | chdir($dir) or die "Cannot cd to $dir: $!\n"; |
| 170 | (my $ext = getcwd()) =~ s{/}{\\}g; |
| 171 | FindExt::scan_ext($ext); |
| 172 | FindExt::set_static_extensions(split ' ', $Config{static_ext}); |
| 173 | chdir $build |
| 174 | or die "Couldn't chdir to '$build': $!"; # restore our start directory |
| 175 | } |
| 176 | |
| 177 | my @ext; |
| 178 | push @ext, FindExt::static_ext() if $static; |
| 179 | push @ext, FindExt::dynamic_ext() if $dynamic; |
| 180 | push @ext, FindExt::nonxs_ext() if $nonxs; |
| 181 | push @ext, 'DynaLoader' if $dynaloader; |
| 182 | |
| 183 | foreach (sort @ext) { |
| 184 | if (%incl and !exists $incl{$_}) { |
| 185 | #warn "Skipping extension $_, not in inclusion list\n"; |
| 186 | next; |
| 187 | } |
| 188 | if (exists $excl{$_}) { |
| 189 | warn "Skipping extension $_, not ported to current platform"; |
| 190 | next; |
| 191 | } |
| 192 | push @extspec, $_; |
| 193 | if($_ eq 'DynaLoader' and $target !~ /clean$/) { |
| 194 | # No, we don't know why nmake can't work out the dependency chain |
| 195 | push @{$extra_passthrough{$_}}, 'DynaLoader.c'; |
| 196 | } elsif(FindExt::is_static($_)) { |
| 197 | push @{$extra_passthrough{$_}}, 'LINKTYPE=static'; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | chdir '..' |
| 202 | or die "Couldn't chdir to build directory: $!"; # now in the Perl build |
| 203 | } |
| 204 | elsif ($is_VMS) { |
| 205 | $perl = $^X; |
| 206 | push @extspec, (split ' ', $Config{static_ext}) if $static; |
| 207 | push @extspec, (split ' ', $Config{dynamic_ext}) if $dynamic; |
| 208 | push @extspec, (split ' ', $Config{nonxs_ext}) if $nonxs; |
| 209 | push @extspec, 'DynaLoader' if $dynaloader; |
| 210 | } |
| 211 | |
| 212 | { |
| 213 | # Cwd needs to be built before Encode recurses into subdirectories. |
| 214 | # Pod::Simple needs to be built before Pod::Functions |
| 215 | # This seems to be the simplest way to ensure this ordering: |
| 216 | my (@first, @other); |
| 217 | foreach (@extspec) { |
| 218 | if ($_ eq 'Cwd' || $_ eq 'Pod/Simple') { |
| 219 | push @first, $_; |
| 220 | } else { |
| 221 | push @other, $_; |
| 222 | } |
| 223 | } |
| 224 | @extspec = (@first, @other); |
| 225 | } |
| 226 | |
| 227 | if ($Config{osname} eq 'catamount' and @extspec) { |
| 228 | # Snowball's chance of building extensions. |
| 229 | die "This is $Config{osname}, not building $extspec[0], sorry.\n"; |
| 230 | } |
| 231 | |
| 232 | foreach my $spec (@extspec) { |
| 233 | my $mname = $spec; |
| 234 | $mname =~ s!/!::!g; |
| 235 | my $ext_pathname; |
| 236 | |
| 237 | # Try new style ext/Data-Dumper/ first |
| 238 | my $copy = $spec; |
| 239 | $copy =~ tr!/!-!; |
| 240 | foreach my $dir (@ext_dirs) { |
| 241 | if (-d "$dir/$copy") { |
| 242 | $ext_pathname = "$dir/$copy"; |
| 243 | last; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (!defined $ext_pathname) { |
| 248 | if (-d "ext/$spec") { |
| 249 | # Old style ext/Data/Dumper/ |
| 250 | $ext_pathname = "ext/$spec"; |
| 251 | } else { |
| 252 | warn "Can't find extension $spec in any of @ext_dirs"; |
| 253 | next; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | print "\tMaking $mname ($target)\n"; |
| 258 | |
| 259 | build_extension($ext_pathname, $perl, $mname, |
| 260 | [@pass_through, @{$extra_passthrough{$spec} || []}]); |
| 261 | } |
| 262 | |
| 263 | sub build_extension { |
| 264 | my ($ext_dir, $perl, $mname, $pass_through) = @_; |
| 265 | |
| 266 | unless (chdir "$ext_dir") { |
| 267 | warn "Cannot cd to $ext_dir: $!"; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | my $up = $ext_dir; |
| 272 | $up =~ s![^/]+!..!g; |
| 273 | |
| 274 | $perl ||= "$up/miniperl"; |
| 275 | my $return_dir = $up; |
| 276 | my $lib_dir = "$up/lib"; |
| 277 | $ENV{PERL_CORE} = 1; |
| 278 | |
| 279 | my $makefile; |
| 280 | if ($is_VMS) { |
| 281 | $makefile = 'descrip.mms'; |
| 282 | if ($target =~ /clean$/ |
| 283 | && !-f $makefile |
| 284 | && -f "${makefile}_old") { |
| 285 | $makefile = "${makefile}_old"; |
| 286 | } |
| 287 | } else { |
| 288 | $makefile = 'Makefile'; |
| 289 | } |
| 290 | |
| 291 | if (!-f $makefile) { |
| 292 | if (!-f 'Makefile.PL') { |
| 293 | print "\nCreating Makefile.PL in $ext_dir for $mname\n"; |
| 294 | my ($fromname, $key, $value); |
| 295 | if ($mname eq 'podlators') { |
| 296 | # We need to special case this somewhere, and this is fewer |
| 297 | # lines of code than a core-only Makefile.PL, and no more |
| 298 | # complex |
| 299 | $fromname = 'VERSION'; |
| 300 | $key = 'DISTNAME'; |
| 301 | $value = 'podlators'; |
| 302 | $mname = 'Pod'; |
| 303 | } else { |
| 304 | $key = 'ABSTRACT_FROM'; |
| 305 | # We need to cope well with various possible layouts |
| 306 | my @dirs = split /::/, $mname; |
| 307 | my $leaf = pop @dirs; |
| 308 | my $leafname = "$leaf.pm"; |
| 309 | my $pathname = join '/', @dirs, $leafname; |
| 310 | my @locations = ($leafname, $pathname, "lib/$pathname"); |
| 311 | foreach (@locations) { |
| 312 | if (-f $_) { |
| 313 | $fromname = $_; |
| 314 | last; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | unless ($fromname) { |
| 319 | die "For $mname tried @locations in in $ext_dir but can't find source"; |
| 320 | } |
| 321 | ($value = $fromname) =~ s/\.pm\z/.pod/; |
| 322 | $value = $fromname unless -e $value; |
| 323 | } |
| 324 | open my $fh, '>', 'Makefile.PL' |
| 325 | or die "Can't open Makefile.PL for writing: $!"; |
| 326 | printf $fh <<'EOM', $0, $mname, $fromname, $key, $value; |
| 327 | #-*- buffer-read-only: t -*- |
| 328 | |
| 329 | # This Makefile.PL was written by %s. |
| 330 | # It will be deleted automatically by make realclean |
| 331 | |
| 332 | use strict; |
| 333 | use ExtUtils::MakeMaker; |
| 334 | |
| 335 | # This is what the .PL extracts to. Not the ultimate file that is installed. |
| 336 | # (ie Win32 runs pl2bat after this) |
| 337 | |
| 338 | # Doing this here avoids all sort of quoting issues that would come from |
| 339 | # attempting to write out perl source with literals to generate the arrays and |
| 340 | # hash. |
| 341 | my @temps = 'Makefile.PL'; |
| 342 | foreach (glob('scripts/pod*.PL')) { |
| 343 | # The various pod*.PL extractors change directory. Doing that with relative |
| 344 | # paths in @INC breaks. It seems the lesser of two evils to copy (to avoid) |
| 345 | # the chdir doing anything, than to attempt to convert lib paths to |
| 346 | # absolute, and potentially run into problems with quoting special |
| 347 | # characters in the path to our build dir (such as spaces) |
| 348 | require File::Copy; |
| 349 | |
| 350 | my $temp = $_; |
| 351 | $temp =~ s!scripts/!!; |
| 352 | File::Copy::copy($_, $temp) or die "Can't copy $temp to $_: $!"; |
| 353 | push @temps, $temp; |
| 354 | } |
| 355 | |
| 356 | my $script_ext = $^O eq 'VMS' ? '.com' : ''; |
| 357 | my %%pod_scripts; |
| 358 | foreach (glob('pod*.PL')) { |
| 359 | my $script = $_; |
| 360 | s/.PL$/$script_ext/i; |
| 361 | $pod_scripts{$script} = $_; |
| 362 | } |
| 363 | my @exe_files = values %%pod_scripts; |
| 364 | |
| 365 | WriteMakefile( |
| 366 | NAME => '%s', |
| 367 | VERSION_FROM => '%s', |
| 368 | %-13s => '%s', |
| 369 | realclean => { FILES => "@temps" }, |
| 370 | (%%pod_scripts ? ( |
| 371 | PL_FILES => \%%pod_scripts, |
| 372 | EXE_FILES => \@exe_files, |
| 373 | clean => { FILES => "@exe_files" }, |
| 374 | ) : ()), |
| 375 | ); |
| 376 | |
| 377 | # ex: set ro: |
| 378 | EOM |
| 379 | close $fh or die "Can't close Makefile.PL: $!"; |
| 380 | } |
| 381 | eval { |
| 382 | my $ftime = time - 4; |
| 383 | utime $ftime, $ftime, 'Makefile.PL'; |
| 384 | }; |
| 385 | print "\nRunning Makefile.PL in $ext_dir\n"; |
| 386 | |
| 387 | # Presumably this can be simplified |
| 388 | my @cross; |
| 389 | if (defined $::Cross::platform) { |
| 390 | # Inherited from win32/buildext.pl |
| 391 | @cross = "-MCross=$::Cross::platform"; |
| 392 | } elsif ($opts{cross}) { |
| 393 | # Inherited from make_ext.pl |
| 394 | @cross = '-MCross'; |
| 395 | } |
| 396 | |
| 397 | my @args = ("-I$lib_dir", @cross, 'Makefile.PL'); |
| 398 | if ($is_VMS) { |
| 399 | my $libd = VMS::Filespec::vmspath($lib_dir); |
| 400 | push @args, "INST_LIB=$libd", "INST_ARCHLIB=$libd"; |
| 401 | } else { |
| 402 | push @args, 'INSTALLDIRS=perl', 'INSTALLMAN1DIR=none', |
| 403 | 'INSTALLMAN3DIR=none'; |
| 404 | } |
| 405 | push @args, @$pass_through; |
| 406 | _quote_args(\@args) if $is_VMS; |
| 407 | print join(' ', @run, $perl, @args), "\n"; |
| 408 | my $code = system @run, $perl, @args; |
| 409 | warn "$code from $ext_dir\'s Makefile.PL" if $code; |
| 410 | |
| 411 | # Right. The reason for this little hack is that we're sitting inside |
| 412 | # a program run by ./miniperl, but there are tasks we need to perform |
| 413 | # when the 'realclean', 'distclean' or 'veryclean' targets are run. |
| 414 | # Unfortunately, they can be run *after* 'clean', which deletes |
| 415 | # ./miniperl |
| 416 | # So we do our best to leave a set of instructions identical to what |
| 417 | # we would do if we are run directly as 'realclean' etc |
| 418 | # Whilst we're perfect, unfortunately the targets we call are not, as |
| 419 | # some of them rely on a $(PERL) for their own distclean targets. |
| 420 | # But this always used to be a problem with the old /bin/sh version of |
| 421 | # this. |
| 422 | if ($is_Unix) { |
| 423 | my $suffix = '.sh'; |
| 424 | foreach my $clean_target ('realclean', 'veryclean') { |
| 425 | my $file = "$return_dir/$clean_target$suffix"; |
| 426 | open my $fh, '>>', $file or die "open $file: $!"; |
| 427 | # Quite possible that we're being run in parallel here. |
| 428 | # Can't use Fcntl this early to get the LOCK_EX |
| 429 | flock $fh, 2 or warn "flock $file: $!"; |
| 430 | print $fh <<"EOS"; |
| 431 | cd $ext_dir |
| 432 | if test ! -f Makefile -a -f Makefile.old; then |
| 433 | echo "Note: Using Makefile.old" |
| 434 | make -f Makefile.old $clean_target MAKE='@make' @pass_through |
| 435 | else |
| 436 | if test ! -f Makefile ; then |
| 437 | echo "Warning: No Makefile!" |
| 438 | fi |
| 439 | make $clean_target MAKE='@make' @pass_through |
| 440 | fi |
| 441 | cd $return_dir |
| 442 | EOS |
| 443 | close $fh or die "close $file: $!"; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if (not -f $makefile) { |
| 449 | print "Warning: No Makefile!\n"; |
| 450 | } |
| 451 | |
| 452 | if ($is_VMS) { |
| 453 | _macroify_passthrough($pass_through); |
| 454 | unshift @$pass_through, "/DESCRIPTION=$makefile"; |
| 455 | } |
| 456 | |
| 457 | if (!$target or $target !~ /clean$/) { |
| 458 | # Give makefile an opportunity to rewrite itself. |
| 459 | # reassure users that life goes on... |
| 460 | my @args = ('config', @$pass_through); |
| 461 | system(@run, @make, @args) and print "@run @make @args failed, continuing anyway...\n"; |
| 462 | } |
| 463 | my @targ = ($target, @$pass_through); |
| 464 | print "Making $target in $ext_dir\n@run @make @targ\n"; |
| 465 | my $code = system(@run, @make, @targ); |
| 466 | die "Unsuccessful make($ext_dir): code=$code" if $code != 0; |
| 467 | |
| 468 | chdir $return_dir || die "Cannot cd to $return_dir: $!"; |
| 469 | } |
| 470 | |
| 471 | sub _quote_args { |
| 472 | my $args = shift; # must be array reference |
| 473 | |
| 474 | # Do not quote qualifiers that begin with '/'. |
| 475 | map { if (!/^\//) { |
| 476 | $_ =~ s/\"/""/g; # escape C<"> by doubling |
| 477 | $_ = q(").$_.q("); |
| 478 | } |
| 479 | } @{$args} |
| 480 | ; |
| 481 | } |
| 482 | |
| 483 | sub _macroify_passthrough { |
| 484 | my $passthrough = shift; |
| 485 | _quote_args($passthrough); |
| 486 | my $macro = '/MACRO=(' . join(',',@$passthrough) . ')'; |
| 487 | @$passthrough = (); |
| 488 | @$passthrough[0] = $macro; |
| 489 | } |