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.4 by root, Mon Dec 6 21:10:41 2010 UTC vs.
Revision 1.12 by root, Fri Dec 10 20:29: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;
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;
24my %pmbin;
16my @libs; 25my @libs;
17my @static_ext; 26my @static_ext;
18my $extralibs; 27my $extralibs;
28my @staticlibs;
29my @incext;
19 30
20@ARGV 31@ARGV
21 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";
22 33
34# remove "." from @INC - staticperl.sh does it for us, but be on the safe side
35BEGIN { @INC = grep !/^\.$/, @INC }
36
23$|=1; 37$|=1;
24 38
25our ($TRACER_W, $TRACER_R); 39our ($TRACER_W, $TRACER_R);
26 40
27sub find_inc($) { 41sub find_incdir($) {
28 for (@INC) { 42 for (@INC) {
29 next if ref; 43 next if ref;
30 return $_ if -e "$_/$_[0]"; 44 return $_ if -e "$_/$_[0]";
31 } 45 }
32 46
33 undef 47 undef
34} 48}
35 49
50sub find_inc($) {
51 my $dir = find_incdir $_[0];
52
53 return "$dir/$_[0]"
54 if defined $dir;
55
56 undef
57}
58
36BEGIN { 59BEGIN {
37 # 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
38 my ($W_TRACER, $R_TRACER); # used by tracer 61 my ($W_TRACER, $R_TRACER); # used by tracer
39 62
40 pipe $R_TRACER, $TRACER_W or die "pipe: $!"; 63 pipe $R_TRACER, $TRACER_W or die "pipe: $!";
43 unless (fork) { 66 unless (fork) {
44 close $TRACER_R; 67 close $TRACER_R;
45 close $TRACER_W; 68 close $TRACER_W;
46 69
47 unshift @INC, sub { 70 unshift @INC, sub {
48 my $dir = find_inc $_[1] 71 my $dir = find_incdir $_[1]
49 or return; 72 or return;
50 73
51 syswrite $W_TRACER, "-\n$dir\n$_[1]\n"; 74 syswrite $W_TRACER, "-\n$dir\n$_[1]\n";
52 75
53 open my $fh, "<:perlio", "$dir/$_[1]" 76 open my $fh, "<:perlio", "$dir/$_[1]"
60 if (/use (.*)$/) { 83 if (/use (.*)$/) {
61 my $mod = $1; 84 my $mod = $1;
62 eval "require $mod"; 85 eval "require $mod";
63 warn "ERROR: $@ (while loading '$mod')\n" 86 warn "ERROR: $@ (while loading '$mod')\n"
64 if $@; 87 if $@;
65 syswrite $W_TRACER, "\n";
66 } elsif (/eval (.*)$/) { 88 } elsif (/eval (.*)$/) {
67 my $eval = $1; 89 my $eval = $1;
68 eval $eval; 90 eval $eval;
69 warn "ERROR: $@ (in '$eval')\n" 91 warn "ERROR: $@ (in '$eval')\n"
70 if $@; 92 if $@;
71 } 93 }
94
95 syswrite $W_TRACER, "\n";
72 } 96 }
73 97
74 exit 0; 98 exit 0;
75 } 99 }
76} 100}
77 101
78# module loading is now safe 102# module loading is now safe
79use Config;
80 103
81sub trace_module { 104sub trace_parse {
82 syswrite $TRACER_W, "use $_[0]\n";
83
84 for (;;) { 105 for (;;) {
85 <$TRACER_R> =~ /^-$/ or last; 106 <$TRACER_R> =~ /^-$/ or last;
86 my $dir = <$TRACER_R>; chomp $dir; 107 my $dir = <$TRACER_R>; chomp $dir;
87 my $name = <$TRACER_R>; chomp $name; 108 my $name = <$TRACER_R>; chomp $name;
88 109
89 $pm{$name} = "$dir/$name"; 110 $pm{$name} = "$dir/$name";
90 111
91 if ($name =~ /^(.*)\.pm$/) { 112 print "+ found potential dependency $name\n"
92 my $auto = "auto/$1"; 113 if $VERBOSE >= 3;
93 my $autodir = "$dir/$auto";
94
95 if (-d $autodir) {
96 opendir my $dir, $autodir
97 or die "$autodir: $!\n";
98
99 for (readdir $dir) {
100 # AutoLoader
101 $pm{"$auto/$_"} = "$autodir/$_"
102 if /\.(?:al|ix)$/;
103
104 # static ext
105 if (/\Q$Config{_a}\E$/o) {
106 push @libs, "$autodir/$_";
107 push @static_ext, $name;
108 }
109
110 # extralibs.ld
111 if ($_ eq "extralibs.ld") {
112 open my $fh, "<:perlio", "$autodir/$_"
113 or die "$autodir/$_";
114
115 local $/;
116 $extralibs .= " " . <$fh>;
117 }
118
119 # dynamic object
120 warn "WARNING: found shared object - can't link statically ($_)\n"
121 if /\.\Q$Config{dlext}\E$/o;
122
123 #TODO: extralibs?
124 }
125 }
126 }
127 } 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;
128} 123}
129 124
130sub trace_eval { 125sub trace_eval {
126 print "tracing eval $_[0]\n"
127 if $VERBOSE >= 2;
128
131 syswrite $TRACER_W, "eval $_[0]\n"; 129 syswrite $TRACER_W, "eval $_[0]\n";
130 trace_parse;
132} 131}
133 132
134sub trace_finish { 133sub trace_finish {
135 close $TRACER_W; 134 close $TRACER_W;
136 close $TRACER_R; 135 close $TRACER_R;
138 137
139############################################################################# 138#############################################################################
140# now we can use modules 139# now we can use modules
141 140
142use common::sense; 141use common::sense;
142use Config;
143use 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}
144 176
145sub dump_string { 177sub dump_string {
146 my ($fh, $data) = @_; 178 my ($fh, $data) = @_;
147 179
148 if (length $data) { 180 if (length $data) {
158 } else { 190 } else {
159 print $fh " \"\"\n"; 191 print $fh " \"\"\n";
160 } 192 }
161} 193}
162 194
163# required for @INC loading, unfortunately 195#############################################################################
164trace_module "PerlIO::scalar";
165 196
166#trace_module "Term::ReadLine::readline"; # Term::ReadLine::Perl dependency 197sub glob2re {
167# URI is difficult 198 for (quotemeta $_[0]) {
168#trace_module "URI::http"; 199 s/\\\*/\x00/g;
169#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#############################################################################
170 265
171sub cmd_boot { 266sub cmd_boot {
172 $pm{"//boot"} = $_[0]; 267 $pm{"//boot"} = $_[0];
173} 268}
174 269
178 273
179 my $file = $1; 274 my $file = $1;
180 my $as = defined $2 ? $2 : "/$1"; 275 my $as = defined $2 ? $2 : "/$1";
181 276
182 $pm{$as} = $file; 277 $pm{$as} = $file;
278 $pmbin{$as} = 1 if $_[1];
183} 279}
280
281sub cmd_staticlib {
282 push @staticlibs, $_
283 for split /\s+/, $_[0];
284}
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;
184 304
185sub cmd_file { 305sub cmd_file {
186 open my $fh, "<", $_[0] 306 open my $fh, "<", $_[0]
187 or die "$_[0]: $!\n"; 307 or die "$_[0]: $!\n";
188 308
309 local @ARGV;
310
189 while (<$fh>) { 311 while (<$fh>) {
190 chomp; 312 chomp;
313 next unless /\S/;
314 next if /^\s*#/;
315
316 s/^\s*-*/--/;
191 my ($cmd, $args) = split / /, $_, 2; 317 my ($cmd, $args) = split / /, $_, 2;
192 $cmd =~ s/^-+//;
193 318
194 if ($cmd eq "strip") { 319 push @ARGV, $cmd;
195 $STRIP = $args; 320 push @ARGV, $args if defined $args;
196 } elsif ($cmd eq "eval") { 321 }
197 trace_eval $_; 322
198 } elsif ($cmd eq "use") { 323 parse_argv;
199 trace_module $_ 324}
200 for split / /, $args; 325
201 } elsif ($cmd eq "boot") { 326use Getopt::Long;
202 cmd_boot $args; 327
203 } elsif ($cmd eq "static") { 328sub parse_argv {
204 $STATIC = 1; 329 GetOptions
205 } elsif ($cmd eq "add") { 330 "strip=s" => \$STRIP,
206 cmd_add $args; 331 "cache=s" => \$CACHE, # internal option
207 } elsif (/^\s*#/) { 332 "verbose|v" => sub { ++$VERBOSE },
208 # comment 333 "quiet|q" => sub { --$VERBOSE },
209 } elsif (/\S/) { 334 "perl" => \$PERL,
210 die "$_: unsupported directive\n"; 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
351Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case");
352
353parse_argv;
354
355die "cannot specify both --app and --perl\n"
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;
211 } 384 }
212 } 385 }
213}
214 386
215use Getopt::Long; 387 my @pmi = keys %pmi;
388 @pm{@pmi} = delete @pmi{@pmi};
389}
216 390
217Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case"); 391#############################################################################
392# scan for AutoLoader and static archives
218 393
219GetOptions 394sub scan_al {
220 "strip=s" => \$STRIP, 395 my ($auto, $autodir) = @_;
221 "verbose|v" => sub { ++$VERBOSE }, 396
222 "quiet|q" => sub { --$VERBOSE }, 397 my $ix = "$autodir/autosplit.ix";
223 "perl" => \$PERL, 398
224 "eval|e=s" => sub { trace_eval $_[1] }, 399 print "processing autoload index for '$auto'\n"
225 "use|M=s" => sub { trace_module $_[1] }, 400 if $VERBOSE >= 6;
226 "boot=s" => sub { cmd_boot $_[1] }, 401
227 "add=s" => sub { cmd_add $_[1] }, 402 $pm{"$auto/autosplit.ix"} = $ix;
228 "static" => sub { $STATIC = 1 }, 403
229 "<>" => sub { cmd_file $_[0] }, 404 open my $fh, "<:perlio", $ix
230 or exit 1; 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;
231 500
232my $data; 501my $data;
233my @index; 502my @index;
234my @order = sort { 503my @order = sort {
235 length $a <=> length $b 504 length $a <=> length $b
242 511
243for my $pm (@order) { 512for my $pm (@order) {
244 my $path = $pm{$pm}; 513 my $path = $pm{$pm};
245 514
246 128 > length $pm 515 128 > length $pm
247 or die "$pm: path too long (only 128 octets supported)\n"; 516 or die "ERROR: $pm: path too long (only 128 octets supported)\n";
248 517
249 my $src = ref $path 518 my $src = ref $path
250 ? $$path 519 ? $$path
251 : do { 520 : do {
252 open my $pm, "<:perlio", $path 521 open my $pm, "<", $path
253 or die "$path: $!"; 522 or die "$path: $!";
254 523
255 local $/; 524 local $/;
256 525
257 <$pm> 526 <$pm>
258 }; 527 };
259 528
529 my $size = length $src;
530
531 unless ($pmbin{$pm}) { # only do this unless the file is binary
260 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) { 532 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) {
261 if ($src =~ /^ unimpl \"/m) { 533 if ($src =~ /^ unimpl \"/m) {
262 warn "$pm: skipping (not implemented anyways).\n" 534 print "$pm: skipping (only raises runtime error).\n"
263 if $VERBOSE >= 2; 535 if $VERBOSE >= 3;
264 next; 536 next;
537 }
265 } 538 }
266 }
267 539
268 if ($STRIP =~ /ppi/i) { 540 $src = cache +($STRIP eq "ppi" ? "$UNISTRIP,$OPTIMISE_SIZE" : undef), $src, sub {
269 require PPI; 541 if ($UNISTRIP && $pm =~ /^unicore\/.*\.pl$/) {
542 print "applying unicore stripping $pm\n"
543 if $VERBOSE >= 6;
270 544
271 my $ppi = PPI::Document->new (\$src); 545 # special stripping for unicore swashes and properties
272 $ppi->prune ("PPI::Token::Comment"); 546 # much more could be done by going binary
273 $ppi->prune ("PPI::Token::Pod"); 547 $src =~ s{
548 (^return\ <<'END';\n) (.*?\n) (END(?:\n|\Z))
549 }{
550 my ($pre, $data, $post) = ($1, $2, $3);
274 551
275 # prune END stuff 552 for ($data) {
276 for (my $last = $ppi->last_element; $last; ) { 553 s/^([0-9a-fA-F]+)\t([0-9a-fA-F]+)\t/sprintf "%X\t%X", hex $1, hex $2/gem
277 my $prev = $last->previous_token; 554 if $OPTIMISE_SIZE;
278 555
279 if ($last->isa (PPI::Token::Whitespace::)) { 556# s{
280 $last->delete; 557# ^([0-9a-fA-F]+)\t([0-9a-fA-F]*)\t
281 } elsif ($last->isa (PPI::Statement::End::)) { 558# }{
282 $last->delete; 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"
283 last; 570 }smex;
284 } elsif ($last->isa (PPI::Token::Pod::)) {
285 $last->delete;
286 } else {
287 last;
288 } 571 }
289 572
573 if ($STRIP =~ /ppi/i) {
574 require PPI;
575
576 if (my $ppi = PPI::Document->new (\$src)) {
577 $ppi->prune ("PPI::Token::Comment");
578 $ppi->prune ("PPI::Token::Pod");
579
580 # prune END stuff
581 for (my $last = $ppi->last_element; $last; ) {
582 my $prev = $last->previous_token;
583
584 if ($last->isa (PPI::Token::Whitespace::)) {
585 $last->delete;
586 } elsif ($last->isa (PPI::Statement::End::)) {
587 $last->delete;
588 last;
589 } elsif ($last->isa (PPI::Token::Pod::)) {
590 $last->delete;
591 } else {
592 last;
593 }
594
290 $last = $prev; 595 $last = $prev;
291 } 596 }
292 597
293 # prune some but not all insignificant whitespace 598 # prune some but not all insignificant whitespace
294 for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) { 599 for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) {
295 my $prev = $ws->previous_token; 600 my $prev = $ws->previous_token;
296 my $next = $ws->next_token; 601 my $next = $ws->next_token;
297 602
298 if (!$prev || !$next) { 603 if (!$prev || !$next) {
299 $ws->delete; 604 $ws->delete;
300 } else { 605 } else {
301 if ( 606 if (
302 $next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float 607 $next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float
303 or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/ 608 or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/
304 or $prev->isa (PPI::Token::Structure::) 609 or $prev->isa (PPI::Token::Structure::)
305 # decrease size, decrease compressability 610 or ($OPTIMISE_SIZE &&
306 #or ($prev->isa (PPI::Token::Word::) 611 ($prev->isa (PPI::Token::Word::)
307 # && (PPI::Token::Symbol:: eq ref $next 612 && (PPI::Token::Symbol:: eq ref $next
308 # || $next->isa (PPI::Structure::Block::) 613 || $next->isa (PPI::Structure::Block::)
309 # || $next->isa (PPI::Structure::List::) 614 || $next->isa (PPI::Structure::List::)
310 # || $next->isa (PPI::Structure::Condition::))) 615 || $next->isa (PPI::Structure::Condition::)))
616 )
311 ) { 617 ) {
312 $ws->delete; 618 $ws->delete;
313 } elsif ($prev->isa (PPI::Token::Whitespace::)) { 619 } elsif ($prev->isa (PPI::Token::Whitespace::)) {
314 $ws->{content} = ' '; 620 $ws->{content} = ' ';
315 $prev->delete; 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;
316 } else { 664 } else {
317 $ws->{content} = ' '; 665 warn "WARNING: $pm{$pm}: PPI failed to parse this file\n";
666 }
667 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses it's own pod
668 require Pod::Strip;
669
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;
677 }
678
679 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
680 if (open my $fh, "-|") {
681 <$fh>;
682 } else {
683 eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
684 exit 0;
318 } 685 }
319 } 686 }
687
688 $src
320 } 689 };
321 690
322 # prune whitespace around blocks
323 if (0) {
324 # these usually decrease size, but decrease compressability more
325 for my $struct (PPI::Structure::Block::, PPI::Structure::Condition::) {
326 for my $node (@{ $ppi->find ($struct) }) {
327 my $n1 = $node->first_token;
328 my $n2 = $n1->previous_token;
329 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
330 $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
331 my $n1 = $node->last_token;
332 my $n2 = $n1->next_token;
333 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
334 $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
335 }
336 }
337
338 for my $node (@{ $ppi->find (PPI::Structure::List::) }) {
339 my $n1 = $node->first_token;
340 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
341 my $n1 = $node->last_token;
342 $n1->delete if $n1->isa (PPI::Token::Whitespace::);
343 }
344 }
345
346 # reformat qw() lists which often have lots of whitespace
347 for my $node (@{ $ppi->find (PPI::Token::QuoteLike::Words::) }) {
348 if ($node->{content} =~ /^qw(.)(.*)(.)$/s) {
349 my ($a, $qw, $b) = ($1, $2, $3);
350 $qw =~ s/^\s+//;
351 $qw =~ s/\s+$//;
352 $qw =~ s/\s+/ /g;
353 $node->{content} = "qw$a$qw$b";
354 }
355 }
356
357 $src = $ppi->serialize;
358 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses it's own pod
359 require Pod::Strip;
360
361 my $stripper = Pod::Strip->new;
362
363 my $out;
364 $stripper->output_string (\$out);
365 $stripper->parse_string_document ($src);
366 $src = $out;
367 }
368
369 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
370 if (open my $fh, "-|") {
371 <$fh>;
372 } else {
373 eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
374 exit 0;
375 }
376 }
377
378# if ($pm eq "Opcode.pm") { 691# if ($pm eq "Opcode.pm") {
379# open my $fh, ">x" or die; print $fh $src;#d# 692# open my $fh, ">x" or die; print $fh $src;#d#
380# exit 1; 693# exit 1;
694# }
381# } 695 }
382 696
383 warn "adding $pm\n" 697 print "adding $pm (original size $size, stored size ", length $src, ")\n"
384 if $VERBOSE >= 2; 698 if $VERBOSE >= 2;
385 699
386 push @index, ((length $pm) << 25) | length $data; 700 push @index, ((length $pm) << 25) | length $data;
387 $data .= $pm . $src; 701 $data .= $pm . $src;
388} 702}
389 703
390length $data < 2**25 704length $data < 2**25
391 or die "bundle too large (only 32MB supported)\n"; 705 or die "ERROR: bundle too large (only 32MB supported)\n";
392 706
393my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16; 707my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16;
394 708
395############################################################################# 709#############################################################################
396# output 710# output
397 711
398print "generating $PREFIX.h... "; 712print "generating $PREFIX.h... "
713 if $VERBOSE >= 1;
399 714
400{ 715{
401 open my $fh, ">", "$PREFIX.h" 716 open my $fh, ">", "$PREFIX.h"
402 or die "$PREFIX.h: $!\n"; 717 or die "$PREFIX.h: $!\n";
403 718
404 print $fh <<EOF; 719 print $fh <<EOF;
405/* do not edit, automatically created by mkstaticbundle */ 720/* do not edit, automatically created by mkstaticbundle */
721
406#include <EXTERN.h> 722#include <EXTERN.h>
407#include <perl.h> 723#include <perl.h>
408#include <XSUB.h> 724#include <XSUB.h>
409 725
410/* public API */ 726/* public API */
411EXTERN_C PerlInterpreter *staticperl; 727EXTERN_C PerlInterpreter *staticperl;
728EXTERN_C void staticperl_xs_init (pTHX);
412EXTERN_C void staticperl_init (void); 729EXTERN_C void staticperl_init (void);
413EXTERN_C void staticperl_cleanup (void); 730EXTERN_C void staticperl_cleanup (void);
731
414EOF 732EOF
415} 733}
416 734
417print "\n"; 735print "\n"
736 if $VERBOSE >= 1;
418 737
419############################################################################# 738#############################################################################
420# output 739# output
421 740
422print "generating $PREFIX.c... "; 741print "generating $PREFIX.c... "
742 if $VERBOSE >= 1;
423 743
424open my $fh, ">", "$PREFIX.c" 744open my $fh, ">", "$PREFIX.c"
425 or die "$PREFIX.c: $!\n"; 745 or die "$PREFIX.c: $!\n";
426 746
427print $fh <<EOF; 747print $fh <<EOF;
428/* do not edit, automatically created by mkstaticbundle */ 748/* do not edit, automatically created by mkstaticbundle */
429
430#include <EXTERN.h>
431#include <perl.h>
432#include <XSUB.h>
433 749
434#include "bundle.h" 750#include "bundle.h"
435 751
436/* public API */ 752/* public API */
437PerlInterpreter *staticperl; 753PerlInterpreter *staticperl;
461printf $fh "0x%08x\n};\n", (length $data); 777printf $fh "0x%08x\n};\n", (length $data);
462 778
463print $fh "static const char $varpfx\_data [] =\n"; 779print $fh "static const char $varpfx\_data [] =\n";
464dump_string $fh, $data; 780dump_string $fh, $data;
465 781
466print $fh ";\n\n";; 782print $fh ";\n\n";
467 783
468############################################################################# 784#############################################################################
469# bootstrap 785# bootstrap
470 786
471# boot file for staticperl 787# boot file for staticperl
576 } 892 }
577 893
578 XSRETURN ($varpfx\_count); 894 XSRETURN ($varpfx\_count);
579} 895}
580 896
581static char *args[] = {
582 "staticperl",
583 "-e",
584 "0"
585};
586
587EOF 897EOF
588 898
589############################################################################# 899#############################################################################
590# xs_init 900# xs_init
591 901
592print $fh <<EOF; 902print $fh <<EOF;
593static void 903void
594xs_init (pTHX) 904staticperl_xs_init (pTHX)
595{ 905{
596EOF 906EOF
597 907
598@static_ext = ("DynaLoader", sort @static_ext); 908@static_ext = ("DynaLoader", sort @static_ext);
599 909
630EOF 940EOF
631 941
632############################################################################# 942#############################################################################
633# optional perl_init/perl_destroy 943# optional perl_init/perl_destroy
634 944
635if ($PERL) { 945if ($APP) {
636 print $fh <<EOF; 946 print $fh <<EOF;
637 947
638int 948int
639main (int argc, char *argv []) 949main (int argc, char *argv [])
640{ 950{
641 extern char **environ; 951 extern char **environ;
642 int exitstatus; 952 int exitstatus;
643 953
954 static char *args[] = {
955 "staticperl",
956 "-e",
957 "0"
958 };
959
644 PERL_SYS_INIT3 (&argc, &argv, &environ); 960 PERL_SYS_INIT3 (&argc, &argv, &environ);
645 staticperl = perl_alloc (); 961 staticperl = perl_alloc ();
646 perl_construct (staticperl); 962 perl_construct (staticperl);
647 963
648 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 964 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
649 965
966 exitstatus = perl_parse (staticperl, staticperl_xs_init, sizeof (args) / sizeof (*args), args, environ);
967 if (!exitstatus)
968 perl_run (staticperl);
969
970 exitstatus = perl_destruct (staticperl);
971 perl_free (staticperl);
972 PERL_SYS_TERM ();
973
974 return exitstatus;
975}
976EOF
977} elsif ($PERL) {
978 print $fh <<EOF;
979
980int
981main (int argc, char *argv [])
982{
983 extern char **environ;
984 int exitstatus;
985
986 PERL_SYS_INIT3 (&argc, &argv, &environ);
987 staticperl = perl_alloc ();
988 perl_construct (staticperl);
989
990 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
991
650 exitstatus = perl_parse (staticperl, xs_init, argc, argv, environ); 992 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
651 if (!exitstatus) 993 if (!exitstatus)
652 perl_run (staticperl); 994 perl_run (staticperl);
653 995
654 exitstatus = perl_destruct (staticperl); 996 exitstatus = perl_destruct (staticperl);
655 perl_free (staticperl); 997 perl_free (staticperl);
666{ 1008{
667 extern char **environ; 1009 extern char **environ;
668 int argc = sizeof (args) / sizeof (args [0]); 1010 int argc = sizeof (args) / sizeof (args [0]);
669 char **argv = args; 1011 char **argv = args;
670 1012
1013 static char *args[] = {
1014 "staticperl",
1015 "-e",
1016 "0"
1017 };
1018
671 PERL_SYS_INIT3 (&argc, &argv, &environ); 1019 PERL_SYS_INIT3 (&argc, &argv, &environ);
672 staticperl = perl_alloc (); 1020 staticperl = perl_alloc ();
673 perl_construct (staticperl); 1021 perl_construct (staticperl);
674 PL_origalen = 1; 1022 PL_origalen = 1;
675 PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 1023 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
676 perl_parse (staticperl, xs_init, argc, argv, environ); 1024 perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
677 1025
678 perl_run (staticperl); 1026 perl_run (staticperl);
679} 1027}
680 1028
681EXTERN_C void 1029EXTERN_C void
687 PERL_SYS_TERM (); 1035 PERL_SYS_TERM ();
688} 1036}
689EOF 1037EOF
690} 1038}
691 1039
692print -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;
693 1042
694############################################################################# 1043#############################################################################
695# libs, cflags 1044# libs, cflags
696 1045
697{ 1046{
698 print "generating $PREFIX.ccopts... "; 1047 print "generating $PREFIX.ccopts... "
1048 if $VERBOSE >= 1;
699 1049
700 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";
701 $str =~ s/([\(\)])/\\$1/g; 1051 $str =~ s/([\(\)])/\\$1/g;
702
703 print "$str\n\n";
704 1052
705 open my $fh, ">$PREFIX.ccopts" 1053 open my $fh, ">$PREFIX.ccopts"
706 or die "$PREFIX.ccopts: $!"; 1054 or die "$PREFIX.ccopts: $!";
707 print $fh $str; 1055 print $fh $str;
1056
1057 print "$str\n\n"
1058 if $VERBOSE >= 1;
708} 1059}
709 1060
710{ 1061{
711 print "generating $PREFIX.ldopts... "; 1062 print "generating $PREFIX.ldopts... ";
712 1063
713 my $str = $STATIC ? "--static " : ""; 1064 my $str = $STATIC ? "-static " : "";
714 1065
715 $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}"; 1066 $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}";
716 1067
717 my %seen; 1068 my %seen;
718 $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g); 1069 $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g);
719 1070
1071 for (@staticlibs) {
1072 $str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx;
1073 }
1074
720 $str =~ s/([\(\)])/\\$1/g; 1075 $str =~ s/([\(\)])/\\$1/g;
721
722 print "$str\n\n";
723 1076
724 open my $fh, ">$PREFIX.ldopts" 1077 open my $fh, ">$PREFIX.ldopts"
725 or die "$PREFIX.ldopts: $!"; 1078 or die "$PREFIX.ldopts: $!";
726 print $fh $str; 1079 print $fh $str;
727}
728 1080
729if ($PERL) { 1081 print "$str\n\n"
1082 if $VERBOSE >= 1;
1083}
1084
1085if ($PERL or defined $APP) {
1086 $APP = "perl" unless defined $APP;
1087
1088 print "building $APP...\n"
1089 if $VERBOSE >= 1;
1090
730 system "$Config{cc} \$(cat bundle.ccopts\) -o perl bundle.c \$(cat bundle.ldopts\)"; 1091 system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)";
731 1092
732 unlink "$PREFIX.$_" 1093 unlink "$PREFIX.$_"
733 for qw(ccopts ldopts c h); 1094 for qw(ccopts ldopts c h);
734}
735 1095
1096 print "\n"
1097 if $VERBOSE >= 1;
1098}
1099

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines