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