ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/mkbundle
Revision: 1.40
Committed: Thu Aug 3 03:06:47 2023 UTC (3 years, 1 month ago) by root
Branch: MAIN
Changes since 1.39: +3 -6 lines
Log Message:
*** empty log message ***

File Contents

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