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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines