ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/mkbundle
Revision: 1.21
Committed: Thu Feb 10 11:46:02 2011 UTC (15 years, 7 months ago) by root
Branch: MAIN
Changes since 1.20: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

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