ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/mkbundle
Revision: 1.16
Committed: Thu Jan 20 21:32:47 2011 UTC (13 years, 5 months ago) by root
Branch: MAIN
Changes since 1.15: +4 -1 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
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 eval "{ package $pkg; use $mod; }";
89 warn "ERROR: $@ (while loading '$mod')\n"
90 if $@;
91 } elsif (/eval (.*)$/) {
92 my $eval = $1;
93 eval $eval;
94 warn "ERROR: $@ (in '$eval')\n"
95 if $@;
96 }
97
98 syswrite $W_TRACER, "\n";
99 }
100
101 exit 0;
102 }
103 }
104
105 # module loading is now safe
106
107 sub trace_parse {
108 for (;;) {
109 <$TRACER_R> =~ /^-$/ or last;
110 my $dir = <$TRACER_R>; chomp $dir;
111 my $name = <$TRACER_R>; chomp $name;
112
113 $pm{$name} = "$dir/$name";
114
115 print "+ found potential dependency $name\n"
116 if $VERBOSE >= 3;
117 }
118 }
119
120 sub trace_module {
121 print "tracing module $_[0]\n"
122 if $VERBOSE >= 2;
123
124 syswrite $TRACER_W, "use $_[0]\n";
125 trace_parse;
126 }
127
128 sub trace_eval {
129 print "tracing eval $_[0]\n"
130 if $VERBOSE >= 2;
131
132 syswrite $TRACER_W, "eval $_[0]\n";
133 trace_parse;
134 }
135
136 sub trace_finish {
137 close $TRACER_W;
138 close $TRACER_R;
139 }
140
141 #############################################################################
142 # now we can use modules
143
144 use common::sense;
145 use Config;
146 use Digest::MD5;
147
148 sub 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 }
179
180 sub dump_string {
181 my ($fh, $data) = @_;
182
183 if (length $data) {
184 for (
185 my $ofs = 0;
186 length (my $substr = substr $data, $ofs, 80);
187 $ofs += 80
188 ) {
189 $substr =~ s/([^\x20-\x21\x23-\x5b\x5d-\x7e])/sprintf "\\%03o", ord $1/ge;
190 $substr =~ s/\?/\\?/g; # trigraphs...
191 print $fh " \"$substr\"\n";
192 }
193 } else {
194 print $fh " \"\"\n";
195 }
196 }
197
198 #############################################################################
199
200 sub glob2re {
201 for (quotemeta $_[0]) {
202 s/\\\*/\x00/g;
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
215 our %INCSKIP = (
216 "unicore/TestProp.pl" => undef, # 3.5MB of insanity, apparently just some testcase
217 );
218
219 sub 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
252 my $inctrees;
253
254 sub 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 #############################################################################
266
267 sub cmd_boot {
268 $pm{"//boot"} = $_[0];
269 }
270
271 sub cmd_add {
272 $_[0] =~ /^(.*)(?:\s+(\S+))$/
273 or die "$_[0]: cannot parse";
274
275 my $file = $1;
276 my $as = defined $2 ? $2 : "/$1";
277
278 $pm{$as} = $file;
279 $pmbin{$as} = 1 if $_[1];
280 }
281
282 sub cmd_staticlib {
283 push @staticlibs, $_
284 for split /\s+/, $_[0];
285 }
286
287 sub cmd_include {
288 push @incext, [$_[1], glob2re $_[0]];
289 }
290
291 sub 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
304 sub parse_argv;
305
306 sub cmd_file {
307 open my $fh, "<", $_[0]
308 or die "$_[0]: $!\n";
309
310 local @ARGV;
311
312 while (<$fh>) {
313 chomp;
314 next unless /\S/;
315 next if /^\s*#/;
316
317 s/^\s*-*/--/;
318 my ($cmd, $args) = split / /, $_, 2;
319
320 push @ARGV, $cmd;
321 push @ARGV, $args if defined $args;
322 }
323
324 parse_argv;
325 }
326
327 use Getopt::Long;
328
329 sub parse_argv {
330 GetOptions
331 "strip=s" => \$STRIP,
332 "cache=s" => \$CACHE, # internal option
333 "verbose|v" => sub { ++$VERBOSE },
334 "quiet|q" => sub { --$VERBOSE },
335 "perl" => \$PERL,
336 "app=s" => \$APP,
337 "eval|e=s" => sub { trace_eval $_[1] },
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
352 Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case");
353
354 parse_argv;
355
356 die "cannot specify both --app and --perl\n"
357 if $PERL and defined $APP;
358
359 # required for @INC loading, unfortunately
360 trace_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;
385 }
386 }
387
388 my @pmi = keys %pmi;
389 @pm{@pmi} = delete @pmi{@pmi};
390 }
391
392 #############################################################################
393 # scan for AutoLoader, static archives and other dependencies
394
395 sub scan_al {
396 my ($auto, $autodir) = @_;
397
398 my $ix = "$autodir/autosplit.ix";
399
400 print "processing autoload index for '$auto'\n"
401 if $VERBOSE >= 6;
402
403 $pm{"$auto/autosplit.ix"} = $ix;
404
405 open my $fh, "<:perlio", $ix
406 or die "$ix: $!";
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
431 for 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
500 print "processing bundle files (try more -v power if you get bored waiting here)...\n"
501 if $VERBOSE >= 1;
502
503 my $data;
504 my @index;
505 my @order = sort {
506 length $a <=> length $b
507 or $a cmp $b
508 } keys %pm;
509
510 # sorting by name - better compression, but needs more metadata
511 # sorting by length - faster lookup
512 # usually, the metadata overhead beats the loss through compression
513
514 for my $pm (@order) {
515 my $path = $pm{$pm};
516
517 128 > length $pm
518 or die "ERROR: $pm: path too long (only 128 octets supported)\n";
519
520 my $src = ref $path
521 ? $$path
522 : do {
523 open my $pm, "<", $path
524 or die "$path: $!";
525
526 local $/;
527
528 <$pm>
529 };
530
531 my $size = length $src;
532
533 unless ($pmbin{$pm}) { # only do this unless the file is binary
534 if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) {
535 if ($src =~ /^ unimpl \"/m) {
536 print "$pm: skipping (raises runtime error only).\n"
537 if $VERBOSE >= 3;
538 next;
539 }
540 }
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
575 if ($STRIP =~ /ppi/i) {
576 require PPI;
577
578 if (my $ppi = PPI::Document->new (\$src)) {
579 $ppi->prune ("PPI::Token::Comment");
580 $ppi->prune ("PPI::Token::Pod");
581
582 # prune END stuff
583 for (my $last = $ppi->last_element; $last; ) {
584 my $prev = $last->previous_token;
585
586 if ($last->isa (PPI::Token::Whitespace::)) {
587 $last->delete;
588 } elsif ($last->isa (PPI::Statement::End::)) {
589 $last->delete;
590 last;
591 } elsif ($last->isa (PPI::Token::Pod::)) {
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;
666 } else {
667 warn "WARNING: $pm{$pm}: PPI failed to parse this file\n";
668 }
669 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses its own pod
670 require Pod::Strip;
671
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;
679 }
680
681 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
682 if (open my $fh, "-|") {
683 <$fh>;
684 } else {
685 eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
686 exit 0;
687 }
688 }
689
690 $src
691 };
692
693 # if ($pm eq "Opcode.pm") {
694 # open my $fh, ">x" or die; print $fh $src;#d#
695 # exit 1;
696 # }
697 }
698
699 print "adding $pm (original size $size, stored size ", length $src, ")\n"
700 if $VERBOSE >= 2;
701
702 push @index, ((length $pm) << 25) | length $data;
703 $data .= $pm . $src;
704 }
705
706 length $data < 2**25
707 or die "ERROR: bundle too large (only 32MB supported)\n";
708
709 my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16;
710
711 #############################################################################
712 # output
713
714 print "generating $PREFIX.h... "
715 if $VERBOSE >= 1;
716
717 {
718 open my $fh, ">", "$PREFIX.h"
719 or die "$PREFIX.h: $!\n";
720
721 print $fh <<EOF;
722 /* do not edit, automatically created by mkstaticbundle */
723
724 #include <EXTERN.h>
725 #include <perl.h>
726 #include <XSUB.h>
727
728 /* public API */
729 EXTERN_C PerlInterpreter *staticperl;
730 EXTERN_C void staticperl_xs_init (pTHX);
731 EXTERN_C void staticperl_init (void);
732 EXTERN_C void staticperl_cleanup (void);
733
734 EOF
735 }
736
737 print "\n"
738 if $VERBOSE >= 1;
739
740 #############################################################################
741 # output
742
743 print "generating $PREFIX.c... "
744 if $VERBOSE >= 1;
745
746 open my $fh, ">", "$PREFIX.c"
747 or die "$PREFIX.c: $!\n";
748
749 print $fh <<EOF;
750 /* do not edit, automatically created by mkstaticbundle */
751
752 #include "bundle.h"
753
754 /* public API */
755 PerlInterpreter *staticperl;
756
757 EOF
758
759 #############################################################################
760 # bundle data
761
762 my $count = @index;
763
764 print $fh <<EOF;
765 #include "bundle.h"
766
767 /* bundle data */
768
769 static const U32 $varpfx\_count = $count;
770 static const U32 $varpfx\_index [$count + 1] = {
771 EOF
772
773 my $col;
774 for (@index) {
775 printf $fh "0x%08x,", $_;
776 print $fh "\n" unless ++$col % 10;
777
778 }
779 printf $fh "0x%08x\n};\n", (length $data);
780
781 print $fh "static const char $varpfx\_data [] =\n";
782 dump_string $fh, $data;
783
784 print $fh ";\n\n";
785
786 #############################################################################
787 # bootstrap
788
789 # boot file for staticperl
790 # this file will be eval'ed at initialisation time
791
792 my $bootstrap = '
793 BEGIN {
794 package ' . $PACKAGE . ';
795
796 PerlIO::scalar->bootstrap;
797
798 @INC = sub {
799 my $data = find "$_[1]"
800 or return;
801
802 $INC{$_[1]} = $_[1];
803
804 open my $fh, "<", \$data;
805 $fh
806 };
807 }
808 ';
809
810 $bootstrap .= "require '//boot';"
811 if exists $pm{"//boot"};
812
813 $bootstrap =~ s/\s+/ /g;
814 $bootstrap =~ s/(\W) /$1/g;
815 $bootstrap =~ s/ (\W)/$1/g;
816
817 print $fh "const char bootstrap [] = ";
818 dump_string $fh, $bootstrap;
819 print $fh ";\n\n";
820
821 print $fh <<EOF;
822 /* search all bundles for the given file, using binary search */
823 XS(find)
824 {
825 dXSARGS;
826
827 if (items != 1)
828 Perl_croak (aTHX_ "Usage: $PACKAGE\::find (\$path)");
829
830 {
831 STRLEN namelen;
832 char *name = SvPV (ST (0), namelen);
833 SV *res = 0;
834
835 int l = 0, r = $varpfx\_count;
836
837 while (l <= r)
838 {
839 int m = (l + r) >> 1;
840 U32 idx = $varpfx\_index [m];
841 int comp = namelen - (idx >> 25);
842
843 if (!comp)
844 {
845 int ofs = idx & 0x1FFFFFFU;
846 comp = memcmp (name, $varpfx\_data + ofs, namelen);
847
848 if (!comp)
849 {
850 /* found */
851 int ofs2 = $varpfx\_index [m + 1] & 0x1FFFFFFU;
852
853 ofs += namelen;
854 res = newSVpvn ($varpfx\_data + ofs, ofs2 - ofs);
855 goto found;
856 }
857 }
858
859 if (comp < 0)
860 r = m - 1;
861 else
862 l = m + 1;
863 }
864
865 XSRETURN (0);
866
867 found:
868 ST (0) = res;
869 sv_2mortal (ST (0));
870 }
871
872 XSRETURN (1);
873 }
874
875 /* list all files in the bundle */
876 XS(list)
877 {
878 dXSARGS;
879
880 if (items != 0)
881 Perl_croak (aTHX_ "Usage: $PACKAGE\::list");
882
883 {
884 int i;
885
886 EXTEND (SP, $varpfx\_count);
887
888 for (i = 0; i < $varpfx\_count; ++i)
889 {
890 U32 idx = $varpfx\_index [i];
891
892 PUSHs (newSVpvn ($varpfx\_data + (idx & 0x1FFFFFFU), idx >> 25));
893 }
894 }
895
896 XSRETURN ($varpfx\_count);
897 }
898
899 EOF
900
901 #############################################################################
902 # xs_init
903
904 print $fh <<EOF;
905 void
906 staticperl_xs_init (pTHX)
907 {
908 EOF
909
910 @static_ext = ("DynaLoader", sort @static_ext);
911
912 # prototypes
913 for (@static_ext) {
914 s/\.pm$//;
915 (my $cname = $_) =~ s/\//__/g;
916 print $fh " EXTERN_C void boot_$cname (pTHX_ CV* cv);\n";
917 }
918
919 print $fh <<EOF;
920 char *file = __FILE__;
921 dXSUB_SYS;
922
923 newXSproto ("$PACKAGE\::find", find, file, "\$");
924 newXSproto ("$PACKAGE\::list", list, file, "");
925 EOF
926
927 # calls
928 for (@static_ext) {
929 s/\.pm$//;
930
931 (my $cname = $_) =~ s/\//__/g;
932 (my $pname = $_) =~ s/\//::/g;
933
934 my $bootstrap = $pname eq "DynaLoader" ? "boot" : "bootstrap";
935
936 print $fh " newXS (\"$pname\::$bootstrap\", boot_$cname, file);\n";
937 }
938
939 print $fh <<EOF;
940 Perl_av_create_and_unshift_one (&PL_preambleav, newSVpv (bootstrap, sizeof (bootstrap) - 1));
941 }
942 EOF
943
944 #############################################################################
945 # optional perl_init/perl_destroy
946
947 if ($APP) {
948 print $fh <<EOF;
949
950 int
951 main (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 }
978 EOF
979 } elsif ($PERL) {
980 print $fh <<EOF;
981
982 int
983 main (int argc, char *argv [])
984 {
985 extern char **environ;
986 int exitstatus;
987
988 PERL_SYS_INIT3 (&argc, &argv, &environ);
989 staticperl = perl_alloc ();
990 perl_construct (staticperl);
991
992 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
993
994 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
995 if (!exitstatus)
996 perl_run (staticperl);
997
998 exitstatus = perl_destruct (staticperl);
999 perl_free (staticperl);
1000 PERL_SYS_TERM ();
1001
1002 return exitstatus;
1003 }
1004 EOF
1005 } else {
1006 print $fh <<EOF;
1007
1008 EXTERN_C void
1009 staticperl_init (void)
1010 {
1011 extern char **environ;
1012 int argc = sizeof (args) / sizeof (args [0]);
1013 char **argv = args;
1014
1015 static char *args[] = {
1016 "staticperl",
1017 "-e",
1018 "0"
1019 };
1020
1021 PERL_SYS_INIT3 (&argc, &argv, &environ);
1022 staticperl = perl_alloc ();
1023 perl_construct (staticperl);
1024 PL_origalen = 1;
1025 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1026 perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
1027
1028 perl_run (staticperl);
1029 }
1030
1031 EXTERN_C void
1032 staticperl_cleanup (void)
1033 {
1034 perl_destruct (staticperl);
1035 perl_free (staticperl);
1036 staticperl = 0;
1037 PERL_SYS_TERM ();
1038 }
1039 EOF
1040 }
1041
1042 print -s "$PREFIX.c", " octets (", (length $data) , " data octets).\n\n"
1043 if $VERBOSE >= 1;
1044
1045 #############################################################################
1046 # libs, cflags
1047
1048 {
1049 print "generating $PREFIX.ccopts... "
1050 if $VERBOSE >= 1;
1051
1052 my $str = "$Config{ccflags} $Config{optimize} $Config{cppflags} -I$Config{archlibexp}/CORE";
1053 $str =~ s/([\(\)])/\\$1/g;
1054
1055 open my $fh, ">$PREFIX.ccopts"
1056 or die "$PREFIX.ccopts: $!";
1057 print $fh $str;
1058
1059 print "$str\n\n"
1060 if $VERBOSE >= 1;
1061 }
1062
1063 {
1064 print "generating $PREFIX.ldopts... ";
1065
1066 my $str = $STATIC ? "-static " : "";
1067
1068 $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}";
1069
1070 my %seen;
1071 $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g);
1072
1073 for (@staticlibs) {
1074 $str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx;
1075 }
1076
1077 $str =~ s/([\(\)])/\\$1/g;
1078
1079 open my $fh, ">$PREFIX.ldopts"
1080 or die "$PREFIX.ldopts: $!";
1081 print $fh $str;
1082
1083 print "$str\n\n"
1084 if $VERBOSE >= 1;
1085 }
1086
1087 if ($PERL or defined $APP) {
1088 $APP = "perl" unless defined $APP;
1089
1090 print "building $APP...\n"
1091 if $VERBOSE >= 1;
1092
1093 system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)";
1094
1095 unlink "$PREFIX.$_"
1096 for qw(ccopts ldopts c h);
1097
1098 print "\n"
1099 if $VERBOSE >= 1;
1100 }
1101