ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/mkbundle
Revision: 1.29
Committed: Sun Jul 10 01:37:56 2011 UTC (12 years, 10 months ago) by root
Branch: MAIN
Changes since 1.28: +80 -45 lines
Log Message:
*** empty log message ***

File Contents

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