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