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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines