ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/mkbundle
Revision: 1.32
Committed: Mon Jul 18 07:34:48 2011 UTC (12 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-1_4, rel-1_42, rel-1_41
Changes since 1.31: +2 -2 lines
Log Message:
1.4

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