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