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