1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
|
#!/usr/bin/perl
use 5.006;
use strict;
use warnings;
use Config;
our ($PARL, $OrigPARL);
# bootstrap ourselves on a binary-only install.
unless (eval { require PAR; 1 }) {
$PARL ||= _can_run("parl$Config{_exe}") or die("Can't find par loader");
exec($PARL, $0, @ARGV);
}
use Archive::Zip;
use Cwd;
use ExtUtils::MakeMaker; # just for maybe_command()
use File::Basename;
use File::Spec;
use File::Temp qw(tempfile);
use Getopt::Long;
use Module::ScanDeps 0.10;
use PAR::Filter;
our $VERSION = 0.05;
$| = 1;
$SIG{INT} = sub { exit() }; # exit gracefully and clean up after ourselves.
sub opt(*); # imal quoting
sub is_win32();
sub vprint($@);
our ($Options);
our (@Input, $Output);
our ($logfh);
our ($par_file);
our (@SharedLibs);
my $dynperl = $Config{useshrplib} && ($Config{useshrplib} ne 'false');
main();
sub main {
parse_argv();
check_write($Output);
generate_code();
run_code();
_die("XXX: Not reached?");
}
#######################################################################
sub compile_par {
my ($cfh, $lose);
my $root = '';
$root = "$Config{archname}/" if opt('m');
if (opt(S) || opt(p)) {
# We need to keep it.
if (opt(e) or !@Input) {
$par_file = "a.par";
} else {
$par_file = $Input[0];
# File off extension if present
# hold on: plx is executable; also, careful of ordering!
$par_file =~ s/\.(?:p(?:lx|l|h)|m)\z//i;
$par_file .= ".par";
}
$par_file = $Output if opt(p) && $Output =~ /\.par\z/i;
$Output = $par_file if opt(p);
check_write($par_file);
} else {
# Don't need to keep it, be safe with a tempfile.
$lose = 1;
($cfh, $par_file) = tempfile("ppXXXXX", SUFFIX => ".par");
close $cfh; # See comment just below
}
vprint 1, "Writing PAR on $par_file";
my (@modules, @data, @exclude);
foreach my $name (@{opt(M) || []}) {
_name2moddata($name, \@modules, \@data);
}
foreach my $name ('PAR', @{opt(X) || []}) {
_name2moddata($name, \@exclude, \@exclude);
}
my %map;
unshift @INC, @{opt(I) || []};
unshift @SharedLibs, map _find_shlib($_), @{opt(l) || []};
my %skip = map { $_, 1 } map Module::ScanDeps::_find_in_inc($_), @exclude;
my @files = ((map Module::ScanDeps::_find_in_inc($_), @modules), @Input);
my $scan_dispatch = (
opt(n) ? \&Module::ScanDeps::scan_deps_runtime
: \&Module::ScanDeps::scan_deps
);
$scan_dispatch->(
rv => \%map,
files => \@files,
execute => opt(x),
compile => opt(c),
skip => \%skip,
(opt(n)) ? () : (
recurse => 1,
first => 1,
),
);
# Reset the exclusion list so we won't implicitly ignore files in -M
%skip = map { $_, 1 } map Module::ScanDeps::_find_in_inc($_), @exclude;
Module::ScanDeps::add_deps(
rv => \%map,
modules => \@modules,
skip => \%skip,
);
my %text;
$text{$_} = ($map{$_}{type} =~ /^(?:module|autoload)$/) for keys %map;
$map{$_} = $map{$_}{file} for keys %map;
my %manifest = map { $_ => 1 } ('MANIFEST', 'META.yml');
my $size = 0;
my $zip = Archive::Zip->new;
my $old_member;
if (opt('m') and -e $par_file) {
$zip->read($par_file);
if ($old_member = $zip->memberNamed( 'MANIFEST' )) {
$manifest{$_}++ for grep /^\S/, split(/\n/, $old_member->contents);
}
else {
$old_member = 1;
}
}
my %zip_args = (
'desiredCompressionMethod'
=> Archive::Zip::COMPRESSION_DEFLATED(),
'desiredCompressionLevel'
=> Archive::Zip::COMPRESSION_LEVEL_BEST_COMPRESSION(),
);
$zip->addDirectory('', substr($root, 0, -1)) if $root and %map and $] >= 5.008;
$zip->addDirectory('', $root.'lib') if %map and $] >= 5.008;
my $verbatim = ($ENV{PAR_VERBATIM} || 0);
my $mod_filter = PAR::Filter->new(
'PatchContent',
@{ opt(F) || ($verbatim ? [] : ['PodStrip']) },
);
foreach my $pfile (sort grep length $map{$_}, keys %map) {
next if !opt(B) and ($map{$pfile} eq "$Config{privlib}/$pfile"
or $map{$pfile} eq "$Config{archlib}/$pfile");
next unless $zip;
vprint 2, "... adding $map{$pfile} as ${root}lib/$pfile";
if ($text{$pfile} or $pfile =~ /utf8_heavy\.pl$/i) {
my $content_ref = $mod_filter->apply($map{$pfile}, $pfile);
$size += length( $$content_ref );
$zip->addString( $content_ref => $root."lib/$pfile", %zip_args );
}
elsif (
basename($map{$pfile}) =~ /^Tk\.dll$/i and opt(i)
and eval { require Win32::Exe; 1 }
and eval { require Win32::Exe::IconFile; 1 }
and $] < 5.008 # XXX - broken on ActivePerl 5.8+
) {
my $tkdll = Win32::Exe->new($map{$pfile});
my $ico = Win32::Exe::IconFile->new(opt(i));
$tkdll->set_icons(scalar $ico->icons);
$zip->addString($tkdll->dump => $root."lib/$pfile", %zip_args);
}
else {
$zip->addFile($map{$pfile} => $root."lib/$pfile");
$size += -s $map{$pfile};
}
$manifest{$root."lib/$pfile"}++;
}
@Input = grep !/\.pm\z/i, @Input;
$zip->addDirectory('', 'script') if @Input and $] >= 5.008;
my $script_filter = PAR::Filter->new( @{ opt(f) } ) if opt(f);
foreach my $input (@Input) {
my $name = basename($input);
$size += -s $input;
if ($script_filter) {
$zip->addString(
$script_filter->apply($input, $name) => "script/$name",
%zip_args,
);
}
else {
$zip->addFile($input => "script/$name");
}
$manifest{"script/$name"}++;
}
my $shlib = "shlib/$Config{archname}";
$zip->addDirectory('', $shlib) if @SharedLibs and $] >= 5.008;
foreach my $input (@SharedLibs) {
next unless -e $input;
$size += -s $input;
my $name = basename($input);
vprint 2, "... adding $input as $shlib/$name";
$zip->addFile($input => "$shlib/$name");
$manifest{"$shlib/$name"}++;
}
foreach my $input (@data) {
unless (-r $input and !-d $input) {
warn "'$input' does not exist or is not readable; skipping\n";
next;
}
my $name = basename($input);
$size += -s $input;
$zip->addFile($input => $name);
$manifest{$name}++;
}
# Add a script/main.pl except when building PAR file with multiple scripts
if (@Input and (@Input == 1 or !opt(p))) {
$zip->addString(
((@Input == 1)
? _main_pl_single("script/" . basename($Input[0]))
: _main_pl_multi()) => "script/main.pl", %zip_args
);
$manifest{"script/main.pl"}++;
}
my $clean = (opt(C) ? 1 : 0);
my $dist_name = (opt(p) ? $par_file : $Output);
my $manifest = join("\n", ' <!-- accessible as jar:file:///NAME.par!/MANIFEST in compliant browsers -->', (sort keys %manifest), q( # <html><body onload="var X=document.body.innerHTML.split(/\n/);var Y='<iframe src="META.yml" style="float:right;height:40%;width:40%"></iframe><ul>';for(var x in X){if(!X[x].match(/^\s*#/)&&X[x].length)Y+='<li><a href="'+X[x]+'">'+X[x]+'</a>'}document.body.innerHTML=Y">));
my $meta_yaml = << "YAML";
build_requires: {}
conflicts: {}
dist_name: $dist_name
distribution_type: par
dynamic_config: 0
generated_by: 'Perl Packager version $VERSION'
license: unknown
par:
clean: $clean
signature: ''
verbatim: $verbatim
version: $PAR::VERSION
YAML
$size += length($_) for ($manifest, $meta_yaml);
vprint 2, "... making $_" for qw(MANIFEST META.yml);
$zip->addString($manifest => 'MANIFEST', %zip_args);
$zip->addString($meta_yaml => 'META.yml', %zip_args);
if ($old_member) {
$zip->overwrite;
}
else {
$zip->writeToFileNamed($par_file);
}
my $newsize = -s $par_file;
vprint 2, sprintf(
"*** %s: %d bytes read, %d compressed, %2.2d%% saved.\n",
$par_file, $size, $newsize, (100 - ($newsize / $size * 100))
);
if ( opt('s') ) {
if (eval {
require PAR::Dist; require Module::Signature; Module::Signature->VERSION >= 0.25
}) {
vprint 0, "Signing $par_file";
PAR::Dist::sign_par($par_file);
}
else {
vprint -1, "*** Signing requires PAR::Dist with Module::Signature 0.25 or later. Skipping";
}
}
par_to_exe() unless opt(p);
if ($lose) {
vprint 2, "Unlinking $par_file";
unlink $par_file or _die("Can't unlink $par_file: $!");
}
}
sub _name2moddata {
my ($name, $mod, $dat) = @_;
if ($name =~ /^[\w:]+$/) {
$name =~ s/::/\//g;
push @$mod, "$name.pm";
}
elsif ($name =~ /\.(?:pm|ix|al)$/i) {
push @$mod, $name;
}
else {
push @$dat, $name;
}
}
sub par_to_exe {
my $parl = 'parl';
$parl = 'parldyn' if (opt(d) and $dynperl);
$parl .= $Config{_exe};
$parl = 'par.pl' if opt(P);
$PARL ||= _can_run($parl, opt(P)) or _die("Can't find par loader");
if ($^O ne 'MSWin32' or opt(p) or opt(P)) {
generate_output();
}
elsif (!opt(N) and !opt(i)) {
generate_output();
fix_console() if opt(g);
}
elsif (eval { require Win32::Exe; 1 }) {
move_parl();
Win32::Exe->new($PARL)->update(
icon => opt(i),
info => opt(N),
);
append_parl();
generate_output();
Win32::Exe->new($Output)->update(
icon => opt(i),
info => opt(N),
);
fix_console();
unlink($PARL);
unlink("$PARL.bak");
return;
}
else {
die "--icon and --info support needs Win32::Exe";
}
}
sub fix_console {
return unless opt(g);
vprint 1, "Fixing $Output to remove its console window";
strip_console($Output);
if ($dynperl and !opt(d)) {
# we have a static.exe that needs taking care of.
my $buf;
open _FH, $OrigPARL || $PARL or die $!;
binmode _FH;
seek _FH, -8, 2;
read _FH, $buf, 8;
die unless $buf eq "\nPAR.pm\n";
seek _FH, -12, 2;
read _FH, $buf, 4;
seek _FH, -12 - unpack("N", $buf) - 4, 2;
read _FH, $buf, 4;
strip_console($Output, unpack("N", $buf));
}
}
sub move_parl {
$OrigPARL = $PARL;
my $cfh;
local $/;
open _FH, $PARL or die $!;
binmode(_FH);
($cfh, $PARL) = tempfile("parlXXXX", SUFFIX => ".exe", UNLINK => 1);
binmode($cfh);
print $cfh <_FH>;
close $cfh;
}
sub append_parl {
my $buf;
seek _FH, -8, 2;
read _FH, $buf, 8;
die unless $buf eq "\nPAR.pm\n";
seek _FH, -12, 2;
read _FH, $buf, 4;
seek _FH, -12 - unpack("N", $buf), 2;
open my $cfh, ">>", $PARL or die $!;
binmode($cfh);
print $cfh <_FH>;
close $cfh;
}
sub generate_output {
my @args = ('-B', "-O$Output", $par_file);
unshift @args, '-q' unless opt(v);
if (opt(L)) {
unshift @args, "-L".opt(L);
}
if (opt(P)) {
unshift @args, $PARL;
$PARL = $^X;
}
vprint 0, "Running $PARL @args";
system($PARL, @args);
}
sub strip_console {
my $file = shift;
my $preoff = shift || 0;
my ($record, $magic, $signature, $offset, $size);
open my $exe, "+< $file" or die "Cannot open $file: $!\n";
binmode $exe;
seek $exe, $preoff, 0;
# read IMAGE_DOS_HEADER structure
read $exe, $record, 64;
($magic, $offset) = unpack "Sx58L", $record;
die "$ARGV[0] is not an MSDOS executable file.\n"
unless $magic == 0x5a4d; # "MZ"
# read signature, IMAGE_FILE_HEADER and first WORD of IMAGE_OPTIONAL_HEADER
seek $exe, $preoff + $offset, 0;
read $exe, $record, 4+20+2;
($signature,$size,$magic) = unpack "Lx16Sx2S", $record;
die "PE header not found" unless $signature == 0x4550; # "PE\0\0"
die "Optional header is neither in NT32 nor in NT64 format"
unless ($size == 224 && $magic == 0x10b) || # IMAGE_NT_OPTIONAL_HDR32_MAGIC
($size == 240 && $magic == 0x20b); # IMAGE_NT_OPTIONAL_HDR64_MAGIC
# Offset 68 in the IMAGE_OPTIONAL_HEADER(32|64) is the 16 bit subsystem code
seek $exe, $preoff + $offset+4+20+68, 0;
print $exe pack "S", 2; # IMAGE_WINDOWS
close $exe;
}
sub generate_code {
vprint 0, "Packing @Input";
if (check_par($Input[0])) {
# invoked as "pp foo.par" - never unlink it
$par_file = $Input[0];
$Options->{S} = 1;
par_to_exe();
}
else {
compile_par();
}
exit(0) if (!opt('r'));
}
sub run_code {
$Output = File::Spec->catfile(".", $Output);
my @Loader = ();
push @Loader, $^X if opt('P');
push @Loader, $^X, "-MPAR" if opt('p');
vprint 0, "Running @Loader $Output @ARGV";
system(@Loader, $Output, @ARGV);
exit(0);
}
sub vprint ($@) {
my $level = shift;
my $msg = "@_";
$msg .= "\n" unless substr($msg, -1) eq "\n";
if (opt(v) > $level) {
print "$0: $msg" if !opt(L);
print $logfh "$0: $msg" if opt(L);
}
}
sub parse_argv {
Getopt::Long::Configure("no_ignore_case");
# no difference in exists and defined for %ENV; also, a "0"
# argument or a "" would not help pp, so skip
unshift @ARGV, split ' ', $ENV{PP_OPTS} if $ENV{PP_OPTS};
$Options = {};
Getopt::Long::GetOptions( $Options,
'M|add:s@', # Include modules
'B|bundle', # Bundle core modules
'C|clean', # Clean up temporary files
'c|compile', # Compile code to get dependencies
'd|dependent', # Do not package libperl
'e|eval:s', # Packing one-liner
'x|execute', # Execute code to get dependencies
'X|exclude:s@', # Exclude modules
'f|filter:s@', # Input filters for scripts
'g|gui', # No console window
'h|help', # Help me
'i|icon:s', # Icon file
'N|info:s@', # Executable header info
'I|lib:s@', # Include directories (for perl)
'l|link:s@', # Include additional shared libraries
'L|log:s', # Where to log packaging process information
'F|modfilter:s@', # Input filter for perl modules
'm|multiarch', # Build multiarch PAR file
'n|noscan', # Skips static scanning
'o|output:s', # Output file
'p|par', # Generate PAR only
'P|perlscript', # Generate perl script
'r|run', # Run the resulting executable
'S|save', # Preserve intermediate PAR files
's|sign', # Sign PAR files
'v|verbose:i', # Verbosity level
'vv', # Verbosity level 2
'vvv', # Verbosity level 3
'V|version', # Show version
);
$Options->{p} = 1 if opt('m');
$Options->{v} = 1 if exists $Options->{v} and !$Options->{v};
$Options->{v} = 2 if exists $Options->{vv};
$Options->{v} = 3 if exists $Options->{vvv};
$Options->{B} = 1 unless opt(p) or opt(P);
helpme() if opt(h); # And exit
show_version() if opt(V); # And exit
$Output = opt(o) || a_out();
open $logfh, '>>', opt(L) or die ("XXX: Cannot open log: $!") if (opt(L));
if (opt(e)) {
warn "$0: using -e 'code' as input file, ignoring @ARGV\n" if @ARGV and !opt(r);
my ($fh, $fake_input) = tempfile("ppXXXXX", SUFFIX => ".pl", UNLINK => 1);
print $fh $Options->{e};
close $fh;
@Input = $fake_input;
}
else {
@Input = shift @ARGV if @ARGV;
_die("$0: No input files specified\n") unless @Input or opt(M);
push @Input, @ARGV if @ARGV and !opt(r);
check_read(@Input) if @Input;
check_perl(@Input) if @Input;
sanity_check();
}
}
sub a_out {
return 'a' . (
opt(p) ? '.par' :
opt(P) ? '.pl' : ($Config{_exe} || '.out')
);
}
sub opt(*) {
my $opt = shift;
return exists($Options->{$opt}) && ($Options->{$opt} || 0);
}
sub sanity_check {
# Check the input and output files make sense, are read/writable.
if ("@Input" eq $Output) {
my $a_out = a_out();
if ("@Input" eq $a_out) {
_die("$0: Packing $a_out to itself is probably not what you want to do.\n");
# You fully deserve what you get now. No you *don't*. typos happen.
} else {
warn "$0: Will not write output on top of input file, ",
"packing to $a_out instead\n";
$Output = $a_out;
}
}
}
sub check_read {
foreach my $file (@_) {
unless (-r $file) {
_die("$0: Input file $file is a directory, not a file\n") if -d _;
unless (-e _) {
_die("$0: Input file $file was not found\n");
} else {
_die("$0: Cannot read input file $file: $!\n");
}
}
unless (-f _) {
# XXX: die? don't try this on /dev/tty
warn "$0: WARNING: input $file is not a plain file\n";
}
}
}
sub check_write {
foreach my $file (@_) {
if (-d $file) {
_die("$0: Cannot write on $file, is a directory\n");
}
if (-e _) {
_die("$0: Cannot write on $file: $!\n") unless -w _;
}
unless (-w cwd()) {
_die("$0: Cannot write in this directory: $!\n");
}
}
}
sub check_perl {
my $file = shift;
return if check_par($file);
unless (-T $file) {
warn "$0: Binary `$file' sure doesn't smell like perl source!\n";
if (my $file_checker = _can_run("file")) {
print "Checking file type... ";
system($file_checker, $file);
}
_die("Please try a perlier file!\n");
}
open(my $handle, "<", $file) or _die("XXX: Can't open $file: $!");
local $_ = <$handle>;
if (/^#!/ && !/perl/) {
_die("$0: $file is a ", /^#!\s*(\S+)/, " script, not perl\n");
}
}
sub check_par {
my $file = shift or return;
open(my $handle, "<", $file) or _die("XXX: Can't open $file: $!");
binmode($handle);
local $/ = \4;
return (<$handle> eq "PK\x03\x04");
}
sub helpme {
print "Perl Packager, version $VERSION (PAR version $PAR::VERSION)\n\n";
{
no warnings;
exec "pod2usage $0";
exec "perldoc $0";
exec "pod2text $0";
}
}
sub show_version {
print << ".";
Perl Packager, version $VERSION (PAR version $PAR::VERSION)
Copyright 2002, 2003, 2004 by Audrey Tang <autrijus\@autrijus.org>
Neither this program nor the associated "parl" program impose any
licensing restrictions on files generated by their execution, in
accordance with the 8th article of the Artistic License:
"Aggregation of this Package with a commercial distribution is
always permitted provided that the use of this Package is embedded;
that is, when no overt attempt is made to make this Package's
interfaces visible to the end user of the commercial distribution.
Such use shall not be construed as a distribution of this Package."
Therefore, you are absolutely free to place any license on the resulting
executable, as long as the packed 3rd-party libraries are also available
under the Artistic License.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. There is NO warranty; not even for
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
.
exit;
}
sub pod_strip {
my ($pl_text, $filename) = @_;
no warnings 'uninitialized';
my $data = '';
$data = $1 if $pl_text =~ s/((?:^__DATA__\r?\n).*)//ms;
my $line = 1;
if ($pl_text =~ /^=(?:head\d|pod|begin|item|over|for|back|end)\b/) {
$pl_text = "\n$pl_text";
$line--;
}
$pl_text =~ s{(
(.*?\n)
=(?:head\d|pod|begin|item|over|for|back|end)\b
.*?\n
(?:=cut[\t ]*[\r\n]*?|\Z)
(\r?\n)?
)}{
my ($pre, $post) = ($2, $3);
"$pre#line " . (
$line += ( () = ( $1 =~ /\n/g ) )
) . $post;
}gsex;
$pl_text = '#line 1 "' . ($filename) . "\"\n" . $pl_text
if length $filename;
$pl_text =~ s/^#line 1 (.*\n)(#!.*\n)/$2#line 2 $1/g;
return $pl_text . $data;
}
sub _die {
$logfh->print(@_) if opt(L);
die @_;
}
sub _find_shlib {
my $file = shift;
return $file if -e $file;
if (not exists $ENV{$Config{ldlibpthname}}) {
print "Can't find $file. Environment variable " .
"$Config{ldlibpthname} does not exist.\n";
return;
}
for my $dir (
File::Basename::dirname($0),
split(/\Q$Config{path_sep}\E/, $ENV{$Config{ldlibpthname}})
) {
my $abs = File::Spec->catfile($dir, $file);
return $abs if -e $abs;
$abs = File::Spec->catfile($dir, "$file.$Config{dlext}");
return $abs if -e $abs;
}
# be extra magical and prepend "lib" to the filename
return _find_shlib("lib$file") unless $file =~ /^lib/;
}
sub _can_run {
my ($command, $no_exec) = @_;
for my $dir (
File::Basename::dirname($0),
split(/\Q$Config{path_sep}\E/, $ENV{PATH})
) {
my $abs = File::Spec->catfile($dir, $command);
return $abs if $no_exec or $abs = MM->maybe_command($abs);
}
return;
}
1;
sub _main_pl_multi {
return << '__MAIN__';
my $file = $ENV{PAR_PROGNAME};
my $zip = $PAR::LibCache{$ENV{PAR_PROGNAME}} || Archive::Zip->new(__FILE__);
$file =~ s/^.*[\/\\]//;
$file =~ s/\.[^.]*$//i ;
my $member = eval { $zip->memberNamed($file) }
|| $zip->memberNamed("$file.pl")
|| $zip->memberNamed("script/$file")
|| $zip->memberNamed("script/$file.pl")
or die qq(Can't open perl script "$file": No such file or directory);
PAR::_run_member($member, 1);
__MAIN__
}
sub _main_pl_single {
my $file = shift;
return << "__MAIN__";
my \$zip = \$PAR::LibCache{\$ENV{PAR_PROGNAME}} || Archive::Zip->new(__FILE__);
my \$member = eval { \$zip->memberNamed('$file') }
or die qq(Can't open perl script "$file": No such file or directory (\$zip));
PAR::_run_member(\$member, 1);
__MAIN__
}
END {
unlink $par_file if ($par_file && !opt(S) && !opt(p));
}
__END__
=head1 NAME
pp - Perl Packager
=head1 SYNOPSIS
B<pp> S<[ B<-BILMSVXdeghilmoprsv> ]> S<[ I<parfile> | I<scriptfile> ]>...
=head1 EXAMPLES
Note: When running on Microsoft Windows, the F<a.out> below will be
replaced by F<a.exe> instead.
% pp hello # Pack 'hello' into executable 'a.out'
% pp -o hello hello.pl # Pack 'hello.pl' into executable 'hello'
% pp -o foo foo.pl bar.pl # Pack 'foo.pl' and 'bar.pl' into 'foo'
% ./foo # Run 'foo.pl' inside 'foo'
% mv foo bar; ./bar # Run 'bar.pl' inside 'foo'
% mv bar baz; ./baz # Error: Can't open perl script "baz"
% pp -p file # Creates a PAR file, 'a.par'
% pp -o hello a.par # Pack 'a.par' to executable 'hello'
% pp -S -o hello file # Combine the two steps above
% pp -p -o out.par file # Creates 'out.par' from 'file'
% pp -B -p -o out.par file # same as above, but bundles core modules
% pp -P -o out.pl file # Creates 'out.pl' from 'file'
% pp -B -p -o out.pl file # same as above, but bundles core modules
# (-B is assumed when making executables)
% pp -e "print 123" # Pack a one-liner into 'a.out'
% pp -p -e "print 123" # Creates a PAR file 'a.par'
% pp -P -e "print 123" # Creates a perl script 'a.pl'
% pp -c hello # Check dependencies from "perl -c hello"
% pp -x hello # Check dependencies from "perl hello"
% pp -n -x hello # same as above, but skips static scanning
% pp -I /foo hello # Extra paths (notice space after -I)
% pp -M Foo::Bar hello # Extra modules (notice space after -M)
% pp -M abbrev.pl hello # Extra files under @INC
% pp -X Foo::Bar hello # Exclude modules (notice space after -X)
% pp -r hello # Pack 'hello' into 'a.out', runs 'a.out'
% pp -r hello a b c # Pack 'hello' into 'a.out', runs 'a.out'
# with arguments 'a b c'
% pp hello --log=c # Pack 'hello' into 'a.out', logs
# messages into 'c'
# Pack 'hello' into a console-less 'out.exe' with icon (Win32 only)
% pp --gui --icon hello.ico -o out.exe hello
=head1 DESCRIPTION
F<pp> creates standalone executables from Perl programs, using the
compressed packager provided by L<PAR>, and dependency detection
heuristics offered by L<Module::ScanDeps>. Source files are compressed
verbatim without compilation.
You may think of F<pp> as "F<perlcc> that works without hassle". :-)
A GUI interface is also available as the F<tkpp> command.
It does not provide the compilation-step acceleration provided by
F<perlcc> (however, see B<-f> below for byte-compiled, source-hiding
techniques), but makes up for it with better reliability, smaller
executable size, and full retrieval of original source code.
When a single input program is specified, the resulting executable will
behave identically as that program. However, when multiple programs
are packaged, the produced executable will run the one that has the
same basename as C<$0> (i.e. the filename used to invoke it). If
nothing matches, it dies with the error C<Can't open perl script "$0">.
=head1 OPTIONS
Options are available in a I<short> form and a I<long> form. For
example, the three lines below are all equivalent:
% pp -o output.exe input.pl
% pp --output output.exe input.pl
% pp --output=output.exe input.pl
=over 4
=item B<-M>, B<--add>=I<MODULE>|I<FILE>
Add the specified module into the package, along with its dependencies.
Also accepts filenames relative to the C<@INC> path; i.e. C<-M
Module::ScanDeps> means the same thing as C<-M Module/ScanDeps.pm>.
If I<FILE> does not have a C<.pm>/C<.ix>/C<.al> extension, it will not
be scanned for dependencies, and will be placed under C</> instead of
C</lib/> inside the PAR file.
You may specify C<-M> multiple times.
=item B<-B>, B<--bundle>
Bundle core modules in the resulting package. This option is enabled
by default, except when C<-p> or C<-P> is specified.
=item B<-C>, B<--clean>
Clean up temporary files extracted from the application at runtime.
By default, these files are cached in the temporary directory; this
allows the program to start up faster next time.
=item B<-d>, B<--dependent>
Reduce the executable size by not including a copy of perl interpreter.
Executables built this way will need a separate F<perl5x.dll>
or F<libperl.so> to function correctly. This option is only available
if perl is built as a shared library.
=item B<-c>, B<--compile>
Run C<perl -c inputfile> to determine additonal run-time dependencies.
=item B<-e>, B<--eval>=I<STRING>
Package a one-liner, much the same as C<perl -e '...'>
=item B<-x>, B<--execute>
Run C<perl inputfile> to determine additonal run-time dependencies.
=item B<-X>, B<--exclude>=I<MODULE>
Exclude the given module from the dependency search patch and from the
package.
=item B<-f>, B<--filter>=I<FILTER>
Filter source script(s) with a L<PAR::Filter> subclass. You may specify
multiple such filters.
If you wish to hide the source code from casual prying, this will do:
% pp -f Bleach source.pl
Users with Perl 5.8.1 and above may also try out the experimental
byte-compiling filter, which will strip away all comments and indents:
% pp -f Bytecode source.pl
=item B<-g>, B<--gui>
Build an executable that does not have a console window. This option is
ignored on non-MSWin32 platforms or when C<-p> is specified.
=item B<-h>, B<--help>
Show basic usage information.
=item B<-i>, B<--icon>=I<FILE>
Specify an icon file (in F<.ico>, F<.exe> or F<.dll> format) for the
executable. This option is ignored on non-MSWin32 platforms or when C<-p>
is specified.
=item B<-N>, B<--info>=I<KEY=VAL>
Add additional information for the packed file, both in C<META.yml>
and in the executable header (if applicable). The name/value pair is
joined by C<=>. You may specify C<-N> multiple times, or use C<;> to
link several pairs.
For Win32 executables, these special C<KEY> names are recognized:
Comments CompanyName FileDescription FileVersion
InternalName LegalCopyright LegalTrademarks OriginalFilename
ProductName ProductVersion
=item B<-I>, B<--lib>=I<DIR>
Add the given directory to the perl library file search path. May
be specified multiple times.
=item B<-l>, B<--link>=I<FILE>|I<LIBRARY>
Add the given shared library (a.k.a. shared object or DLL) into the
packed file. Also accepts names under library paths; i.e.
C<-l ncurses> means the same thing as C<-l libncurses.so> or
C<-l /usr/local/lib/libncurses.so> in most Unixes. May be specified
multiple times.
=item B<-L>, B<--log>=I<FILE>
Log the output of packaging to a file rather than to stdout.
=item B<-F>, B<--modfilter>=I<FILTER>
Filter included perl module(s) with a L<PAR::Filter> subclass.
You may specify multiple such filters.
=item B<-m>, B<--multiarch>
Build a multi-architecture PAR file. Implies B<-p>.
=item B<-n>, B<--noscan>
Skip the default static scanning altogether, using run-time
dependencies from B<-c> or B<-x> exclusively.
=item B<-o>, B<--output>=I<FILE>
File name for the final packaged executable.
=item B<-p>, B<--par>
Create PAR archives only; do not package to a standalone binary.
=item B<-P>, B<--perlscript>
Create stand-alone perl script; do not package to a standalone binary.
=item B<-r>, B<--run>
Run the resulting packaged script after packaging it.
=item B<-S>, B<--save>
Do not delete generated PAR file after packaging.
=item B<-s>, B<--sign>
Cryptographically sign the generated PAR or binary file using
L<Module::Signature>.
=item B<-v>, B<--verbose>[=I<NUMBER>]
Increase verbosity of output; I<NUMBER> is an integer from C<1> to C<3>,
C<3> being the most verbose. Defaults to C<1> if specified without an
argument. Alternatively, B<-vv> sets verbose level to C<2>, and B<-vvv>
sets it to C<3>.
=item B<-V>, B<--version>
Display the version number and copyrights of this program.
=back
=head1 ENVIRONMENT
=over 4
=item PP_OPTS
Command-line options (switches). Switches in this variable are taken
as if they were on every F<pp> command line.
=back
=head1 NOTES
Here are some recipes showing how to utilize F<pp> to bundle
F<source.pl> with all its dependencies, on target machines with
different expected settings:
=over 4
=item Stand-alone setup
% pp -o packed.exe source.pl # makes packed.exe
# Now, deploy 'packed.exe' to target machine...
$ packed.exe # run it
=item Perl interpreter only, without core modules:
% pp -B -P -o packed.pl source.pl # makes packed.exe
# Now, deploy 'packed.exe' to target machine...
$ perl packed.pl # run it
=item Perl with core module installed:
% pp -P -o packed.pl source.pl # makes packed.exe
# Now, deploy 'packed.pl' to target machine...
$ perl packed.pl # run it
=item Perl with PAR.pm and its dependencies installed:
% pp -p source.pl # makes source.par
% echo "use PAR 'source.par';" > packed.pl;
% cat source.pl >> packed.pl; # makes packed.pl
# Now, deploy 'source.par' and 'packed.pl' to target machine...
$ perl packed.pl # run it
=back
Note that even if your perl was built with a shared library, the
'Stand-alone setup' above will I<not> need a separate F<perl5x.dll>
or F<libperl.so> to function correctly. Use C<--dependent> if you
are willing to ship the shared library with the application, which
can significantly reduce the executable size.
=head1 SEE ALSO
L<tkpp>, L<par.pl>, L<parl>, L<perlcc>
L<PAR>, L<Module::ScanDeps>
=head1 ACKNOWLEDGMENTS
Simon Cozens, Tom Christiansen and Edward Peschko for writing
F<perlcc>; this program try to mimic its interface as close
as possible, and copied liberally from their code.
Jan Dubois for writing the F<exetype.pl> utility, which has been
partially adapted into the C<-g> flag.
Mattia Barbon for providing the C<myldr> binary loader code.
Jeff Goff for suggesting the name C<pp>.
=head1 AUTHORS
Audrey Tang E<lt>cpan@audreyt.orgE<gt>
L<http://par.perl.org/> is the official PAR website. You can write
to the mailing list at E<lt>par@perl.orgE<gt>, or send an empty mail to
E<lt>par-subscribe@perl.orgE<gt> to participate in the discussion.
Please submit bug reports to E<lt>bug-par@rt.cpan.orgE<gt>.
=head1 COPYRIGHT
Copyright 2002, 2003, 2004 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>.
Neither this program nor the associated L<parl> program impose any
licensing restrictions on files generated by their execution, in
accordance with the 8th article of the Artistic License:
"Aggregation of this Package with a commercial distribution is
always permitted provided that the use of this Package is embedded;
that is, when no overt attempt is made to make this Package's
interfaces visible to the end user of the commercial distribution.
Such use shall not be construed as a distribution of this Package."
Therefore, you are absolutely free to place any license on the resulting
executable, as long as the packed 3rd-party libraries are also available
under the Artistic License.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See L<http://www.perl.com/perl/misc/Artistic.html>
=cut
|