ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/bin/staticperl
Revision: 1.91
Committed: Fri Oct 19 22:25:42 2018 UTC (8 years ago) by root
Branch: MAIN
CVS Tags: rel-1_45
Changes since 1.90: +51 -7 lines
Log Message:
*** empty log message ***

File Contents

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