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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines