ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/bin/staticperl
Revision: 1.98
Committed: Tue Aug 1 03:49:00 2023 UTC (3 years, 2 months ago) by root
Branch: MAIN
Changes since 1.97: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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