ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/bin/staticperl
Revision: 1.22
Committed: Sun Dec 12 00:38:36 2010 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.21: +11 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/bin/sh
2    
3     #############################################################################
4     # configuration to fill in
5    
6     STATICPERL=~/.staticperl
7 root 1.19 CPAN=http://mirror.netcologne.de/cpan # which mirror to use
8 root 1.1 EMAIL="read the documentation <rtfm@example.org>"
9    
10     # perl build variables
11 root 1.10 PERL_VERSION=5.12.2 # 5.8.9 is also a good choice
12 root 1.8 PERL_CONFIGURE="" # additional Configure arguments
13 root 1.1 PERL_CPPFLAGS="-DPERL_DISABLE_PMC -DPERL_ARENA_SIZE=65536 -D_GNU_SOURCE -DNDEBUG -USITELIB_EXP -USITEARCHEXP -UARCHLIB_EXP"
14     PERL_OPTIMIZE="-Os -ffunction-sections -fdata-sections -finline-limit=8 -ffast-math"
15    
16     ARCH="$(uname -m)"
17    
18     case "$ARCH" in
19     i*86 | x86_64 | amd64 )
20     PERL_OPTIMIZE="$PERL_OPTIMIZE -mpush-args -mno-inline-stringops-dynamically -mno-align-stringops -mno-ieee-fp" # x86/amd64
21     case "$ARCH" in
22     i*86 )
23     PERL_OPTIMIZE="$PERL_OPTIMIZE -fomit-frame-pointer -march=pentium3 -mtune=i386" # x86 only
24     ;;
25     esac
26     ;;
27     esac
28    
29     # -Wl,--gc-sections makes it impossible to check for undefined references
30     # for some reason so we need to patch away the "-no" after Configure and before make :/
31 root 1.21 # --allow-multiple-definition exists to work around uclibc's pthread static linking bug
32     PERL_LDFLAGS="-Wl,--no-gc-sections -Wl,--allow-multiple-definition"
33 root 1.1 PERL_LIBS="-lm -lcrypt" # perl loves to add lotsa crap itself
34    
35     # some configuration options for modules
36     export PERL_MM_USE_DEFAULT=1
37     #export CORO_INTERFACE=p # needed without nptl on x86, due to bugs in linuxthreads - very slow
38     export EV_EXTRA_DEFS='-DEV_FEATURES=4+8+16+64 -DEV_USE_SELECT=0 -DEV_USE_POLL=1 -DEV_USE_EPOLL=1 -DEV_NO_LOOPS -DEV_COMPAT3=0'
39    
40     # which extra modules to install by default from CPAN that are
41     # required by mkbundle
42 root 1.2 STATICPERL_MODULES="common::sense Pod::Strip PPI::XS Pod::Usage"
43    
44     # which extra modules you might want to install
45     EXTRA_MODULES=""
46 root 1.1
47     # overridable functions
48 root 1.11 preconfigure() { : ; }
49 root 1.1 postconfigure() { : ; }
50     postbuild() { : ; }
51     postinstall() { : ; }
52    
53     # now source user config, if any
54 root 1.19 if [ "$STATICPERLRC" ]; then
55     . "$STATICPERLRC"
56     else
57     [ -r /etc/staticperlrc ] && . /etc/staticperlrc
58     [ -r ~/.staticperlrc ] && . ~/.staticperlrc
59     [ -r "$STATICPERL/rc" ] && . "$STATICPERL/rc"
60     fi
61 root 1.1
62     #############################################################################
63     # support
64    
65 root 1.19 MKBUNDLE="${MKBUNDLE:=$STATICPERL/mkbundle}"
66     PERL_PREFIX="${PERL_PREFIX:=$STATICPERL/perl}" # where the perl gets installed
67    
68 root 1.18 unset PERL5OPT PERL5LIB PERLLIB PERL_UNICODE PERLIO_DEBUG
69     export LC_ALL=C # just to be on the safe side
70    
71 root 1.1 # set version in a way that Makefile.PL can extract
72     VERSION=VERSION; eval \
73 root 1.21 $VERSION=0.911
74 root 1.1
75     BZ2=bz2
76     BZIP2=bzip2
77    
78     fatal() {
79     printf -- "\nFATAL: %s\n\n" "$*" >&2
80     exit 1
81     }
82    
83     verbose() {
84     printf -- "%s\n" "$*"
85     }
86    
87     verblock() {
88     verbose
89     verbose "***"
90     while read line; do
91     verbose "*** $line"
92     done
93     verbose "***"
94     verbose
95     }
96    
97     rcd() {
98     cd "$1" || fatal "$1: cannot enter"
99     }
100    
101     trace() {
102     prefix="$1"; shift
103     # "$@" 2>&1 | while read line; do
104     # echo "$prefix: $line"
105     # done
106     "$@"
107     }
108    
109     trap wait 0
110    
111     #############################################################################
112     # clean
113    
114     distclean() {
115     verblock <<EOF
116 root 1.19 deleting everything installed by this script (rm -rf $STATICPERL)
117 root 1.1 EOF
118    
119     rm -rf "$STATICPERL"
120     }
121    
122     #############################################################################
123     # download/configure/compile/install perl
124    
125     clean() {
126 root 1.11 rm -rf "$STATICPERL/src/perl-$PERL_VERSION"
127 root 1.1 }
128    
129     fetch() {
130     rcd "$STATICPERL"
131    
132     mkdir -p src
133     rcd src
134    
135 root 1.10 if ! [ -d "perl-$PERL_VERSION" ]; then
136     if ! [ -e "perl-$PERL_VERSION.tar.$BZ2" ]; then
137 root 1.1
138 root 1.10 URL="$CPAN/src/5.0/perl-$PERL_VERSION.tar.$BZ2"
139 root 1.1
140     verblock <<EOF
141     downloading perl
142     to manually download perl yourself, place
143 root 1.10 perl-$PERL_VERSION.tar.$BZ2 in $STATICPERL
144 root 1.1 trying $URL
145     EOF
146    
147 root 1.10 rm -f perl-$PERL_VERSION.tar.$BZ2~ # just to be on the safe side
148     wget -O perl-$PERL_VERSION.tar.$BZ2~ "$URL" \
149     || curl >perl-$PERL_VERSION.tar.$BZ2~ "$URL" \
150     || fatal "$URL: unable to download"
151     mv perl-$PERL_VERSION.tar.$BZ2~ perl-$PERL_VERSION.tar.$BZ2
152 root 1.1 fi
153    
154     verblock <<EOF
155     unpacking perl
156     EOF
157    
158     mkdir -p unpack
159 root 1.19 $BZIP2 -d <perl-$PERL_VERSION.tar.bz2 | tar xfC - unpack \
160 root 1.10 || fatal "perl-$PERL_VERSION.tar.bz2: error during unpacking"
161 root 1.12 chmod -R u+w unpack/perl-$PERL_VERSION
162 root 1.10 mv unpack/perl-$PERL_VERSION perl-$PERL_VERSION
163 root 1.1 rmdir -p unpack
164     fi
165     }
166    
167     # similar to GNU-sed -i or perl -pi
168     sedreplace() {
169     sed -e "$1" <"$2" > "$2~" || fatal "error while running sed"
170     mv "$2~" "$2"
171     }
172    
173     configure() {
174     fetch
175    
176 root 1.10 rcd "$STATICPERL/src/perl-$PERL_VERSION"
177 root 1.1
178     [ -e staticstamp.configure ] && return
179    
180     verblock <<EOF
181 root 1.10 configuring $STATICPERL/src/perl-$PERL_VERSION
182 root 1.1 EOF
183    
184 root 1.12 rm -f "$PERL_PREFIX/staticstamp.install"
185 root 1.1
186 root 1.12 make distclean >/dev/null 2>&1
187 root 1.1
188     # I hate them
189     grep -q -- -fstack-protector Configure && \
190     sedreplace 's/-fstack-protector/-fno-stack-protector/g' Configure
191    
192 root 1.11 preconfigure
193    
194 root 1.1 # trace configure \
195     sh Configure -Duselargefiles \
196     -Uuse64bitint \
197     -Dusemymalloc=n \
198     -Uusedl \
199     -Uusethreads \
200     -Uuseithreads \
201     -Uusemultiplicity \
202     -Duseperlio \
203     -Uusesfio \
204     -Uuseshrplib \
205     -Dcppflags="$PERL_CPPFLAGS" \
206     -Dccflags="-g2 -fno-strict-aliasing" \
207     -Doptimize="$PERL_OPTIMIZE" \
208     -Dldflags="$PERL_LDFLAGS" \
209     -Dlibs="$PERL_LIBS" \
210 root 1.10 -Dprefix="$PERL_PREFIX" \
211     -Dbin="$PERL_PREFIX/bin" \
212     -Dprivlib="$PERL_PREFIX/lib" \
213     -Darchlib="$PERL_PREFIX/lib" \
214 root 1.1 -Uusevendorprefix \
215 root 1.10 -Dsitelib="$PERL_PREFIX/lib" \
216     -Dsitearch="$PERL_PREFIX/lib" \
217 root 1.1 -Usitelibexp \
218     -Uman1dir \
219     -Uman3dir \
220     -Usiteman1dir \
221     -Usiteman3dir \
222     -Dpager=/usr/bin/less \
223     -Demail="$EMAIL" \
224     -Dcf_email="$EMAIL" \
225     -Dcf_by="$EMAIL" \
226 root 1.8 $PERL_CONFIGURE \
227 root 1.1 -dE || fatal "Configure failed"
228    
229     sedreplace '
230     s/-Wl,--no-gc-sections/-Wl,--gc-sections/g
231     s/ *-fno-stack-protector */ /g
232     ' config.sh
233    
234     sh Configure -S || fatal "Configure -S failed"
235    
236     postconfigure || fatal "postconfigure hook failed"
237    
238     touch staticstamp.configure
239     }
240    
241     build() {
242     configure
243    
244 root 1.10 rcd "$STATICPERL/src/perl-$PERL_VERSION"
245 root 1.1
246     verblock <<EOF
247 root 1.10 building $STATICPERL/src/perl-$PERL_VERSION
248 root 1.1 EOF
249    
250 root 1.10 rm -f "$PERL_PREFIX/staticstamp.install"
251 root 1.1
252     make || fatal "make: error while building perl"
253    
254     postbuild || fatal "postbuild hook failed"
255     }
256    
257     install() {
258 root 1.13 if ! [ -e "$PERL_PREFIX/staticstamp.install" ]; then
259     build
260 root 1.1
261 root 1.13 verblock <<EOF
262 root 1.10 installing $STATICPERL/src/perl-$PERL_VERSION
263     to $PERL_PREFIX
264 root 1.1 EOF
265    
266 root 1.19 ln -sf "perl/bin/" "$STATICPERL/bin"
267     ln -sf "perl/lib/" "$STATICPERL/lib"
268    
269     ln -sf "$PERL_PREFIX" "$STATICPERL/perl" # might get overwritten
270     rm -rf "$PERL_PREFIX" # by this rm -rf
271    
272 root 1.13 make install || fatal "make install: error while installing"
273 root 1.1
274 root 1.13 rcd "$PERL_PREFIX"
275 root 1.2
276 root 1.13 # create a "make install" replacement for CPAN
277     cat >"$PERL_PREFIX"/bin/cpan-make-install <<EOF
278 root 1.14 make || exit
279    
280 root 1.1 if find blib/arch/auto -type f | grep -q -v .exists; then
281     echo Probably an XS module, rebuilding perl
282 root 1.14 if make perl; then
283     mv perl "$PERL_PREFIX"/bin/perl
284     make -f Makefile.aperl map_clean
285     else
286     make -f Makefile.aperl map_clean
287     exit 1
288     fi
289 root 1.1 fi
290 root 1.14
291     make install UNINST=1
292 root 1.1 EOF
293 root 1.13 chmod 755 "$PERL_PREFIX"/bin/cpan-make-install
294    
295     # trick CPAN into avoiding ~/.cpan completely
296     echo 1 >"$PERL_PREFIX/lib/CPAN/MyConfig.pm"
297 root 1.1
298 root 1.13 "$PERL_PREFIX"/bin/perl -MCPAN -e '
299     CPAN::Shell->o (conf => urllist => push => "'"$CPAN"'");
300     CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
301     CPAN::Shell->o (conf => q<init>);
302     CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
303     CPAN::Shell->o (conf => q<build_dir>, "'"$STATICPERL"'/cpan/build");
304     CPAN::Shell->o (conf => q<prefs_dir>, "'"$STATICPERL"'/cpan/prefs");
305     CPAN::Shell->o (conf => q<histfile> , "'"$STATICPERL"'/cpan/histfile");
306     CPAN::Shell->o (conf => q<keep_source_where>, "'"$STATICPERL"'/cpan/sources");
307     CPAN::Shell->o (conf => q<make_install_make_command>, "'"$PERL_PREFIX"'/bin/cpan-make-install");
308     CPAN::Shell->o (conf => q<prerequisites_policy>, q<follow>);
309     CPAN::Shell->o (conf => q<build_requires_install_policy>, q<no>);
310     CPAN::Shell->o (conf => q<commit>);
311     ' || fatal "error while initialising CPAN"
312 root 1.2
313 root 1.13 touch "$PERL_PREFIX/staticstamp.install"
314     fi
315    
316 root 1.14 if ! [ -e "$PERL_PREFIX/staticstamp.postinstall" ]; then
317 root 1.13 NOCHECK_INSTALL=+
318     instcpan $STATICPERL_MODULES
319     [ $EXTRA_MODULES ] && instcpan $EXTRA_MODULES
320 root 1.1
321 root 1.13 postinstall || fatal "postinstall hook failed"
322 root 1.1
323 root 1.13 touch "$PERL_PREFIX/staticstamp.postinstall"
324     fi
325 root 1.1 }
326    
327     #############################################################################
328     # install a module from CPAN
329    
330     instcpan() {
331     [ $NOCHECK_INSTALL ] || install
332    
333     verblock <<EOF
334     installing modules from CPAN
335     $@
336     EOF
337    
338     for mod in "$@"; do
339 root 1.10 "$PERL_PREFIX"/bin/perl -MCPAN -e 'notest install => "'"$mod"'"' \
340 root 1.1 || fatal "$mod: unable to install from CPAN"
341     done
342     rm -rf "$STATICPERL/build"
343     }
344    
345     #############################################################################
346     # install a module from unpacked sources
347    
348     instsrc() {
349     [ $NOCHECK_INSTALL ] || install
350    
351     verblock <<EOF
352     installing modules from source
353     $@
354     EOF
355    
356     for mod in "$@"; do
357     echo
358     echo $mod
359     (
360     rcd $mod
361     make -f Makefile.aperl map_clean >/dev/null 2>&1
362     make distclean >/dev/null 2>&1
363 root 1.10 "$PERL_PREFIX"/bin/perl Makefile.PL || fatal "$mod: error running Makefile.PL"
364 root 1.1 make || fatal "$mod: error building module"
365 root 1.10 "$PERL_PREFIX"/bin/cpan-make-install || fatal "$mod: error installing module"
366 root 1.1 make distclean >/dev/null 2>&1
367     exit 0
368     ) || exit $?
369     done
370     }
371    
372     #############################################################################
373     # main
374    
375     podusage() {
376     echo
377 root 1.22
378 root 1.10 if [ -e "$PERL_PREFIX/bin/perl" ]; then
379     "$PERL_PREFIX/bin/perl" -MPod::Usage -e \
380 root 1.1 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
381     2>/dev/null && exit
382     fi
383 root 1.22
384 root 1.1 # try whatever perl we can find
385     perl -MPod::Usage -e \
386     'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
387     2>/dev/null && exit
388    
389 root 1.22 fatal "displaying documentation requires a working perl - try '$0 install' to build one in a safe location"
390 root 1.1 }
391    
392     usage() {
393     podusage 0
394     }
395    
396     catmkbundle() {
397     {
398     read dummy
399 root 1.10 echo "#!$PERL_PREFIX/bin/perl"
400 root 1.1 cat
401     } <<'MKBUNDLE'
402     #!/opt/bin/perl
403    
404     #############################################################################
405     # cannot load modules till after the tracer BEGIN block
406    
407     our $VERBOSE = 1;
408     our $STRIP = "pod"; # none, pod or ppi
409 root 1.18 our $UNISTRIP = 1; # always on, try to strip unicore swash data
410 root 1.1 our $PERL = 0;
411 root 1.17 our $APP;
412 root 1.1 our $VERIFY = 0;
413     our $STATIC = 0;
414 root 1.19 our $PACKLIST = 0;
415 root 1.1
416 root 1.18 our $OPTIMISE_SIZE = 0; # optimise for raw file size instead of for compression?
417    
418     our $CACHE;
419     our $CACHEVER = 1; # do not change unless you know what you are doing
420    
421 root 1.1 my $PREFIX = "bundle";
422     my $PACKAGE = "static";
423    
424     my %pm;
425 root 1.8 my %pmbin;
426 root 1.1 my @libs;
427     my @static_ext;
428     my $extralibs;
429 root 1.18 my @staticlibs;
430     my @incext;
431 root 1.1
432     @ARGV
433     or die "$0: use 'staticperl help' (or read the sources of staticperl)\n";
434    
435 root 1.18 # remove "." from @INC - staticperl.sh does it for us, but be on the safe side
436     BEGIN { @INC = grep !/^\.$/, @INC }
437    
438 root 1.1 $|=1;
439    
440     our ($TRACER_W, $TRACER_R);
441    
442 root 1.19 sub find_incdir($) {
443 root 1.1 for (@INC) {
444     next if ref;
445     return $_ if -e "$_/$_[0]";
446     }
447    
448     undef
449     }
450    
451 root 1.19 sub find_inc($) {
452     my $dir = find_incdir $_[0];
453    
454     return "$dir/$_[0]"
455     if defined $dir;
456    
457     undef
458     }
459    
460 root 1.1 BEGIN {
461     # create a loader process to detect @INC requests before we load any modules
462     my ($W_TRACER, $R_TRACER); # used by tracer
463    
464     pipe $R_TRACER, $TRACER_W or die "pipe: $!";
465     pipe $TRACER_R, $W_TRACER or die "pipe: $!";
466    
467     unless (fork) {
468     close $TRACER_R;
469     close $TRACER_W;
470    
471     unshift @INC, sub {
472 root 1.19 my $dir = find_incdir $_[1]
473 root 1.1 or return;
474    
475     syswrite $W_TRACER, "-\n$dir\n$_[1]\n";
476    
477     open my $fh, "<:perlio", "$dir/$_[1]"
478     or warn "ERROR: $dir/$_[1]: $!\n";
479    
480     $fh
481     };
482    
483     while (<$R_TRACER>) {
484     if (/use (.*)$/) {
485     my $mod = $1;
486     eval "require $mod";
487     warn "ERROR: $@ (while loading '$mod')\n"
488     if $@;
489     } elsif (/eval (.*)$/) {
490     my $eval = $1;
491     eval $eval;
492     warn "ERROR: $@ (in '$eval')\n"
493     if $@;
494     }
495 root 1.19
496     syswrite $W_TRACER, "\n";
497 root 1.1 }
498    
499     exit 0;
500     }
501     }
502    
503     # module loading is now safe
504 root 1.7
505 root 1.19 sub trace_parse {
506 root 1.1 for (;;) {
507     <$TRACER_R> =~ /^-$/ or last;
508     my $dir = <$TRACER_R>; chomp $dir;
509     my $name = <$TRACER_R>; chomp $name;
510    
511     $pm{$name} = "$dir/$name";
512 root 1.19
513     print "+ found potential dependency $name\n"
514     if $VERBOSE >= 3;
515 root 1.1 }
516     }
517    
518 root 1.19 sub trace_module {
519     print "tracing module $_[0]\n"
520     if $VERBOSE >= 2;
521    
522     syswrite $TRACER_W, "use $_[0]\n";
523     trace_parse;
524     }
525    
526 root 1.1 sub trace_eval {
527 root 1.19 print "tracing eval $_[0]\n"
528     if $VERBOSE >= 2;
529    
530 root 1.1 syswrite $TRACER_W, "eval $_[0]\n";
531 root 1.19 trace_parse;
532 root 1.1 }
533    
534     sub trace_finish {
535     close $TRACER_W;
536     close $TRACER_R;
537     }
538    
539     #############################################################################
540     # now we can use modules
541    
542     use common::sense;
543 root 1.18 use Config;
544 root 1.1 use Digest::MD5;
545    
546 root 1.18 sub cache($$$) {
547     my ($variant, $src, $filter) = @_;
548    
549 root 1.19 if (length $CACHE and 2048 <= length $src and defined $variant) {
550 root 1.18 my $file = "$CACHE/" . Digest::MD5::md5_hex "$CACHEVER\x00$variant\x00$src";
551    
552     if (open my $fh, "<:perlio", $file) {
553 root 1.19 print "using cache for $file\n"
554     if $VERBOSE >= 7;
555    
556 root 1.18 local $/;
557     return <$fh>;
558     }
559    
560     $src = $filter->($src);
561    
562 root 1.19 print "creating cache entry $file\n"
563     if $VERBOSE >= 8;
564    
565 root 1.18 if (open my $fh, ">:perlio", "$file~") {
566     if ((syswrite $fh, $src) == length $src) {
567     close $fh;
568     rename "$file~", $file;
569     }
570     }
571    
572     return $src;
573     }
574    
575     $filter->($src)
576     }
577    
578 root 1.1 sub dump_string {
579     my ($fh, $data) = @_;
580    
581     if (length $data) {
582     for (
583     my $ofs = 0;
584     length (my $substr = substr $data, $ofs, 80);
585     $ofs += 80
586     ) {
587     $substr =~ s/([^\x20-\x21\x23-\x5b\x5d-\x7e])/sprintf "\\%03o", ord $1/ge;
588     $substr =~ s/\?/\\?/g; # trigraphs...
589     print $fh " \"$substr\"\n";
590     }
591     } else {
592     print $fh " \"\"\n";
593     }
594     }
595    
596 root 1.18 #############################################################################
597    
598     sub glob2re {
599     for (quotemeta $_[0]) {
600     s/\\\*/\x00/g;
601     s/\x00\x00/.*/g;
602     s/\x00/[^\/]*/g;
603     s/\\\?/[^\/]/g;
604    
605     $_ = s/^\\\/// ? "^$_\$" : "(?:^|/)$_\$";
606    
607     s/(?: \[\^\/\] | \. ) \*\$$//x;
608    
609     return qr<$_>s
610     }
611     }
612    
613     our %INCSKIP = (
614     "unicore/TestProp.pl" => undef, # 3.5MB of insanity, apparently just some testcase
615     );
616    
617     sub get_dirtree {
618     my $root = shift;
619    
620     my @tree;
621     my $skip;
622    
623     my $scan; $scan = sub {
624     for (sort do {
625     opendir my $fh, $_[0]
626     or return;
627     readdir $fh
628     }) {
629     next if /^\./;
630    
631     my $path = "$_[0]/$_";
632    
633     if (-d "$path/.") {
634     $scan->($path);
635     } else {
636     next unless /\.(?:pm|pl)$/;
637    
638     $path = substr $path, $skip;
639     push @tree, $path
640     unless exists $INCSKIP{$path};
641     }
642     }
643     };
644    
645     $root =~ s/\/$//;
646     $skip = 1 + length $root;
647     $scan->($root);
648    
649     \@tree
650     }
651    
652     my $inctrees;
653    
654     sub get_inctrees {
655     unless ($inctrees) {
656     my %inctree;
657     $inctree{$_} ||= [$_, get_dirtree $_] # entries in @INC are often duplicates
658     for @INC;
659     $inctrees = [values %inctree];
660     }
661    
662     @$inctrees
663     }
664 root 1.1
665 root 1.18 #############################################################################
666 root 1.1
667     sub cmd_boot {
668     $pm{"//boot"} = $_[0];
669     }
670    
671     sub cmd_add {
672 root 1.3 $_[0] =~ /^(.*)(?:\s+(\S+))$/
673 root 1.1 or die "$_[0]: cannot parse";
674    
675     my $file = $1;
676     my $as = defined $2 ? $2 : "/$1";
677    
678     $pm{$as} = $file;
679 root 1.8 $pmbin{$as} = 1 if $_[1];
680 root 1.1 }
681    
682 root 1.18 sub cmd_staticlib {
683     push @staticlibs, $_
684     for split /\s+/, $_[0];
685     }
686    
687     sub cmd_include {
688     push @incext, [$_[1], glob2re $_[0]];
689     }
690    
691     sub cmd_incglob {
692     my ($pattern) = @_;
693    
694     $pattern = glob2re $pattern;
695    
696     for (get_inctrees) {
697     my ($dir, $files) = @$_;
698    
699     $pm{$_} = "$dir/$_"
700     for grep /$pattern/, @$files;
701     }
702     }
703    
704 root 1.19 sub parse_argv;
705    
706 root 1.1 sub cmd_file {
707     open my $fh, "<", $_[0]
708     or die "$_[0]: $!\n";
709    
710 root 1.19 local @ARGV;
711    
712 root 1.1 while (<$fh>) {
713     chomp;
714 root 1.19 next unless /\S/;
715     next if /^\s*#/;
716    
717     s/^\s*-*/--/;
718 root 1.1 my ($cmd, $args) = split / /, $_, 2;
719    
720 root 1.19 push @ARGV, $cmd;
721     push @ARGV, $args if defined $args;
722 root 1.1 }
723 root 1.19
724     parse_argv;
725 root 1.1 }
726    
727     use Getopt::Long;
728    
729 root 1.19 sub parse_argv {
730     GetOptions
731     "strip=s" => \$STRIP,
732     "cache=s" => \$CACHE, # internal option
733     "verbose|v" => sub { ++$VERBOSE },
734     "quiet|q" => sub { --$VERBOSE },
735     "perl" => \$PERL,
736     "app=s" => \$APP,
737     "eval|e=s" => sub { trace_eval $_[1] },
738     "use|M=s" => sub { trace_module $_[1] },
739     "boot=s" => sub { cmd_boot $_[1] },
740     "add=s" => sub { cmd_add $_[1], 0 },
741     "addbin=s" => sub { cmd_add $_[1], 1 },
742     "incglob=s" => sub { cmd_incglob $_[1] },
743     "include|i=s" => sub { cmd_include $_[1], 1 },
744     "exclude|x=s" => sub { cmd_include $_[1], 0 },
745     "static!" => \$STATIC,
746     "usepacklist!" => \$PACKLIST,
747     "staticlib=s" => sub { cmd_staticlib $_[1] },
748     "<>" => sub { cmd_file $_[0] },
749     or exit 1;
750     }
751    
752 root 1.1 Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case");
753    
754 root 1.19 parse_argv;
755 root 1.1
756 root 1.17 die "cannot specify both --app and --perl\n"
757     if $PERL and defined $APP;
758    
759 root 1.18 # required for @INC loading, unfortunately
760     trace_module "PerlIO::scalar";
761    
762     #############################################################################
763     # include/exclude apply
764    
765     {
766     my %pmi;
767    
768     for (@incext) {
769     my ($inc, $glob) = @$_;
770    
771     my @match = grep /$glob/, keys %pm;
772    
773     if ($inc) {
774     # include
775     @pmi{@match} = delete @pm{@match};
776 root 1.19
777     print "applying include $glob - protected ", (scalar @match), " files.\n"
778     if $VERBOSE >= 5;
779 root 1.18 } else {
780     # exclude
781     delete @pm{@match};
782 root 1.19
783     print "applying exclude $glob - excluded ", (scalar @match), " files.\n"
784     if $VERBOSE >= 5;
785 root 1.18 }
786     }
787    
788     my @pmi = keys %pmi;
789     @pm{@pmi} = delete @pmi{@pmi};
790     }
791    
792     #############################################################################
793     # scan for AutoLoader and static archives
794    
795     sub scan_al {
796     my ($auto, $autodir) = @_;
797    
798     my $ix = "$autodir/autosplit.ix";
799    
800 root 1.19 print "processing autoload index for '$auto'\n"
801     if $VERBOSE >= 6;
802    
803 root 1.18 $pm{"$auto/autosplit.ix"} = $ix;
804    
805     open my $fh, "<:perlio", $ix
806     or die "$ix: $!";
807    
808     my $package;
809    
810     while (<$fh>) {
811     if (/^\s*sub\s+ ([^[:space:];]+) \s* (?:\([^)]*\))? \s*;?\s*$/x) {
812     my $al = "auto/$package/$1.al";
813     my $inc = find_inc $al;
814    
815     defined $inc or die "$al: autoload file not found, but should be there.\n";
816    
817 root 1.19 $pm{$al} = $inc;
818     print "found autoload function '$al'\n"
819     if $VERBOSE >= 6;
820 root 1.18
821     } elsif (/^\s*package\s+([^[:space:];]+)\s*;?\s*$/) {
822     ($package = $1) =~ s/::/\//g;
823     } elsif (/^\s*(?:#|1?\s*;?\s*$)/) {
824     # nop
825     } else {
826 root 1.19 warn "WARNING: $ix: unparsable line, please report: $_";
827 root 1.18 }
828     }
829     }
830    
831     for my $pm (keys %pm) {
832     if ($pm =~ /^(.*)\.pm$/) {
833     my $auto = "auto/$1";
834     my $autodir = find_inc $auto;
835    
836 root 1.19 if (defined $autodir && -d $autodir) {
837 root 1.18 # AutoLoader
838     scan_al $auto, $autodir
839     if -f "$autodir/autosplit.ix";
840    
841     # extralibs.ld
842     if (open my $fh, "<:perlio", "$autodir/extralibs.ld") {
843 root 1.19 print "found extralibs for $pm\n"
844     if $VERBOSE >= 6;
845    
846 root 1.18 local $/;
847     $extralibs .= " " . <$fh>;
848     }
849    
850     $pm =~ /([^\/]+).pm$/ or die "$pm: unable to match last component";
851    
852     my $base = $1;
853    
854     # static ext
855     if (-f "$autodir/$base$Config{_a}") {
856 root 1.19 print "found static archive for $pm\n"
857     if $VERBOSE >= 3;
858    
859 root 1.18 push @libs, "$autodir/$base$Config{_a}";
860     push @static_ext, $pm;
861     }
862    
863     # dynamic object
864     die "ERROR: found shared object - can't link statically ($_)\n"
865     if -f "$autodir/$base.$Config{dlext}";
866 root 1.19
867     if ($PACKLIST && open my $fh, "<:perlio", "$autodir/.packlist") {
868     print "found .packlist for $pm\n"
869     if $VERBOSE >= 3;
870    
871     while (<$fh>) {
872     chomp;
873    
874     # only include certain files (.al, .ix, .pm, .pl)
875     if (/\.(pm|pl|al|ix)$/) {
876     for my $inc (@INC) {
877     # in addition, we only add files that are below some @INC path
878     $inc =~ s/\/*$/\//;
879    
880     if ($inc eq substr $_, 0, length $inc) {
881     my $base = substr $_, length $inc;
882     $pm{$base} = $_;
883    
884     print "+ added .packlist dependency $base\n"
885     if $VERBOSE >= 3;
886     }
887    
888     last;
889     }
890     }
891     }
892     }
893 root 1.18 }
894     }
895     }
896    
897     #############################################################################
898    
899 root 1.19 print "processing bundle files (try more -v power if you get bored waiting here)...\n"
900     if $VERBOSE >= 1;
901    
902 root 1.1 my $data;
903     my @index;
904     my @order = sort {
905     length $a <=> length $b
906     or $a cmp $b
907     } keys %pm;
908    
909     # sorting by name - better compression, but needs more metadata
910     # sorting by length - faster lookup
911     # usually, the metadata overhead beats the loss through compression
912    
913     for my $pm (@order) {
914     my $path = $pm{$pm};
915    
916     128 > length $pm
917 root 1.18 or die "ERROR: $pm: path too long (only 128 octets supported)\n";
918 root 1.1
919     my $src = ref $path
920     ? $$path
921     : do {
922 root 1.7 open my $pm, "<", $path
923 root 1.1 or die "$path: $!";
924    
925     local $/;
926    
927     <$pm>
928     };
929    
930 root 1.18 my $size = length $src;
931    
932 root 1.8 unless ($pmbin{$pm}) { # only do this unless the file is binary
933     if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) {
934     if ($src =~ /^ unimpl \"/m) {
935 root 1.22 print "$pm: skipping (raises runtime error only).\n"
936 root 1.19 if $VERBOSE >= 3;
937 root 1.8 next;
938     }
939 root 1.1 }
940    
941 root 1.19 $src = cache +($STRIP eq "ppi" ? "$UNISTRIP,$OPTIMISE_SIZE" : undef), $src, sub {
942 root 1.18 if ($UNISTRIP && $pm =~ /^unicore\/.*\.pl$/) {
943 root 1.19 print "applying unicore stripping $pm\n"
944     if $VERBOSE >= 6;
945    
946 root 1.18 # special stripping for unicore swashes and properties
947     # much more could be done by going binary
948     $src =~ s{
949     (^return\ <<'END';\n) (.*?\n) (END(?:\n|\Z))
950     }{
951     my ($pre, $data, $post) = ($1, $2, $3);
952    
953     for ($data) {
954     s/^([0-9a-fA-F]+)\t([0-9a-fA-F]+)\t/sprintf "%X\t%X", hex $1, hex $2/gem
955     if $OPTIMISE_SIZE;
956    
957     # s{
958     # ^([0-9a-fA-F]+)\t([0-9a-fA-F]*)\t
959     # }{
960     # # ww - smaller filesize, UU - compress better
961     # pack "C0UU",
962     # hex $1,
963     # length $2 ? (hex $2) - (hex $1) : 0
964     # }gemx;
965 root 1.1
966 root 1.18 s/#.*\n/\n/mg;
967     s/\s+\n/\n/mg;
968     }
969 root 1.8
970 root 1.18 "$pre$data$post"
971     }smex;
972 root 1.1 }
973    
974 root 1.18 if ($STRIP =~ /ppi/i) {
975     require PPI;
976    
977     if (my $ppi = PPI::Document->new (\$src)) {
978     $ppi->prune ("PPI::Token::Comment");
979     $ppi->prune ("PPI::Token::Pod");
980    
981     # prune END stuff
982     for (my $last = $ppi->last_element; $last; ) {
983     my $prev = $last->previous_token;
984    
985     if ($last->isa (PPI::Token::Whitespace::)) {
986     $last->delete;
987     } elsif ($last->isa (PPI::Statement::End::)) {
988     $last->delete;
989     last;
990     } elsif ($last->isa (PPI::Token::Pod::)) {
991     $last->delete;
992     } else {
993     last;
994     }
995    
996     $last = $prev;
997     }
998    
999     # prune some but not all insignificant whitespace
1000     for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) {
1001     my $prev = $ws->previous_token;
1002     my $next = $ws->next_token;
1003    
1004     if (!$prev || !$next) {
1005     $ws->delete;
1006     } else {
1007     if (
1008     $next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float
1009     or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/
1010     or $prev->isa (PPI::Token::Structure::)
1011     or ($OPTIMISE_SIZE &&
1012     ($prev->isa (PPI::Token::Word::)
1013     && (PPI::Token::Symbol:: eq ref $next
1014     || $next->isa (PPI::Structure::Block::)
1015     || $next->isa (PPI::Structure::List::)
1016     || $next->isa (PPI::Structure::Condition::)))
1017     )
1018     ) {
1019     $ws->delete;
1020     } elsif ($prev->isa (PPI::Token::Whitespace::)) {
1021     $ws->{content} = ' ';
1022     $prev->delete;
1023     } else {
1024     $ws->{content} = ' ';
1025     }
1026     }
1027     }
1028 root 1.1
1029 root 1.18 # prune whitespace around blocks
1030     if ($OPTIMISE_SIZE) {
1031     # these usually decrease size, but decrease compressability more
1032     for my $struct (PPI::Structure::Block::, PPI::Structure::Condition::) {
1033     for my $node (@{ $ppi->find ($struct) }) {
1034     my $n1 = $node->first_token;
1035     my $n2 = $n1->previous_token;
1036     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
1037     $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
1038     my $n1 = $node->last_token;
1039     my $n2 = $n1->next_token;
1040     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
1041     $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
1042     }
1043     }
1044    
1045     for my $node (@{ $ppi->find (PPI::Structure::List::) }) {
1046     my $n1 = $node->first_token;
1047     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
1048     my $n1 = $node->last_token;
1049     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
1050     }
1051 root 1.8 }
1052 root 1.1
1053 root 1.18 # reformat qw() lists which often have lots of whitespace
1054     for my $node (@{ $ppi->find (PPI::Token::QuoteLike::Words::) }) {
1055     if ($node->{content} =~ /^qw(.)(.*)(.)$/s) {
1056     my ($a, $qw, $b) = ($1, $2, $3);
1057     $qw =~ s/^\s+//;
1058     $qw =~ s/\s+$//;
1059     $qw =~ s/\s+/ /g;
1060     $node->{content} = "qw$a$qw$b";
1061     }
1062 root 1.8 }
1063 root 1.18
1064     $src = $ppi->serialize;
1065     } else {
1066     warn "WARNING: $pm{$pm}: PPI failed to parse this file\n";
1067 root 1.8 }
1068 root 1.18 } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses it's own pod
1069     require Pod::Strip;
1070 root 1.8
1071 root 1.18 my $stripper = Pod::Strip->new;
1072    
1073     my $out;
1074     $stripper->output_string (\$out);
1075     $stripper->parse_string_document ($src)
1076     or die;
1077     $src = $out;
1078 root 1.1 }
1079    
1080 root 1.18 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
1081     if (open my $fh, "-|") {
1082     <$fh>;
1083     } else {
1084     eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
1085     exit 0;
1086 root 1.8 }
1087 root 1.1 }
1088 root 1.8
1089 root 1.18 $src
1090     };
1091 root 1.1
1092 root 1.8 # if ($pm eq "Opcode.pm") {
1093     # open my $fh, ">x" or die; print $fh $src;#d#
1094     # exit 1;
1095     # }
1096 root 1.1 }
1097    
1098 root 1.19 print "adding $pm (original size $size, stored size ", length $src, ")\n"
1099 root 1.1 if $VERBOSE >= 2;
1100    
1101     push @index, ((length $pm) << 25) | length $data;
1102     $data .= $pm . $src;
1103     }
1104    
1105     length $data < 2**25
1106 root 1.19 or die "ERROR: bundle too large (only 32MB supported)\n";
1107 root 1.1
1108     my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16;
1109    
1110     #############################################################################
1111     # output
1112    
1113 root 1.19 print "generating $PREFIX.h... "
1114     if $VERBOSE >= 1;
1115 root 1.1
1116     {
1117     open my $fh, ">", "$PREFIX.h"
1118     or die "$PREFIX.h: $!\n";
1119    
1120     print $fh <<EOF;
1121     /* do not edit, automatically created by mkstaticbundle */
1122 root 1.8
1123 root 1.1 #include <EXTERN.h>
1124     #include <perl.h>
1125     #include <XSUB.h>
1126    
1127     /* public API */
1128     EXTERN_C PerlInterpreter *staticperl;
1129 root 1.7 EXTERN_C void staticperl_xs_init (pTHX);
1130 root 1.1 EXTERN_C void staticperl_init (void);
1131     EXTERN_C void staticperl_cleanup (void);
1132 root 1.8
1133 root 1.1 EOF
1134     }
1135    
1136 root 1.19 print "\n"
1137     if $VERBOSE >= 1;
1138 root 1.1
1139     #############################################################################
1140     # output
1141    
1142 root 1.19 print "generating $PREFIX.c... "
1143     if $VERBOSE >= 1;
1144 root 1.1
1145     open my $fh, ">", "$PREFIX.c"
1146     or die "$PREFIX.c: $!\n";
1147    
1148     print $fh <<EOF;
1149     /* do not edit, automatically created by mkstaticbundle */
1150    
1151     #include "bundle.h"
1152    
1153     /* public API */
1154     PerlInterpreter *staticperl;
1155    
1156     EOF
1157    
1158     #############################################################################
1159     # bundle data
1160    
1161     my $count = @index;
1162    
1163     print $fh <<EOF;
1164     #include "bundle.h"
1165    
1166     /* bundle data */
1167    
1168     static const U32 $varpfx\_count = $count;
1169     static const U32 $varpfx\_index [$count + 1] = {
1170     EOF
1171    
1172     my $col;
1173     for (@index) {
1174     printf $fh "0x%08x,", $_;
1175     print $fh "\n" unless ++$col % 10;
1176    
1177     }
1178     printf $fh "0x%08x\n};\n", (length $data);
1179    
1180     print $fh "static const char $varpfx\_data [] =\n";
1181     dump_string $fh, $data;
1182    
1183 root 1.19 print $fh ";\n\n";
1184 root 1.1
1185     #############################################################################
1186     # bootstrap
1187    
1188     # boot file for staticperl
1189     # this file will be eval'ed at initialisation time
1190    
1191     my $bootstrap = '
1192     BEGIN {
1193     package ' . $PACKAGE . ';
1194    
1195     PerlIO::scalar->bootstrap;
1196    
1197     @INC = sub {
1198     my $data = find "$_[1]"
1199     or return;
1200    
1201     $INC{$_[1]} = $_[1];
1202    
1203     open my $fh, "<", \$data;
1204     $fh
1205     };
1206     }
1207     ';
1208    
1209     $bootstrap .= "require '//boot';"
1210     if exists $pm{"//boot"};
1211    
1212     $bootstrap =~ s/\s+/ /g;
1213     $bootstrap =~ s/(\W) /$1/g;
1214     $bootstrap =~ s/ (\W)/$1/g;
1215    
1216     print $fh "const char bootstrap [] = ";
1217     dump_string $fh, $bootstrap;
1218     print $fh ";\n\n";
1219    
1220     print $fh <<EOF;
1221     /* search all bundles for the given file, using binary search */
1222     XS(find)
1223     {
1224     dXSARGS;
1225    
1226     if (items != 1)
1227     Perl_croak (aTHX_ "Usage: $PACKAGE\::find (\$path)");
1228    
1229     {
1230     STRLEN namelen;
1231     char *name = SvPV (ST (0), namelen);
1232     SV *res = 0;
1233    
1234     int l = 0, r = $varpfx\_count;
1235    
1236     while (l <= r)
1237     {
1238     int m = (l + r) >> 1;
1239     U32 idx = $varpfx\_index [m];
1240     int comp = namelen - (idx >> 25);
1241    
1242     if (!comp)
1243     {
1244     int ofs = idx & 0x1FFFFFFU;
1245     comp = memcmp (name, $varpfx\_data + ofs, namelen);
1246    
1247     if (!comp)
1248     {
1249     /* found */
1250     int ofs2 = $varpfx\_index [m + 1] & 0x1FFFFFFU;
1251    
1252     ofs += namelen;
1253     res = newSVpvn ($varpfx\_data + ofs, ofs2 - ofs);
1254     goto found;
1255     }
1256     }
1257    
1258     if (comp < 0)
1259     r = m - 1;
1260     else
1261     l = m + 1;
1262     }
1263    
1264     XSRETURN (0);
1265    
1266     found:
1267     ST (0) = res;
1268     sv_2mortal (ST (0));
1269     }
1270    
1271     XSRETURN (1);
1272     }
1273    
1274     /* list all files in the bundle */
1275     XS(list)
1276     {
1277     dXSARGS;
1278    
1279     if (items != 0)
1280     Perl_croak (aTHX_ "Usage: $PACKAGE\::list");
1281    
1282     {
1283     int i;
1284    
1285     EXTEND (SP, $varpfx\_count);
1286    
1287     for (i = 0; i < $varpfx\_count; ++i)
1288     {
1289     U32 idx = $varpfx\_index [i];
1290    
1291     PUSHs (newSVpvn ($varpfx\_data + (idx & 0x1FFFFFFU), idx >> 25));
1292     }
1293     }
1294    
1295     XSRETURN ($varpfx\_count);
1296     }
1297    
1298     EOF
1299    
1300     #############################################################################
1301     # xs_init
1302    
1303     print $fh <<EOF;
1304 root 1.7 void
1305     staticperl_xs_init (pTHX)
1306 root 1.1 {
1307     EOF
1308    
1309     @static_ext = ("DynaLoader", sort @static_ext);
1310    
1311     # prototypes
1312     for (@static_ext) {
1313     s/\.pm$//;
1314     (my $cname = $_) =~ s/\//__/g;
1315     print $fh " EXTERN_C void boot_$cname (pTHX_ CV* cv);\n";
1316     }
1317    
1318     print $fh <<EOF;
1319     char *file = __FILE__;
1320     dXSUB_SYS;
1321    
1322     newXSproto ("$PACKAGE\::find", find, file, "\$");
1323     newXSproto ("$PACKAGE\::list", list, file, "");
1324     EOF
1325    
1326     # calls
1327     for (@static_ext) {
1328     s/\.pm$//;
1329    
1330     (my $cname = $_) =~ s/\//__/g;
1331     (my $pname = $_) =~ s/\//::/g;
1332    
1333     my $bootstrap = $pname eq "DynaLoader" ? "boot" : "bootstrap";
1334    
1335     print $fh " newXS (\"$pname\::$bootstrap\", boot_$cname, file);\n";
1336     }
1337    
1338     print $fh <<EOF;
1339     Perl_av_create_and_unshift_one (&PL_preambleav, newSVpv (bootstrap, sizeof (bootstrap) - 1));
1340     }
1341     EOF
1342    
1343     #############################################################################
1344     # optional perl_init/perl_destroy
1345    
1346 root 1.17 if ($APP) {
1347     print $fh <<EOF;
1348    
1349     int
1350     main (int argc, char *argv [])
1351     {
1352     extern char **environ;
1353     int exitstatus;
1354    
1355     static char *args[] = {
1356     "staticperl",
1357     "-e",
1358     "0"
1359     };
1360    
1361     PERL_SYS_INIT3 (&argc, &argv, &environ);
1362     staticperl = perl_alloc ();
1363     perl_construct (staticperl);
1364    
1365     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1366    
1367     exitstatus = perl_parse (staticperl, staticperl_xs_init, sizeof (args) / sizeof (*args), args, environ);
1368     if (!exitstatus)
1369     perl_run (staticperl);
1370    
1371     exitstatus = perl_destruct (staticperl);
1372     perl_free (staticperl);
1373     PERL_SYS_TERM ();
1374    
1375     return exitstatus;
1376     }
1377     EOF
1378     } elsif ($PERL) {
1379 root 1.1 print $fh <<EOF;
1380    
1381     int
1382     main (int argc, char *argv [])
1383     {
1384     extern char **environ;
1385     int exitstatus;
1386    
1387     PERL_SYS_INIT3 (&argc, &argv, &environ);
1388     staticperl = perl_alloc ();
1389     perl_construct (staticperl);
1390    
1391     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1392    
1393 root 1.7 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
1394 root 1.1 if (!exitstatus)
1395     perl_run (staticperl);
1396    
1397     exitstatus = perl_destruct (staticperl);
1398     perl_free (staticperl);
1399     PERL_SYS_TERM ();
1400    
1401     return exitstatus;
1402     }
1403     EOF
1404     } else {
1405     print $fh <<EOF;
1406    
1407     EXTERN_C void
1408     staticperl_init (void)
1409     {
1410     extern char **environ;
1411     int argc = sizeof (args) / sizeof (args [0]);
1412     char **argv = args;
1413    
1414 root 1.17 static char *args[] = {
1415     "staticperl",
1416     "-e",
1417     "0"
1418     };
1419    
1420 root 1.1 PERL_SYS_INIT3 (&argc, &argv, &environ);
1421     staticperl = perl_alloc ();
1422     perl_construct (staticperl);
1423     PL_origalen = 1;
1424     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1425 root 1.7 perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
1426 root 1.1
1427     perl_run (staticperl);
1428     }
1429    
1430     EXTERN_C void
1431     staticperl_cleanup (void)
1432     {
1433     perl_destruct (staticperl);
1434     perl_free (staticperl);
1435     staticperl = 0;
1436     PERL_SYS_TERM ();
1437     }
1438     EOF
1439     }
1440    
1441 root 1.19 print -s "$PREFIX.c", " octets (", (length $data) , " data octets).\n\n"
1442     if $VERBOSE >= 1;
1443 root 1.1
1444     #############################################################################
1445     # libs, cflags
1446    
1447     {
1448 root 1.19 print "generating $PREFIX.ccopts... "
1449     if $VERBOSE >= 1;
1450 root 1.1
1451     my $str = "$Config{ccflags} $Config{optimize} $Config{cppflags} -I$Config{archlibexp}/CORE";
1452     $str =~ s/([\(\)])/\\$1/g;
1453    
1454     open my $fh, ">$PREFIX.ccopts"
1455     or die "$PREFIX.ccopts: $!";
1456     print $fh $str;
1457 root 1.19
1458     print "$str\n\n"
1459     if $VERBOSE >= 1;
1460 root 1.1 }
1461    
1462     {
1463     print "generating $PREFIX.ldopts... ";
1464    
1465 root 1.18 my $str = $STATIC ? "-static " : "";
1466 root 1.1
1467     $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}";
1468    
1469     my %seen;
1470     $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g);
1471    
1472 root 1.18 for (@staticlibs) {
1473     $str =~ s/(^|\s) (-l\Q$_\E) ($|\s)/$1-Wl,-Bstatic $2 -Wl,-Bdynamic$3/gx;
1474     }
1475    
1476 root 1.1 $str =~ s/([\(\)])/\\$1/g;
1477    
1478     open my $fh, ">$PREFIX.ldopts"
1479     or die "$PREFIX.ldopts: $!";
1480     print $fh $str;
1481 root 1.19
1482     print "$str\n\n"
1483     if $VERBOSE >= 1;
1484 root 1.1 }
1485    
1486 root 1.17 if ($PERL or defined $APP) {
1487     $APP = "perl" unless defined $APP;
1488    
1489 root 1.19 print "building $APP...\n"
1490     if $VERBOSE >= 1;
1491 root 1.17
1492     system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)";
1493 root 1.1
1494 root 1.19 unlink "$PREFIX.$_"
1495     for qw(ccopts ldopts c h);
1496 root 1.18
1497 root 1.19 print "\n"
1498     if $VERBOSE >= 1;
1499 root 1.1 }
1500    
1501     MKBUNDLE
1502     }
1503    
1504     bundle() {
1505     catmkbundle >"$MKBUNDLE~" || fatal "$MKBUNDLE~: cannot create"
1506     chmod 755 "$MKBUNDLE~" && mv "$MKBUNDLE~" "$MKBUNDLE"
1507 root 1.18 CACHE="$STATICPERL/cache"
1508     mkdir -p "$CACHE"
1509     "$PERL_PREFIX/bin/perl" -- "$MKBUNDLE" --cache "$CACHE" "$@"
1510 root 1.1 }
1511    
1512     if [ $# -gt 0 ]; then
1513     while [ $# -gt 0 ]; do
1514     mkdir -p "$STATICPERL" || fatal "$STATICPERL: cannot create"
1515 root 1.10 mkdir -p "$PERL_PREFIX" || fatal "$PERL_PREFIX: cannot create"
1516 root 1.1
1517     command="${1#--}"; shift
1518     case "$command" in
1519 root 1.19 version )
1520     echo "staticperl version $VERSION"
1521     ;;
1522 root 1.1 fetch | configure | build | install | clean | distclean)
1523 root 1.10 ( "$command" )
1524 root 1.1 ;;
1525     instsrc )
1526 root 1.10 ( instsrc "$@" )
1527 root 1.1 exit
1528     ;;
1529     instcpan )
1530 root 1.10 ( instcpan "$@" )
1531 root 1.1 exit
1532     ;;
1533     cpan )
1534 root 1.10 ( install )
1535     "$PERL_PREFIX/bin/cpan" "$@"
1536 root 1.1 exit
1537     ;;
1538     mkbundle )
1539 root 1.10 ( install )
1540 root 1.1 bundle "$@"
1541     exit
1542     ;;
1543     mkperl )
1544 root 1.10 ( install )
1545 root 1.1 bundle --perl "$@"
1546     exit
1547     ;;
1548 root 1.17 mkapp )
1549     ( install )
1550     bundle --app "$@"
1551     exit
1552     ;;
1553 root 1.1 help )
1554     podusage 2
1555     ;;
1556     * )
1557     exec 1>&2
1558     echo
1559     echo "Unknown command: $command"
1560     podusage 0
1561     ;;
1562     esac
1563     done
1564     else
1565     usage
1566     fi
1567    
1568     exit 0
1569    
1570     =head1 NAME
1571    
1572 root 1.7 staticperl - perl, libc, 100 modules, all in one 500kb file
1573 root 1.1
1574     =head1 SYNOPSIS
1575    
1576     staticperl help # print the embedded documentation
1577     staticperl fetch # fetch and unpack perl sources
1578     staticperl configure # fetch and then configure perl
1579     staticperl build # configure and then build perl
1580     staticperl install # build and then install perl
1581     staticperl clean # clean most intermediate files (restart at configure)
1582     staticperl distclean # delete everything installed by this script
1583     staticperl cpan # invoke CPAN shell
1584     staticperl instmod path... # install unpacked modules
1585     staticperl instcpan modulename... # install modules from CPAN
1586     staticperl mkbundle <bundle-args...> # see documentation
1587     staticperl mkperl <bundle-args...> # see documentation
1588 root 1.17 staticperl mkapp appname <bundle-args...> # see documentation
1589 root 1.1
1590     Typical Examples:
1591    
1592     staticperl install # fetch, configure, build and install perl
1593     staticperl cpan # run interactive cpan shell
1594     staticperl mkperl -M '"Config_heavy.pl"' # build a perl that supports -V
1595     staticperl mkperl -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI -MURI::http
1596     # build a perl with the above modules linked in
1597 root 1.17 staticperl mkapp myapp --boot mainprog mymodules
1598     # build a binary "myapp" from mainprog and mymodules
1599 root 1.1
1600     =head1 DESCRIPTION
1601    
1602 root 1.18 This script helps you to create single-file perl interpreters
1603     or applications, or embedding a perl interpreter in your
1604     applications. Single-file means that it is fully self-contained - no
1605     separate shared objects, no autoload fragments, no .pm or .pl files are
1606     needed. And when linking statically, you can create (or embed) a single
1607     file that contains perl interpreter, libc, all the modules you need, all
1608     the libraries you need and of course your actual program.
1609 root 1.1
1610 root 1.7 With F<uClibc> and F<upx> on x86, you can create a single 500kb binary
1611     that contains perl and 100 modules such as POSIX, AnyEvent, EV, IO::AIO,
1612     Coro and so on. Or any other choice of modules.
1613 root 1.1
1614 root 1.19 To see how this turns out, you can try out smallperl and bigperl, two
1615     pre-built static and compressed perl binaries with many and even more
1616     modules: just follow the links at L<http://staticperl.schmorp.de/>.
1617    
1618 root 1.4 The created files do not need write access to the file system (like PAR
1619 root 1.1 does). In fact, since this script is in many ways similar to PAR::Packer,
1620     here are the differences:
1621    
1622     =over 4
1623    
1624     =item * The generated executables are much smaller than PAR created ones.
1625    
1626     Shared objects and the perl binary contain a lot of extra info, while
1627     the static nature of F<staticperl> allows the linker to remove all
1628     functionality and meta-info not required by the final executable. Even
1629     extensions statically compiled into perl at build time will only be
1630     present in the final executable when needed.
1631    
1632     In addition, F<staticperl> can strip perl sources much more effectively
1633     than PAR.
1634    
1635     =item * The generated executables start much faster.
1636    
1637     There is no need to unpack files, or even to parse Zip archives (which is
1638     slow and memory-consuming business).
1639    
1640     =item * The generated executables don't need a writable filesystem.
1641    
1642     F<staticperl> loads all required files directly from memory. There is no
1643     need to unpack files into a temporary directory.
1644    
1645 root 1.18 =item * More control over included files, more burden.
1646 root 1.1
1647 root 1.4 PAR tries to be maintenance and hassle-free - it tries to include more
1648 root 1.18 files than necessary to make sure everything works out of the box. It
1649     mostly succeeds at this, but he extra files (such as the unicode database)
1650     can take substantial amounts of memory and file size.
1651 root 1.1
1652     With F<staticperl>, the burden is mostly with the developer - only direct
1653     compile-time dependencies and L<AutoLoader> are handled automatically.
1654     This means the modules to include often need to be tweaked manually.
1655    
1656 root 1.18 All this does not preclude more permissive modes to be implemented in
1657     the future, but right now, you have to resolve state hidden dependencies
1658     manually.
1659    
1660 root 1.1 =item * PAR works out of the box, F<staticperl> does not.
1661    
1662     Maintaining your own custom perl build can be a pain in the ass, and while
1663     F<staticperl> tries to make this easy, it still requires a custom perl
1664     build and possibly fiddling with some modules. PAR is likely to produce
1665     results faster.
1666    
1667 root 1.13 Ok, PAR never has worked for me out of the box, and for some people,
1668     F<staticperl> does work out of the box, as they don't count "fiddling with
1669     module use lists" against it, but nevertheless, F<staticperl> is certainly
1670     a bit more difficult to use.
1671    
1672 root 1.1 =back
1673    
1674     =head1 HOW DOES IT WORK?
1675    
1676     Simple: F<staticperl> downloads, compile and installs a perl version of
1677     your choice in F<~/.staticperl>. You can add extra modules either by
1678     letting F<staticperl> install them for you automatically, or by using CPAN
1679     and doing it interactively. This usually takes 5-10 minutes, depending on
1680 root 1.4 the speed of your computer and your internet connection.
1681 root 1.1
1682     It is possible to do program development at this stage, too.
1683    
1684     Afterwards, you create a list of files and modules you want to include,
1685 root 1.4 and then either build a new perl binary (that acts just like a normal perl
1686 root 1.1 except everything is compiled in), or you create bundle files (basically C
1687     sources you can use to embed all files into your project).
1688    
1689 root 1.18 This step is very fast (a few seconds if PPI is not used for stripping, or
1690     the stripped files are in the cache), and can be tweaked and repeated as
1691     often as necessary.
1692 root 1.1
1693     =head1 THE F<STATICPERL> SCRIPT
1694    
1695     This module installs a script called F<staticperl> into your perl
1696 root 1.22 binary directory. The script is fully self-contained, and can be
1697     used without perl (for example, in an uClibc chroot environment). In
1698     fact, it can be extracted from the C<App::Staticperl> distribution
1699     tarball as F<bin/staticperl>, without any installation. The
1700     newest (possibly alpha) version can also be downloaded from
1701     L<http://staticperl.schmorp.de/staticperl>.
1702 root 1.1
1703     F<staticperl> interprets the first argument as a command to execute,
1704     optionally followed by any parameters.
1705    
1706     There are two command categories: the "phase 1" commands which deal with
1707     installing perl and perl modules, and the "phase 2" commands, which deal
1708     with creating binaries and bundle files.
1709    
1710     =head2 PHASE 1 COMMANDS: INSTALLING PERL
1711    
1712     The most important command is F<install>, which does basically
1713     everything. The default is to download and install perl 5.12.2 and a few
1714     modules required by F<staticperl> itself, but all this can (and should) be
1715     changed - see L<CONFIGURATION>, below.
1716    
1717     The command
1718    
1719     staticperl install
1720    
1721     Is normally all you need: It installs the perl interpreter in
1722     F<~/.staticperl/perl>. It downloads, configures, builds and installs the
1723     perl interpreter if required.
1724    
1725     Most of the following commands simply run one or more steps of this
1726     sequence.
1727    
1728 root 1.4 To force recompilation or reinstallation, you need to run F<staticperl
1729 root 1.1 distclean> first.
1730    
1731     =over 4
1732    
1733 root 1.19 =item F<staticperl version>
1734    
1735     Prints some info about the version of the F<staticperl> script you are using.
1736    
1737 root 1.1 =item F<staticperl fetch>
1738    
1739     Runs only the download and unpack phase, unless this has already happened.
1740    
1741     =item F<staticperl configure>
1742    
1743     Configures the unpacked perl sources, potentially after downloading them first.
1744    
1745     =item F<staticperl build>
1746    
1747     Builds the configured perl sources, potentially after automatically
1748     configuring them.
1749    
1750     =item F<staticperl install>
1751    
1752 root 1.4 Wipes the perl installation directory (usually F<~/.staticperl/perl>) and
1753     installs the perl distribution, potentially after building it first.
1754 root 1.1
1755     =item F<staticperl cpan> [args...]
1756    
1757 root 1.4 Starts an interactive CPAN shell that you can use to install further
1758     modules. Installs the perl first if necessary, but apart from that,
1759 root 1.1 no magic is involved: you could just as well run it manually via
1760     F<~/.staticperl/perl/bin/cpan>.
1761    
1762     Any additional arguments are simply passed to the F<cpan> command.
1763    
1764     =item F<staticperl instcpan> module...
1765    
1766     Tries to install all the modules given and their dependencies, using CPAN.
1767    
1768     Example:
1769    
1770     staticperl instcpan EV AnyEvent::HTTPD Coro
1771    
1772     =item F<staticperl instsrc> directory...
1773    
1774     In the unlikely case that you have unpacked perl modules around and want
1775 root 1.4 to install from these instead of from CPAN, you can do this using this
1776 root 1.1 command by specifying all the directories with modules in them that you
1777     want to have built.
1778    
1779     =item F<staticperl clean>
1780    
1781 root 1.11 Deletes the perl source directory (and potentially cleans up other
1782     intermediate files). This can be used to clean up files only needed for
1783     building perl, without removing the installed perl interpreter, or to
1784     force a re-build from scratch.
1785    
1786     At the moment, it doesn't delete downloaded tarballs.
1787 root 1.1
1788     =item F<staticperl distclean>
1789    
1790     This wipes your complete F<~/.staticperl> directory. Be careful with this,
1791     it nukes your perl download, perl sources, perl distribution and any
1792     installed modules. It is useful if you wish to start over "from scratch"
1793     or when you want to uninstall F<staticperl>.
1794    
1795     =back
1796    
1797     =head2 PHASE 2 COMMANDS: BUILDING PERL BUNDLES
1798    
1799     Building (linking) a new F<perl> binary is handled by a separate
1800     script. To make it easy to use F<staticperl> from a F<chroot>, the script
1801     is embedded into F<staticperl>, which will write it out and call for you
1802     with any arguments you pass:
1803    
1804     staticperl mkbundle mkbundle-args...
1805    
1806     In the oh so unlikely case of something not working here, you
1807 root 1.2 can run the script manually as well (by default it is written to
1808 root 1.1 F<~/.staticperl/mkbundle>).
1809    
1810     F<mkbundle> is a more conventional command and expect the argument
1811 root 1.4 syntax commonly used on UNIX clones. For example, this command builds
1812 root 1.1 a new F<perl> binary and includes F<Config.pm> (for F<perl -V>),
1813     F<AnyEvent::HTTPD>, F<URI> and a custom F<httpd> script (from F<eg/httpd>
1814     in this distribution):
1815    
1816     # first make sure we have perl and the required modules
1817     staticperl instcpan AnyEvent::HTTPD
1818    
1819     # now build the perl
1820     staticperl mkperl -M'"Config_heavy.pl"' -MAnyEvent::Impl::Perl \
1821     -MAnyEvent::HTTPD -MURI::http \
1822     --add 'eg/httpd httpd.pm'
1823    
1824     # finally, invoke it
1825     ./perl -Mhttpd
1826    
1827 root 1.2 As you can see, things are not quite as trivial: the L<Config> module has
1828     a hidden dependency which is not even a perl module (F<Config_heavy.pl>),
1829     L<AnyEvent> needs at least one event loop backend that we have to
1830 root 1.4 specify manually (here L<AnyEvent::Impl::Perl>), and the F<URI> module
1831 root 1.2 (required by L<AnyEvent::HTTPD>) implements various URI schemes as extra
1832     modules - since L<AnyEvent::HTTPD> only needs C<http> URIs, we only need
1833 root 1.4 to include that module. I found out about these dependencies by carefully
1834     watching any error messages about missing modules...
1835 root 1.2
1836 root 1.17 Instead of building a new perl binary, you can also build a standalone
1837     application:
1838    
1839     # build the app
1840     staticperl mkapp app --boot eg/httpd \
1841     -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI::http
1842    
1843     # run it
1844     ./app
1845    
1846 root 1.2 =head3 OPTION PROCESSING
1847    
1848 root 1.4 All options can be given as arguments on the command line (typically
1849     using long (e.g. C<--verbose>) or short option (e.g. C<-v>) style). Since
1850     specifying a lot of modules can make the command line very cumbersome,
1851 root 1.2 you can put all long options into a "bundle specification file" (with or
1852     without C<--> prefix) and specify this bundle file instead.
1853    
1854     For example, the command given earlier could also look like this:
1855    
1856     staticperl mkperl httpd.bundle
1857    
1858     And all options could be in F<httpd.bundle>:
1859 root 1.1
1860 root 1.2 use "Config_heavy.pl"
1861     use AnyEvent::Impl::Perl
1862     use AnyEvent::HTTPD
1863     use URI::http
1864     add eg/httpd httpd.pm
1865    
1866     All options that specify modules or files to be added are processed in the
1867 root 1.4 order given on the command line (that affects the C<--use> and C<--eval>
1868 root 1.2 options at the moment).
1869    
1870 root 1.19 =head3 PACKAGE SELECTION WORKFLOW
1871    
1872     F<staticperl mkbundle> has a number of options to control package
1873     selection. This section describes how they interact with each other. Also,
1874     since I am still a newbie w.r.t. these issues, maybe future versions of
1875     F<staticperl> will change this, so watch out :)
1876    
1877     The idiom "in order" means "in order that they are specified on the
1878     commandline". If you use a bundle specification file, then the options
1879     will be processed as if they were given in place of the bundle file name.
1880    
1881     =over 4
1882    
1883     =item 1. apply all C<--use>, C<--eval>, C<--add>, C<--addbin> and
1884     C<--incglob> options, in order.
1885    
1886     In addition, C<--use> and C<--eval> dependencies will be added when the
1887     options are processed.
1888    
1889     =item 2. apply all C<--include> and C<--exclude> options, in order.
1890    
1891     All this step does is potentially reduce the number of files already
1892     selected or found in phase 1.
1893    
1894     =item 3. find all modules (== F<.pm> files), gather their static archives
1895     (F<.a>) and AutoLoader splitfiles (F<.ix> and F<.al> files), find any
1896     extra libraries they need for linking (F<extralibs.ld>) and optionally
1897     evaluate any F<.packlist> files.
1898    
1899     This step is required to link against XS extensions and also adds files
1900     required for L<AutoLoader> to do it's job.
1901    
1902     =back
1903    
1904     After this, all the files selected for bundling will be read and processed
1905     (stripped), the bundle files will be written, and optionally a new F<perl>
1906     or application binary will be linked.
1907    
1908 root 1.2 =head3 MKBUNDLE OPTIONS
1909    
1910     =over 4
1911    
1912     =item --verbose | -v
1913    
1914     Increases the verbosity level by one (the default is C<1>).
1915    
1916     =item --quiet | -q
1917    
1918     Decreases the verbosity level by one.
1919    
1920     =item --strip none|pod|ppi
1921    
1922     Specify the stripping method applied to reduce the file of the perl
1923     sources included.
1924    
1925     The default is C<pod>, which uses the L<Pod::Strip> module to remove all
1926 root 1.4 pod documentation, which is very fast and reduces file size a lot.
1927 root 1.2
1928     The C<ppi> method uses L<PPI> to parse and condense the perl sources. This
1929 root 1.18 saves a lot more than just L<Pod::Strip>, and is generally safer,
1930     but is also a lot slower (some files take almost a minute to strip -
1931     F<staticperl> maintains a cache of stripped files to speed up subsequent
1932     runs for this reason). Note that this method doesn't optimise for raw file
1933     size, but for best compression (that means that the uncompressed file size
1934     is a bit larger, but the files compress better, e.g. with F<upx>).
1935 root 1.2
1936 root 1.7 Last not least, if you need accurate line numbers in error messages,
1937     or in the unlikely case where C<pod> is too slow, or some module gets
1938     mistreated, you can specify C<none> to not mangle included perl sources in
1939     any way.
1940 root 1.2
1941     =item --perl
1942    
1943     After writing out the bundle files, try to link a new perl interpreter. It
1944     will be called F<perl> and will be left in the current working
1945     directory. The bundle files will be removed.
1946    
1947 root 1.4 This switch is automatically used when F<staticperl> is invoked with the
1948 root 1.2 C<mkperl> command (instead of C<mkbundle>):
1949    
1950     # build a new ./perl with only common::sense in it - very small :)
1951     staticperl mkperl -Mcommon::sense
1952    
1953 root 1.17 =item --app name
1954    
1955     After writing out the bundle files, try to link a new standalone
1956     program. It will be called C<name>, and the bundle files get removed after
1957     linking it.
1958    
1959     The difference to the (mutually exclusive) C<--perl> option is that the
1960     binary created by this option will not try to act as a perl interpreter -
1961     instead it will simply initialise the perl interpreter, clean it up and
1962     exit.
1963    
1964     This switch is automatically used when F<staticperl> is invoked with the
1965     C<mkapp> command (instead of C<mkbundle>):
1966    
1967     To let it do something useful you I<must> add some boot code, e.g. with
1968     the C<--boot> option.
1969    
1970     Example: create a standalone perl binary that will execute F<appfile> when
1971     it is started.
1972    
1973     staticperl mkbundle --app myexe --boot appfile
1974    
1975 root 1.2 =item --use module | -Mmodule
1976    
1977     Include the named module and all direct dependencies. This is done by
1978     C<require>'ing the module in a subprocess and tracing which other modules
1979     and files it actually loads. If the module uses L<AutoLoader>, then all
1980     splitfiles will be included as well.
1981    
1982     Example: include AnyEvent and AnyEvent::Impl::Perl.
1983    
1984     staticperl mkbundle --use AnyEvent --use AnyEvent::Impl::Perl
1985    
1986     Sometimes you want to load old-style "perl libraries" (F<.pl> files), or
1987     maybe other weirdly named files. To do that, you need to quote the name in
1988 root 1.4 single or double quotes. When given on the command line, you probably need
1989 root 1.2 to quote once more to avoid your shell interpreting it. Common cases that
1990     need this are F<Config_heavy.pl> and F<utf8_heavy.pl>.
1991    
1992     Example: include the required files for F<perl -V> to work in all its
1993     glory (F<Config.pm> is included automatically by this).
1994    
1995     # bourne shell
1996     staticperl mkbundle --use '"Config_heavy.pl"'
1997    
1998     # bundle specification file
1999     use "Config_heavy.pl"
2000    
2001     The C<-Mmodule> syntax is included as an alias that might be easier to
2002     remember than C<use>. Or maybe it confuses people. Time will tell. Or
2003     maybe not. Argh.
2004    
2005     =item --eval "perl code" | -e "perl code"
2006    
2007     Sometimes it is easier (or necessary) to specify dependencies using perl
2008     code, or maybe one of the modules you use need a special use statement. In
2009     that case, you can use C<eval> to execute some perl snippet or set some
2010     variables or whatever you need. All files C<require>'d or C<use>'d in the
2011     script are included in the final bundle.
2012    
2013     Keep in mind that F<mkbundle> will only C<require> the modules named
2014     by the C<--use> option, so do not expect the symbols from modules you
2015 root 1.4 C<--use>'d earlier on the command line to be available.
2016 root 1.2
2017     Example: force L<AnyEvent> to detect a backend and therefore include it
2018     in the final bundle.
2019    
2020     staticperl mkbundle --eval 'use AnyEvent; AnyEvent::detect'
2021    
2022     # or like this
2023     staticperl mkbundle -MAnyEvent --eval 'use AnyEvent; AnyEvent::detect'
2024    
2025     Example: use a separate "bootstrap" script that C<use>'s lots of modules
2026     and include this in the final bundle, to be executed automatically.
2027    
2028     staticperl mkbundle --eval 'do "bootstrap"' --boot bootstrap
2029    
2030     =item --boot filename
2031    
2032     Include the given file in the bundle and arrange for it to be executed
2033     (using a C<require>) before anything else when the new perl is
2034     initialised. This can be used to modify C<@INC> or anything else before
2035 root 1.4 the perl interpreter executes scripts given on the command line (or via
2036 root 1.2 C<-e>). This works even in an embedded interpreter.
2037    
2038 root 1.19 =item --usepacklist
2039    
2040     Read F<.packlist> files for each distribution that happens to match a
2041     module name you specified. Sounds weird, and it is, so expect semantics to
2042     change somehow in the future.
2043    
2044     The idea is that most CPAN distributions have a F<.pm> file that matches
2045     the name of the distribution (which is rather reasonable after all).
2046    
2047     If this switch is enabled, then if any of the F<.pm> files that have been
2048     selected match an install distribution, then all F<.pm>, F<.pl>, F<.al>
2049     and F<.ix> files installed by this distribution are also included.
2050    
2051     For example, using this switch, when the L<URI> module is specified, then
2052     all L<URI> submodules that have been installed via the CPAN distribution
2053     are included as well, so you don't have to manually specify them.
2054    
2055 root 1.18 =item --incglob pattern
2056    
2057     This goes through all library directories and tries to match any F<.pm>
2058     and F<.pl> files against the extended glob pattern (see below). If a file
2059     matches, it is added. This switch will automatically detect L<AutoLoader>
2060     files and the required link libraries for XS modules, but it will I<not>
2061     scan the file for dependencies (at the moment).
2062    
2063     This is mainly useful to include "everything":
2064    
2065     --incglob '*'
2066    
2067     Or to include perl libraries, or trees of those, such as the unicode
2068     database files needed by many other modules:
2069    
2070     --incglob '/unicore/**.pl'
2071    
2072     =item --add file | --add "file alias"
2073 root 1.2
2074     Adds the given (perl) file into the bundle (and optionally call it
2075     "alias"). This is useful to include any custom files into the bundle.
2076    
2077     Example: embed the file F<httpd> as F<httpd.pm> when creating the bundle.
2078    
2079     staticperl mkperl --add "httpd httpd.pm"
2080    
2081     It is also a great way to add any custom modules:
2082    
2083     # specification file
2084     add file1 myfiles/file1
2085     add file2 myfiles/file2
2086     add file3 myfiles/file3
2087    
2088 root 1.18 =item --binadd file | --add "file alias"
2089 root 1.8
2090     Just like C<--add>, except that it treats the file as binary and adds it
2091     without any processing.
2092    
2093     You should probably add a C</> prefix to avoid clashing with embedded
2094     perl files (whose paths do not start with C</>), and/or use a special
2095     directory, such as C</res/name>.
2096    
2097     You can later get a copy of these files by calling C<staticperl::find
2098     "alias">.
2099    
2100 root 1.18 =item --include pattern | -i pattern | --exclude pattern | -x pattern
2101    
2102     These two options define an include/exclude filter that is used after all
2103     files selected by the other options have been found. Each include/exclude
2104     is applied to all files found so far - an include makes sure that the
2105     given files will be part of the resulting file set, an exclude will
2106     exclude files. The patterns are "extended glob patterns" (see below).
2107    
2108     For example, to include everything, except C<Devel> modules, but still
2109     include F<Devel::PPPort>, you could use this:
2110    
2111     --incglob '*' -i '/Devel/PPPort.pm' -x '/Devel/**'
2112    
2113 root 1.2 =item --static
2114    
2115     When C<--perl> is also given, link statically instead of dynamically. The
2116     default is to link the new perl interpreter fully dynamic (that means all
2117     perl modules are linked statically, but all external libraries are still
2118     referenced dynamically).
2119    
2120     Keep in mind that Solaris doesn't support static linking at all, and
2121     systems based on GNU libc don't really support it in a usable fashion
2122     either. Try uClibc if you want to create fully statically linked
2123     executables, or try the C<--staticlibs> option to link only some libraries
2124     statically.
2125    
2126 root 1.18 =item --staticlib libname
2127    
2128     When not linking fully statically, this option allows you to link specific
2129     libraries statically. What it does is simply replace all occurances of
2130     C<-llibname> with the GCC-specific C<-Wl,-Bstatic -llibname -Wl,-Bdynamic>
2131     option.
2132    
2133     This will have no effect unless the library is actually linked against,
2134     specifically, C<--staticlib> will not link against the named library
2135     unless it would be linked against anyway.
2136    
2137     Example: link libcrypt statically into the binary.
2138    
2139     staticperl mkperl -MIO::AIO --staticlib crypt
2140    
2141     # ldopts might nwo contain:
2142     # -lm -Wl,-Bstatic -lcrypt -Wl,-Bdynamic -lpthread
2143    
2144 root 1.2 =item any other argument
2145    
2146     Any other argument is interpreted as a bundle specification file, which
2147     supports most long options (without extra quoting), one option per line.
2148    
2149     =back
2150    
2151 root 1.18 =head3 EXTENDED GLOB PATTERNS
2152    
2153     Some options of F<staticperl mkbundle> expect an I<extended glob
2154     pattern>. This is neither a normal shell glob nor a regex, but something
2155     in between. The idea has been copied from rsync, and there are the current
2156     matching rules:
2157    
2158     =over 4
2159    
2160     =item Patterns starting with F</> will be a anchored at the root of the library tree.
2161    
2162     That is, F</unicore> will match the F<unicore> directory in C<@INC>, but
2163     nothing inside, and neither any other file or directory called F<unicore>
2164     anywhere else in the hierarchy.
2165    
2166     =item Patterns not starting with F</> will be anchored at the end of the path.
2167    
2168     That is, F<idna.pl> will match any file called F<idna.pl> anywhere in the
2169     hierarchy, but not any directories of the same name.
2170    
2171     =item A F<*> matches any single component.
2172    
2173     That is, F</unicore/*.pl> would match all F<.pl> files directly inside
2174     C</unicore>, not any deeper level F<.pl> files. Or in other words, F<*>
2175     will not match slashes.
2176    
2177     =item A F<**> matches anything.
2178    
2179     That is, F</unicore/**.pl> would match all F<.pl> files under F</unicore>,
2180     no matter how deeply nested they are inside subdirectories.
2181    
2182     =item A F<?> matches a single character within a component.
2183    
2184     That is, F</Encode/??.pm> matches F</Encode/JP.pm>, but not the
2185     hypothetical F</Encode/J/.pm>, as F<?> does not match F</>.
2186    
2187     =back
2188    
2189     =head2 F<STATICPERL> CONFIGURATION AND HOOKS
2190 root 1.2
2191 root 1.19 During (each) startup, F<staticperl> tries to source some shell files to
2192     allow you to fine-tune/override configuration settings.
2193    
2194     In them you can override shell variables, or define shell functions
2195     ("hooks") to be called at specific phases during installation. For
2196     example, you could define a C<postinstall> hook to install additional
2197     modules from CPAN each time you start from scratch.
2198    
2199     If the env variable C<$STATICPERLRC> is set, then F<staticperl> will try
2200     to source the file named with it only. Otherwise, it tries the following
2201     shell files in order:
2202 root 1.2
2203     /etc/staticperlrc
2204     ~/.staticperlrc
2205     $STATICPERL/rc
2206    
2207     Note that the last file is erased during F<staticperl distclean>, so
2208     generally should not be used.
2209    
2210     =head3 CONFIGURATION VARIABLES
2211    
2212     =head4 Variables you I<should> override
2213    
2214     =over 4
2215    
2216     =item C<EMAIL>
2217    
2218     The e-mail address of the person who built this binary. Has no good
2219     default, so should be specified by you.
2220    
2221     =item C<CPAN>
2222    
2223     The URL of the CPAN mirror to use (e.g. L<http://mirror.netcologne.de/cpan/>).
2224    
2225 root 1.6 =item C<EXTRA_MODULES>
2226 root 1.2
2227 root 1.6 Additional modules installed during F<staticperl install>. Here you can
2228     set which modules you want have to installed from CPAN.
2229 root 1.2
2230 root 1.10 Example: I really really need EV, AnyEvent, Coro and AnyEvent::AIO.
2231 root 1.2
2232 root 1.10 EXTRA_MODULES="EV AnyEvent Coro AnyEvent::AIO"
2233 root 1.2
2234 root 1.6 Note that you can also use a C<postinstall> hook to achieve this, and
2235     more.
2236 root 1.2
2237 root 1.10 =back
2238    
2239     =head4 Variables you might I<want> to override
2240    
2241     =over 4
2242    
2243     =item C<STATICPERL>
2244    
2245     The directory where staticperl stores all its files
2246     (default: F<~/.staticperl>).
2247    
2248 root 1.6 =item C<PERL_MM_USE_DEFAULT>, C<EV_EXTRA_DEFS>, ...
2249 root 1.2
2250     Usually set to C<1> to make modules "less inquisitive" during their
2251     installation, you can set any environment variable you want - some modules
2252     (such as L<Coro> or L<EV>) use environment variables for further tweaking.
2253    
2254 root 1.10 =item C<PERL_VERSION>
2255 root 1.6
2256 root 1.10 The perl version to install - default is currently C<5.12.2>, but C<5.8.9>
2257     is also a good choice (5.8.9 is much smaller than 5.12.2, while 5.10.1 is
2258     about as big as 5.12.2).
2259 root 1.2
2260 root 1.10 =item C<PERL_PREFIX>
2261 root 1.2
2262 root 1.6 The prefix where perl gets installed (default: F<$STATICPERL/perl>),
2263     i.e. where the F<bin> and F<lib> subdirectories will end up.
2264 root 1.2
2265 root 1.8 =item C<PERL_CONFIGURE>
2266    
2267     Additional Configure options - these are simply passed to the perl
2268     Configure script. For example, if you wanted to enable dynamic loading,
2269     you could pass C<-Dusedl>. To enable ithreads (Why would you want that
2270     insanity? Don't! Use L<forks> instead!) you would pass C<-Duseithreads>
2271     and so on.
2272    
2273     More commonly, you would either activate 64 bit integer support
2274     (C<-Duse64bitint>), or disable large files support (-Uuselargefiles), to
2275     reduce filesize further.
2276    
2277 root 1.6 =item C<PERL_CPPFLAGS>, C<PERL_OPTIMIZE>, C<PERL_LDFLAGS>, C<PERL_LIBS>
2278 root 1.2
2279 root 1.6 These flags are passed to perl's F<Configure> script, and are generally
2280     optimised for small size (at the cost of performance). Since they also
2281     contain subtle workarounds around various build issues, changing these
2282     usually requires understanding their default values - best look at the top
2283     of the F<staticperl> script for more info on these.
2284 root 1.2
2285     =back
2286    
2287 root 1.5 =head4 Variables you probably I<do not want> to override
2288 root 1.2
2289     =over 4
2290    
2291     =item C<MKBUNDLE>
2292    
2293     Where F<staticperl> writes the C<mkbundle> command to
2294     (default: F<$STATICPERL/mkbundle>).
2295 root 1.1
2296 root 1.2 =item C<STATICPERL_MODULES>
2297 root 1.1
2298 root 1.2 Additional modules needed by C<mkbundle> - should therefore not be changed
2299     unless you know what you are doing.
2300    
2301     =back
2302    
2303     =head3 OVERRIDABLE HOOKS
2304    
2305     In addition to environment variables, it is possible to provide some
2306     shell functions that are called at specific times. To provide your own
2307 root 1.4 commands, just define the corresponding function.
2308 root 1.2
2309     Example: install extra modules from CPAN and from some directories
2310     at F<staticperl install> time.
2311    
2312     postinstall() {
2313 root 1.5 rm -rf lib/threads* # weg mit Schaden
2314 root 1.2 instcpan IO::AIO EV
2315     instsrc ~/src/AnyEvent
2316     instsrc ~/src/XML-Sablotron-1.0100001
2317 root 1.5 instcpan Anyevent::AIO AnyEvent::HTTPD
2318 root 1.2 }
2319    
2320     =over 4
2321    
2322 root 1.11 =item preconfigure
2323    
2324     Called just before running F<./Configur> in the perl source
2325     directory. Current working directory is the perl source directory.
2326    
2327     This can be used to set any C<PERL_xxx> variables, which might be costly
2328     to compute.
2329    
2330 root 1.2 =item postconfigure
2331    
2332     Called after configuring, but before building perl. Current working
2333     directory is the perl source directory.
2334    
2335 root 1.11 Could be used to tailor/patch config.sh (followed by F<sh Configure -S>)
2336     or do any other modifications.
2337 root 1.2
2338     =item postbuild
2339    
2340     Called after building, but before installing perl. Current working
2341     directory is the perl source directory.
2342    
2343     I have no clue what this could be used for - tell me.
2344    
2345     =item postinstall
2346    
2347     Called after perl and any extra modules have been installed in C<$PREFIX>,
2348     but before setting the "installation O.K." flag.
2349    
2350     The current working directory is C<$PREFIX>, but maybe you should not rely
2351     on that.
2352    
2353     This hook is most useful to customise the installation, by deleting files,
2354     or installing extra modules using the C<instcpan> or C<instsrc> functions.
2355    
2356     The script must return with a zero exit status, or the installation will
2357     fail.
2358 root 1.1
2359 root 1.2 =back
2360 root 1.1
2361 root 1.7 =head1 ANATOMY OF A BUNDLE
2362    
2363     When not building a new perl binary, C<mkbundle> will leave a number of
2364     files in the current working directory, which can be used to embed a perl
2365     interpreter in your program.
2366    
2367     Intimate knowledge of L<perlembed> and preferably some experience with
2368     embedding perl is highly recommended.
2369    
2370     C<mkperl> (or the C<--perl> option) basically does this to link the new
2371     interpreter (it also adds a main program to F<bundle.>):
2372    
2373     $Config{cc} $(cat bundle.ccopts) -o perl bundle.c $(cat bundle.ldopts)
2374    
2375     =over 4
2376    
2377     =item bundle.h
2378    
2379     A header file that contains the prototypes of the few symbols "exported"
2380     by bundle.c, and also exposes the perl headers to the application.
2381    
2382     =over 4
2383    
2384     =item staticperl_init ()
2385    
2386     Initialises the perl interpreter. You can use the normal perl functions
2387     after calling this function, for example, to define extra functions or
2388     to load a .pm file that contains some initialisation code, or the main
2389     program function:
2390    
2391     XS (xsfunction)
2392     {
2393     dXSARGS;
2394    
2395     // now we have items, ST(i) etc.
2396     }
2397    
2398     static void
2399     run_myapp(void)
2400     {
2401     staticperl_init ();
2402     newXSproto ("myapp::xsfunction", xsfunction, __FILE__, "$$;$");
2403     eval_pv ("require myapp::main", 1); // executes "myapp/main.pm"
2404     }
2405    
2406     =item staticperl_xs_init (pTHX)
2407    
2408     Sometimes you need direct control over C<perl_parse> and C<perl_run>, in
2409     which case you do not want to use C<staticperl_init> but call them on your
2410     own.
2411    
2412     Then you need this function - either pass it directly as the C<xs_init>
2413     function to C<perl_parse>, or call it from your own C<xs_init> function.
2414    
2415     =item staticperl_cleanup ()
2416    
2417     In the unlikely case that you want to destroy the perl interpreter, here
2418     is the corresponding function.
2419    
2420     =item PerlInterpreter *staticperl
2421    
2422     The perl interpreter pointer used by staticperl. Not normally so useful,
2423     but there it is.
2424    
2425     =back
2426    
2427     =item bundle.ccopts
2428    
2429     Contains the compiler options required to compile at least F<bundle.c> and
2430     any file that includes F<bundle.h> - you should probably use it in your
2431     C<CFLAGS>.
2432    
2433     =item bundle.ldopts
2434    
2435     The linker options needed to link the final program.
2436    
2437     =back
2438    
2439     =head1 RUNTIME FUNCTIONALITY
2440    
2441     Binaries created with C<mkbundle>/C<mkperl> contain extra functions, which
2442     are required to access the bundled perl sources, but might be useful for
2443     other purposes.
2444    
2445     In addition, for the embedded loading of perl files to work, F<staticperl>
2446     overrides the C<@INC> array.
2447    
2448     =over 4
2449    
2450     =item $file = staticperl::find $path
2451    
2452     Returns the data associated with the given C<$path>
2453     (e.g. C<Digest/MD5.pm>, C<auto/POSIX/autosplit.ix>), which is basically
2454     the UNIX path relative to the perl library directory.
2455    
2456     Returns C<undef> if the file isn't embedded.
2457    
2458 root 1.8 =item @paths = staticperl::list
2459 root 1.7
2460     Returns the list of all paths embedded in this binary.
2461    
2462     =back
2463    
2464 root 1.8 =head1 FULLY STATIC BINARIES - BUILDROOT
2465    
2466     To make truly static (Linux-) libraries, you might want to have a look at
2467     buildroot (L<http://buildroot.uclibc.org/>).
2468    
2469     Buildroot is primarily meant to set up a cross-compile environment (which
2470     is not so useful as perl doesn't quite like cross compiles), but it can also compile
2471     a chroot environment where you can use F<staticperl>.
2472    
2473     To do so, download buildroot, and enable "Build options => development
2474     files in target filesystem" and optionally "Build options => gcc
2475     optimization level (optimize for size)". At the time of writing, I had
2476     good experiences with GCC 4.4.x but not GCC 4.5.
2477    
2478     To minimise code size, I used C<-pipe -ffunction-sections -fdata-sections
2479     -finline-limit=8 -fno-builtin-strlen -mtune=i386>. The C<-mtune=i386>
2480     doesn't decrease codesize much, but it makes the file much more
2481     compressible.
2482    
2483     If you don't need Coro or threads, you can go with "linuxthreads.old" (or
2484     no thread support). For Coro, it is highly recommended to switch to a
2485     uClibc newer than 0.9.31 (at the time of this writing, I used the 20101201
2486     snapshot) and enable NPTL, otherwise Coro needs to be configured with the
2487     ultra-slow pthreads backend to work around linuxthreads bugs (it also uses
2488     twice the address space needed for stacks).
2489    
2490     If you use C<linuxthreads.old>, then you should also be aware that
2491     uClibc shares C<errno> between all threads when statically linking. See
2492     L<http://lists.uclibc.org/pipermail/uclibc/2010-June/044157.html> for a
2493     workaround (And L<https://bugs.uclibc.org/2089> for discussion).
2494    
2495 root 1.10 C<ccache> support is also recommended, especially if you want
2496     to play around with buildroot options. Enabling the C<miniperl>
2497     package will probably enable all options required for a successful
2498     perl build. F<staticperl> itself additionally needs either C<wget>
2499     (recommended, for CPAN) or C<curl>.
2500 root 1.8
2501     As for shells, busybox should provide all that is needed, but the default
2502     busybox configuration doesn't include F<comm> which is needed by perl -
2503     either make a custom busybox config, or compile coreutils.
2504    
2505     For the latter route, you might find that bash has some bugs that keep
2506     it from working properly in a chroot - either use dash (and link it to
2507     F</bin/sh> inside the chroot) or link busybox to F</bin/sh>, using it's
2508     built-in ash shell.
2509    
2510     Finally, you need F</dev/null> inside the chroot for many scripts to work
2511     - F<cp /dev/null output/target/dev> or bind-mounting your F</dev> will
2512     both provide this.
2513    
2514     After you have compiled and set up your buildroot target, you can copy
2515     F<staticperl> from the C<App::Staticperl> distribution or from your
2516     perl f<bin> directory (if you installed it) into the F<output/target>
2517     filesystem, chroot inside and run it.
2518    
2519 root 1.18 =head1 RECIPES / SPECIFIC MODULES
2520    
2521     This section contains some common(?) recipes and information about
2522     problems with some common modules or perl constructs that require extra
2523     files to be included.
2524    
2525     =head2 MODULES
2526    
2527     =over 4
2528    
2529     =item utf8
2530    
2531     Some functionality in the utf8 module, such as swash handling (used
2532     for unicode character ranges in regexes) is implemented in the
2533     C<"utf8_heavy.pl"> library:
2534    
2535     -M'"utf8_heavy.pl"'
2536    
2537     Many Unicode properties in turn are defined in separate modules,
2538     such as C<"unicore/Heavy.pl"> and more specific data tables such as
2539     C<"unicore/To/Digit.pl"> or C<"unicore/lib/Perl/Word.pl">. These tables
2540     are big (7MB uncompressed, although F<staticperl> contains special
2541     handling for those files), so including them on demand by your application
2542     only might pay off.
2543    
2544     To simply include the whole unicode database, use:
2545    
2546     --incglob '/unicore/*.pl'
2547    
2548     =item AnyEvent
2549    
2550     AnyEvent needs a backend implementation that it will load in a delayed
2551     fashion. The L<AnyEvent::Impl::Perl> backend is the default choice
2552     for AnyEvent if it can't find anything else, and is usually a safe
2553     fallback. If you plan to use e.g. L<EV> (L<POE>...), then you need to
2554     include the L<AnyEvent::Impl::EV> (L<AnyEvent::Impl::POE>...) backend as
2555     well.
2556    
2557     If you want to handle IRIs or IDNs (L<AnyEvent::Util> punycode and idn
2558     functions), you also need to include C<"AnyEvent/Util/idna.pl"> and
2559     C<"AnyEvent/Util/uts46data.pl">.
2560    
2561 root 1.19 Or you can use C<--usepacklist> and specify C<-MAnyEvent> to include
2562     everything.
2563    
2564 root 1.18 =item Carp
2565    
2566     Carp had (in older versions of perl) a dependency on L<Carp::Heavy>. As of
2567     perl 5.12.2 (maybe earlier), this dependency no longer exists.
2568    
2569     =item Config
2570    
2571     The F<perl -V> switch (as well as many modules) needs L<Config>, which in
2572     turn might need L<"Config_heavy.pl">. Including the latter gives you
2573     both.
2574    
2575     =item Term::ReadLine::Perl
2576    
2577 root 1.19 Also needs L<Term::ReadLine::readline>, or C<--usepacklist>.
2578 root 1.18
2579     =item URI
2580    
2581     URI implements schemes as separate modules - the generic URL scheme is
2582     implemented in L<URI::_generic>, HTTP is implemented in L<URI::http>. If
2583 root 1.19 you need to use any of these schemes, you should include these manually,
2584     or use C<--usepacklist>.
2585 root 1.18
2586     =back
2587    
2588     =head2 RECIPES
2589    
2590     =over 4
2591    
2592     =item Linking everything in
2593    
2594     To link just about everything installed in the perl library into a new
2595     perl, try this:
2596    
2597     staticperl mkperl --strip ppi --incglob '*'
2598    
2599     =item Getting rid of netdb function
2600    
2601     The perl core has lots of netdb functions (C<getnetbyname>, C<getgrent>
2602     and so on) that few applications use. You can avoid compiling them in by
2603     putting the following fragment into a C<preconfigure> hook:
2604    
2605     preconfigure() {
2606     for sym in \
2607     d_getgrnam_r d_endgrent d_endgrent_r d_endhent \
2608     d_endhostent_r d_endnent d_endnetent_r d_endpent \
2609     d_endprotoent_r d_endpwent d_endpwent_r d_endsent \
2610     d_endservent_r d_getgrent d_getgrent_r d_getgrgid_r \
2611     d_getgrnam_r d_gethbyaddr d_gethent d_getsbyport \
2612     d_gethostbyaddr_r d_gethostbyname_r d_gethostent_r \
2613     d_getlogin_r d_getnbyaddr d_getnbyname d_getnent \
2614     d_getnetbyaddr_r d_getnetbyname_r d_getnetent_r \
2615     d_getpent d_getpbyname d_getpbynumber d_getprotobyname_r \
2616     d_getprotobynumber_r d_getprotoent_r d_getpwent \
2617     d_getpwent_r d_getpwnam_r d_getpwuid_r d_getsent \
2618     d_getservbyname_r d_getservbyport_r d_getservent_r \
2619     d_getspnam_r d_getsbyname
2620     # d_gethbyname
2621     do
2622     PERL_CONFIGURE="$PERL_CONFIGURE -U$sym"
2623     done
2624     }
2625    
2626     This mostly gains space when linking staticaly, as the functions will
2627 root 1.22 likely not be linked in. The gain for dynamically-linked binaries is
2628 root 1.18 smaller.
2629    
2630     Also, this leaves C<gethostbyname> in - not only is it actually used
2631     often, the L<Socket> module also exposes it, so leaving it out usually
2632     gains little. Why Socket exposes a C function that is in the core already
2633     is anybody's guess.
2634    
2635     =back
2636    
2637 root 1.1 =head1 AUTHOR
2638    
2639     Marc Lehmann <schmorp@schmorp.de>
2640     http://software.schmorp.de/pkg/staticperl.html
2641