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