ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/bin/staticperl
Revision: 1.95
Committed: Tue Feb 15 18:37:34 2022 UTC (4 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-1_46
Changes since 1.94: +2 -2 lines
Log Message:
1.46

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