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