ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/mkbundle
(Generate patch)

Comparing App-Staticperl/mkbundle (file contents):
Revision 1.8 by root, Wed Dec 8 09:13:55 2010 UTC vs.
Revision 1.17 by root, Wed Feb 9 09:52:27 2011 UTC

3############################################################################# 3#############################################################################
4# cannot load modules till after the tracer BEGIN block 4# cannot load modules till after the tracer BEGIN block
5 5
6our $VERBOSE = 1; 6our $VERBOSE = 1;
7our $STRIP = "pod"; # none, pod or ppi 7our $STRIP = "pod"; # none, pod or ppi
8our $UNISTRIP = 1; # always on, try to strip unicore swash data
8our $PERL = 0; 9our $PERL = 0;
10our $APP;
9our $VERIFY = 0; 11our $VERIFY = 0;
10our $STATIC = 0; 12our $STATIC = 0;
13our $PACKLIST = 0;
14
15our $OPTIMISE_SIZE = 0; # optimise for raw file size instead of for compression?
16
17our $CACHE;
18our $CACHEVER = 1; # do not change unless you know what you are doing
11 19
12my $PREFIX = "bundle"; 20my $PREFIX = "bundle";
13my $PACKAGE = "static"; 21my $PACKAGE = "static";
14 22
15my %pm; 23my %pm;
16my %pmbin; 24my %pmbin;
17my @libs; 25my @libs;
18my @static_ext; 26my @static_ext;
19my $extralibs; 27my $extralibs;
28my @staticlibs;
29my @incext;
20 30
21@ARGV 31@ARGV
22 or die "$0: use 'staticperl help' (or read the sources of staticperl)\n"; 32 or die "$0: use 'staticperl help' (or read the sources of staticperl)\n";
23 33
34# remove "." from @INC - staticperl.sh does it for us, but be on the safe side
35BEGIN { @INC = grep !/^\.$/, @INC }
36
24$|=1; 37$|=1;
25 38
26our ($TRACER_W, $TRACER_R); 39our ($TRACER_W, $TRACER_R);
27 40
28sub find_inc($) { 41sub find_incdir($) {
29 for (@INC) { 42 for (@INC) {
30 next if ref; 43 next if ref;
31 return $_ if -e "$_/$_[0]"; 44 return $_ if -e "$_/$_[0]";
32 } 45 }
33 46
34 undef 47 undef
35} 48}
36 49
50sub find_inc($) {
51 my $dir = find_incdir $_[0];
52
53 return "$dir/$_[0]"
54 if defined $dir;
55
56 undef
57}
58
37BEGIN { 59BEGIN {
38 # create a loader process to detect @INC requests before we load any modules 60 # create a loader process to detect @INC requests before we load any modules
39 my ($W_TRACER, $R_TRACER); # used by tracer 61 my ($W_TRACER, $R_TRACER); # used by tracer
40 62
41 pipe $R_TRACER, $TRACER_W or die "pipe: $!"; 63 pipe $R_TRACER, $TRACER_W or die "pipe: $!";
43 65
44 unless (fork) { 66 unless (fork) {
45 close $TRACER_R; 67 close $TRACER_R;
46 close $TRACER_W; 68 close $TRACER_W;
47 69
70 my $pkg = "pkg000000";
71
48 unshift @INC, sub { 72 unshift @INC, sub {
49 my $dir = find_inc $_[1] 73 my $dir = find_incdir $_[1]
50 or return; 74 or return;
51 75
52 syswrite $W_TRACER, "-\n$dir\n$_[1]\n"; 76 syswrite $W_TRACER, "-\n$dir\n$_[1]\n";
53 77
54 open my $fh, "<:perlio", "$dir/$_[1]" 78 open my $fh, "<:perlio", "$dir/$_[1]"
58 }; 82 };
59 83
60 while (<$R_TRACER>) { 84 while (<$R_TRACER>) {
61 if (/use (.*)$/) { 85 if (/use (.*)$/) {
62 my $mod = $1; 86 my $mod = $1;
87 my $pkg = ++$pkg;
88 my $eval = $mod = $mod =~ /[^A-Za-z0-9_:]/
63 eval "require $mod"; 89 ? "require $mod"
90 : "{ package $pkg; use $mod; }";
91 eval $eval;
64 warn "ERROR: $@ (while loading '$mod')\n" 92 warn "ERROR: $@ (while loading '$mod')\n"
65 if $@; 93 if $@;
66 syswrite $W_TRACER, "\n";
67 } elsif (/eval (.*)$/) { 94 } elsif (/eval (.*)$/) {
68 my $eval = $1; 95 my $eval = $1;
69 eval $eval; 96 eval $eval;
70 warn "ERROR: $@ (in '$eval')\n" 97 warn "ERROR: $@ (in '$eval')\n"
71 if $@; 98 if $@;
72 } 99 }
100
101 syswrite $W_TRACER, "\n";
73 } 102 }
74 103
75 exit 0; 104 exit 0;
76 } 105 }
77} 106}
78 107
79# module loading is now safe 108# module loading is now safe
80use Config;
81 109
82sub scan_al { 110sub trace_parse {
83 my ($auto, $autodir, $ix) = @_;
84
85 $pm{"$auto/$ix"} = "$autodir/$ix";
86
87 open my $fh, "<:perlio", "$autodir/$ix"
88 or die "$autodir/$ix: $!";
89
90 my $package;
91
92 while (<$fh>) {
93 if (/^\s*sub\s+ ([^[:space:];]+) \s* (?:\([^)]*\))? \s*;?\s*$/x) {
94 my $al = "auto/$package/$1.al";
95 my $inc = find_inc $al;
96
97 defined $inc or die "$al: autoload file not found, but should be there.\n";
98
99 $pm{$al} = "$inc/$al";
100
101 } elsif (/^\s*package\s+([^[:space:];]+)\s*;?\s*$/) {
102 ($package = $1) =~ s/::/\//g;
103 } elsif (/^\s*(?:#|1?\s*;?\s*$)/) {
104 # nop
105 } else {
106 warn "$autodir/$ix: unparsable line, please report: $_";
107 }
108 }
109}
110
111sub trace_module {
112 syswrite $TRACER_W, "use $_[0]\n";
113
114 for (;;) { 111 for (;;) {
115 <$TRACER_R> =~ /^-$/ or last; 112 <$TRACER_R> =~ /^-$/ or last;
116 my $dir = <$TRACER_R>; chomp $dir; 113 my $dir = <$TRACER_R>; chomp $dir;
117 my $name = <$TRACER_R>; chomp $name; 114 my $name = <$TRACER_R>; chomp $name;
118 115
119 $pm{$name} = "$dir/$name"; 116 $pm{$name} = "$dir/$name";
120 117
121 if ($name =~ /^(.*)\.pm$/) { 118 print "+ found potential dependency $name\n"
122 my $auto = "auto/$1"; 119 if $VERBOSE >= 3;
123 my $autodir = "$dir/$auto";
124
125 if (-d $autodir) {
126 opendir my $dir, $autodir
127 or die "$autodir: $!\n";
128
129 for (readdir $dir) {
130 # AutoLoader
131 scan_al $auto, $autodir, $_
132 if /\.ix$/;
133
134 # static ext
135 if (/\Q$Config{_a}\E$/o) {
136 push @libs, "$autodir/$_";
137 push @static_ext, $name;
138 }
139
140 # extralibs.ld
141 if ($_ eq "extralibs.ld") {
142 open my $fh, "<:perlio", "$autodir/$_"
143 or die "$autodir/$_";
144
145 local $/;
146 $extralibs .= " " . <$fh>;
147 }
148
149 # dynamic object
150 warn "WARNING: found shared object - can't link statically ($_)\n"
151 if /\.\Q$Config{dlext}\E$/o;
152 }
153 }
154 }
155 } 120 }
121}
122
123sub trace_module {
124 print "tracing module $_[0]\n"
125 if $VERBOSE >= 2;
126
127 syswrite $TRACER_W, "use $_[0]\n";
128 trace_parse;
156} 129}
157 130
158sub trace_eval { 131sub trace_eval {
132 print "tracing eval $_[0]\n"
133 if $VERBOSE >= 2;
134
159 syswrite $TRACER_W, "eval $_[0]\n"; 135 syswrite $TRACER_W, "eval $_[0]\n";
136 trace_parse;
160} 137}
161 138
162sub trace_finish { 139sub trace_finish {
163 close $TRACER_W; 140 close $TRACER_W;
164 close $TRACER_R; 141 close $TRACER_R;
166 143
167############################################################################# 144#############################################################################
168# now we can use modules 145# now we can use modules
169 146
170use common::sense; 147use common::sense;
148use Config;
171use Digest::MD5; 149use Digest::MD5;
150
151sub cache($$$) {
152 my ($variant, $src, $filter) = @_;
153
154 if (length $CACHE and 2048 <= length $src and defined $variant) {
155 my $file = "$CACHE/" . Digest::MD5::md5_hex "$CACHEVER\x00$variant\x00$src";
156
157 if (open my $fh, "<:perlio", $file) {
158 print "using cache for $file\n"
159 if $VERBOSE >= 7;
160
161 local $/;
162 return <$fh>;
163 }
164
165 $src = $filter->($src);
166
167 print "creating cache entry $file\n"
168 if $VERBOSE >= 8;
169
170 if (open my $fh, ">:perlio", "$file~") {
171 if ((syswrite $fh, $src) == length $src) {
172 close $fh;
173 rename "$file~", $file;
174 }
175 }
176
177 return $src;
178 }
179
180 $filter->($src)
181}
172 182
173sub dump_string { 183sub dump_string {
174 my ($fh, $data) = @_; 184 my ($fh, $data) = @_;
175 185
176 if (length $data) { 186 if (length $data) {
186 } else { 196 } else {
187 print $fh " \"\"\n"; 197 print $fh " \"\"\n";
188 } 198 }
189} 199}
190 200
191# required for @INC loading, unfortunately 201#############################################################################
192trace_module "PerlIO::scalar";
193 202
194#trace_module "Term::ReadLine::readline"; # Term::ReadLine::Perl dependency 203sub glob2re {
195# URI is difficult 204 for (quotemeta $_[0]) {
196#trace_module "URI::http"; 205 s/\\\*/\x00/g;
197#trace_module "URI::_generic"; 206 s/\x00\x00/.*/g;
207 s/\x00/[^\/]*/g;
208 s/\\\?/[^\/]/g;
209
210 $_ = s/^\\\/// ? "^$_\$" : "(?:^|/)$_\$";
211
212 s/(?: \[\^\/\] | \. ) \*\$$//x;
213
214 return qr<$_>s
215 }
216}
217
218our %INCSKIP = (
219 "unicore/TestProp.pl" => undef, # 3.5MB of insanity, apparently just some testcase
220);
221
222sub get_dirtree {
223 my $root = shift;
224
225 my @tree;
226 my $skip;
227
228 my $scan; $scan = sub {
229 for (sort do {
230 opendir my $fh, $_[0]
231 or return;
232 readdir $fh
233 }) {
234 next if /^\./;
235
236 my $path = "$_[0]/$_";
237
238 if (-d "$path/.") {
239 $scan->($path);
240 } else {
241 $path = substr $path, $skip;
242 push @tree, $path
243 unless exists $INCSKIP{$path};
244 }
245 }
246 };
247
248 $root =~ s/\/$//;
249 $skip = 1 + length $root;
250 $scan->($root);
251
252 \@tree
253}
254
255my $inctrees;
256
257sub get_inctrees {
258 unless ($inctrees) {
259 my %inctree;
260 $inctree{$_} ||= [$_, get_dirtree $_] # entries in @INC are often duplicates
261 for @INC;
262 $inctrees = [values %inctree];
263 }
264
265 @$inctrees
266}
267
268#############################################################################
198 269
199sub cmd_boot { 270sub cmd_boot {
200 $pm{"//boot"} = $_[0]; 271 $pm{"//boot"} = $_[0];
201} 272}
202 273
209 280
210 $pm{$as} = $file; 281 $pm{$as} = $file;
211 $pmbin{$as} = 1 if $_[1]; 282 $pmbin{$as} = 1 if $_[1];
212} 283}
213 284
285sub cmd_staticlib {
286 push @staticlibs, $_
287 for split /\s+/, $_[0];
288}
289
290sub cmd_include {
291 push @incext, [$_[1], glob2re $_[0]];
292}
293
294sub cmd_incglob {
295 my ($pattern) = @_;
296
297 $pattern = glob2re $pattern;
298
299 for (get_inctrees) {
300 my ($dir, $files) = @$_;
301
302 $pm{$_} = "$dir/$_"
303 for grep /$pattern/ && /\.(pl|pm)$/, @$files;
304 }
305}
306
307sub parse_argv;
308
214sub cmd_file { 309sub cmd_file {
215 open my $fh, "<", $_[0] 310 open my $fh, "<", $_[0]
216 or die "$_[0]: $!\n"; 311 or die "$_[0]: $!\n";
217 312
313 local @ARGV;
314
218 while (<$fh>) { 315 while (<$fh>) {
219 chomp; 316 chomp;
317 next unless /\S/;
318 next if /^\s*#/;
319
320 s/^\s*-*/--/;
220 my ($cmd, $args) = split / /, $_, 2; 321 my ($cmd, $args) = split / /, $_, 2;
221 $cmd =~ s/^-+//;
222 322
223 if ($cmd eq "strip") { 323 push @ARGV, $cmd;
224 $STRIP = $args; 324 push @ARGV, $args if defined $args;
225 } elsif ($cmd eq "eval") { 325 }
226 trace_eval $_; 326
227 } elsif ($cmd eq "use") { 327 parse_argv;
228 trace_module $_ 328}
229 for split / /, $args; 329
230 } elsif ($cmd eq "boot") { 330use Getopt::Long;
231 cmd_boot $args; 331
232 } elsif ($cmd eq "static") { 332sub parse_argv {
233 $STATIC = 1; 333 GetOptions
234 } elsif ($cmd eq "add") { 334 "strip=s" => \$STRIP,
235 cmd_add $args, 0; 335 "cache=s" => \$CACHE, # internal option
236 } elsif ($cmd eq "addbin") { 336 "verbose|v" => sub { ++$VERBOSE },
237 cmd_add $args, 1; 337 "quiet|q" => sub { --$VERBOSE },
238 } elsif (/^\s*#/) { 338 "perl" => \$PERL,
239 # comment 339 "app=s" => \$APP,
240 } elsif (/\S/) { 340 "eval|e=s" => sub { trace_eval $_[1] },
241 die "$_: unsupported directive\n"; 341 "use|M=s" => sub { trace_module $_[1] },
342 "boot=s" => sub { cmd_boot $_[1] },
343 "add=s" => sub { cmd_add $_[1], 0 },
344 "addbin=s" => sub { cmd_add $_[1], 1 },
345 "incglob=s" => sub { cmd_incglob $_[1] },
346 "include|i=s" => sub { cmd_include $_[1], 1 },
347 "exclude|x=s" => sub { cmd_include $_[1], 0 },
348 "static!" => \$STATIC,
349 "usepacklists!" => \$PACKLIST,
350 "staticlib=s" => sub { cmd_staticlib $_[1] },
351 "<>" => sub { cmd_file $_[0] },
352 or exit 1;
353}
354
355Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case");
356
357parse_argv;
358
359die "cannot specify both --app and --perl\n"
360 if $PERL and defined $APP;
361
362# required for @INC loading, unfortunately
363trace_module "PerlIO::scalar";
364
365#############################################################################
366# apply include/exclude
367
368{
369 my %pmi;
370
371 for (@incext) {
372 my ($inc, $glob) = @$_;
373
374 my @match = grep /$glob/, keys %pm;
375
376 if ($inc) {
377 # include
378 @pmi{@match} = delete @pm{@match};
379
380 print "applying include $glob - protected ", (scalar @match), " files.\n"
381 if $VERBOSE >= 5;
382 } else {
383 # exclude
384 delete @pm{@match};
385
386 print "applying exclude $glob - removed ", (scalar @match), " files.\n"
387 if $VERBOSE >= 5;
242 } 388 }
243 } 389 }
244}
245 390
246use Getopt::Long; 391 my @pmi = keys %pmi;
392 @pm{@pmi} = delete @pmi{@pmi};
393}
247 394
248Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case"); 395#############################################################################
396# scan for AutoLoader, static archives and other dependencies
249 397
250GetOptions 398sub scan_al {
251 "strip=s" => \$STRIP, 399 my ($auto, $autodir) = @_;
252 "verbose|v" => sub { ++$VERBOSE }, 400
253 "quiet|q" => sub { --$VERBOSE }, 401 my $ix = "$autodir/autosplit.ix";
254 "perl" => \$PERL, 402
255 "eval|e=s" => sub { trace_eval $_[1] }, 403 print "processing autoload index for '$auto'\n"
256 "use|M=s" => sub { trace_module $_[1] }, 404 if $VERBOSE >= 6;
257 "boot=s" => sub { cmd_boot $_[1] }, 405
258 "add=s" => sub { cmd_add $_[1], 0 }, 406 $pm{"$auto/autosplit.ix"} = $ix;
259 "addbin=s" => sub { cmd_add $_[1], 1 }, 407
260 "static" => sub { $STATIC = 1 }, 408 open my $fh, "<:perlio", $ix
261 "<>" => sub { cmd_file $_[0] }, 409 or die "$ix: $!";
262 or exit 1; 410
411 my $package;
412
413 while (<$fh>) {
414 if (/^\s*sub\s+ ([^[:space:];]+) \s* (?:\([^)]*\))? \s*;?\s*$/x) {
415 my $al = "auto/$package/$1.al";
416 my $inc = find_inc $al;
417
418 defined $inc or die "$al: autoload file not found, but should be there.\n";
419
420 $pm{$al} = $inc;
421 print "found autoload function '$al'\n"
422 if $VERBOSE >= 6;
423
424 } elsif (/^\s*package\s+([^[:space:];]+)\s*;?\s*$/) {
425 ($package = $1) =~ s/::/\//g;
426 } elsif (/^\s*(?:#|1?\s*;?\s*$)/) {
427 # nop
428 } else {
429 warn "WARNING: $ix: unparsable line, please report: $_";
430 }
431 }
432}
433
434for my $pm (keys %pm) {
435 if ($pm =~ /^(.*)\.pm$/) {
436 my $auto = "auto/$1";
437 my $autodir = find_inc $auto;
438
439 if (defined $autodir && -d $autodir) {
440 # AutoLoader
441 scan_al $auto, $autodir
442 if -f "$autodir/autosplit.ix";
443
444 # extralibs.ld
445 if (open my $fh, "<:perlio", "$autodir/extralibs.ld") {
446 print "found extralibs for $pm\n"
447 if $VERBOSE >= 6;
448
449 local $/;
450 $extralibs .= " " . <$fh>;
451 }
452
453 $pm =~ /([^\/]+).pm$/ or die "$pm: unable to match last component";
454
455 my $base = $1;
456
457 # static ext
458 if (-f "$autodir/$base$Config{_a}") {
459 print "found static archive for $pm\n"
460 if $VERBOSE >= 3;
461
462 push @libs, "$autodir/$base$Config{_a}";
463 push @static_ext, $pm;
464 }
465
466 # dynamic object
467 die "ERROR: found shared object - can't link statically ($_)\n"
468 if -f "$autodir/$base.$Config{dlext}";
469
470 if ($PACKLIST && open my $fh, "<:perlio", "$autodir/.packlist") {
471 print "found .packlist for $pm\n"
472 if $VERBOSE >= 3;
473
474 while (<$fh>) {
475 chomp;
476 s/ .*$//; # newer-style .packlists might contain key=value pairs
477
478 # only include certain files (.al, .ix, .pm, .pl)
479 if (/\.(pm|pl|al|ix)$/) {
480 for my $inc (@INC) {
481 # in addition, we only add files that are below some @INC path
482 $inc =~ s/\/*$/\//;
483
484 if ($inc eq substr $_, 0, length $inc) {
485 my $base = substr $_, length $inc;
486 $pm{$base} = $_;
487
488 print "+ added .packlist dependency $base\n"
489 if $VERBOSE >= 3;
490 }
491
492 last;
493 }
494 }
495 }
496 }
497 }
498 }
499}
500
501#############################################################################
502
503print "processing bundle files (try more -v power if you get bored waiting here)...\n"
504 if $VERBOSE >= 1;
263 505
264my $data; 506my $data;
265my @index; 507my @index;
266my @order = sort { 508my @order = sort {
267 length $a <=> length $b 509 length $a <=> length $b
274 516
275for my $pm (@order) { 517for my $pm (@order) {
276 my $path = $pm{$pm}; 518 my $path = $pm{$pm};
277 519
278 128 > length $pm 520 128 > length $pm
279 or die "$pm: path too long (only 128 octets supported)\n"; 521 or die "ERROR: $pm: path too long (only 128 octets supported)\n";
280 522
281 my $src = ref $path 523 my $src = ref $path
282 ? $$path 524 ? $$path
283 : do { 525 : do {
284 open my $pm, "<", $path 526 open my $pm, "<", $path
287 local $/; 529 local $/;
288 530
289 <$pm> 531 <$pm>
290 }; 532 };
291 533
534 my $size = length $src;
535
292 unless ($pmbin{$pm}) { # only do this unless the file is binary 536 unless ($pmbin{$pm}) { # only do this unless the file is binary
293
294 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) { 537 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) {
295 if ($src =~ /^ unimpl \"/m) { 538 if ($src =~ /^ unimpl \"/m) {
296 warn "$pm: skipping (not implemented anyways).\n" 539 print "$pm: skipping (raises runtime error only).\n"
297 if $VERBOSE >= 2; 540 if $VERBOSE >= 3;
298 next; 541 next;
299 } 542 }
300 } 543 }
301 544
545 $src = cache +($STRIP eq "ppi" ? "$UNISTRIP,$OPTIMISE_SIZE" : undef), $src, sub {
546 if ($UNISTRIP && $pm =~ /^unicore\/.*\.pl$/) {
547 print "applying unicore stripping $pm\n"
548 if $VERBOSE >= 6;
549
550 # special stripping for unicore swashes and properties
551 # much more could be done by going binary
552 $src =~ s{
553 (^return\ <<'END';\n) (.*?\n) (END(?:\n|\Z))
554 }{
555 my ($pre, $data, $post) = ($1, $2, $3);
556
557 for ($data) {
558 s/^([0-9a-fA-F]+)\t([0-9a-fA-F]+)\t/sprintf "%X\t%X", hex $1, hex $2/gem
559 if $OPTIMISE_SIZE;
560
561# s{
562# ^([0-9a-fA-F]+)\t([0-9a-fA-F]*)\t
563# }{
564# # ww - smaller filesize, UU - compress better
565# pack "C0UU",
566# hex $1,
567# length $2 ? (hex $2) - (hex $1) : 0
568# }gemx;
569
570 s/#.*\n/\n/mg;
571 s/\s+\n/\n/mg;
572 }
573
574 "$pre$data$post"
575 }smex;
576 }
577
302 if ($STRIP =~ /ppi/i) { 578 if ($STRIP =~ /ppi/i) {
303 require PPI; 579 require PPI;
304 580
305 my $ppi = PPI::Document->new (\$src); 581 if (my $ppi = PPI::Document->new (\$src)) {
306 $ppi->prune ("PPI::Token::Comment"); 582 $ppi->prune ("PPI::Token::Comment");
307 $ppi->prune ("PPI::Token::Pod"); 583 $ppi->prune ("PPI::Token::Pod");
308 584
309 # prune END stuff 585 # prune END stuff
310 for (my $last = $ppi->last_element; $last; ) { 586 for (my $last = $ppi->last_element; $last; ) {
311 my $prev = $last->previous_token; 587 my $prev = $last->previous_token;
312 588
313 if ($last->isa (PPI::Token::Whitespace::)) { 589 if ($last->isa (PPI::Token::Whitespace::)) {
314 $last->delete; 590 $last->delete;
315 } elsif ($last->isa (PPI::Statement::End::)) { 591 } elsif ($last->isa (PPI::Statement::End::)) {
316 $last->delete; 592 $last->delete;
317 last; 593 last;
318 } elsif ($last->isa (PPI::Token::Pod::)) { 594 } elsif ($last->isa (PPI::Token::Pod::)) {
319 $last->delete; 595 $last->delete;
596 } else {
597 last;
598 }
599
600 $last = $prev;
601 }
602
603 # prune some but not all insignificant whitespace
604 for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) {
605 my $prev = $ws->previous_token;
606 my $next = $ws->next_token;
607
608 if (!$prev || !$next) {
609 $ws->delete;
610 } else {
611 if (
612 $next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float
613 or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/
614 or $prev->isa (PPI::Token::Structure::)
615 or ($OPTIMISE_SIZE &&
616 ($prev->isa (PPI::Token::Word::)
617 && (PPI::Token::Symbol:: eq ref $next
618 || $next->isa (PPI::Structure::Block::)
619 || $next->isa (PPI::Structure::List::)
620 || $next->isa (PPI::Structure::Condition::)))
621 )
622 ) {
623 $ws->delete;
624 } elsif ($prev->isa (PPI::Token::Whitespace::)) {
625 $ws->{content} = ' ';
626 $prev->delete;
627 } else {
628 $ws->{content} = ' ';
629 }
630 }
631 }
632
633 # prune whitespace around blocks
634 if ($OPTIMISE_SIZE) {
635 # these usually decrease size, but decrease compressability more
636 for my $struct (PPI::Structure::Block::, PPI::Structure::Condition::) {
637 for my $node (@{ $ppi->find ($struct) }) {
638 my $n1 = $node->first_token;
639 my $n2 = $n1->previous_token;
640 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
641 $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
642 my $n1 = $node->last_token;
643 my $n2 = $n1->next_token;
644 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
645 $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
646 }
647 }
648
649 for my $node (@{ $ppi->find (PPI::Structure::List::) }) {
650 my $n1 = $node->first_token;
651 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
652 my $n1 = $node->last_token;
653 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
654 }
655 }
656
657 # reformat qw() lists which often have lots of whitespace
658 for my $node (@{ $ppi->find (PPI::Token::QuoteLike::Words::) }) {
659 if ($node->{content} =~ /^qw(.)(.*)(.)$/s) {
660 my ($a, $qw, $b) = ($1, $2, $3);
661 $qw =~ s/^\s+//;
662 $qw =~ s/\s+$//;
663 $qw =~ s/\s+/ /g;
664 $node->{content} = "qw$a$qw$b";
665 }
666 }
667
668 $src = $ppi->serialize;
320 } else { 669 } else {
321 last; 670 warn "WARNING: $pm{$pm}: PPI failed to parse this file\n";
322 } 671 }
672 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses its own pod
673 require Pod::Strip;
323 674
324 $last = $prev; 675 my $stripper = Pod::Strip->new;
676
677 my $out;
678 $stripper->output_string (\$out);
679 $stripper->parse_string_document ($src)
680 or die;
681 $src = $out;
325 } 682 }
326 683
327 # prune some but not all insignificant whitespace 684 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
328 for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) { 685 if (open my $fh, "-|") {
329 my $prev = $ws->previous_token;
330 my $next = $ws->next_token;
331
332 if (!$prev || !$next) {
333 $ws->delete; 686 <$fh>;
334 } else { 687 } else {
335 if ( 688 eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
336 $next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float
337 or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/
338 or $prev->isa (PPI::Token::Structure::)
339 # decrease size, decrease compressability
340 #or ($prev->isa (PPI::Token::Word::)
341 # && (PPI::Token::Symbol:: eq ref $next
342 # || $next->isa (PPI::Structure::Block::)
343 # || $next->isa (PPI::Structure::List::)
344 # || $next->isa (PPI::Structure::Condition::)))
345 ) {
346 $ws->delete;
347 } elsif ($prev->isa (PPI::Token::Whitespace::)) {
348 $ws->{content} = ' ';
349 $prev->delete;
350 } else {
351 $ws->{content} = ' ';
352 } 689 exit 0;
353 } 690 }
354 } 691 }
355 692
356 # prune whitespace around blocks
357 if (0) {
358 # these usually decrease size, but decrease compressability more
359 for my $struct (PPI::Structure::Block::, PPI::Structure::Condition::) {
360 for my $node (@{ $ppi->find ($struct) }) {
361 my $n1 = $node->first_token;
362 my $n2 = $n1->previous_token;
363 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
364 $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
365 my $n1 = $node->last_token;
366 my $n2 = $n1->next_token;
367 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
368 $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
369 }
370 }
371
372 for my $node (@{ $ppi->find (PPI::Structure::List::) }) {
373 my $n1 = $node->first_token;
374 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
375 my $n1 = $node->last_token;
376 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
377 }
378 } 693 $src
379
380 # reformat qw() lists which often have lots of whitespace
381 for my $node (@{ $ppi->find (PPI::Token::QuoteLike::Words::) }) {
382 if ($node->{content} =~ /^qw(.)(.*)(.)$/s) {
383 my ($a, $qw, $b) = ($1, $2, $3);
384 $qw =~ s/^\s+//;
385 $qw =~ s/\s+$//;
386 $qw =~ s/\s+/ /g;
387 $node->{content} = "qw$a$qw$b";
388 }
389 }
390
391 $src = $ppi->serialize;
392 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses it's own pod
393 require Pod::Strip;
394
395 my $stripper = Pod::Strip->new;
396
397 my $out;
398 $stripper->output_string (\$out);
399 $stripper->parse_string_document ($src)
400 or die;
401 $src = $out;
402 } 694 };
403
404 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
405 if (open my $fh, "-|") {
406 <$fh>;
407 } else {
408 eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
409 exit 0;
410 }
411 }
412 695
413# if ($pm eq "Opcode.pm") { 696# if ($pm eq "Opcode.pm") {
414# open my $fh, ">x" or die; print $fh $src;#d# 697# open my $fh, ">x" or die; print $fh $src;#d#
415# exit 1; 698# exit 1;
416# } 699# }
417 } 700 }
418 701
419 warn "adding $pm\n" 702 print "adding $pm (original size $size, stored size ", length $src, ")\n"
420 if $VERBOSE >= 2; 703 if $VERBOSE >= 2;
421 704
422 push @index, ((length $pm) << 25) | length $data; 705 push @index, ((length $pm) << 25) | length $data;
423 $data .= $pm . $src; 706 $data .= $pm . $src;
424} 707}
425 708
426length $data < 2**25 709length $data < 2**25
427 or die "bundle too large (only 32MB supported)\n"; 710 or die "ERROR: bundle too large (only 32MB supported)\n";
428 711
429my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16; 712my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16;
430 713
431############################################################################# 714#############################################################################
432# output 715# output
433 716
434print "generating $PREFIX.h... "; 717print "generating $PREFIX.h... "
718 if $VERBOSE >= 1;
435 719
436{ 720{
437 open my $fh, ">", "$PREFIX.h" 721 open my $fh, ">", "$PREFIX.h"
438 or die "$PREFIX.h: $!\n"; 722 or die "$PREFIX.h: $!\n";
439 723
451EXTERN_C void staticperl_cleanup (void); 735EXTERN_C void staticperl_cleanup (void);
452 736
453EOF 737EOF
454} 738}
455 739
456print "\n"; 740print "\n"
741 if $VERBOSE >= 1;
457 742
458############################################################################# 743#############################################################################
459# output 744# output
460 745
461print "generating $PREFIX.c... "; 746print "generating $PREFIX.c... "
747 if $VERBOSE >= 1;
462 748
463open my $fh, ">", "$PREFIX.c" 749open my $fh, ">", "$PREFIX.c"
464 or die "$PREFIX.c: $!\n"; 750 or die "$PREFIX.c: $!\n";
465 751
466print $fh <<EOF; 752print $fh <<EOF;
496printf $fh "0x%08x\n};\n", (length $data); 782printf $fh "0x%08x\n};\n", (length $data);
497 783
498print $fh "static const char $varpfx\_data [] =\n"; 784print $fh "static const char $varpfx\_data [] =\n";
499dump_string $fh, $data; 785dump_string $fh, $data;
500 786
501print $fh ";\n\n";; 787print $fh ";\n\n";
502 788
503############################################################################# 789#############################################################################
504# bootstrap 790# bootstrap
505 791
506# boot file for staticperl 792# boot file for staticperl
611 } 897 }
612 898
613 XSRETURN ($varpfx\_count); 899 XSRETURN ($varpfx\_count);
614} 900}
615 901
616static char *args[] = {
617 "staticperl",
618 "-e",
619 "0"
620};
621
622EOF 902EOF
623 903
624############################################################################# 904#############################################################################
625# xs_init 905# xs_init
626 906
665EOF 945EOF
666 946
667############################################################################# 947#############################################################################
668# optional perl_init/perl_destroy 948# optional perl_init/perl_destroy
669 949
950if ($APP) {
951 print $fh <<EOF;
952
953int
954main (int argc, char *argv [])
955{
956 extern char **environ;
957 int i, exitstatus;
958 char **args = malloc ((argc + 3) * sizeof (const char *));
959
960 args [0] = argv [0];
961 args [1] = "-e";
962 args [2] = "0";
963 args [3] = "--";
964
965 for (i = 1; i < argc; ++i)
966 args [i + 3] = argv [i];
967
968 PERL_SYS_INIT3 (&argc, &argv, &environ);
969 staticperl = perl_alloc ();
970 perl_construct (staticperl);
971
972 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
973
974 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc + 3, args, environ);
975 free (args);
976 if (!exitstatus)
977 perl_run (staticperl);
978
979 exitstatus = perl_destruct (staticperl);
980 perl_free (staticperl);
981 PERL_SYS_TERM ();
982
983 return exitstatus;
984}
985EOF
670if ($PERL) { 986} elsif ($PERL) {
671 print $fh <<EOF; 987 print $fh <<EOF;
672 988
673int 989int
674main (int argc, char *argv []) 990main (int argc, char *argv [])
675{ 991{
697 print $fh <<EOF; 1013 print $fh <<EOF;
698 1014
699EXTERN_C void 1015EXTERN_C void
700staticperl_init (void) 1016staticperl_init (void)
701{ 1017{
1018 static char *args[] = {
1019 "staticperl",
1020 "-e",
1021 "0"
1022 };
1023
702 extern char **environ; 1024 extern char **environ;
703 int argc = sizeof (args) / sizeof (args [0]); 1025 int argc = sizeof (args) / sizeof (args [0]);
704 char **argv = args; 1026 char **argv = args;
705 1027
706 PERL_SYS_INIT3 (&argc, &argv, &environ); 1028 PERL_SYS_INIT3 (&argc, &argv, &environ);
722 PERL_SYS_TERM (); 1044 PERL_SYS_TERM ();
723} 1045}
724EOF 1046EOF
725} 1047}
726 1048
727print -s "$PREFIX.c", " octets (", (length $data) , " data octets).\n\n"; 1049print -s "$PREFIX.c", " octets (", (length $data) , " data octets).\n\n"
1050 if $VERBOSE >= 1;
728 1051
729############################################################################# 1052#############################################################################
730# libs, cflags 1053# libs, cflags
731 1054
732{ 1055{
733 print "generating $PREFIX.ccopts... "; 1056 print "generating $PREFIX.ccopts... "
1057 if $VERBOSE >= 1;
734 1058
735 my $str = "$Config{ccflags} $Config{optimize} $Config{cppflags} -I$Config{archlibexp}/CORE"; 1059 my $str = "$Config{ccflags} $Config{optimize} $Config{cppflags} -I$Config{archlibexp}/CORE";
736 $str =~ s/([\(\)])/\\$1/g; 1060 $str =~ s/([\(\)])/\\$1/g;
737
738 print "$str\n\n";
739 1061
740 open my $fh, ">$PREFIX.ccopts" 1062 open my $fh, ">$PREFIX.ccopts"
741 or die "$PREFIX.ccopts: $!"; 1063 or die "$PREFIX.ccopts: $!";
742 print $fh $str; 1064 print $fh $str;
1065
1066 print "$str\n\n"
1067 if $VERBOSE >= 1;
743} 1068}
744 1069
745{ 1070{
746 print "generating $PREFIX.ldopts... "; 1071 print "generating $PREFIX.ldopts... ";
747 1072
748 my $str = $STATIC ? "--static " : ""; 1073 my $str = $STATIC ? "-static " : "";
749 1074
750 $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}"; 1075 $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}";
751 1076
752 my %seen; 1077 my %seen;
753 $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g); 1078 $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g);
754 1079
1080 for (@staticlibs) {
1081 $str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx;
1082 }
1083
755 $str =~ s/([\(\)])/\\$1/g; 1084 $str =~ s/([\(\)])/\\$1/g;
756
757 print "$str\n\n";
758 1085
759 open my $fh, ">$PREFIX.ldopts" 1086 open my $fh, ">$PREFIX.ldopts"
760 or die "$PREFIX.ldopts: $!"; 1087 or die "$PREFIX.ldopts: $!";
761 print $fh $str; 1088 print $fh $str;
762}
763 1089
764if ($PERL) { 1090 print "$str\n\n"
1091 if $VERBOSE >= 1;
1092}
1093
1094if ($PERL or defined $APP) {
1095 $APP = "perl" unless defined $APP;
1096
1097 print "building $APP...\n"
1098 if $VERBOSE >= 1;
1099
765 system "$Config{cc} \$(cat bundle.ccopts\) -o perl bundle.c \$(cat bundle.ldopts\)"; 1100 system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)";
766 1101
767 unlink "$PREFIX.$_" 1102 unlink "$PREFIX.$_"
768 for qw(ccopts ldopts c h); 1103 for qw(ccopts ldopts c h);
769}
770 1104
1105 print "\n"
1106 if $VERBOSE >= 1;
1107}
1108

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines