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