ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/bin/staticperl
Revision: 1.83
Committed: Thu Sep 18 03:57:14 2014 UTC (12 years ago) by root
Branch: MAIN
Changes since 1.82: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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