ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/bin/staticperl
Revision: 1.90
Committed: Sun Jul 8 23:30:49 2018 UTC (8 years, 3 months ago) by root
Branch: MAIN
Changes since 1.89: +4 -0 lines
Log Message:
*** empty log message ***

File Contents

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