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