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