1 |
#!/opt/bin/perl |
2 |
|
3 |
############################################################################# |
4 |
# cannot load modules till after the tracer BEGIN block |
5 |
|
6 |
our $VERBOSE = 1; |
7 |
our $STRIP = "pod"; # none, pod or ppi |
8 |
our $UNISTRIP = 1; # always on, try to strip unicore swash data |
9 |
our $PERL = 0; |
10 |
our $APP; |
11 |
our $VERIFY = 0; |
12 |
our $STATIC = 0; |
13 |
our $PACKLIST = 0; |
14 |
|
15 |
our $OPTIMISE_SIZE = 0; # optimise for raw file size instead of for compression? |
16 |
|
17 |
our $CACHE; |
18 |
our $CACHEVER = 1; # do not change unless you know what you are doing |
19 |
|
20 |
my $PREFIX = "bundle"; |
21 |
my $PACKAGE = "static"; |
22 |
|
23 |
my %pm; |
24 |
my %pmbin; |
25 |
my @libs; |
26 |
my @static_ext; |
27 |
my $extralibs; |
28 |
my @staticlibs; |
29 |
my @incext; |
30 |
|
31 |
@ARGV |
32 |
or die "$0: use 'staticperl help' (or read the sources of staticperl)\n"; |
33 |
|
34 |
# remove "." from @INC - staticperl.sh does it for us, but be on the safe side |
35 |
BEGIN { @INC = grep !/^\.$/, @INC } |
36 |
|
37 |
$|=1; |
38 |
|
39 |
our ($TRACER_W, $TRACER_R); |
40 |
|
41 |
sub find_incdir($) { |
42 |
for (@INC) { |
43 |
next if ref; |
44 |
return $_ if -e "$_/$_[0]"; |
45 |
} |
46 |
|
47 |
undef |
48 |
} |
49 |
|
50 |
sub find_inc($) { |
51 |
my $dir = find_incdir $_[0]; |
52 |
|
53 |
return "$dir/$_[0]" |
54 |
if defined $dir; |
55 |
|
56 |
undef |
57 |
} |
58 |
|
59 |
BEGIN { |
60 |
# create a loader process to detect @INC requests before we load any modules |
61 |
my ($W_TRACER, $R_TRACER); # used by tracer |
62 |
|
63 |
pipe $R_TRACER, $TRACER_W or die "pipe: $!"; |
64 |
pipe $TRACER_R, $W_TRACER or die "pipe: $!"; |
65 |
|
66 |
unless (fork) { |
67 |
close $TRACER_R; |
68 |
close $TRACER_W; |
69 |
|
70 |
my $pkg = "pkg000000"; |
71 |
|
72 |
unshift @INC, sub { |
73 |
my $dir = find_incdir $_[1] |
74 |
or return; |
75 |
|
76 |
syswrite $W_TRACER, "-\n$dir\n$_[1]\n"; |
77 |
|
78 |
open my $fh, "<:perlio", "$dir/$_[1]" |
79 |
or warn "ERROR: $dir/$_[1]: $!\n"; |
80 |
|
81 |
$fh |
82 |
}; |
83 |
|
84 |
while (<$R_TRACER>) { |
85 |
if (/use (.*)$/) { |
86 |
my $mod = $1; |
87 |
my $pkg = ++$pkg; |
88 |
my $eval = $mod = $mod =~ /[^A-Za-z0-9_:]/ |
89 |
? "require $mod" |
90 |
: "{ package $pkg; use $mod; }"; |
91 |
eval $eval; |
92 |
warn "ERROR: $@ (while loading '$mod')\n" |
93 |
if $@; |
94 |
} elsif (/eval (.*)$/) { |
95 |
my $eval = $1; |
96 |
eval $eval; |
97 |
warn "ERROR: $@ (in '$eval')\n" |
98 |
if $@; |
99 |
} |
100 |
|
101 |
syswrite $W_TRACER, "\n"; |
102 |
} |
103 |
|
104 |
exit 0; |
105 |
} |
106 |
} |
107 |
|
108 |
# module loading is now safe |
109 |
|
110 |
sub trace_parse { |
111 |
for (;;) { |
112 |
<$TRACER_R> =~ /^-$/ or last; |
113 |
my $dir = <$TRACER_R>; chomp $dir; |
114 |
my $name = <$TRACER_R>; chomp $name; |
115 |
|
116 |
$pm{$name} = "$dir/$name"; |
117 |
|
118 |
print "+ found potential dependency $name\n" |
119 |
if $VERBOSE >= 3; |
120 |
} |
121 |
} |
122 |
|
123 |
sub trace_module { |
124 |
print "tracing module $_[0]\n" |
125 |
if $VERBOSE >= 2; |
126 |
|
127 |
syswrite $TRACER_W, "use $_[0]\n"; |
128 |
trace_parse; |
129 |
} |
130 |
|
131 |
sub trace_eval { |
132 |
print "tracing eval $_[0]\n" |
133 |
if $VERBOSE >= 2; |
134 |
|
135 |
syswrite $TRACER_W, "eval $_[0]\n"; |
136 |
trace_parse; |
137 |
} |
138 |
|
139 |
sub trace_finish { |
140 |
close $TRACER_W; |
141 |
close $TRACER_R; |
142 |
} |
143 |
|
144 |
############################################################################# |
145 |
# now we can use modules |
146 |
|
147 |
use common::sense; |
148 |
use Config; |
149 |
use Digest::MD5; |
150 |
|
151 |
sub cache($$$) { |
152 |
my ($variant, $src, $filter) = @_; |
153 |
|
154 |
if (length $CACHE and 2048 <= length $src and defined $variant) { |
155 |
my $file = "$CACHE/" . Digest::MD5::md5_hex "$CACHEVER\x00$variant\x00$src"; |
156 |
|
157 |
if (open my $fh, "<:perlio", $file) { |
158 |
print "using cache for $file\n" |
159 |
if $VERBOSE >= 7; |
160 |
|
161 |
local $/; |
162 |
return <$fh>; |
163 |
} |
164 |
|
165 |
$src = $filter->($src); |
166 |
|
167 |
print "creating cache entry $file\n" |
168 |
if $VERBOSE >= 8; |
169 |
|
170 |
if (open my $fh, ">:perlio", "$file~") { |
171 |
if ((syswrite $fh, $src) == length $src) { |
172 |
close $fh; |
173 |
rename "$file~", $file; |
174 |
} |
175 |
} |
176 |
|
177 |
return $src; |
178 |
} |
179 |
|
180 |
$filter->($src) |
181 |
} |
182 |
|
183 |
sub dump_string { |
184 |
my ($fh, $data) = @_; |
185 |
|
186 |
if (length $data) { |
187 |
for ( |
188 |
my $ofs = 0; |
189 |
length (my $substr = substr $data, $ofs, 80); |
190 |
$ofs += 80 |
191 |
) { |
192 |
$substr =~ s/([^\x20-\x21\x23-\x5b\x5d-\x7e])/sprintf "\\%03o", ord $1/ge; |
193 |
$substr =~ s/\?/\\?/g; # trigraphs... |
194 |
print $fh " \"$substr\"\n"; |
195 |
} |
196 |
} else { |
197 |
print $fh " \"\"\n"; |
198 |
} |
199 |
} |
200 |
|
201 |
############################################################################# |
202 |
|
203 |
sub glob2re { |
204 |
for (quotemeta $_[0]) { |
205 |
s/\\\*/\x00/g; |
206 |
s/\x00\x00/.*/g; |
207 |
s/\x00/[^\/]*/g; |
208 |
s/\\\?/[^\/]/g; |
209 |
|
210 |
$_ = s/^\\\/// ? "^$_\$" : "(?:^|/)$_\$"; |
211 |
|
212 |
s/(?: \[\^\/\] | \. ) \*\$$//x; |
213 |
|
214 |
return qr<$_>s |
215 |
} |
216 |
} |
217 |
|
218 |
our %INCSKIP = ( |
219 |
"unicore/TestProp.pl" => undef, # 3.5MB of insanity, apparently just some testcase |
220 |
); |
221 |
|
222 |
sub get_dirtree { |
223 |
my $root = shift; |
224 |
|
225 |
my @tree; |
226 |
my $skip; |
227 |
|
228 |
my $scan; $scan = sub { |
229 |
for (sort do { |
230 |
opendir my $fh, $_[0] |
231 |
or return; |
232 |
readdir $fh |
233 |
}) { |
234 |
next if /^\./; |
235 |
|
236 |
my $path = "$_[0]/$_"; |
237 |
|
238 |
if (-d "$path/.") { |
239 |
$scan->($path); |
240 |
} else { |
241 |
$path = substr $path, $skip; |
242 |
push @tree, $path |
243 |
unless exists $INCSKIP{$path}; |
244 |
} |
245 |
} |
246 |
}; |
247 |
|
248 |
$root =~ s/\/$//; |
249 |
$skip = 1 + length $root; |
250 |
$scan->($root); |
251 |
|
252 |
\@tree |
253 |
} |
254 |
|
255 |
my $inctrees; |
256 |
|
257 |
sub get_inctrees { |
258 |
unless ($inctrees) { |
259 |
my %inctree; |
260 |
$inctree{$_} ||= [$_, get_dirtree $_] # entries in @INC are often duplicates |
261 |
for @INC; |
262 |
$inctrees = [values %inctree]; |
263 |
} |
264 |
|
265 |
@$inctrees |
266 |
} |
267 |
|
268 |
############################################################################# |
269 |
|
270 |
sub cmd_boot { |
271 |
$pm{"&&boot"} = $_[0]; |
272 |
} |
273 |
|
274 |
sub cmd_add { |
275 |
$_[0] =~ /^(.*?)(?:\s+(\S+))?$/ |
276 |
or die "$_[0]: cannot parse"; |
277 |
|
278 |
my $file = $1; |
279 |
my $as = defined $2 ? $2 : "&$1"; |
280 |
|
281 |
warn "<$file,$as>\n";#d# |
282 |
|
283 |
$pm{$as} = $file; |
284 |
$pmbin{$as} = 1 if $_[1]; |
285 |
} |
286 |
|
287 |
sub cmd_staticlib { |
288 |
push @staticlibs, $_ |
289 |
for split /\s+/, $_[0]; |
290 |
} |
291 |
|
292 |
sub cmd_include { |
293 |
push @incext, [$_[1], glob2re $_[0]]; |
294 |
} |
295 |
|
296 |
sub cmd_incglob { |
297 |
my ($pattern) = @_; |
298 |
|
299 |
$pattern = glob2re $pattern; |
300 |
|
301 |
for (get_inctrees) { |
302 |
my ($dir, $files) = @$_; |
303 |
|
304 |
$pm{$_} = "$dir/$_" |
305 |
for grep /$pattern/ && /\.(pl|pm)$/, @$files; |
306 |
} |
307 |
} |
308 |
|
309 |
sub parse_argv; |
310 |
|
311 |
sub cmd_file { |
312 |
open my $fh, "<", $_[0] |
313 |
or die "$_[0]: $!\n"; |
314 |
|
315 |
local @ARGV; |
316 |
|
317 |
while (<$fh>) { |
318 |
chomp; |
319 |
next unless /\S/; |
320 |
next if /^\s*#/; |
321 |
|
322 |
s/^\s*-*/--/; |
323 |
my ($cmd, $args) = split / /, $_, 2; |
324 |
|
325 |
push @ARGV, $cmd; |
326 |
push @ARGV, $args if defined $args; |
327 |
} |
328 |
|
329 |
parse_argv; |
330 |
} |
331 |
|
332 |
use Getopt::Long; |
333 |
|
334 |
sub parse_argv { |
335 |
GetOptions |
336 |
"strip=s" => \$STRIP, |
337 |
"cache=s" => \$CACHE, # internal option |
338 |
"verbose|v" => sub { ++$VERBOSE }, |
339 |
"quiet|q" => sub { --$VERBOSE }, |
340 |
"perl" => \$PERL, |
341 |
"app=s" => \$APP, |
342 |
"eval|e=s" => sub { trace_eval $_[1] }, |
343 |
"use|M=s" => sub { trace_module $_[1] }, |
344 |
"boot=s" => sub { cmd_boot $_[1] }, |
345 |
"add=s" => sub { cmd_add $_[1], 0 }, |
346 |
"addbin=s" => sub { cmd_add $_[1], 1 }, |
347 |
"incglob=s" => sub { cmd_incglob $_[1] }, |
348 |
"include|i=s" => sub { cmd_include $_[1], 1 }, |
349 |
"exclude|x=s" => sub { cmd_include $_[1], 0 }, |
350 |
"static!" => \$STATIC, |
351 |
"usepacklists!" => \$PACKLIST, |
352 |
"staticlib=s" => sub { cmd_staticlib $_[1] }, |
353 |
"<>" => sub { cmd_file $_[0] }, |
354 |
or exit 1; |
355 |
} |
356 |
|
357 |
Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case"); |
358 |
|
359 |
parse_argv; |
360 |
|
361 |
die "cannot specify both --app and --perl\n" |
362 |
if $PERL and defined $APP; |
363 |
|
364 |
# required for @INC loading, unfortunately |
365 |
trace_module "PerlIO::scalar"; |
366 |
|
367 |
############################################################################# |
368 |
# apply include/exclude |
369 |
|
370 |
{ |
371 |
my %pmi; |
372 |
|
373 |
for (@incext) { |
374 |
my ($inc, $glob) = @$_; |
375 |
|
376 |
my @match = grep /$glob/, keys %pm; |
377 |
|
378 |
if ($inc) { |
379 |
# include |
380 |
@pmi{@match} = delete @pm{@match}; |
381 |
|
382 |
print "applying include $glob - protected ", (scalar @match), " files.\n" |
383 |
if $VERBOSE >= 5; |
384 |
} else { |
385 |
# exclude |
386 |
delete @pm{@match}; |
387 |
|
388 |
print "applying exclude $glob - removed ", (scalar @match), " files.\n" |
389 |
if $VERBOSE >= 5; |
390 |
} |
391 |
} |
392 |
|
393 |
my @pmi = keys %pmi; |
394 |
@pm{@pmi} = delete @pmi{@pmi}; |
395 |
} |
396 |
|
397 |
############################################################################# |
398 |
# scan for AutoLoader, static archives and other dependencies |
399 |
|
400 |
sub scan_al { |
401 |
my ($auto, $autodir) = @_; |
402 |
|
403 |
my $ix = "$autodir/autosplit.ix"; |
404 |
|
405 |
print "processing autoload index for '$auto'\n" |
406 |
if $VERBOSE >= 6; |
407 |
|
408 |
$pm{"$auto/autosplit.ix"} = $ix; |
409 |
|
410 |
open my $fh, "<:perlio", $ix |
411 |
or die "$ix: $!"; |
412 |
|
413 |
my $package; |
414 |
|
415 |
while (<$fh>) { |
416 |
if (/^\s*sub\s+ ([^[:space:];]+) \s* (?:\([^)]*\))? \s*;?\s*$/x) { |
417 |
my $al = "auto/$package/$1.al"; |
418 |
my $inc = find_inc $al; |
419 |
|
420 |
defined $inc or die "$al: autoload file not found, but should be there.\n"; |
421 |
|
422 |
$pm{$al} = $inc; |
423 |
print "found autoload function '$al'\n" |
424 |
if $VERBOSE >= 6; |
425 |
|
426 |
} elsif (/^\s*package\s+([^[:space:];]+)\s*;?\s*$/) { |
427 |
($package = $1) =~ s/::/\//g; |
428 |
} elsif (/^\s*(?:#|1?\s*;?\s*$)/) { |
429 |
# nop |
430 |
} else { |
431 |
warn "WARNING: $ix: unparsable line, please report: $_"; |
432 |
} |
433 |
} |
434 |
} |
435 |
|
436 |
for my $pm (keys %pm) { |
437 |
if ($pm =~ /^(.*)\.pm$/) { |
438 |
my $auto = "auto/$1"; |
439 |
my $autodir = find_inc $auto; |
440 |
|
441 |
if (defined $autodir && -d $autodir) { |
442 |
# AutoLoader |
443 |
scan_al $auto, $autodir |
444 |
if -f "$autodir/autosplit.ix"; |
445 |
|
446 |
# extralibs.ld |
447 |
if (open my $fh, "<:perlio", "$autodir/extralibs.ld") { |
448 |
print "found extralibs for $pm\n" |
449 |
if $VERBOSE >= 6; |
450 |
|
451 |
local $/; |
452 |
$extralibs .= " " . <$fh>; |
453 |
} |
454 |
|
455 |
$pm =~ /([^\/]+).pm$/ or die "$pm: unable to match last component"; |
456 |
|
457 |
my $base = $1; |
458 |
|
459 |
# static ext |
460 |
if (-f "$autodir/$base$Config{_a}") { |
461 |
print "found static archive for $pm\n" |
462 |
if $VERBOSE >= 3; |
463 |
|
464 |
push @libs, "$autodir/$base$Config{_a}"; |
465 |
push @static_ext, $pm; |
466 |
} |
467 |
|
468 |
# dynamic object |
469 |
die "ERROR: found shared object - can't link statically ($_)\n" |
470 |
if -f "$autodir/$base.$Config{dlext}"; |
471 |
|
472 |
if ($PACKLIST && open my $fh, "<:perlio", "$autodir/.packlist") { |
473 |
print "found .packlist for $pm\n" |
474 |
if $VERBOSE >= 3; |
475 |
|
476 |
while (<$fh>) { |
477 |
chomp; |
478 |
s/ .*$//; # newer-style .packlists might contain key=value pairs |
479 |
|
480 |
# only include certain files (.al, .ix, .pm, .pl) |
481 |
if (/\.(pm|pl|al|ix)$/) { |
482 |
for my $inc (@INC) { |
483 |
# in addition, we only add files that are below some @INC path |
484 |
$inc =~ s/\/*$/\//; |
485 |
|
486 |
if ($inc eq substr $_, 0, length $inc) { |
487 |
my $base = substr $_, length $inc; |
488 |
$pm{$base} = $_; |
489 |
|
490 |
print "+ added .packlist dependency $base\n" |
491 |
if $VERBOSE >= 3; |
492 |
} |
493 |
|
494 |
last; |
495 |
} |
496 |
} |
497 |
} |
498 |
} |
499 |
} |
500 |
} |
501 |
} |
502 |
|
503 |
############################################################################# |
504 |
|
505 |
print "processing bundle files (try more -v power if you get bored waiting here)...\n" |
506 |
if $VERBOSE >= 1; |
507 |
|
508 |
my $data; |
509 |
my @index; |
510 |
my @order = sort { |
511 |
length $a <=> length $b |
512 |
or $a cmp $b |
513 |
} keys %pm; |
514 |
|
515 |
# sorting by name - better compression, but needs more metadata |
516 |
# sorting by length - faster lookup |
517 |
# usually, the metadata overhead beats the loss through compression |
518 |
|
519 |
for my $pm (@order) { |
520 |
my $path = $pm{$pm}; |
521 |
|
522 |
128 > length $pm |
523 |
or die "ERROR: $pm: path too long (only 128 octets supported)\n"; |
524 |
|
525 |
my $src = ref $path |
526 |
? $$path |
527 |
: do { |
528 |
open my $pm, "<", $path |
529 |
or die "$path: $!"; |
530 |
|
531 |
local $/; |
532 |
|
533 |
<$pm> |
534 |
}; |
535 |
|
536 |
my $size = length $src; |
537 |
|
538 |
unless ($pmbin{$pm}) { # only do this unless the file is binary |
539 |
if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) { |
540 |
if ($src =~ /^ unimpl \"/m) { |
541 |
print "$pm: skipping (raises runtime error only).\n" |
542 |
if $VERBOSE >= 3; |
543 |
next; |
544 |
} |
545 |
} |
546 |
|
547 |
$src = cache +($STRIP eq "ppi" ? "$UNISTRIP,$OPTIMISE_SIZE" : undef), $src, sub { |
548 |
if ($UNISTRIP && $pm =~ /^unicore\/.*\.pl$/) { |
549 |
print "applying unicore stripping $pm\n" |
550 |
if $VERBOSE >= 6; |
551 |
|
552 |
# special stripping for unicore swashes and properties |
553 |
# much more could be done by going binary |
554 |
$src =~ s{ |
555 |
(^return\ <<'END';\n) (.*?\n) (END(?:\n|\Z)) |
556 |
}{ |
557 |
my ($pre, $data, $post) = ($1, $2, $3); |
558 |
|
559 |
for ($data) { |
560 |
s/^([0-9a-fA-F]+)\t([0-9a-fA-F]+)\t/sprintf "%X\t%X", hex $1, hex $2/gem |
561 |
if $OPTIMISE_SIZE; |
562 |
|
563 |
# s{ |
564 |
# ^([0-9a-fA-F]+)\t([0-9a-fA-F]*)\t |
565 |
# }{ |
566 |
# # ww - smaller filesize, UU - compress better |
567 |
# pack "C0UU", |
568 |
# hex $1, |
569 |
# length $2 ? (hex $2) - (hex $1) : 0 |
570 |
# }gemx; |
571 |
|
572 |
s/#.*\n/\n/mg; |
573 |
s/\s+\n/\n/mg; |
574 |
} |
575 |
|
576 |
"$pre$data$post" |
577 |
}smex; |
578 |
} |
579 |
|
580 |
if ($STRIP =~ /ppi/i) { |
581 |
require PPI; |
582 |
|
583 |
if (my $ppi = PPI::Document->new (\$src)) { |
584 |
$ppi->prune ("PPI::Token::Comment"); |
585 |
$ppi->prune ("PPI::Token::Pod"); |
586 |
|
587 |
# prune END stuff |
588 |
for (my $last = $ppi->last_element; $last; ) { |
589 |
my $prev = $last->previous_token; |
590 |
|
591 |
if ($last->isa (PPI::Token::Whitespace::)) { |
592 |
$last->delete; |
593 |
} elsif ($last->isa (PPI::Statement::End::)) { |
594 |
$last->delete; |
595 |
last; |
596 |
} elsif ($last->isa (PPI::Token::Pod::)) { |
597 |
$last->delete; |
598 |
} else { |
599 |
last; |
600 |
} |
601 |
|
602 |
$last = $prev; |
603 |
} |
604 |
|
605 |
# prune some but not all insignificant whitespace |
606 |
for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) { |
607 |
my $prev = $ws->previous_token; |
608 |
my $next = $ws->next_token; |
609 |
|
610 |
if (!$prev || !$next) { |
611 |
$ws->delete; |
612 |
} else { |
613 |
if ( |
614 |
$next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float |
615 |
or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/ |
616 |
or $prev->isa (PPI::Token::Structure::) |
617 |
or ($OPTIMISE_SIZE && |
618 |
($prev->isa (PPI::Token::Word::) |
619 |
&& (PPI::Token::Symbol:: eq ref $next |
620 |
|| $next->isa (PPI::Structure::Block::) |
621 |
|| $next->isa (PPI::Structure::List::) |
622 |
|| $next->isa (PPI::Structure::Condition::))) |
623 |
) |
624 |
) { |
625 |
$ws->delete; |
626 |
} elsif ($prev->isa (PPI::Token::Whitespace::)) { |
627 |
$ws->{content} = ' '; |
628 |
$prev->delete; |
629 |
} else { |
630 |
$ws->{content} = ' '; |
631 |
} |
632 |
} |
633 |
} |
634 |
|
635 |
# prune whitespace around blocks |
636 |
if ($OPTIMISE_SIZE) { |
637 |
# these usually decrease size, but decrease compressability more |
638 |
for my $struct (PPI::Structure::Block::, PPI::Structure::Condition::) { |
639 |
for my $node (@{ $ppi->find ($struct) }) { |
640 |
my $n1 = $node->first_token; |
641 |
my $n2 = $n1->previous_token; |
642 |
$n1->delete if $n1->isa (PPI::Token::Whitespace::); |
643 |
$n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::); |
644 |
my $n1 = $node->last_token; |
645 |
my $n2 = $n1->next_token; |
646 |
$n1->delete if $n1->isa (PPI::Token::Whitespace::); |
647 |
$n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::); |
648 |
} |
649 |
} |
650 |
|
651 |
for my $node (@{ $ppi->find (PPI::Structure::List::) }) { |
652 |
my $n1 = $node->first_token; |
653 |
$n1->delete if $n1->isa (PPI::Token::Whitespace::); |
654 |
my $n1 = $node->last_token; |
655 |
$n1->delete if $n1->isa (PPI::Token::Whitespace::); |
656 |
} |
657 |
} |
658 |
|
659 |
# reformat qw() lists which often have lots of whitespace |
660 |
for my $node (@{ $ppi->find (PPI::Token::QuoteLike::Words::) }) { |
661 |
if ($node->{content} =~ /^qw(.)(.*)(.)$/s) { |
662 |
my ($a, $qw, $b) = ($1, $2, $3); |
663 |
$qw =~ s/^\s+//; |
664 |
$qw =~ s/\s+$//; |
665 |
$qw =~ s/\s+/ /g; |
666 |
$node->{content} = "qw$a$qw$b"; |
667 |
} |
668 |
} |
669 |
|
670 |
$src = $ppi->serialize; |
671 |
} else { |
672 |
warn "WARNING: $pm{$pm}: PPI failed to parse this file\n"; |
673 |
} |
674 |
} elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses its own pod |
675 |
require Pod::Strip; |
676 |
|
677 |
my $stripper = Pod::Strip->new; |
678 |
|
679 |
my $out; |
680 |
$stripper->output_string (\$out); |
681 |
$stripper->parse_string_document ($src) |
682 |
or die; |
683 |
$src = $out; |
684 |
} |
685 |
|
686 |
if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") { |
687 |
if (open my $fh, "-|") { |
688 |
<$fh>; |
689 |
} else { |
690 |
eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n"; |
691 |
exit 0; |
692 |
} |
693 |
} |
694 |
|
695 |
$src |
696 |
}; |
697 |
|
698 |
# if ($pm eq "Opcode.pm") { |
699 |
# open my $fh, ">x" or die; print $fh $src;#d# |
700 |
# exit 1; |
701 |
# } |
702 |
} |
703 |
|
704 |
print "adding $pm (original size $size, stored size ", length $src, ")\n" |
705 |
if $VERBOSE >= 2; |
706 |
|
707 |
push @index, ((length $pm) << 25) | length $data; |
708 |
$data .= $pm . $src; |
709 |
} |
710 |
|
711 |
length $data < 2**25 |
712 |
or die "ERROR: bundle too large (only 32MB supported)\n"; |
713 |
|
714 |
my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16; |
715 |
|
716 |
############################################################################# |
717 |
# output |
718 |
|
719 |
print "generating $PREFIX.h... " |
720 |
if $VERBOSE >= 1; |
721 |
|
722 |
{ |
723 |
open my $fh, ">", "$PREFIX.h" |
724 |
or die "$PREFIX.h: $!\n"; |
725 |
|
726 |
print $fh <<EOF; |
727 |
/* do not edit, automatically created by mkstaticbundle */ |
728 |
|
729 |
#include <EXTERN.h> |
730 |
#include <perl.h> |
731 |
#include <XSUB.h> |
732 |
|
733 |
/* public API */ |
734 |
EXTERN_C PerlInterpreter *staticperl; |
735 |
EXTERN_C void staticperl_xs_init (pTHX); |
736 |
EXTERN_C void staticperl_init (XSINIT_t xs_init); /* argument can be 0 */ |
737 |
EXTERN_C void staticperl_cleanup (void); |
738 |
|
739 |
EOF |
740 |
} |
741 |
|
742 |
print "\n" |
743 |
if $VERBOSE >= 1; |
744 |
|
745 |
############################################################################# |
746 |
# output |
747 |
|
748 |
print "generating $PREFIX.c... " |
749 |
if $VERBOSE >= 1; |
750 |
|
751 |
open my $fh, ">", "$PREFIX.c" |
752 |
or die "$PREFIX.c: $!\n"; |
753 |
|
754 |
print $fh <<EOF; |
755 |
/* do not edit, automatically created by mkstaticbundle */ |
756 |
|
757 |
#include "bundle.h" |
758 |
|
759 |
/* public API */ |
760 |
PerlInterpreter *staticperl; |
761 |
|
762 |
EOF |
763 |
|
764 |
############################################################################# |
765 |
# bundle data |
766 |
|
767 |
my $count = @index; |
768 |
|
769 |
print $fh <<EOF; |
770 |
#include "bundle.h" |
771 |
|
772 |
/* bundle data */ |
773 |
|
774 |
static const U32 $varpfx\_count = $count; |
775 |
static const U32 $varpfx\_index [$count + 1] = { |
776 |
EOF |
777 |
|
778 |
my $col; |
779 |
for (@index) { |
780 |
printf $fh "0x%08x,", $_; |
781 |
print $fh "\n" unless ++$col % 10; |
782 |
|
783 |
} |
784 |
printf $fh "0x%08x\n};\n", (length $data); |
785 |
|
786 |
print $fh "static const char $varpfx\_data [] =\n"; |
787 |
dump_string $fh, $data; |
788 |
|
789 |
print $fh ";\n\n"; |
790 |
|
791 |
############################################################################# |
792 |
# bootstrap |
793 |
|
794 |
# boot file for staticperl |
795 |
# this file will be eval'ed at initialisation time |
796 |
|
797 |
my $bootstrap = ' |
798 |
BEGIN { |
799 |
package ' . $PACKAGE . '; |
800 |
|
801 |
PerlIO::scalar->bootstrap; |
802 |
|
803 |
@INC = sub { |
804 |
my $data = find "$_[1]" |
805 |
or return; |
806 |
|
807 |
$INC{$_[1]} = $_[1]; |
808 |
|
809 |
open my $fh, "<", \$data; |
810 |
$fh |
811 |
}; |
812 |
} |
813 |
'; |
814 |
|
815 |
$bootstrap .= "require '&&boot';" |
816 |
if exists $pm{"&&boot"}; |
817 |
|
818 |
$bootstrap =~ s/\s+/ /g; |
819 |
$bootstrap =~ s/(\W) /$1/g; |
820 |
$bootstrap =~ s/ (\W)/$1/g; |
821 |
|
822 |
print $fh "const char bootstrap [] = "; |
823 |
dump_string $fh, $bootstrap; |
824 |
print $fh ";\n\n"; |
825 |
|
826 |
print $fh <<EOF; |
827 |
/* search all bundles for the given file, using binary search */ |
828 |
XS(find) |
829 |
{ |
830 |
dXSARGS; |
831 |
|
832 |
if (items != 1) |
833 |
Perl_croak (aTHX_ "Usage: $PACKAGE\::find (\$path)"); |
834 |
|
835 |
{ |
836 |
STRLEN namelen; |
837 |
char *name = SvPV (ST (0), namelen); |
838 |
SV *res = 0; |
839 |
|
840 |
int l = 0, r = $varpfx\_count; |
841 |
|
842 |
while (l <= r) |
843 |
{ |
844 |
int m = (l + r) >> 1; |
845 |
U32 idx = $varpfx\_index [m]; |
846 |
int comp = namelen - (idx >> 25); |
847 |
|
848 |
if (!comp) |
849 |
{ |
850 |
int ofs = idx & 0x1FFFFFFU; |
851 |
comp = memcmp (name, $varpfx\_data + ofs, namelen); |
852 |
|
853 |
if (!comp) |
854 |
{ |
855 |
/* found */ |
856 |
int ofs2 = $varpfx\_index [m + 1] & 0x1FFFFFFU; |
857 |
|
858 |
ofs += namelen; |
859 |
res = newSVpvn ($varpfx\_data + ofs, ofs2 - ofs); |
860 |
goto found; |
861 |
} |
862 |
} |
863 |
|
864 |
if (comp < 0) |
865 |
r = m - 1; |
866 |
else |
867 |
l = m + 1; |
868 |
} |
869 |
|
870 |
XSRETURN (0); |
871 |
|
872 |
found: |
873 |
ST (0) = res; |
874 |
sv_2mortal (ST (0)); |
875 |
} |
876 |
|
877 |
XSRETURN (1); |
878 |
} |
879 |
|
880 |
/* list all files in the bundle */ |
881 |
XS(list) |
882 |
{ |
883 |
dXSARGS; |
884 |
|
885 |
if (items != 0) |
886 |
Perl_croak (aTHX_ "Usage: $PACKAGE\::list"); |
887 |
|
888 |
{ |
889 |
int i; |
890 |
|
891 |
EXTEND (SP, $varpfx\_count); |
892 |
|
893 |
for (i = 0; i < $varpfx\_count; ++i) |
894 |
{ |
895 |
U32 idx = $varpfx\_index [i]; |
896 |
|
897 |
PUSHs (newSVpvn ($varpfx\_data + (idx & 0x1FFFFFFU), idx >> 25)); |
898 |
} |
899 |
} |
900 |
|
901 |
XSRETURN ($varpfx\_count); |
902 |
} |
903 |
|
904 |
EOF |
905 |
|
906 |
############################################################################# |
907 |
# xs_init |
908 |
|
909 |
print $fh <<EOF; |
910 |
void |
911 |
staticperl_xs_init (pTHX) |
912 |
{ |
913 |
EOF |
914 |
|
915 |
@static_ext = ("DynaLoader", sort @static_ext); |
916 |
|
917 |
# prototypes |
918 |
for (@static_ext) { |
919 |
s/\.pm$//; |
920 |
(my $cname = $_) =~ s/\//__/g; |
921 |
print $fh " EXTERN_C void boot_$cname (pTHX_ CV* cv);\n"; |
922 |
} |
923 |
|
924 |
print $fh <<EOF; |
925 |
char *file = __FILE__; |
926 |
dXSUB_SYS; |
927 |
|
928 |
newXSproto ("$PACKAGE\::find", find, file, "\$"); |
929 |
newXSproto ("$PACKAGE\::list", list, file, ""); |
930 |
EOF |
931 |
|
932 |
# calls |
933 |
for (@static_ext) { |
934 |
s/\.pm$//; |
935 |
|
936 |
(my $cname = $_) =~ s/\//__/g; |
937 |
(my $pname = $_) =~ s/\//::/g; |
938 |
|
939 |
my $bootstrap = $pname eq "DynaLoader" ? "boot" : "bootstrap"; |
940 |
|
941 |
print $fh " newXS (\"$pname\::$bootstrap\", boot_$cname, file);\n"; |
942 |
} |
943 |
|
944 |
print $fh <<EOF; |
945 |
Perl_av_create_and_unshift_one (&PL_preambleav, newSVpv (bootstrap, sizeof (bootstrap) - 1)); |
946 |
|
947 |
if (PL_oldname) |
948 |
((XSINIT_t)PL_oldname)(aTHX); |
949 |
} |
950 |
EOF |
951 |
|
952 |
############################################################################# |
953 |
# optional perl_init/perl_destroy |
954 |
|
955 |
if ($APP) { |
956 |
print $fh <<EOF; |
957 |
|
958 |
int |
959 |
main (int argc, char *argv []) |
960 |
{ |
961 |
extern char **environ; |
962 |
int i, exitstatus; |
963 |
char **args = malloc ((argc + 3) * sizeof (const char *)); |
964 |
|
965 |
args [0] = argv [0]; |
966 |
args [1] = "-e"; |
967 |
args [2] = "0"; |
968 |
args [3] = "--"; |
969 |
|
970 |
for (i = 1; i < argc; ++i) |
971 |
args [i + 3] = argv [i]; |
972 |
|
973 |
PERL_SYS_INIT3 (&argc, &argv, &environ); |
974 |
staticperl = perl_alloc (); |
975 |
perl_construct (staticperl); |
976 |
|
977 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
978 |
|
979 |
exitstatus = perl_parse (staticperl, staticperl_xs_init, argc + 3, args, environ); |
980 |
free (args); |
981 |
if (!exitstatus) |
982 |
perl_run (staticperl); |
983 |
|
984 |
exitstatus = perl_destruct (staticperl); |
985 |
perl_free (staticperl); |
986 |
PERL_SYS_TERM (); |
987 |
|
988 |
return exitstatus; |
989 |
} |
990 |
EOF |
991 |
} elsif ($PERL) { |
992 |
print $fh <<EOF; |
993 |
|
994 |
int |
995 |
main (int argc, char *argv []) |
996 |
{ |
997 |
extern char **environ; |
998 |
int exitstatus; |
999 |
|
1000 |
PERL_SYS_INIT3 (&argc, &argv, &environ); |
1001 |
staticperl = perl_alloc (); |
1002 |
perl_construct (staticperl); |
1003 |
|
1004 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
1005 |
|
1006 |
exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ); |
1007 |
if (!exitstatus) |
1008 |
perl_run (staticperl); |
1009 |
|
1010 |
exitstatus = perl_destruct (staticperl); |
1011 |
perl_free (staticperl); |
1012 |
PERL_SYS_TERM (); |
1013 |
|
1014 |
return exitstatus; |
1015 |
} |
1016 |
EOF |
1017 |
} else { |
1018 |
print $fh <<EOF; |
1019 |
|
1020 |
EXTERN_C void |
1021 |
staticperl_init (XSINIT_t xs_init) |
1022 |
{ |
1023 |
static char *args[] = { |
1024 |
"staticperl", |
1025 |
"-e", |
1026 |
"0" |
1027 |
}; |
1028 |
|
1029 |
extern char **environ; |
1030 |
int argc = sizeof (args) / sizeof (args [0]); |
1031 |
char **argv = args; |
1032 |
|
1033 |
PERL_SYS_INIT3 (&argc, &argv, &environ); |
1034 |
staticperl = perl_alloc (); |
1035 |
perl_construct (staticperl); |
1036 |
PL_origalen = 1; |
1037 |
PL_exit_flags |= PERL_EXIT_DESTRUCT_END; |
1038 |
PL_oldname = (char *)xs_init; |
1039 |
perl_parse (staticperl, staticperl_xs_init, argc, argv, environ); |
1040 |
|
1041 |
perl_run (staticperl); |
1042 |
} |
1043 |
|
1044 |
EXTERN_C void |
1045 |
staticperl_cleanup (void) |
1046 |
{ |
1047 |
perl_destruct (staticperl); |
1048 |
perl_free (staticperl); |
1049 |
staticperl = 0; |
1050 |
PERL_SYS_TERM (); |
1051 |
} |
1052 |
EOF |
1053 |
} |
1054 |
|
1055 |
print -s "$PREFIX.c", " octets (", (length $data) , " data octets).\n\n" |
1056 |
if $VERBOSE >= 1; |
1057 |
|
1058 |
############################################################################# |
1059 |
# libs, cflags |
1060 |
|
1061 |
{ |
1062 |
print "generating $PREFIX.ccopts... " |
1063 |
if $VERBOSE >= 1; |
1064 |
|
1065 |
my $str = "$Config{ccflags} $Config{optimize} $Config{cppflags} -I$Config{archlibexp}/CORE"; |
1066 |
$str =~ s/([\(\)])/\\$1/g; |
1067 |
|
1068 |
open my $fh, ">$PREFIX.ccopts" |
1069 |
or die "$PREFIX.ccopts: $!"; |
1070 |
print $fh $str; |
1071 |
|
1072 |
print "$str\n\n" |
1073 |
if $VERBOSE >= 1; |
1074 |
} |
1075 |
|
1076 |
{ |
1077 |
print "generating $PREFIX.ldopts... "; |
1078 |
|
1079 |
my $str = $STATIC ? "-static " : ""; |
1080 |
|
1081 |
$str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}"; |
1082 |
|
1083 |
my %seen; |
1084 |
$str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g); |
1085 |
|
1086 |
for (@staticlibs) { |
1087 |
$str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx; |
1088 |
} |
1089 |
|
1090 |
$str =~ s/([\(\)])/\\$1/g; |
1091 |
|
1092 |
open my $fh, ">$PREFIX.ldopts" |
1093 |
or die "$PREFIX.ldopts: $!"; |
1094 |
print $fh $str; |
1095 |
|
1096 |
print "$str\n\n" |
1097 |
if $VERBOSE >= 1; |
1098 |
} |
1099 |
|
1100 |
if ($PERL or defined $APP) { |
1101 |
$APP = "perl" unless defined $APP; |
1102 |
|
1103 |
print "building $APP...\n" |
1104 |
if $VERBOSE >= 1; |
1105 |
|
1106 |
system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)"; |
1107 |
|
1108 |
unlink "$PREFIX.$_" |
1109 |
for qw(ccopts ldopts c h); |
1110 |
|
1111 |
print "\n" |
1112 |
if $VERBOSE >= 1; |
1113 |
} |
1114 |
|