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

Comparing App-Staticperl/mkbundle (file contents):
Revision 1.10 by root, Thu Dec 9 09:51:32 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;
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;
87 my $pkg = ++$pkg;
88 my $eval = $mod = $mod =~ /[^A-Za-z0-9_:]/
66 eval "require $mod"; 89 ? "require $mod"
90 : "{ package $pkg; use $mod; }";
91 eval $eval;
67 warn "ERROR: $@ (while loading '$mod')\n" 92 warn "ERROR: $@ (while loading '$mod')\n"
68 if $@; 93 if $@;
69 syswrite $W_TRACER, "\n";
70 } elsif (/eval (.*)$/) { 94 } elsif (/eval (.*)$/) {
71 my $eval = $1; 95 my $eval = $1;
72 eval $eval; 96 eval $eval;
73 warn "ERROR: $@ (in '$eval')\n" 97 warn "ERROR: $@ (in '$eval')\n"
74 if $@; 98 if $@;
75 } 99 }
100
101 syswrite $W_TRACER, "\n";
76 } 102 }
77 103
78 exit 0; 104 exit 0;
79 } 105 }
80} 106}
81 107
82# module loading is now safe 108# module loading is now safe
83use Config;
84 109
85sub scan_al { 110sub 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 (;;) { 111 for (;;) {
118 <$TRACER_R> =~ /^-$/ or last; 112 <$TRACER_R> =~ /^-$/ or last;
119 my $dir = <$TRACER_R>; chomp $dir; 113 my $dir = <$TRACER_R>; chomp $dir;
120 my $name = <$TRACER_R>; chomp $name; 114 my $name = <$TRACER_R>; chomp $name;
121 115
122 $pm{$name} = "$dir/$name"; 116 $pm{$name} = "$dir/$name";
123 117
124 if ($name =~ /^(.*)\.pm$/) { 118 print "+ found potential dependency $name\n"
125 my $auto = "auto/$1"; 119 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 } 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;
159} 129}
160 130
161sub trace_eval { 131sub trace_eval {
132 print "tracing eval $_[0]\n"
133 if $VERBOSE >= 2;
134
162 syswrite $TRACER_W, "eval $_[0]\n"; 135 syswrite $TRACER_W, "eval $_[0]\n";
136 trace_parse;
163} 137}
164 138
165sub trace_finish { 139sub trace_finish {
166 close $TRACER_W; 140 close $TRACER_W;
167 close $TRACER_R; 141 close $TRACER_R;
169 143
170############################################################################# 144#############################################################################
171# now we can use modules 145# now we can use modules
172 146
173use common::sense; 147use common::sense;
148use Config;
174use 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}
175 182
176sub dump_string { 183sub dump_string {
177 my ($fh, $data) = @_; 184 my ($fh, $data) = @_;
178 185
179 if (length $data) { 186 if (length $data) {
189 } else { 196 } else {
190 print $fh " \"\"\n"; 197 print $fh " \"\"\n";
191 } 198 }
192} 199}
193 200
194# required for @INC loading, unfortunately 201#############################################################################
195trace_module "PerlIO::scalar";
196 202
197#trace_module "Term::ReadLine::readline"; # Term::ReadLine::Perl dependency 203sub glob2re {
198# URI is difficult 204 for (quotemeta $_[0]) {
199#trace_module "URI::http"; 205 s/\\\*/\x00/g;
200#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#############################################################################
201 269
202sub cmd_boot { 270sub cmd_boot {
203 $pm{"//boot"} = $_[0]; 271 $pm{"//boot"} = $_[0];
204} 272}
205 273
217sub cmd_staticlib { 285sub cmd_staticlib {
218 push @staticlibs, $_ 286 push @staticlibs, $_
219 for split /\s+/, $_[0]; 287 for split /\s+/, $_[0];
220} 288}
221 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
222sub cmd_file { 309sub cmd_file {
223 open my $fh, "<", $_[0] 310 open my $fh, "<", $_[0]
224 or die "$_[0]: $!\n"; 311 or die "$_[0]: $!\n";
225 312
313 local @ARGV;
314
226 while (<$fh>) { 315 while (<$fh>) {
227 chomp; 316 chomp;
317 next unless /\S/;
318 next if /^\s*#/;
319
320 s/^\s*-*/--/;
228 my ($cmd, $args) = split / /, $_, 2; 321 my ($cmd, $args) = split / /, $_, 2;
229 $cmd =~ s/^-+//;
230 322
231 if ($cmd eq "strip") { 323 push @ARGV, $cmd;
232 $STRIP = $args; 324 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 } 325 }
326
327 parse_argv;
258} 328}
259 329
260use Getopt::Long; 330use Getopt::Long;
261 331
332sub parse_argv {
333 GetOptions
334 "strip=s" => \$STRIP,
335 "cache=s" => \$CACHE, # internal option
336 "verbose|v" => sub { ++$VERBOSE },
337 "quiet|q" => sub { --$VERBOSE },
338 "perl" => \$PERL,
339 "app=s" => \$APP,
340 "eval|e=s" => sub { trace_eval $_[1] },
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
262Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case"); 355Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case");
263 356
264GetOptions 357parse_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 358
280die "cannot specify both --app and --perl\n" 359die "cannot specify both --app and --perl\n"
281 if $PERL and defined $APP; 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;
388 }
389 }
390
391 my @pmi = keys %pmi;
392 @pm{@pmi} = delete @pmi{@pmi};
393}
394
395#############################################################################
396# scan for AutoLoader, static archives and other dependencies
397
398sub scan_al {
399 my ($auto, $autodir) = @_;
400
401 my $ix = "$autodir/autosplit.ix";
402
403 print "processing autoload index for '$auto'\n"
404 if $VERBOSE >= 6;
405
406 $pm{"$auto/autosplit.ix"} = $ix;
407
408 open my $fh, "<:perlio", $ix
409 or die "$ix: $!";
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;
282 505
283my $data; 506my $data;
284my @index; 507my @index;
285my @order = sort { 508my @order = sort {
286 length $a <=> length $b 509 length $a <=> length $b
293 516
294for my $pm (@order) { 517for my $pm (@order) {
295 my $path = $pm{$pm}; 518 my $path = $pm{$pm};
296 519
297 128 > length $pm 520 128 > length $pm
298 or die "$pm: path too long (only 128 octets supported)\n"; 521 or die "ERROR: $pm: path too long (only 128 octets supported)\n";
299 522
300 my $src = ref $path 523 my $src = ref $path
301 ? $$path 524 ? $$path
302 : do { 525 : do {
303 open my $pm, "<", $path 526 open my $pm, "<", $path
306 local $/; 529 local $/;
307 530
308 <$pm> 531 <$pm>
309 }; 532 };
310 533
534 my $size = length $src;
535
311 unless ($pmbin{$pm}) { # only do this unless the file is binary 536 unless ($pmbin{$pm}) { # only do this unless the file is binary
312
313 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) { 537 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) {
314 if ($src =~ /^ unimpl \"/m) { 538 if ($src =~ /^ unimpl \"/m) {
315 warn "$pm: skipping (not implemented anyways).\n" 539 print "$pm: skipping (raises runtime error only).\n"
316 if $VERBOSE >= 2; 540 if $VERBOSE >= 3;
317 next; 541 next;
318 } 542 }
319 } 543 }
320 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
321 if ($STRIP =~ /ppi/i) { 578 if ($STRIP =~ /ppi/i) {
322 require PPI; 579 require PPI;
323 580
324 my $ppi = PPI::Document->new (\$src); 581 if (my $ppi = PPI::Document->new (\$src)) {
325 $ppi->prune ("PPI::Token::Comment"); 582 $ppi->prune ("PPI::Token::Comment");
326 $ppi->prune ("PPI::Token::Pod"); 583 $ppi->prune ("PPI::Token::Pod");
327 584
328 # prune END stuff 585 # prune END stuff
329 for (my $last = $ppi->last_element; $last; ) { 586 for (my $last = $ppi->last_element; $last; ) {
330 my $prev = $last->previous_token; 587 my $prev = $last->previous_token;
331 588
332 if ($last->isa (PPI::Token::Whitespace::)) { 589 if ($last->isa (PPI::Token::Whitespace::)) {
333 $last->delete; 590 $last->delete;
334 } elsif ($last->isa (PPI::Statement::End::)) { 591 } elsif ($last->isa (PPI::Statement::End::)) {
335 $last->delete; 592 $last->delete;
336 last; 593 last;
337 } elsif ($last->isa (PPI::Token::Pod::)) { 594 } elsif ($last->isa (PPI::Token::Pod::)) {
338 $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;
339 } else { 669 } else {
340 last; 670 warn "WARNING: $pm{$pm}: PPI failed to parse this file\n";
341 } 671 }
672 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses its own pod
673 require Pod::Strip;
342 674
343 $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;
344 } 682 }
345 683
346 # prune some but not all insignificant whitespace 684 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
347 for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) { 685 if (open my $fh, "-|") {
348 my $prev = $ws->previous_token;
349 my $next = $ws->next_token;
350
351 if (!$prev || !$next) {
352 $ws->delete; 686 <$fh>;
353 } else { 687 } else {
354 if ( 688 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 } 689 exit 0;
372 } 690 }
373 } 691 }
374 692
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 } 693 $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 } 694 };
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 695
432# if ($pm eq "Opcode.pm") { 696# if ($pm eq "Opcode.pm") {
433# open my $fh, ">x" or die; print $fh $src;#d# 697# open my $fh, ">x" or die; print $fh $src;#d#
434# exit 1; 698# exit 1;
435# } 699# }
436 } 700 }
437 701
438 warn "adding $pm\n" 702 print "adding $pm (original size $size, stored size ", length $src, ")\n"
439 if $VERBOSE >= 2; 703 if $VERBOSE >= 2;
440 704
441 push @index, ((length $pm) << 25) | length $data; 705 push @index, ((length $pm) << 25) | length $data;
442 $data .= $pm . $src; 706 $data .= $pm . $src;
443} 707}
444 708
445length $data < 2**25 709length $data < 2**25
446 or die "bundle too large (only 32MB supported)\n"; 710 or die "ERROR: bundle too large (only 32MB supported)\n";
447 711
448my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16; 712my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16;
449 713
450############################################################################# 714#############################################################################
451# output 715# output
452 716
453print "generating $PREFIX.h... "; 717print "generating $PREFIX.h... "
718 if $VERBOSE >= 1;
454 719
455{ 720{
456 open my $fh, ">", "$PREFIX.h" 721 open my $fh, ">", "$PREFIX.h"
457 or die "$PREFIX.h: $!\n"; 722 or die "$PREFIX.h: $!\n";
458 723
470EXTERN_C void staticperl_cleanup (void); 735EXTERN_C void staticperl_cleanup (void);
471 736
472EOF 737EOF
473} 738}
474 739
475print "\n"; 740print "\n"
741 if $VERBOSE >= 1;
476 742
477############################################################################# 743#############################################################################
478# output 744# output
479 745
480print "generating $PREFIX.c... "; 746print "generating $PREFIX.c... "
747 if $VERBOSE >= 1;
481 748
482open my $fh, ">", "$PREFIX.c" 749open my $fh, ">", "$PREFIX.c"
483 or die "$PREFIX.c: $!\n"; 750 or die "$PREFIX.c: $!\n";
484 751
485print $fh <<EOF; 752print $fh <<EOF;
515printf $fh "0x%08x\n};\n", (length $data); 782printf $fh "0x%08x\n};\n", (length $data);
516 783
517print $fh "static const char $varpfx\_data [] =\n"; 784print $fh "static const char $varpfx\_data [] =\n";
518dump_string $fh, $data; 785dump_string $fh, $data;
519 786
520print $fh ";\n\n";; 787print $fh ";\n\n";
521 788
522############################################################################# 789#############################################################################
523# bootstrap 790# bootstrap
524 791
525# boot file for staticperl 792# boot file for staticperl
685 952
686int 953int
687main (int argc, char *argv []) 954main (int argc, char *argv [])
688{ 955{
689 extern char **environ; 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
986} elsif ($PERL) {
987 print $fh <<EOF;
988
989int
990main (int argc, char *argv [])
991{
992 extern char **environ;
690 int exitstatus; 993 int exitstatus;
691 994
995 PERL_SYS_INIT3 (&argc, &argv, &environ);
996 staticperl = perl_alloc ();
997 perl_construct (staticperl);
998
999 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1000
1001 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
1002 if (!exitstatus)
1003 perl_run (staticperl);
1004
1005 exitstatus = perl_destruct (staticperl);
1006 perl_free (staticperl);
1007 PERL_SYS_TERM ();
1008
1009 return exitstatus;
1010}
1011EOF
1012} else {
1013 print $fh <<EOF;
1014
1015EXTERN_C void
1016staticperl_init (void)
1017{
692 static char *args[] = { 1018 static char *args[] = {
693 "staticperl", 1019 "staticperl",
694 "-e", 1020 "-e",
695 "0" 1021 "0"
696 }; 1022 };
697 1023
698 PERL_SYS_INIT3 (&argc, &argv, &environ);
699 staticperl = perl_alloc ();
700 perl_construct (staticperl);
701
702 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
703
704 exitstatus = perl_parse (staticperl, staticperl_xs_init, sizeof (args) / sizeof (*args), args, environ);
705 if (!exitstatus)
706 perl_run (staticperl);
707
708 exitstatus = perl_destruct (staticperl);
709 perl_free (staticperl);
710 PERL_SYS_TERM ();
711
712 return exitstatus;
713}
714EOF
715} elsif ($PERL) {
716 print $fh <<EOF;
717
718int
719main (int argc, char *argv [])
720{
721 extern char **environ;
722 int exitstatus;
723
724 PERL_SYS_INIT3 (&argc, &argv, &environ);
725 staticperl = perl_alloc ();
726 perl_construct (staticperl);
727
728 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
729
730 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
731 if (!exitstatus)
732 perl_run (staticperl);
733
734 exitstatus = perl_destruct (staticperl);
735 perl_free (staticperl);
736 PERL_SYS_TERM ();
737
738 return exitstatus;
739}
740EOF
741} else {
742 print $fh <<EOF;
743
744EXTERN_C void
745staticperl_init (void)
746{
747 extern char **environ; 1024 extern char **environ;
748 int argc = sizeof (args) / sizeof (args [0]); 1025 int argc = sizeof (args) / sizeof (args [0]);
749 char **argv = args; 1026 char **argv = args;
750
751 static char *args[] = {
752 "staticperl",
753 "-e",
754 "0"
755 };
756 1027
757 PERL_SYS_INIT3 (&argc, &argv, &environ); 1028 PERL_SYS_INIT3 (&argc, &argv, &environ);
758 staticperl = perl_alloc (); 1029 staticperl = perl_alloc ();
759 perl_construct (staticperl); 1030 perl_construct (staticperl);
760 PL_origalen = 1; 1031 PL_origalen = 1;
773 PERL_SYS_TERM (); 1044 PERL_SYS_TERM ();
774} 1045}
775EOF 1046EOF
776} 1047}
777 1048
778print -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;
779 1051
780############################################################################# 1052#############################################################################
781# libs, cflags 1053# libs, cflags
782 1054
783{ 1055{
784 print "generating $PREFIX.ccopts... "; 1056 print "generating $PREFIX.ccopts... "
1057 if $VERBOSE >= 1;
785 1058
786 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";
787 $str =~ s/([\(\)])/\\$1/g; 1060 $str =~ s/([\(\)])/\\$1/g;
788
789 print "$str\n\n";
790 1061
791 open my $fh, ">$PREFIX.ccopts" 1062 open my $fh, ">$PREFIX.ccopts"
792 or die "$PREFIX.ccopts: $!"; 1063 or die "$PREFIX.ccopts: $!";
793 print $fh $str; 1064 print $fh $str;
1065
1066 print "$str\n\n"
1067 if $VERBOSE >= 1;
794} 1068}
795 1069
796{ 1070{
797 print "generating $PREFIX.ldopts... "; 1071 print "generating $PREFIX.ldopts... ";
798 1072
806 for (@staticlibs) { 1080 for (@staticlibs) {
807 $str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx; 1081 $str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx;
808 } 1082 }
809 1083
810 $str =~ s/([\(\)])/\\$1/g; 1084 $str =~ s/([\(\)])/\\$1/g;
811
812 print "$str\n\n";
813 1085
814 open my $fh, ">$PREFIX.ldopts" 1086 open my $fh, ">$PREFIX.ldopts"
815 or die "$PREFIX.ldopts: $!"; 1087 or die "$PREFIX.ldopts: $!";
816 print $fh $str; 1088 print $fh $str;
1089
1090 print "$str\n\n"
1091 if $VERBOSE >= 1;
817} 1092}
818 1093
819if ($PERL or defined $APP) { 1094if ($PERL or defined $APP) {
820 $APP = "perl" unless defined $APP; 1095 $APP = "perl" unless defined $APP;
821 1096
822 print "generating $APP...\n"; 1097 print "building $APP...\n"
1098 if $VERBOSE >= 1;
823 1099
824 system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)"; 1100 system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)";
825 1101
826 unlink "$PREFIX.$_" 1102 unlink "$PREFIX.$_"
827 for qw(ccopts ldopts c h); 1103 for qw(ccopts ldopts c h);
828}
829 1104
1105 print "\n"
1106 if $VERBOSE >= 1;
1107}
1108

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines