ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/bin/staticperl
Revision: 1.78
Committed: Sun Jan 12 09:41:38 2014 UTC (12 years, 9 months ago) by root
Branch: MAIN
Changes since 1.77: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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