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