ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/bin/staticperl
Revision: 1.17
Committed: Wed Dec 8 22:27:35 2010 UTC (15 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-0_9
Changes since 1.16: +95 -10 lines
Log Message:
0.9

File Contents

# User Rev Content
1 root 1.1 #!/bin/sh
2    
3     #############################################################################
4     # configuration to fill in
5    
6     STATICPERL=~/.staticperl
7     CPAN=http://mirror.netcologne.de/cpan/ # which mirror to use
8     EMAIL="read the documentation <rtfm@example.org>"
9    
10     MKBUNDLE="$STATICPERL/mkbundle"
11    
12     # perl build variables
13 root 1.10 PERL_PREFIX="$STATICPERL/perl" # where the perl gets installed
14     PERL_VERSION=5.12.2 # 5.8.9 is also a good choice
15 root 1.8 PERL_CONFIGURE="" # additional Configure arguments
16 root 1.1 PERL_CPPFLAGS="-DPERL_DISABLE_PMC -DPERL_ARENA_SIZE=65536 -D_GNU_SOURCE -DNDEBUG -USITELIB_EXP -USITEARCHEXP -UARCHLIB_EXP"
17     PERL_OPTIMIZE="-Os -ffunction-sections -fdata-sections -finline-limit=8 -ffast-math"
18    
19     ARCH="$(uname -m)"
20    
21     case "$ARCH" in
22     i*86 | x86_64 | amd64 )
23     PERL_OPTIMIZE="$PERL_OPTIMIZE -mpush-args -mno-inline-stringops-dynamically -mno-align-stringops -mno-ieee-fp" # x86/amd64
24     case "$ARCH" in
25     i*86 )
26     PERL_OPTIMIZE="$PERL_OPTIMIZE -fomit-frame-pointer -march=pentium3 -mtune=i386" # x86 only
27     ;;
28     esac
29     ;;
30     esac
31    
32     # -Wl,--gc-sections makes it impossible to check for undefined references
33     # for some reason so we need to patch away the "-no" after Configure and before make :/
34     # -z muldefs is to work around uclibc's pthread static linking bug
35     PERL_LDFLAGS="-Wl,--no-gc-sections -z muldefs"
36     PERL_LIBS="-lm -lcrypt" # perl loves to add lotsa crap itself
37    
38     # some configuration options for modules
39     export PERL_MM_USE_DEFAULT=1
40     #export CORO_INTERFACE=p # needed without nptl on x86, due to bugs in linuxthreads - very slow
41     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'
42    
43     # which extra modules to install by default from CPAN that are
44     # required by mkbundle
45 root 1.2 STATICPERL_MODULES="common::sense Pod::Strip PPI::XS Pod::Usage"
46    
47     # which extra modules you might want to install
48     EXTRA_MODULES=""
49 root 1.1
50     # overridable functions
51 root 1.11 preconfigure() { : ; }
52 root 1.1 postconfigure() { : ; }
53     postbuild() { : ; }
54     postinstall() { : ; }
55    
56     # now source user config, if any
57     [ -r /etc/staticperlrc ] && . /etc/staticperlrc
58     [ -r ~/.staticperlrc ] && . ~/.staticperlrc
59     [ -r "$STATICPERL/rc" ] && . "$STATICPERL/rc"
60    
61     #############################################################################
62     # support
63    
64     # set version in a way that Makefile.PL can extract
65     VERSION=VERSION; eval \
66 root 1.17 $VERSION=0.9
67 root 1.1
68     BZ2=bz2
69     BZIP2=bzip2
70    
71     fatal() {
72     printf -- "\nFATAL: %s\n\n" "$*" >&2
73     exit 1
74     }
75    
76     verbose() {
77     printf -- "%s\n" "$*"
78     }
79    
80     verblock() {
81     verbose
82     verbose "***"
83     while read line; do
84     verbose "*** $line"
85     done
86     verbose "***"
87     verbose
88     }
89    
90     rcd() {
91     cd "$1" || fatal "$1: cannot enter"
92     }
93    
94     trace() {
95     prefix="$1"; shift
96     # "$@" 2>&1 | while read line; do
97     # echo "$prefix: $line"
98     # done
99     "$@"
100     }
101    
102     trap wait 0
103    
104     #############################################################################
105     # clean
106    
107     distclean() {
108     verblock <<EOF
109     deleting everything installed by this script
110     EOF
111    
112     rm -rf "$STATICPERL"
113     }
114    
115     #############################################################################
116     # download/configure/compile/install perl
117    
118     clean() {
119 root 1.11 rm -rf "$STATICPERL/src/perl-$PERL_VERSION"
120 root 1.1 }
121    
122     fetch() {
123     rcd "$STATICPERL"
124    
125     mkdir -p src
126     rcd src
127    
128 root 1.10 if ! [ -d "perl-$PERL_VERSION" ]; then
129     if ! [ -e "perl-$PERL_VERSION.tar.$BZ2" ]; then
130 root 1.1
131 root 1.10 URL="$CPAN/src/5.0/perl-$PERL_VERSION.tar.$BZ2"
132 root 1.1
133     verblock <<EOF
134     downloading perl
135     to manually download perl yourself, place
136 root 1.10 perl-$PERL_VERSION.tar.$BZ2 in $STATICPERL
137 root 1.1 trying $URL
138     EOF
139    
140 root 1.10 rm -f perl-$PERL_VERSION.tar.$BZ2~ # just to be on the safe side
141     wget -O perl-$PERL_VERSION.tar.$BZ2~ "$URL" \
142     || curl >perl-$PERL_VERSION.tar.$BZ2~ "$URL" \
143     || fatal "$URL: unable to download"
144     mv perl-$PERL_VERSION.tar.$BZ2~ perl-$PERL_VERSION.tar.$BZ2
145 root 1.1 fi
146    
147     verblock <<EOF
148     unpacking perl
149     EOF
150    
151     mkdir -p unpack
152 root 1.12 $BZIP2 -d <perl-$PERL_VERSION.tar.bz2 | tar xC unpack \
153 root 1.10 || fatal "perl-$PERL_VERSION.tar.bz2: error during unpacking"
154 root 1.12 chmod -R u+w unpack/perl-$PERL_VERSION
155 root 1.10 mv unpack/perl-$PERL_VERSION perl-$PERL_VERSION
156 root 1.1 rmdir -p unpack
157     fi
158     }
159    
160     # similar to GNU-sed -i or perl -pi
161     sedreplace() {
162     sed -e "$1" <"$2" > "$2~" || fatal "error while running sed"
163     mv "$2~" "$2"
164     }
165    
166     configure() {
167     fetch
168    
169 root 1.10 rcd "$STATICPERL/src/perl-$PERL_VERSION"
170 root 1.1
171     [ -e staticstamp.configure ] && return
172    
173     verblock <<EOF
174 root 1.10 configuring $STATICPERL/src/perl-$PERL_VERSION
175 root 1.1 EOF
176    
177 root 1.12 rm -f "$PERL_PREFIX/staticstamp.install"
178 root 1.1
179 root 1.12 make distclean >/dev/null 2>&1
180 root 1.1
181     # I hate them
182     grep -q -- -fstack-protector Configure && \
183     sedreplace 's/-fstack-protector/-fno-stack-protector/g' Configure
184    
185 root 1.11 preconfigure
186    
187 root 1.1 # trace configure \
188     sh Configure -Duselargefiles \
189     -Uuse64bitint \
190     -Dusemymalloc=n \
191     -Uusedl \
192     -Uusethreads \
193     -Uuseithreads \
194     -Uusemultiplicity \
195     -Duseperlio \
196     -Uusesfio \
197     -Uuseshrplib \
198     -Dcppflags="$PERL_CPPFLAGS" \
199     -Dccflags="-g2 -fno-strict-aliasing" \
200     -Doptimize="$PERL_OPTIMIZE" \
201     -Dldflags="$PERL_LDFLAGS" \
202     -Dlibs="$PERL_LIBS" \
203 root 1.10 -Dprefix="$PERL_PREFIX" \
204     -Dbin="$PERL_PREFIX/bin" \
205     -Dprivlib="$PERL_PREFIX/lib" \
206     -Darchlib="$PERL_PREFIX/lib" \
207 root 1.1 -Uusevendorprefix \
208 root 1.10 -Dsitelib="$PERL_PREFIX/lib" \
209     -Dsitearch="$PERL_PREFIX/lib" \
210 root 1.1 -Usitelibexp \
211     -Uman1dir \
212     -Uman3dir \
213     -Usiteman1dir \
214     -Usiteman3dir \
215     -Dpager=/usr/bin/less \
216     -Demail="$EMAIL" \
217     -Dcf_email="$EMAIL" \
218     -Dcf_by="$EMAIL" \
219 root 1.8 $PERL_CONFIGURE \
220 root 1.1 -dE || fatal "Configure failed"
221    
222     sedreplace '
223     s/-Wl,--no-gc-sections/-Wl,--gc-sections/g
224     s/ *-fno-stack-protector */ /g
225     ' config.sh
226    
227     sh Configure -S || fatal "Configure -S failed"
228    
229     postconfigure || fatal "postconfigure hook failed"
230    
231     touch staticstamp.configure
232     }
233    
234     build() {
235     configure
236    
237 root 1.10 rcd "$STATICPERL/src/perl-$PERL_VERSION"
238 root 1.1
239     verblock <<EOF
240 root 1.10 building $STATICPERL/src/perl-$PERL_VERSION
241 root 1.1 EOF
242    
243 root 1.10 rm -f "$PERL_PREFIX/staticstamp.install"
244 root 1.1
245     make || fatal "make: error while building perl"
246    
247     postbuild || fatal "postbuild hook failed"
248     }
249    
250     install() {
251 root 1.13 if ! [ -e "$PERL_PREFIX/staticstamp.install" ]; then
252     build
253 root 1.1
254 root 1.13 verblock <<EOF
255 root 1.10 installing $STATICPERL/src/perl-$PERL_VERSION
256     to $PERL_PREFIX
257 root 1.1 EOF
258    
259 root 1.13 rm -rf "$PERL_PREFIX"
260    
261     make install || fatal "make install: error while installing"
262 root 1.1
263 root 1.13 rcd "$PERL_PREFIX"
264 root 1.2
265 root 1.13 # create a "make install" replacement for CPAN
266     cat >"$PERL_PREFIX"/bin/cpan-make-install <<EOF
267 root 1.14 make || exit
268    
269 root 1.1 if find blib/arch/auto -type f | grep -q -v .exists; then
270     echo Probably an XS module, rebuilding perl
271 root 1.14 if make perl; then
272     mv perl "$PERL_PREFIX"/bin/perl
273     make -f Makefile.aperl map_clean
274     else
275     make -f Makefile.aperl map_clean
276     exit 1
277     fi
278 root 1.1 fi
279 root 1.14
280     make install UNINST=1
281 root 1.1 EOF
282 root 1.13 chmod 755 "$PERL_PREFIX"/bin/cpan-make-install
283    
284     # trick CPAN into avoiding ~/.cpan completely
285     echo 1 >"$PERL_PREFIX/lib/CPAN/MyConfig.pm"
286 root 1.1
287 root 1.13 "$PERL_PREFIX"/bin/perl -MCPAN -e '
288     CPAN::Shell->o (conf => urllist => push => "'"$CPAN"'");
289     CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
290     CPAN::Shell->o (conf => q<init>);
291     CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
292     CPAN::Shell->o (conf => q<build_dir>, "'"$STATICPERL"'/cpan/build");
293     CPAN::Shell->o (conf => q<prefs_dir>, "'"$STATICPERL"'/cpan/prefs");
294     CPAN::Shell->o (conf => q<histfile> , "'"$STATICPERL"'/cpan/histfile");
295     CPAN::Shell->o (conf => q<keep_source_where>, "'"$STATICPERL"'/cpan/sources");
296     CPAN::Shell->o (conf => q<make_install_make_command>, "'"$PERL_PREFIX"'/bin/cpan-make-install");
297     CPAN::Shell->o (conf => q<prerequisites_policy>, q<follow>);
298     CPAN::Shell->o (conf => q<build_requires_install_policy>, q<no>);
299     CPAN::Shell->o (conf => q<commit>);
300     ' || fatal "error while initialising CPAN"
301 root 1.2
302 root 1.13 touch "$PERL_PREFIX/staticstamp.install"
303     fi
304    
305 root 1.14 if ! [ -e "$PERL_PREFIX/staticstamp.postinstall" ]; then
306 root 1.13 NOCHECK_INSTALL=+
307     instcpan $STATICPERL_MODULES
308     [ $EXTRA_MODULES ] && instcpan $EXTRA_MODULES
309 root 1.1
310 root 1.13 postinstall || fatal "postinstall hook failed"
311 root 1.1
312 root 1.13 touch "$PERL_PREFIX/staticstamp.postinstall"
313     fi
314 root 1.1 }
315    
316     #############################################################################
317     # install a module from CPAN
318    
319     instcpan() {
320     [ $NOCHECK_INSTALL ] || install
321    
322     verblock <<EOF
323     installing modules from CPAN
324     $@
325     EOF
326    
327     for mod in "$@"; do
328 root 1.10 "$PERL_PREFIX"/bin/perl -MCPAN -e 'notest install => "'"$mod"'"' \
329 root 1.1 || fatal "$mod: unable to install from CPAN"
330     done
331     rm -rf "$STATICPERL/build"
332     }
333    
334     #############################################################################
335     # install a module from unpacked sources
336    
337     instsrc() {
338     [ $NOCHECK_INSTALL ] || install
339    
340     verblock <<EOF
341     installing modules from source
342     $@
343     EOF
344    
345     for mod in "$@"; do
346     echo
347     echo $mod
348     (
349     rcd $mod
350     make -f Makefile.aperl map_clean >/dev/null 2>&1
351     make distclean >/dev/null 2>&1
352 root 1.10 "$PERL_PREFIX"/bin/perl Makefile.PL || fatal "$mod: error running Makefile.PL"
353 root 1.1 make || fatal "$mod: error building module"
354 root 1.10 "$PERL_PREFIX"/bin/cpan-make-install || fatal "$mod: error installing module"
355 root 1.1 make distclean >/dev/null 2>&1
356     exit 0
357     ) || exit $?
358     done
359     }
360    
361     #############################################################################
362     # main
363    
364     podusage() {
365     echo
366 root 1.10 if [ -e "$PERL_PREFIX/bin/perl" ]; then
367     "$PERL_PREFIX/bin/perl" -MPod::Usage -e \
368 root 1.1 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
369     2>/dev/null && exit
370     fi
371     # try whatever perl we can find
372     perl -MPod::Usage -e \
373     'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
374     2>/dev/null && exit
375    
376     fatal "displaying documentation requires a working perl - try '$0 install' first"
377     }
378    
379     usage() {
380     podusage 0
381     }
382    
383     catmkbundle() {
384     {
385     read dummy
386 root 1.10 echo "#!$PERL_PREFIX/bin/perl"
387 root 1.1 cat
388     } <<'MKBUNDLE'
389     #!/opt/bin/perl
390    
391     #############################################################################
392     # cannot load modules till after the tracer BEGIN block
393    
394     our $VERBOSE = 1;
395     our $STRIP = "pod"; # none, pod or ppi
396     our $PERL = 0;
397 root 1.17 our $APP;
398 root 1.1 our $VERIFY = 0;
399     our $STATIC = 0;
400    
401     my $PREFIX = "bundle";
402     my $PACKAGE = "static";
403    
404     my %pm;
405 root 1.8 my %pmbin;
406 root 1.1 my @libs;
407     my @static_ext;
408     my $extralibs;
409    
410     @ARGV
411     or die "$0: use 'staticperl help' (or read the sources of staticperl)\n";
412    
413     $|=1;
414    
415     our ($TRACER_W, $TRACER_R);
416    
417     sub find_inc($) {
418     for (@INC) {
419     next if ref;
420     return $_ if -e "$_/$_[0]";
421     }
422    
423     undef
424     }
425    
426     BEGIN {
427     # create a loader process to detect @INC requests before we load any modules
428     my ($W_TRACER, $R_TRACER); # used by tracer
429    
430     pipe $R_TRACER, $TRACER_W or die "pipe: $!";
431     pipe $TRACER_R, $W_TRACER or die "pipe: $!";
432    
433     unless (fork) {
434     close $TRACER_R;
435     close $TRACER_W;
436    
437     unshift @INC, sub {
438     my $dir = find_inc $_[1]
439     or return;
440    
441     syswrite $W_TRACER, "-\n$dir\n$_[1]\n";
442    
443     open my $fh, "<:perlio", "$dir/$_[1]"
444     or warn "ERROR: $dir/$_[1]: $!\n";
445    
446     $fh
447     };
448    
449     while (<$R_TRACER>) {
450     if (/use (.*)$/) {
451     my $mod = $1;
452     eval "require $mod";
453     warn "ERROR: $@ (while loading '$mod')\n"
454     if $@;
455     syswrite $W_TRACER, "\n";
456     } elsif (/eval (.*)$/) {
457     my $eval = $1;
458     eval $eval;
459     warn "ERROR: $@ (in '$eval')\n"
460     if $@;
461     }
462     }
463    
464     exit 0;
465     }
466     }
467    
468     # module loading is now safe
469     use Config;
470    
471 root 1.7 sub scan_al {
472     my ($auto, $autodir, $ix) = @_;
473    
474     $pm{"$auto/$ix"} = "$autodir/$ix";
475    
476 root 1.8 open my $fh, "<:perlio", "$autodir/$ix"
477 root 1.7 or die "$autodir/$ix: $!";
478    
479     my $package;
480    
481     while (<$fh>) {
482 root 1.16 if (/^\s*sub\s+ ([^[:space:];]+) \s* (?:\([^)]*\))? \s*;?\s*$/x) {
483 root 1.7 my $al = "auto/$package/$1.al";
484     my $inc = find_inc $al;
485    
486     defined $inc or die "$al: autoload file not found, but should be there.\n";
487    
488     $pm{$al} = "$inc/$al";
489    
490     } elsif (/^\s*package\s+([^[:space:];]+)\s*;?\s*$/) {
491     ($package = $1) =~ s/::/\//g;
492     } elsif (/^\s*(?:#|1?\s*;?\s*$)/) {
493     # nop
494     } else {
495     warn "$autodir/$ix: unparsable line, please report: $_";
496     }
497     }
498     }
499    
500 root 1.1 sub trace_module {
501     syswrite $TRACER_W, "use $_[0]\n";
502    
503     for (;;) {
504     <$TRACER_R> =~ /^-$/ or last;
505     my $dir = <$TRACER_R>; chomp $dir;
506     my $name = <$TRACER_R>; chomp $name;
507    
508     $pm{$name} = "$dir/$name";
509    
510     if ($name =~ /^(.*)\.pm$/) {
511     my $auto = "auto/$1";
512     my $autodir = "$dir/$auto";
513    
514     if (-d $autodir) {
515     opendir my $dir, $autodir
516     or die "$autodir: $!\n";
517    
518     for (readdir $dir) {
519     # AutoLoader
520 root 1.7 scan_al $auto, $autodir, $_
521     if /\.ix$/;
522 root 1.1
523     # static ext
524     if (/\Q$Config{_a}\E$/o) {
525     push @libs, "$autodir/$_";
526     push @static_ext, $name;
527     }
528    
529     # extralibs.ld
530     if ($_ eq "extralibs.ld") {
531     open my $fh, "<:perlio", "$autodir/$_"
532     or die "$autodir/$_";
533    
534     local $/;
535     $extralibs .= " " . <$fh>;
536     }
537    
538     # dynamic object
539     warn "WARNING: found shared object - can't link statically ($_)\n"
540     if /\.\Q$Config{dlext}\E$/o;
541     }
542     }
543     }
544     }
545     }
546    
547     sub trace_eval {
548     syswrite $TRACER_W, "eval $_[0]\n";
549     }
550    
551     sub trace_finish {
552     close $TRACER_W;
553     close $TRACER_R;
554     }
555    
556     #############################################################################
557     # now we can use modules
558    
559     use common::sense;
560     use Digest::MD5;
561    
562     sub dump_string {
563     my ($fh, $data) = @_;
564    
565     if (length $data) {
566     for (
567     my $ofs = 0;
568     length (my $substr = substr $data, $ofs, 80);
569     $ofs += 80
570     ) {
571     $substr =~ s/([^\x20-\x21\x23-\x5b\x5d-\x7e])/sprintf "\\%03o", ord $1/ge;
572     $substr =~ s/\?/\\?/g; # trigraphs...
573     print $fh " \"$substr\"\n";
574     }
575     } else {
576     print $fh " \"\"\n";
577     }
578     }
579    
580     # required for @INC loading, unfortunately
581     trace_module "PerlIO::scalar";
582    
583     #trace_module "Term::ReadLine::readline"; # Term::ReadLine::Perl dependency
584     # URI is difficult
585     #trace_module "URI::http";
586     #trace_module "URI::_generic";
587    
588     sub cmd_boot {
589     $pm{"//boot"} = $_[0];
590     }
591    
592     sub cmd_add {
593 root 1.3 $_[0] =~ /^(.*)(?:\s+(\S+))$/
594 root 1.1 or die "$_[0]: cannot parse";
595    
596     my $file = $1;
597     my $as = defined $2 ? $2 : "/$1";
598    
599     $pm{$as} = $file;
600 root 1.8 $pmbin{$as} = 1 if $_[1];
601 root 1.1 }
602    
603     sub cmd_file {
604     open my $fh, "<", $_[0]
605     or die "$_[0]: $!\n";
606    
607     while (<$fh>) {
608     chomp;
609     my ($cmd, $args) = split / /, $_, 2;
610 root 1.2 $cmd =~ s/^-+//;
611 root 1.1
612     if ($cmd eq "strip") {
613     $STRIP = $args;
614 root 1.17 } elsif ($cmd eq "perl") {
615     $PERL = 1;
616     } elsif ($cmd eq "app") {
617     $APP = $args;
618 root 1.1 } elsif ($cmd eq "eval") {
619     trace_eval $_;
620     } elsif ($cmd eq "use") {
621     trace_module $_
622     for split / /, $args;
623     } elsif ($cmd eq "boot") {
624     cmd_boot $args;
625     } elsif ($cmd eq "static") {
626     $STATIC = 1;
627     } elsif ($cmd eq "add") {
628 root 1.8 cmd_add $args, 0;
629     } elsif ($cmd eq "addbin") {
630     cmd_add $args, 1;
631 root 1.1 } elsif (/^\s*#/) {
632     # comment
633     } elsif (/\S/) {
634     die "$_: unsupported directive\n";
635     }
636     }
637     }
638    
639     use Getopt::Long;
640    
641     Getopt::Long::Configure ("bundling", "no_auto_abbrev", "no_ignore_case");
642    
643     GetOptions
644     "strip=s" => \$STRIP,
645     "verbose|v" => sub { ++$VERBOSE },
646     "quiet|q" => sub { --$VERBOSE },
647     "perl" => \$PERL,
648 root 1.17 "app=s" => \$APP,
649 root 1.2 "eval|e=s" => sub { trace_eval $_[1] },
650 root 1.1 "use|M=s" => sub { trace_module $_[1] },
651     "boot=s" => sub { cmd_boot $_[1] },
652 root 1.8 "add=s" => sub { cmd_add $_[1], 0 },
653     "addbin=s" => sub { cmd_add $_[1], 1 },
654 root 1.1 "static" => sub { $STATIC = 1 },
655 root 1.4 "<>" => sub { cmd_file $_[0] },
656 root 1.1 or exit 1;
657    
658 root 1.17 die "cannot specify both --app and --perl\n"
659     if $PERL and defined $APP;
660    
661 root 1.1 my $data;
662     my @index;
663     my @order = sort {
664     length $a <=> length $b
665     or $a cmp $b
666     } keys %pm;
667    
668     # sorting by name - better compression, but needs more metadata
669     # sorting by length - faster lookup
670     # usually, the metadata overhead beats the loss through compression
671    
672     for my $pm (@order) {
673     my $path = $pm{$pm};
674    
675     128 > length $pm
676     or die "$pm: path too long (only 128 octets supported)\n";
677    
678     my $src = ref $path
679     ? $$path
680     : do {
681 root 1.7 open my $pm, "<", $path
682 root 1.1 or die "$path: $!";
683    
684     local $/;
685    
686     <$pm>
687     };
688    
689 root 1.8 unless ($pmbin{$pm}) { # only do this unless the file is binary
690    
691     if ($pm =~ /^auto\/POSIX\/[^\/]+\.al$/) {
692     if ($src =~ /^ unimpl \"/m) {
693     warn "$pm: skipping (not implemented anyways).\n"
694     if $VERBOSE >= 2;
695     next;
696     }
697 root 1.1 }
698    
699 root 1.8 if ($STRIP =~ /ppi/i) {
700     require PPI;
701 root 1.1
702 root 1.8 my $ppi = PPI::Document->new (\$src);
703     $ppi->prune ("PPI::Token::Comment");
704     $ppi->prune ("PPI::Token::Pod");
705    
706     # prune END stuff
707     for (my $last = $ppi->last_element; $last; ) {
708     my $prev = $last->previous_token;
709    
710     if ($last->isa (PPI::Token::Whitespace::)) {
711     $last->delete;
712     } elsif ($last->isa (PPI::Statement::End::)) {
713     $last->delete;
714     last;
715     } elsif ($last->isa (PPI::Token::Pod::)) {
716     $last->delete;
717     } else {
718     last;
719     }
720    
721     $last = $prev;
722 root 1.1 }
723    
724 root 1.8 # prune some but not all insignificant whitespace
725     for my $ws (@{ $ppi->find (PPI::Token::Whitespace::) }) {
726     my $prev = $ws->previous_token;
727     my $next = $ws->next_token;
728 root 1.1
729 root 1.8 if (!$prev || !$next) {
730 root 1.1 $ws->delete;
731     } else {
732 root 1.8 if (
733     $next->isa (PPI::Token::Operator::) && $next->{content} =~ /^(?:,|=|!|!=|==|=>)$/ # no ., because of digits. == float
734     or $prev->isa (PPI::Token::Operator::) && $prev->{content} =~ /^(?:,|=|\.|!|!=|==|=>)$/
735     or $prev->isa (PPI::Token::Structure::)
736     # decrease size, decrease compressability
737     #or ($prev->isa (PPI::Token::Word::)
738     # && (PPI::Token::Symbol:: eq ref $next
739     # || $next->isa (PPI::Structure::Block::)
740     # || $next->isa (PPI::Structure::List::)
741     # || $next->isa (PPI::Structure::Condition::)))
742     ) {
743     $ws->delete;
744     } elsif ($prev->isa (PPI::Token::Whitespace::)) {
745     $ws->{content} = ' ';
746     $prev->delete;
747     } else {
748     $ws->{content} = ' ';
749     }
750 root 1.1 }
751     }
752    
753 root 1.8 # prune whitespace around blocks
754     if (0) {
755     # these usually decrease size, but decrease compressability more
756     for my $struct (PPI::Structure::Block::, PPI::Structure::Condition::) {
757     for my $node (@{ $ppi->find ($struct) }) {
758     my $n1 = $node->first_token;
759     my $n2 = $n1->previous_token;
760     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
761     $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
762     my $n1 = $node->last_token;
763     my $n2 = $n1->next_token;
764     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
765     $n2->delete if $n2 && $n2->isa (PPI::Token::Whitespace::);
766     }
767     }
768    
769     for my $node (@{ $ppi->find (PPI::Structure::List::) }) {
770 root 1.1 my $n1 = $node->first_token;
771     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
772     my $n1 = $node->last_token;
773     $n1->delete if $n1->isa (PPI::Token::Whitespace::);
774     }
775     }
776    
777 root 1.8 # reformat qw() lists which often have lots of whitespace
778     for my $node (@{ $ppi->find (PPI::Token::QuoteLike::Words::) }) {
779     if ($node->{content} =~ /^qw(.)(.*)(.)$/s) {
780     my ($a, $qw, $b) = ($1, $2, $3);
781     $qw =~ s/^\s+//;
782     $qw =~ s/\s+$//;
783     $qw =~ s/\s+/ /g;
784     $node->{content} = "qw$a$qw$b";
785     }
786 root 1.1 }
787 root 1.8
788     $src = $ppi->serialize;
789     } elsif ($STRIP =~ /pod/i && $pm ne "Opcode.pm") { # opcode parses it's own pod
790     require Pod::Strip;
791    
792     my $stripper = Pod::Strip->new;
793    
794     my $out;
795     $stripper->output_string (\$out);
796     $stripper->parse_string_document ($src)
797     or die;
798     $src = $out;
799 root 1.1 }
800    
801 root 1.8 if ($VERIFY && $pm =~ /\.pm$/ && $pm ne "Opcode.pm") {
802     if (open my $fh, "-|") {
803     <$fh>;
804     } else {
805     eval "#line 1 \"$pm\"\n$src" or warn "\n\n\n$pm\n\n$src\n$@\n\n\n";
806     exit 0;
807 root 1.1 }
808     }
809    
810 root 1.8 # if ($pm eq "Opcode.pm") {
811     # open my $fh, ">x" or die; print $fh $src;#d#
812     # exit 1;
813     # }
814 root 1.1 }
815    
816     warn "adding $pm\n"
817     if $VERBOSE >= 2;
818    
819     push @index, ((length $pm) << 25) | length $data;
820     $data .= $pm . $src;
821     }
822    
823     length $data < 2**25
824     or die "bundle too large (only 32MB supported)\n";
825    
826     my $varpfx = "bundle_" . substr +(Digest::MD5::md5_hex $data), 0, 16;
827    
828     #############################################################################
829     # output
830    
831     print "generating $PREFIX.h... ";
832    
833     {
834     open my $fh, ">", "$PREFIX.h"
835     or die "$PREFIX.h: $!\n";
836    
837     print $fh <<EOF;
838     /* do not edit, automatically created by mkstaticbundle */
839 root 1.8
840 root 1.1 #include <EXTERN.h>
841     #include <perl.h>
842     #include <XSUB.h>
843    
844     /* public API */
845     EXTERN_C PerlInterpreter *staticperl;
846 root 1.7 EXTERN_C void staticperl_xs_init (pTHX);
847 root 1.1 EXTERN_C void staticperl_init (void);
848     EXTERN_C void staticperl_cleanup (void);
849 root 1.8
850 root 1.1 EOF
851     }
852    
853     print "\n";
854    
855     #############################################################################
856     # output
857    
858     print "generating $PREFIX.c... ";
859    
860     open my $fh, ">", "$PREFIX.c"
861     or die "$PREFIX.c: $!\n";
862    
863     print $fh <<EOF;
864     /* do not edit, automatically created by mkstaticbundle */
865    
866     #include "bundle.h"
867    
868     /* public API */
869     PerlInterpreter *staticperl;
870    
871     EOF
872    
873     #############################################################################
874     # bundle data
875    
876     my $count = @index;
877    
878     print $fh <<EOF;
879     #include "bundle.h"
880    
881     /* bundle data */
882    
883     static const U32 $varpfx\_count = $count;
884     static const U32 $varpfx\_index [$count + 1] = {
885     EOF
886    
887     my $col;
888     for (@index) {
889     printf $fh "0x%08x,", $_;
890     print $fh "\n" unless ++$col % 10;
891    
892     }
893     printf $fh "0x%08x\n};\n", (length $data);
894    
895     print $fh "static const char $varpfx\_data [] =\n";
896     dump_string $fh, $data;
897    
898     print $fh ";\n\n";;
899    
900     #############################################################################
901     # bootstrap
902    
903     # boot file for staticperl
904     # this file will be eval'ed at initialisation time
905    
906     my $bootstrap = '
907     BEGIN {
908     package ' . $PACKAGE . ';
909    
910     PerlIO::scalar->bootstrap;
911    
912     @INC = sub {
913     my $data = find "$_[1]"
914     or return;
915    
916     $INC{$_[1]} = $_[1];
917    
918     open my $fh, "<", \$data;
919     $fh
920     };
921     }
922     ';
923    
924     $bootstrap .= "require '//boot';"
925     if exists $pm{"//boot"};
926    
927     $bootstrap =~ s/\s+/ /g;
928     $bootstrap =~ s/(\W) /$1/g;
929     $bootstrap =~ s/ (\W)/$1/g;
930    
931     print $fh "const char bootstrap [] = ";
932     dump_string $fh, $bootstrap;
933     print $fh ";\n\n";
934    
935     print $fh <<EOF;
936     /* search all bundles for the given file, using binary search */
937     XS(find)
938     {
939     dXSARGS;
940    
941     if (items != 1)
942     Perl_croak (aTHX_ "Usage: $PACKAGE\::find (\$path)");
943    
944     {
945     STRLEN namelen;
946     char *name = SvPV (ST (0), namelen);
947     SV *res = 0;
948    
949     int l = 0, r = $varpfx\_count;
950    
951     while (l <= r)
952     {
953     int m = (l + r) >> 1;
954     U32 idx = $varpfx\_index [m];
955     int comp = namelen - (idx >> 25);
956    
957     if (!comp)
958     {
959     int ofs = idx & 0x1FFFFFFU;
960     comp = memcmp (name, $varpfx\_data + ofs, namelen);
961    
962     if (!comp)
963     {
964     /* found */
965     int ofs2 = $varpfx\_index [m + 1] & 0x1FFFFFFU;
966    
967     ofs += namelen;
968     res = newSVpvn ($varpfx\_data + ofs, ofs2 - ofs);
969     goto found;
970     }
971     }
972    
973     if (comp < 0)
974     r = m - 1;
975     else
976     l = m + 1;
977     }
978    
979     XSRETURN (0);
980    
981     found:
982     ST (0) = res;
983     sv_2mortal (ST (0));
984     }
985    
986     XSRETURN (1);
987     }
988    
989     /* list all files in the bundle */
990     XS(list)
991     {
992     dXSARGS;
993    
994     if (items != 0)
995     Perl_croak (aTHX_ "Usage: $PACKAGE\::list");
996    
997     {
998     int i;
999    
1000     EXTEND (SP, $varpfx\_count);
1001    
1002     for (i = 0; i < $varpfx\_count; ++i)
1003     {
1004     U32 idx = $varpfx\_index [i];
1005    
1006     PUSHs (newSVpvn ($varpfx\_data + (idx & 0x1FFFFFFU), idx >> 25));
1007     }
1008     }
1009    
1010     XSRETURN ($varpfx\_count);
1011     }
1012    
1013     EOF
1014    
1015     #############################################################################
1016     # xs_init
1017    
1018     print $fh <<EOF;
1019 root 1.7 void
1020     staticperl_xs_init (pTHX)
1021 root 1.1 {
1022     EOF
1023    
1024     @static_ext = ("DynaLoader", sort @static_ext);
1025    
1026     # prototypes
1027     for (@static_ext) {
1028     s/\.pm$//;
1029     (my $cname = $_) =~ s/\//__/g;
1030     print $fh " EXTERN_C void boot_$cname (pTHX_ CV* cv);\n";
1031     }
1032    
1033     print $fh <<EOF;
1034     char *file = __FILE__;
1035     dXSUB_SYS;
1036    
1037     newXSproto ("$PACKAGE\::find", find, file, "\$");
1038     newXSproto ("$PACKAGE\::list", list, file, "");
1039     EOF
1040    
1041     # calls
1042     for (@static_ext) {
1043     s/\.pm$//;
1044    
1045     (my $cname = $_) =~ s/\//__/g;
1046     (my $pname = $_) =~ s/\//::/g;
1047    
1048     my $bootstrap = $pname eq "DynaLoader" ? "boot" : "bootstrap";
1049    
1050     print $fh " newXS (\"$pname\::$bootstrap\", boot_$cname, file);\n";
1051     }
1052    
1053     print $fh <<EOF;
1054     Perl_av_create_and_unshift_one (&PL_preambleav, newSVpv (bootstrap, sizeof (bootstrap) - 1));
1055     }
1056     EOF
1057    
1058     #############################################################################
1059     # optional perl_init/perl_destroy
1060    
1061 root 1.17 if ($APP) {
1062     print $fh <<EOF;
1063    
1064     int
1065     main (int argc, char *argv [])
1066     {
1067     extern char **environ;
1068     int exitstatus;
1069    
1070     static char *args[] = {
1071     "staticperl",
1072     "-e",
1073     "0"
1074     };
1075    
1076     PERL_SYS_INIT3 (&argc, &argv, &environ);
1077     staticperl = perl_alloc ();
1078     perl_construct (staticperl);
1079    
1080     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1081    
1082     exitstatus = perl_parse (staticperl, staticperl_xs_init, sizeof (args) / sizeof (*args), args, environ);
1083     if (!exitstatus)
1084     perl_run (staticperl);
1085    
1086     exitstatus = perl_destruct (staticperl);
1087     perl_free (staticperl);
1088     PERL_SYS_TERM ();
1089    
1090     return exitstatus;
1091     }
1092     EOF
1093     } elsif ($PERL) {
1094 root 1.1 print $fh <<EOF;
1095    
1096     int
1097     main (int argc, char *argv [])
1098     {
1099     extern char **environ;
1100     int exitstatus;
1101    
1102     PERL_SYS_INIT3 (&argc, &argv, &environ);
1103     staticperl = perl_alloc ();
1104     perl_construct (staticperl);
1105    
1106     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1107    
1108 root 1.7 exitstatus = perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
1109 root 1.1 if (!exitstatus)
1110     perl_run (staticperl);
1111    
1112     exitstatus = perl_destruct (staticperl);
1113     perl_free (staticperl);
1114     PERL_SYS_TERM ();
1115    
1116     return exitstatus;
1117     }
1118     EOF
1119     } else {
1120     print $fh <<EOF;
1121    
1122     EXTERN_C void
1123     staticperl_init (void)
1124     {
1125     extern char **environ;
1126     int argc = sizeof (args) / sizeof (args [0]);
1127     char **argv = args;
1128    
1129 root 1.17 static char *args[] = {
1130     "staticperl",
1131     "-e",
1132     "0"
1133     };
1134    
1135 root 1.1 PERL_SYS_INIT3 (&argc, &argv, &environ);
1136     staticperl = perl_alloc ();
1137     perl_construct (staticperl);
1138     PL_origalen = 1;
1139     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
1140 root 1.7 perl_parse (staticperl, staticperl_xs_init, argc, argv, environ);
1141 root 1.1
1142     perl_run (staticperl);
1143     }
1144    
1145     EXTERN_C void
1146     staticperl_cleanup (void)
1147     {
1148     perl_destruct (staticperl);
1149     perl_free (staticperl);
1150     staticperl = 0;
1151     PERL_SYS_TERM ();
1152     }
1153     EOF
1154     }
1155    
1156     print -s "$PREFIX.c", " octets (", (length $data) , " data octets).\n\n";
1157    
1158     #############################################################################
1159     # libs, cflags
1160    
1161     {
1162     print "generating $PREFIX.ccopts... ";
1163    
1164     my $str = "$Config{ccflags} $Config{optimize} $Config{cppflags} -I$Config{archlibexp}/CORE";
1165     $str =~ s/([\(\)])/\\$1/g;
1166    
1167     print "$str\n\n";
1168    
1169     open my $fh, ">$PREFIX.ccopts"
1170     or die "$PREFIX.ccopts: $!";
1171     print $fh $str;
1172     }
1173    
1174     {
1175     print "generating $PREFIX.ldopts... ";
1176    
1177     my $str = $STATIC ? "--static " : "";
1178    
1179     $str .= "$Config{ccdlflags} $Config{ldflags} @libs $Config{archlibexp}/CORE/$Config{libperl} $Config{perllibs}";
1180    
1181     my %seen;
1182     $str .= " $_" for grep !$seen{$_}++, ($extralibs =~ /(\S+)/g);
1183    
1184     $str =~ s/([\(\)])/\\$1/g;
1185    
1186     print "$str\n\n";
1187    
1188     open my $fh, ">$PREFIX.ldopts"
1189     or die "$PREFIX.ldopts: $!";
1190     print $fh $str;
1191     }
1192    
1193 root 1.17 if ($PERL or defined $APP) {
1194     $APP = "perl" unless defined $APP;
1195    
1196     print "generating $APP...\n";
1197    
1198     system "$Config{cc} \$(cat bundle.ccopts\) -o \Q$APP\E bundle.c \$(cat bundle.ldopts\)";
1199 root 1.1
1200     unlink "$PREFIX.$_"
1201     for qw(ccopts ldopts c h);
1202     }
1203    
1204     MKBUNDLE
1205     }
1206    
1207     bundle() {
1208     catmkbundle >"$MKBUNDLE~" || fatal "$MKBUNDLE~: cannot create"
1209     chmod 755 "$MKBUNDLE~" && mv "$MKBUNDLE~" "$MKBUNDLE"
1210 root 1.10 "$PERL_PREFIX/bin/perl" -- "$MKBUNDLE" "$@"
1211 root 1.1 }
1212    
1213     if [ $# -gt 0 ]; then
1214     while [ $# -gt 0 ]; do
1215     mkdir -p "$STATICPERL" || fatal "$STATICPERL: cannot create"
1216 root 1.10 mkdir -p "$PERL_PREFIX" || fatal "$PERL_PREFIX: cannot create"
1217 root 1.1
1218     command="${1#--}"; shift
1219     case "$command" in
1220     fetch | configure | build | install | clean | distclean)
1221     verblock <<EOF
1222     $command
1223     EOF
1224 root 1.10 ( "$command" )
1225 root 1.1 ;;
1226     instsrc )
1227 root 1.10 ( instsrc "$@" )
1228 root 1.1 exit
1229     ;;
1230     instcpan )
1231 root 1.10 ( instcpan "$@" )
1232 root 1.1 exit
1233     ;;
1234     cpan )
1235 root 1.10 ( install )
1236     "$PERL_PREFIX/bin/cpan" "$@"
1237 root 1.1 exit
1238     ;;
1239     mkbundle )
1240 root 1.10 ( install )
1241 root 1.1 bundle "$@"
1242     exit
1243     ;;
1244     mkperl )
1245 root 1.10 ( install )
1246 root 1.1 bundle --perl "$@"
1247     exit
1248     ;;
1249 root 1.17 mkapp )
1250     ( install )
1251     bundle --app "$@"
1252     exit
1253     ;;
1254 root 1.1 help )
1255     podusage 2
1256     ;;
1257     * )
1258     exec 1>&2
1259     echo
1260     echo "Unknown command: $command"
1261     podusage 0
1262     ;;
1263     esac
1264     done
1265     else
1266     usage
1267     fi
1268    
1269     exit 0
1270    
1271     =head1 NAME
1272    
1273 root 1.7 staticperl - perl, libc, 100 modules, all in one 500kb file
1274 root 1.1
1275     =head1 SYNOPSIS
1276    
1277     staticperl help # print the embedded documentation
1278     staticperl fetch # fetch and unpack perl sources
1279     staticperl configure # fetch and then configure perl
1280     staticperl build # configure and then build perl
1281     staticperl install # build and then install perl
1282     staticperl clean # clean most intermediate files (restart at configure)
1283     staticperl distclean # delete everything installed by this script
1284     staticperl cpan # invoke CPAN shell
1285     staticperl instmod path... # install unpacked modules
1286     staticperl instcpan modulename... # install modules from CPAN
1287     staticperl mkbundle <bundle-args...> # see documentation
1288     staticperl mkperl <bundle-args...> # see documentation
1289 root 1.17 staticperl mkapp appname <bundle-args...> # see documentation
1290 root 1.1
1291     Typical Examples:
1292    
1293     staticperl install # fetch, configure, build and install perl
1294     staticperl cpan # run interactive cpan shell
1295     staticperl mkperl -M '"Config_heavy.pl"' # build a perl that supports -V
1296     staticperl mkperl -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI -MURI::http
1297     # build a perl with the above modules linked in
1298 root 1.17 staticperl mkapp myapp --boot mainprog mymodules
1299     # build a binary "myapp" from mainprog and mymodules
1300 root 1.1
1301     =head1 DESCRIPTION
1302    
1303     This script helps you creating single-file perl interpreters, or embedding
1304 root 1.4 a perl interpreter in your applications. Single-file means that it is
1305     fully self-contained - no separate shared objects, no autoload fragments,
1306     no .pm or .pl files are needed. And when linking statically, you can
1307     create (or embed) a single file that contains perl interpreter, libc, all
1308     the modules you need and all the libraries you need.
1309 root 1.1
1310 root 1.7 With F<uClibc> and F<upx> on x86, you can create a single 500kb binary
1311     that contains perl and 100 modules such as POSIX, AnyEvent, EV, IO::AIO,
1312     Coro and so on. Or any other choice of modules.
1313 root 1.1
1314 root 1.4 The created files do not need write access to the file system (like PAR
1315 root 1.1 does). In fact, since this script is in many ways similar to PAR::Packer,
1316     here are the differences:
1317    
1318     =over 4
1319    
1320     =item * The generated executables are much smaller than PAR created ones.
1321    
1322     Shared objects and the perl binary contain a lot of extra info, while
1323     the static nature of F<staticperl> allows the linker to remove all
1324     functionality and meta-info not required by the final executable. Even
1325     extensions statically compiled into perl at build time will only be
1326     present in the final executable when needed.
1327    
1328     In addition, F<staticperl> can strip perl sources much more effectively
1329     than PAR.
1330    
1331     =item * The generated executables start much faster.
1332    
1333     There is no need to unpack files, or even to parse Zip archives (which is
1334     slow and memory-consuming business).
1335    
1336     =item * The generated executables don't need a writable filesystem.
1337    
1338     F<staticperl> loads all required files directly from memory. There is no
1339     need to unpack files into a temporary directory.
1340    
1341     =item * More control over included files.
1342    
1343 root 1.4 PAR tries to be maintenance and hassle-free - it tries to include more
1344     files than necessary to make sure everything works out of the box. The
1345     extra files (such as the unicode database) can take substantial amounts of
1346     memory and file size.
1347 root 1.1
1348     With F<staticperl>, the burden is mostly with the developer - only direct
1349     compile-time dependencies and L<AutoLoader> are handled automatically.
1350     This means the modules to include often need to be tweaked manually.
1351    
1352     =item * PAR works out of the box, F<staticperl> does not.
1353    
1354     Maintaining your own custom perl build can be a pain in the ass, and while
1355     F<staticperl> tries to make this easy, it still requires a custom perl
1356     build and possibly fiddling with some modules. PAR is likely to produce
1357     results faster.
1358    
1359 root 1.13 Ok, PAR never has worked for me out of the box, and for some people,
1360     F<staticperl> does work out of the box, as they don't count "fiddling with
1361     module use lists" against it, but nevertheless, F<staticperl> is certainly
1362     a bit more difficult to use.
1363    
1364 root 1.1 =back
1365    
1366     =head1 HOW DOES IT WORK?
1367    
1368     Simple: F<staticperl> downloads, compile and installs a perl version of
1369     your choice in F<~/.staticperl>. You can add extra modules either by
1370     letting F<staticperl> install them for you automatically, or by using CPAN
1371     and doing it interactively. This usually takes 5-10 minutes, depending on
1372 root 1.4 the speed of your computer and your internet connection.
1373 root 1.1
1374     It is possible to do program development at this stage, too.
1375    
1376     Afterwards, you create a list of files and modules you want to include,
1377 root 1.4 and then either build a new perl binary (that acts just like a normal perl
1378 root 1.1 except everything is compiled in), or you create bundle files (basically C
1379     sources you can use to embed all files into your project).
1380    
1381     This step is very fast (a few seconds if PPI is not used for stripping,
1382     more seconds otherwise, as PPI is very slow), and can be tweaked and
1383     repeated as often as necessary.
1384    
1385     =head1 THE F<STATICPERL> SCRIPT
1386    
1387     This module installs a script called F<staticperl> into your perl
1388     binary directory. The script is fully self-contained, and can be used
1389     without perl (for example, in an uClibc chroot environment). In fact,
1390     it can be extracted from the C<App::Staticperl> distribution tarball as
1391     F<bin/staticperl>, without any installation.
1392    
1393     F<staticperl> interprets the first argument as a command to execute,
1394     optionally followed by any parameters.
1395    
1396     There are two command categories: the "phase 1" commands which deal with
1397     installing perl and perl modules, and the "phase 2" commands, which deal
1398     with creating binaries and bundle files.
1399    
1400     =head2 PHASE 1 COMMANDS: INSTALLING PERL
1401    
1402     The most important command is F<install>, which does basically
1403     everything. The default is to download and install perl 5.12.2 and a few
1404     modules required by F<staticperl> itself, but all this can (and should) be
1405     changed - see L<CONFIGURATION>, below.
1406    
1407     The command
1408    
1409     staticperl install
1410    
1411     Is normally all you need: It installs the perl interpreter in
1412     F<~/.staticperl/perl>. It downloads, configures, builds and installs the
1413     perl interpreter if required.
1414    
1415     Most of the following commands simply run one or more steps of this
1416     sequence.
1417    
1418 root 1.4 To force recompilation or reinstallation, you need to run F<staticperl
1419 root 1.1 distclean> first.
1420    
1421     =over 4
1422    
1423     =item F<staticperl fetch>
1424    
1425     Runs only the download and unpack phase, unless this has already happened.
1426    
1427     =item F<staticperl configure>
1428    
1429     Configures the unpacked perl sources, potentially after downloading them first.
1430    
1431     =item F<staticperl build>
1432    
1433     Builds the configured perl sources, potentially after automatically
1434     configuring them.
1435    
1436     =item F<staticperl install>
1437    
1438 root 1.4 Wipes the perl installation directory (usually F<~/.staticperl/perl>) and
1439     installs the perl distribution, potentially after building it first.
1440 root 1.1
1441     =item F<staticperl cpan> [args...]
1442    
1443 root 1.4 Starts an interactive CPAN shell that you can use to install further
1444     modules. Installs the perl first if necessary, but apart from that,
1445 root 1.1 no magic is involved: you could just as well run it manually via
1446     F<~/.staticperl/perl/bin/cpan>.
1447    
1448     Any additional arguments are simply passed to the F<cpan> command.
1449    
1450     =item F<staticperl instcpan> module...
1451    
1452     Tries to install all the modules given and their dependencies, using CPAN.
1453    
1454     Example:
1455    
1456     staticperl instcpan EV AnyEvent::HTTPD Coro
1457    
1458     =item F<staticperl instsrc> directory...
1459    
1460     In the unlikely case that you have unpacked perl modules around and want
1461 root 1.4 to install from these instead of from CPAN, you can do this using this
1462 root 1.1 command by specifying all the directories with modules in them that you
1463     want to have built.
1464    
1465     =item F<staticperl clean>
1466    
1467 root 1.11 Deletes the perl source directory (and potentially cleans up other
1468     intermediate files). This can be used to clean up files only needed for
1469     building perl, without removing the installed perl interpreter, or to
1470     force a re-build from scratch.
1471    
1472     At the moment, it doesn't delete downloaded tarballs.
1473 root 1.1
1474     =item F<staticperl distclean>
1475    
1476     This wipes your complete F<~/.staticperl> directory. Be careful with this,
1477     it nukes your perl download, perl sources, perl distribution and any
1478     installed modules. It is useful if you wish to start over "from scratch"
1479     or when you want to uninstall F<staticperl>.
1480    
1481     =back
1482    
1483     =head2 PHASE 2 COMMANDS: BUILDING PERL BUNDLES
1484    
1485     Building (linking) a new F<perl> binary is handled by a separate
1486     script. To make it easy to use F<staticperl> from a F<chroot>, the script
1487     is embedded into F<staticperl>, which will write it out and call for you
1488     with any arguments you pass:
1489    
1490     staticperl mkbundle mkbundle-args...
1491    
1492     In the oh so unlikely case of something not working here, you
1493 root 1.2 can run the script manually as well (by default it is written to
1494 root 1.1 F<~/.staticperl/mkbundle>).
1495    
1496     F<mkbundle> is a more conventional command and expect the argument
1497 root 1.4 syntax commonly used on UNIX clones. For example, this command builds
1498 root 1.1 a new F<perl> binary and includes F<Config.pm> (for F<perl -V>),
1499     F<AnyEvent::HTTPD>, F<URI> and a custom F<httpd> script (from F<eg/httpd>
1500     in this distribution):
1501    
1502     # first make sure we have perl and the required modules
1503     staticperl instcpan AnyEvent::HTTPD
1504    
1505     # now build the perl
1506     staticperl mkperl -M'"Config_heavy.pl"' -MAnyEvent::Impl::Perl \
1507     -MAnyEvent::HTTPD -MURI::http \
1508     --add 'eg/httpd httpd.pm'
1509    
1510     # finally, invoke it
1511     ./perl -Mhttpd
1512    
1513 root 1.2 As you can see, things are not quite as trivial: the L<Config> module has
1514     a hidden dependency which is not even a perl module (F<Config_heavy.pl>),
1515     L<AnyEvent> needs at least one event loop backend that we have to
1516 root 1.4 specify manually (here L<AnyEvent::Impl::Perl>), and the F<URI> module
1517 root 1.2 (required by L<AnyEvent::HTTPD>) implements various URI schemes as extra
1518     modules - since L<AnyEvent::HTTPD> only needs C<http> URIs, we only need
1519 root 1.4 to include that module. I found out about these dependencies by carefully
1520     watching any error messages about missing modules...
1521 root 1.2
1522 root 1.17 Instead of building a new perl binary, you can also build a standalone
1523     application:
1524    
1525     # build the app
1526     staticperl mkapp app --boot eg/httpd \
1527     -MAnyEvent::Impl::Perl -MAnyEvent::HTTPD -MURI::http
1528    
1529     # run it
1530     ./app
1531    
1532 root 1.2 =head3 OPTION PROCESSING
1533    
1534 root 1.4 All options can be given as arguments on the command line (typically
1535     using long (e.g. C<--verbose>) or short option (e.g. C<-v>) style). Since
1536     specifying a lot of modules can make the command line very cumbersome,
1537 root 1.2 you can put all long options into a "bundle specification file" (with or
1538     without C<--> prefix) and specify this bundle file instead.
1539    
1540     For example, the command given earlier could also look like this:
1541    
1542     staticperl mkperl httpd.bundle
1543    
1544     And all options could be in F<httpd.bundle>:
1545 root 1.1
1546 root 1.2 use "Config_heavy.pl"
1547     use AnyEvent::Impl::Perl
1548     use AnyEvent::HTTPD
1549     use URI::http
1550     add eg/httpd httpd.pm
1551    
1552     All options that specify modules or files to be added are processed in the
1553 root 1.4 order given on the command line (that affects the C<--use> and C<--eval>
1554 root 1.2 options at the moment).
1555    
1556     =head3 MKBUNDLE OPTIONS
1557    
1558     =over 4
1559    
1560     =item --verbose | -v
1561    
1562     Increases the verbosity level by one (the default is C<1>).
1563    
1564     =item --quiet | -q
1565    
1566     Decreases the verbosity level by one.
1567    
1568     =item --strip none|pod|ppi
1569    
1570     Specify the stripping method applied to reduce the file of the perl
1571     sources included.
1572    
1573     The default is C<pod>, which uses the L<Pod::Strip> module to remove all
1574 root 1.4 pod documentation, which is very fast and reduces file size a lot.
1575 root 1.2
1576     The C<ppi> method uses L<PPI> to parse and condense the perl sources. This
1577 root 1.4 saves a lot more than just L<Pod::Strip>, and is generally safer, but
1578     is also a lot slower, so is best used for production builds. Note that
1579     this method doesn't optimise for raw file size, but for best compression
1580     (that means that the uncompressed file size is a bit larger, but the files
1581     compress better, e.g. with F<upx>).
1582 root 1.2
1583 root 1.7 Last not least, if you need accurate line numbers in error messages,
1584     or in the unlikely case where C<pod> is too slow, or some module gets
1585     mistreated, you can specify C<none> to not mangle included perl sources in
1586     any way.
1587 root 1.2
1588     =item --perl
1589    
1590     After writing out the bundle files, try to link a new perl interpreter. It
1591     will be called F<perl> and will be left in the current working
1592     directory. The bundle files will be removed.
1593    
1594 root 1.4 This switch is automatically used when F<staticperl> is invoked with the
1595 root 1.2 C<mkperl> command (instead of C<mkbundle>):
1596    
1597     # build a new ./perl with only common::sense in it - very small :)
1598     staticperl mkperl -Mcommon::sense
1599    
1600 root 1.17 =item --app name
1601    
1602     After writing out the bundle files, try to link a new standalone
1603     program. It will be called C<name>, and the bundle files get removed after
1604     linking it.
1605    
1606     The difference to the (mutually exclusive) C<--perl> option is that the
1607     binary created by this option will not try to act as a perl interpreter -
1608     instead it will simply initialise the perl interpreter, clean it up and
1609     exit.
1610    
1611     This switch is automatically used when F<staticperl> is invoked with the
1612     C<mkapp> command (instead of C<mkbundle>):
1613    
1614     To let it do something useful you I<must> add some boot code, e.g. with
1615     the C<--boot> option.
1616    
1617     Example: create a standalone perl binary that will execute F<appfile> when
1618     it is started.
1619    
1620     staticperl mkbundle --app myexe --boot appfile
1621    
1622 root 1.2 =item --use module | -Mmodule
1623    
1624     Include the named module and all direct dependencies. This is done by
1625     C<require>'ing the module in a subprocess and tracing which other modules
1626     and files it actually loads. If the module uses L<AutoLoader>, then all
1627     splitfiles will be included as well.
1628    
1629     Example: include AnyEvent and AnyEvent::Impl::Perl.
1630    
1631     staticperl mkbundle --use AnyEvent --use AnyEvent::Impl::Perl
1632    
1633     Sometimes you want to load old-style "perl libraries" (F<.pl> files), or
1634     maybe other weirdly named files. To do that, you need to quote the name in
1635 root 1.4 single or double quotes. When given on the command line, you probably need
1636 root 1.2 to quote once more to avoid your shell interpreting it. Common cases that
1637     need this are F<Config_heavy.pl> and F<utf8_heavy.pl>.
1638    
1639     Example: include the required files for F<perl -V> to work in all its
1640     glory (F<Config.pm> is included automatically by this).
1641    
1642     # bourne shell
1643     staticperl mkbundle --use '"Config_heavy.pl"'
1644    
1645     # bundle specification file
1646     use "Config_heavy.pl"
1647    
1648     The C<-Mmodule> syntax is included as an alias that might be easier to
1649     remember than C<use>. Or maybe it confuses people. Time will tell. Or
1650     maybe not. Argh.
1651    
1652     =item --eval "perl code" | -e "perl code"
1653    
1654     Sometimes it is easier (or necessary) to specify dependencies using perl
1655     code, or maybe one of the modules you use need a special use statement. In
1656     that case, you can use C<eval> to execute some perl snippet or set some
1657     variables or whatever you need. All files C<require>'d or C<use>'d in the
1658     script are included in the final bundle.
1659    
1660     Keep in mind that F<mkbundle> will only C<require> the modules named
1661     by the C<--use> option, so do not expect the symbols from modules you
1662 root 1.4 C<--use>'d earlier on the command line to be available.
1663 root 1.2
1664     Example: force L<AnyEvent> to detect a backend and therefore include it
1665     in the final bundle.
1666    
1667     staticperl mkbundle --eval 'use AnyEvent; AnyEvent::detect'
1668    
1669     # or like this
1670     staticperl mkbundle -MAnyEvent --eval 'use AnyEvent; AnyEvent::detect'
1671    
1672     Example: use a separate "bootstrap" script that C<use>'s lots of modules
1673     and include this in the final bundle, to be executed automatically.
1674    
1675     staticperl mkbundle --eval 'do "bootstrap"' --boot bootstrap
1676    
1677     =item --boot filename
1678    
1679     Include the given file in the bundle and arrange for it to be executed
1680     (using a C<require>) before anything else when the new perl is
1681     initialised. This can be used to modify C<@INC> or anything else before
1682 root 1.4 the perl interpreter executes scripts given on the command line (or via
1683 root 1.2 C<-e>). This works even in an embedded interpreter.
1684    
1685     =item --add "file" | --add "file alias"
1686    
1687     Adds the given (perl) file into the bundle (and optionally call it
1688     "alias"). This is useful to include any custom files into the bundle.
1689    
1690     Example: embed the file F<httpd> as F<httpd.pm> when creating the bundle.
1691    
1692     staticperl mkperl --add "httpd httpd.pm"
1693    
1694     It is also a great way to add any custom modules:
1695    
1696     # specification file
1697     add file1 myfiles/file1
1698     add file2 myfiles/file2
1699     add file3 myfiles/file3
1700    
1701 root 1.8 =item --binadd "file" | --add "file alias"
1702    
1703     Just like C<--add>, except that it treats the file as binary and adds it
1704     without any processing.
1705    
1706     You should probably add a C</> prefix to avoid clashing with embedded
1707     perl files (whose paths do not start with C</>), and/or use a special
1708     directory, such as C</res/name>.
1709    
1710     You can later get a copy of these files by calling C<staticperl::find
1711     "alias">.
1712    
1713 root 1.2 =item --static
1714    
1715     When C<--perl> is also given, link statically instead of dynamically. The
1716     default is to link the new perl interpreter fully dynamic (that means all
1717     perl modules are linked statically, but all external libraries are still
1718     referenced dynamically).
1719    
1720     Keep in mind that Solaris doesn't support static linking at all, and
1721     systems based on GNU libc don't really support it in a usable fashion
1722     either. Try uClibc if you want to create fully statically linked
1723     executables, or try the C<--staticlibs> option to link only some libraries
1724     statically.
1725    
1726     =item any other argument
1727    
1728     Any other argument is interpreted as a bundle specification file, which
1729     supports most long options (without extra quoting), one option per line.
1730    
1731     =back
1732    
1733     =head2 F<STATCPERL> CONFIGURATION AND HOOKS
1734    
1735     During (each) startup, F<staticperl> tries to source the following shell
1736     files in order:
1737    
1738     /etc/staticperlrc
1739     ~/.staticperlrc
1740     $STATICPERL/rc
1741    
1742     They can be used to override shell variables, or define functions to be
1743     called at specific phases.
1744    
1745     Note that the last file is erased during F<staticperl distclean>, so
1746     generally should not be used.
1747    
1748     =head3 CONFIGURATION VARIABLES
1749    
1750     =head4 Variables you I<should> override
1751    
1752     =over 4
1753    
1754     =item C<EMAIL>
1755    
1756     The e-mail address of the person who built this binary. Has no good
1757     default, so should be specified by you.
1758    
1759     =item C<CPAN>
1760    
1761     The URL of the CPAN mirror to use (e.g. L<http://mirror.netcologne.de/cpan/>).
1762    
1763 root 1.6 =item C<EXTRA_MODULES>
1764 root 1.2
1765 root 1.6 Additional modules installed during F<staticperl install>. Here you can
1766     set which modules you want have to installed from CPAN.
1767 root 1.2
1768 root 1.10 Example: I really really need EV, AnyEvent, Coro and AnyEvent::AIO.
1769 root 1.2
1770 root 1.10 EXTRA_MODULES="EV AnyEvent Coro AnyEvent::AIO"
1771 root 1.2
1772 root 1.6 Note that you can also use a C<postinstall> hook to achieve this, and
1773     more.
1774 root 1.2
1775 root 1.10 =back
1776    
1777     =head4 Variables you might I<want> to override
1778    
1779     =over 4
1780    
1781     =item C<STATICPERL>
1782    
1783     The directory where staticperl stores all its files
1784     (default: F<~/.staticperl>).
1785    
1786 root 1.6 =item C<PERL_MM_USE_DEFAULT>, C<EV_EXTRA_DEFS>, ...
1787 root 1.2
1788     Usually set to C<1> to make modules "less inquisitive" during their
1789     installation, you can set any environment variable you want - some modules
1790     (such as L<Coro> or L<EV>) use environment variables for further tweaking.
1791    
1792 root 1.10 =item C<PERL_VERSION>
1793 root 1.6
1794 root 1.10 The perl version to install - default is currently C<5.12.2>, but C<5.8.9>
1795     is also a good choice (5.8.9 is much smaller than 5.12.2, while 5.10.1 is
1796     about as big as 5.12.2).
1797 root 1.2
1798 root 1.10 =item C<PERL_PREFIX>
1799 root 1.2
1800 root 1.6 The prefix where perl gets installed (default: F<$STATICPERL/perl>),
1801     i.e. where the F<bin> and F<lib> subdirectories will end up.
1802 root 1.2
1803 root 1.8 =item C<PERL_CONFIGURE>
1804    
1805     Additional Configure options - these are simply passed to the perl
1806     Configure script. For example, if you wanted to enable dynamic loading,
1807     you could pass C<-Dusedl>. To enable ithreads (Why would you want that
1808     insanity? Don't! Use L<forks> instead!) you would pass C<-Duseithreads>
1809     and so on.
1810    
1811     More commonly, you would either activate 64 bit integer support
1812     (C<-Duse64bitint>), or disable large files support (-Uuselargefiles), to
1813     reduce filesize further.
1814    
1815 root 1.6 =item C<PERL_CPPFLAGS>, C<PERL_OPTIMIZE>, C<PERL_LDFLAGS>, C<PERL_LIBS>
1816 root 1.2
1817 root 1.6 These flags are passed to perl's F<Configure> script, and are generally
1818     optimised for small size (at the cost of performance). Since they also
1819     contain subtle workarounds around various build issues, changing these
1820     usually requires understanding their default values - best look at the top
1821     of the F<staticperl> script for more info on these.
1822 root 1.2
1823     =back
1824    
1825 root 1.5 =head4 Variables you probably I<do not want> to override
1826 root 1.2
1827     =over 4
1828    
1829     =item C<MKBUNDLE>
1830    
1831     Where F<staticperl> writes the C<mkbundle> command to
1832     (default: F<$STATICPERL/mkbundle>).
1833 root 1.1
1834 root 1.2 =item C<STATICPERL_MODULES>
1835 root 1.1
1836 root 1.2 Additional modules needed by C<mkbundle> - should therefore not be changed
1837     unless you know what you are doing.
1838    
1839     =back
1840    
1841     =head3 OVERRIDABLE HOOKS
1842    
1843     In addition to environment variables, it is possible to provide some
1844     shell functions that are called at specific times. To provide your own
1845 root 1.4 commands, just define the corresponding function.
1846 root 1.2
1847     Example: install extra modules from CPAN and from some directories
1848     at F<staticperl install> time.
1849    
1850     postinstall() {
1851 root 1.5 rm -rf lib/threads* # weg mit Schaden
1852 root 1.2 instcpan IO::AIO EV
1853     instsrc ~/src/AnyEvent
1854     instsrc ~/src/XML-Sablotron-1.0100001
1855 root 1.5 instcpan Anyevent::AIO AnyEvent::HTTPD
1856 root 1.2 }
1857    
1858     =over 4
1859    
1860 root 1.11 =item preconfigure
1861    
1862     Called just before running F<./Configur> in the perl source
1863     directory. Current working directory is the perl source directory.
1864    
1865     This can be used to set any C<PERL_xxx> variables, which might be costly
1866     to compute.
1867    
1868 root 1.2 =item postconfigure
1869    
1870     Called after configuring, but before building perl. Current working
1871     directory is the perl source directory.
1872    
1873 root 1.11 Could be used to tailor/patch config.sh (followed by F<sh Configure -S>)
1874     or do any other modifications.
1875 root 1.2
1876     =item postbuild
1877    
1878     Called after building, but before installing perl. Current working
1879     directory is the perl source directory.
1880    
1881     I have no clue what this could be used for - tell me.
1882    
1883     =item postinstall
1884    
1885     Called after perl and any extra modules have been installed in C<$PREFIX>,
1886     but before setting the "installation O.K." flag.
1887    
1888     The current working directory is C<$PREFIX>, but maybe you should not rely
1889     on that.
1890    
1891     This hook is most useful to customise the installation, by deleting files,
1892     or installing extra modules using the C<instcpan> or C<instsrc> functions.
1893    
1894     The script must return with a zero exit status, or the installation will
1895     fail.
1896 root 1.1
1897 root 1.2 =back
1898 root 1.1
1899 root 1.7 =head1 ANATOMY OF A BUNDLE
1900    
1901     When not building a new perl binary, C<mkbundle> will leave a number of
1902     files in the current working directory, which can be used to embed a perl
1903     interpreter in your program.
1904    
1905     Intimate knowledge of L<perlembed> and preferably some experience with
1906     embedding perl is highly recommended.
1907    
1908     C<mkperl> (or the C<--perl> option) basically does this to link the new
1909     interpreter (it also adds a main program to F<bundle.>):
1910    
1911     $Config{cc} $(cat bundle.ccopts) -o perl bundle.c $(cat bundle.ldopts)
1912    
1913     =over 4
1914    
1915     =item bundle.h
1916    
1917     A header file that contains the prototypes of the few symbols "exported"
1918     by bundle.c, and also exposes the perl headers to the application.
1919    
1920     =over 4
1921    
1922     =item staticperl_init ()
1923    
1924     Initialises the perl interpreter. You can use the normal perl functions
1925     after calling this function, for example, to define extra functions or
1926     to load a .pm file that contains some initialisation code, or the main
1927     program function:
1928    
1929     XS (xsfunction)
1930     {
1931     dXSARGS;
1932    
1933     // now we have items, ST(i) etc.
1934     }
1935    
1936     static void
1937     run_myapp(void)
1938     {
1939     staticperl_init ();
1940     newXSproto ("myapp::xsfunction", xsfunction, __FILE__, "$$;$");
1941     eval_pv ("require myapp::main", 1); // executes "myapp/main.pm"
1942     }
1943    
1944     =item staticperl_xs_init (pTHX)
1945    
1946     Sometimes you need direct control over C<perl_parse> and C<perl_run>, in
1947     which case you do not want to use C<staticperl_init> but call them on your
1948     own.
1949    
1950     Then you need this function - either pass it directly as the C<xs_init>
1951     function to C<perl_parse>, or call it from your own C<xs_init> function.
1952    
1953     =item staticperl_cleanup ()
1954    
1955     In the unlikely case that you want to destroy the perl interpreter, here
1956     is the corresponding function.
1957    
1958     =item PerlInterpreter *staticperl
1959    
1960     The perl interpreter pointer used by staticperl. Not normally so useful,
1961     but there it is.
1962    
1963     =back
1964    
1965     =item bundle.ccopts
1966    
1967     Contains the compiler options required to compile at least F<bundle.c> and
1968     any file that includes F<bundle.h> - you should probably use it in your
1969     C<CFLAGS>.
1970    
1971     =item bundle.ldopts
1972    
1973     The linker options needed to link the final program.
1974    
1975     =back
1976    
1977     =head1 RUNTIME FUNCTIONALITY
1978    
1979     Binaries created with C<mkbundle>/C<mkperl> contain extra functions, which
1980     are required to access the bundled perl sources, but might be useful for
1981     other purposes.
1982    
1983     In addition, for the embedded loading of perl files to work, F<staticperl>
1984     overrides the C<@INC> array.
1985    
1986     =over 4
1987    
1988     =item $file = staticperl::find $path
1989    
1990     Returns the data associated with the given C<$path>
1991     (e.g. C<Digest/MD5.pm>, C<auto/POSIX/autosplit.ix>), which is basically
1992     the UNIX path relative to the perl library directory.
1993    
1994     Returns C<undef> if the file isn't embedded.
1995    
1996 root 1.8 =item @paths = staticperl::list
1997 root 1.7
1998     Returns the list of all paths embedded in this binary.
1999    
2000     =back
2001    
2002 root 1.8 =head1 FULLY STATIC BINARIES - BUILDROOT
2003    
2004     To make truly static (Linux-) libraries, you might want to have a look at
2005     buildroot (L<http://buildroot.uclibc.org/>).
2006    
2007     Buildroot is primarily meant to set up a cross-compile environment (which
2008     is not so useful as perl doesn't quite like cross compiles), but it can also compile
2009     a chroot environment where you can use F<staticperl>.
2010    
2011     To do so, download buildroot, and enable "Build options => development
2012     files in target filesystem" and optionally "Build options => gcc
2013     optimization level (optimize for size)". At the time of writing, I had
2014     good experiences with GCC 4.4.x but not GCC 4.5.
2015    
2016     To minimise code size, I used C<-pipe -ffunction-sections -fdata-sections
2017     -finline-limit=8 -fno-builtin-strlen -mtune=i386>. The C<-mtune=i386>
2018     doesn't decrease codesize much, but it makes the file much more
2019     compressible.
2020    
2021     If you don't need Coro or threads, you can go with "linuxthreads.old" (or
2022     no thread support). For Coro, it is highly recommended to switch to a
2023     uClibc newer than 0.9.31 (at the time of this writing, I used the 20101201
2024     snapshot) and enable NPTL, otherwise Coro needs to be configured with the
2025     ultra-slow pthreads backend to work around linuxthreads bugs (it also uses
2026     twice the address space needed for stacks).
2027    
2028     If you use C<linuxthreads.old>, then you should also be aware that
2029     uClibc shares C<errno> between all threads when statically linking. See
2030     L<http://lists.uclibc.org/pipermail/uclibc/2010-June/044157.html> for a
2031     workaround (And L<https://bugs.uclibc.org/2089> for discussion).
2032    
2033 root 1.10 C<ccache> support is also recommended, especially if you want
2034     to play around with buildroot options. Enabling the C<miniperl>
2035     package will probably enable all options required for a successful
2036     perl build. F<staticperl> itself additionally needs either C<wget>
2037     (recommended, for CPAN) or C<curl>.
2038 root 1.8
2039     As for shells, busybox should provide all that is needed, but the default
2040     busybox configuration doesn't include F<comm> which is needed by perl -
2041     either make a custom busybox config, or compile coreutils.
2042    
2043     For the latter route, you might find that bash has some bugs that keep
2044     it from working properly in a chroot - either use dash (and link it to
2045     F</bin/sh> inside the chroot) or link busybox to F</bin/sh>, using it's
2046     built-in ash shell.
2047    
2048     Finally, you need F</dev/null> inside the chroot for many scripts to work
2049     - F<cp /dev/null output/target/dev> or bind-mounting your F</dev> will
2050     both provide this.
2051    
2052     After you have compiled and set up your buildroot target, you can copy
2053     F<staticperl> from the C<App::Staticperl> distribution or from your
2054     perl f<bin> directory (if you installed it) into the F<output/target>
2055     filesystem, chroot inside and run it.
2056    
2057 root 1.1 =head1 AUTHOR
2058    
2059     Marc Lehmann <schmorp@schmorp.de>
2060     http://software.schmorp.de/pkg/staticperl.html
2061