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.16 by root, Thu Jan 20 21:32:47 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines