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