ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/staticperl.sh
Revision: 1.47
Committed: Mon Jun 27 21:56:41 2011 UTC (13 years ago) by root
Content type: application/x-sh
Branch: MAIN
Changes since 1.46: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/bin/sh
2
3 #############################################################################
4 # configuration to fill in (or to replace in your .staticperlrc)
5
6 STATICPERL=~/.staticperl
7 CPAN=http://mirror.netcologne.de/cpan # which mirror to use
8 EMAIL="read the documentation <rtfm@example.org>"
9 DLCACHE=
10
11 # perl build variables
12 MAKE=make
13 PERL_VERSION=5.12.3 # 5.8.9 is also a good choice
14 PERL_CC=cc
15 PERL_CONFIGURE="" # additional Configure arguments
16 PERL_CCFLAGS="-g -DPERL_DISABLE_PMC -DPERL_ARENA_SIZE=16376 -DNO_PERL_MALLOC_ENV -D_GNU_SOURCE -DNDEBUG"
17 PERL_OPTIMIZE="-Os -ffunction-sections -fdata-sections -finline-limit=8 -ffast-math"
18
19 ARCH="$(uname -m)"
20
21 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
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 # --allow-multiple-definition exists to work around uclibc's pthread static linking bug
35 #PERL_LDFLAGS="-Wl,--no-gc-sections -Wl,--allow-multiple-definition"
36 PERL_LDFLAGS=
37 PERL_LIBS="-lm -lcrypt" # perl loves to add lotsa crap itself
38
39 # some configuration options for modules
40 PERL_MM_USE_DEFAULT=1
41 PERL_MM_OPT="MAN1PODS= MAN3PODS="
42 #CORO_INTERFACE=p # needed without nptl on x86, due to bugs in linuxthreads - very slow
43 #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
46 # which extra modules to install by default from CPAN that are
47 # required by mkbundle
48 STATICPERL_MODULES="common::sense Pod::Strip PPI::XS Pod::Usage"
49
50 # which extra modules you might want to install
51 EXTRA_MODULES=""
52
53 # overridable functions
54 preconfigure() { : ; }
55 patchconfig() { : ; }
56 postconfigure() { : ; }
57 postbuild() { : ; }
58 postinstall() { : ; }
59
60 # now source user config, if any
61 if [ "$STATICPERLRC" ]; then
62 . "$STATICPERLRC"
63 else
64 [ -r /etc/staticperlrc ] && . /etc/staticperlrc
65 [ -r ~/.staticperlrc ] && . ~/.staticperlrc
66 [ -r "$STATICPERL/rc" ] && . "$STATICPERL/rc"
67 fi
68
69 #############################################################################
70 # support
71
72 PERL_PREFIX="${PERL_PREFIX:=$STATICPERL/perl}" # where the perl gets installed
73
74 unset PERL5OPT PERL5LIB PERLLIB PERL_UNICODE PERLIO_DEBUG
75 unset PERL_MB_OPT
76 LC_ALL=C; export LC_ALL # just to be on the safe side
77
78 # prepend PATH - not required by staticperl itself, but might make
79 # life easier when working in e.g. "staticperl cpan / look"
80 PATH="$PERL_PREFIX/perl/bin:$PATH"
81
82 # set version in a way that Makefile.PL can extract
83 VERSION=VERSION; eval \
84 $VERSION="1.31"
85
86 BZ2=bz2
87 BZIP2=bzip2
88
89 fatal() {
90 printf -- "\nFATAL: %s\n\n" "$*" >&2
91 exit 1
92 }
93
94 verbose() {
95 printf -- "%s\n" "$*"
96 }
97
98 verblock() {
99 verbose
100 verbose "***"
101 while read line; do
102 verbose "*** $line"
103 done
104 verbose "***"
105 verbose
106 }
107
108 rcd() {
109 cd "$1" || fatal "$1: cannot enter"
110 }
111
112 trace() {
113 prefix="$1"; shift
114 # "$@" 2>&1 | while read line; do
115 # echo "$prefix: $line"
116 # done
117 "$@"
118 }
119
120 trap wait 0
121
122 #############################################################################
123 # clean
124
125 distclean() {
126 verblock <<EOF
127 deleting everything installed by this script (rm -rf $STATICPERL)
128 EOF
129
130 rm -rf "$STATICPERL"
131 }
132
133 #############################################################################
134 # download/configure/compile/install perl
135
136 clean() {
137 rm -rf "$STATICPERL/src/perl-$PERL_VERSION"
138 }
139
140 realclean() {
141 rm -f "$PERL_PREFIX/staticstamp.postinstall"
142 rm -f "$PERL_PREFIX/staticstamp.install"
143 rm -f "$STATICPERL/src/perl-"*"/staticstamp.configure"
144 }
145
146 fetch() {
147 rcd "$STATICPERL"
148
149 mkdir -p src
150 rcd src
151
152 if ! [ -d "perl-$PERL_VERSION" ]; then
153 PERLTAR=perl-$PERL_VERSION.tar.$BZ2
154
155 if ! [ -e $PERLTAR ]; then
156 URL="$CPAN/src/5.0/$PERLTAR"
157
158 verblock <<EOF
159 downloading perl
160 to manually download perl yourself, place
161 perl-$PERL_VERSION.tar.$BZ2 in $STATICPERL
162 trying $URL
163
164 either curl or wget is required for automatic download.
165 curl is tried first, then wget.
166
167 you can configure a download cache directory via DLCACHE
168 in your .staticperlrc to avoid repeated downloads.
169 EOF
170
171 rm -f $PERLTAR~ # just to be on the safe side
172 { [ "$DLCACHE" ] && cp "$DLCACHE"/$PERLTAR $PERLTAR~; } \
173 || wget -O $PERLTAR~ "$URL" \
174 || curl -f >$PERLTAR~ "$URL" \
175 || fatal "$URL: unable to download"
176 rm -f $PERLTAR
177 mv $PERLTAR~ $PERLTAR
178 if [ "$DLCACHE" ]; then
179 mkdir -p "$DLCACHE"
180 cp $PERLTAR "$DLCACHE"/$PERLTAR~ && \
181 mv "$DLCACHE"/$PERLTAR~ "$DLCACHE"/$PERLTAR
182 fi
183 fi
184
185 verblock <<EOF
186 unpacking perl
187 EOF
188
189 mkdir -p unpack
190 rm -rf unpack/perl-$PERL_VERSION
191 $BZIP2 -d <$PERLTAR | ( cd unpack && tar xf - ) \
192 || fatal "$PERLTAR: error during unpacking"
193 chmod -R u+w unpack/perl-$PERL_VERSION
194 mv unpack/perl-$PERL_VERSION perl-$PERL_VERSION
195 rmdir -p unpack
196 fi
197 }
198
199 # similar to GNU-sed -i or perl -pi
200 sedreplace() {
201 sed -e "$1" <"$2" > "$2~" || fatal "error while running sed"
202 rm -f "$2"
203 mv "$2~" "$2"
204 }
205
206 configure_failure() {
207 cat <<EOF
208
209
210 ***
211 *** Configure failed - see above for the exact error message(s).
212 ***
213 *** Most commonly, this is because the default PERL_CCFLAGS or PERL_OPTIMIZE
214 *** flags are not supported by your compiler. Less often, this is because
215 *** PERL_LIBS either contains a library not available on your system (such as
216 *** -lcrypt), or because it lacks a required library (e.g. -lsocket or -lnsl).
217 ***
218 *** You can provide your own flags by creating a ~/.staticperlrc file with
219 *** variable assignments. For example (these are the actual values used):
220 ***
221
222 PERL_CC="$PERL_CC"
223 PERL_CCFLAGS="$PERL_CCFLAGS"
224 PERL_OPTIMIZE="$PERL_OPTIMIZE"
225 PERL_LDFLAGS="$PERL_LDFLAGS"
226 PERL_LIBS="$PERL_LIBS"
227
228 EOF
229 exit 1
230 }
231
232 configure() {
233 fetch
234
235 rcd "$STATICPERL/src/perl-$PERL_VERSION"
236
237 [ -e staticstamp.configure ] && return
238
239 verblock <<EOF
240 configuring $STATICPERL/src/perl-$PERL_VERSION
241 EOF
242
243 rm -f "$PERL_PREFIX/staticstamp.install"
244
245 "$MAKE" distclean >/dev/null 2>&1
246
247 sedreplace '/^#define SITELIB/d' config_h.SH
248
249 # I hate them for this
250 grep -q -- -fstack-protector Configure && \
251 sedreplace 's/-fstack-protector/-fno-stack-protector/g' Configure
252
253 # what did that bloke think
254 grep -q -- usedl=.define hints/darwin.sh && \
255 sedreplace '/^usedl=.define.;$/d' hints/darwin.sh
256
257 preconfigure || fatal "preconfigure hook failed"
258
259 # trace configure \
260 sh Configure -Duselargefiles \
261 -Uuse64bitint \
262 -Dusemymalloc=n \
263 -Uusedl \
264 -Uusethreads \
265 -Uuseithreads \
266 -Uusemultiplicity \
267 -Uusesfio \
268 -Uuseshrplib \
269 -Uinstallusrbinperl \
270 -A ccflags=" $PERL_CCFLAGS" \
271 -Dcc="$PERL_CC" \
272 -Doptimize="$PERL_OPTIMIZE" \
273 -Dldflags="$PERL_LDFLAGS" \
274 -Dlibs="$PERL_LIBS" \
275 -Dprefix="$PERL_PREFIX" \
276 -Dbin="$PERL_PREFIX/bin" \
277 -Dprivlib="$PERL_PREFIX/lib" \
278 -Darchlib="$PERL_PREFIX/lib" \
279 -Uusevendorprefix \
280 -Dsitelib="$PERL_PREFIX/lib" \
281 -Dsitearch="$PERL_PREFIX/lib" \
282 -Uman1dir \
283 -Uman3dir \
284 -Usiteman1dir \
285 -Usiteman3dir \
286 -Dpager=/usr/bin/less \
287 -Demail="$EMAIL" \
288 -Dcf_email="$EMAIL" \
289 -Dcf_by="$EMAIL" \
290 $PERL_CONFIGURE \
291 -Duseperlio \
292 -dE || configure_failure
293
294 sedreplace '
295 s/-Wl,--no-gc-sections/-Wl,--gc-sections/g
296 s/ *-fno-stack-protector */ /g
297 ' config.sh
298
299 patchconfig || fatal "patchconfig hook failed"
300
301 sh Configure -S || fatal "Configure -S failed"
302
303 postconfigure || fatal "postconfigure hook failed"
304
305 touch staticstamp.configure
306 }
307
308 write_shellscript() {
309 {
310 echo "#!/bin/sh"
311 echo "STATICPERL=\"$STATICPERL\""
312 echo "PERL_PREFIX=\"$PERL_PREFIX\""
313 echo "MAKE=\"$MAKE\""
314 cat
315 } >"$PERL_PREFIX/bin/$1"
316 chmod 755 "$PERL_PREFIX/bin/$1"
317 }
318
319 build() {
320 configure
321
322 rcd "$STATICPERL/src/perl-$PERL_VERSION"
323
324 verblock <<EOF
325 building $STATICPERL/src/perl-$PERL_VERSION
326 EOF
327
328 rm -f "$PERL_PREFIX/staticstamp.install"
329
330 "$MAKE" || fatal "make: error while building perl"
331
332 postbuild || fatal "postbuild hook failed"
333 }
334
335 install() {
336 if ! [ -e "$PERL_PREFIX/staticstamp.install" ]; then
337 build
338
339 verblock <<EOF
340 installing $STATICPERL/src/perl-$PERL_VERSION
341 to $PERL_PREFIX
342 EOF
343
344 ln -sf "perl/bin/" "$STATICPERL/bin"
345 ln -sf "perl/lib/" "$STATICPERL/lib"
346
347 mkdir "$STATICPERL/patched"
348
349 ln -sf "$PERL_PREFIX" "$STATICPERL/perl" # might get overwritten
350 rm -rf "$PERL_PREFIX" # by this rm -rf
351
352 "$MAKE" install || fatal "make install: error while installing"
353
354 rcd "$PERL_PREFIX"
355
356 # create a "make install" replacement for CPAN
357 write_shellscript SP-make-install-make <<'EOF'
358 #CAT make-install-make.sh
359 EOF
360
361 # create a "patch modules" helper
362 write_shellscript SP-patch-postinstall <<'EOF'
363 #CAT patch-postinstall.sh
364 EOF
365
366 "$PERL_PREFIX/bin/SP-patch-postinstall"
367
368 # help to trick CPAN into avoiding ~/.cpan completely
369 echo 1 >"$PERL_PREFIX/lib/CPAN/MyConfig.pm"
370
371 # we call cpan with -MCPAN::MyConfig in this script, which
372 # is strictly unnecssary as we have to patch CPAN anyway,
373 # so consider it "for good measure".
374 "$PERL_PREFIX"/bin/perl -MCPAN::MyConfig -MCPAN -e '
375 CPAN::Shell->o (conf => urllist => push => "'"$CPAN"'");
376 CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
377 CPAN::Shell->o (conf => q<init>);
378 CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
379 CPAN::Shell->o (conf => q<build_dir>, "'"$STATICPERL"'/cpan/build");
380 CPAN::Shell->o (conf => q<prefs_dir>, "'"$STATICPERL"'/cpan/prefs");
381 CPAN::Shell->o (conf => q<histfile> , "'"$STATICPERL"'/cpan/histfile");
382 CPAN::Shell->o (conf => q<keep_source_where>, "'"$STATICPERL"'/cpan/sources");
383 CPAN::Shell->o (conf => q<make_install_make_command>, "'"$PERL_PREFIX"'/bin/SP-make-install-make");
384 CPAN::Shell->o (conf => q<prerequisites_policy>, q<follow>);
385 CPAN::Shell->o (conf => q<build_requires_install_policy>, q<no>);
386 CPAN::Shell->o (conf => q<prefer_installer>, "EUMM");
387 CPAN::Shell->o (conf => q<commit>);
388 ' || fatal "error while initialising CPAN"
389
390 touch "$PERL_PREFIX/staticstamp.install"
391 fi
392
393 if ! [ -e "$PERL_PREFIX/staticstamp.postinstall" ]; then
394 NOCHECK_INSTALL=+
395 instcpan $STATICPERL_MODULES
396 [ $EXTRA_MODULES ] && instcpan $EXTRA_MODULES
397
398 postinstall || fatal "postinstall hook failed"
399
400 touch "$PERL_PREFIX/staticstamp.postinstall"
401 fi
402 }
403
404 #############################################################################
405 # install a module from CPAN
406
407 instcpan() {
408 [ $NOCHECK_INSTALL ] || install
409
410 verblock <<EOF
411 installing modules from CPAN
412 $@
413 EOF
414
415 for mod in "$@"; do
416 "$PERL_PREFIX"/bin/perl -MCPAN::MyConfig -MCPAN -e 'notest install => "'"$mod"'"' \
417 || fatal "$mod: unable to install from CPAN"
418 done
419 rm -rf "$STATICPERL/build"
420 }
421
422 #############################################################################
423 # install a module from unpacked sources
424
425 instsrc() {
426 [ $NOCHECK_INSTALL ] || install
427
428 verblock <<EOF
429 installing modules from source
430 $@
431 EOF
432
433 for mod in "$@"; do
434 echo
435 echo $mod
436 (
437 rcd $mod
438 "$MAKE" -f Makefile.aperl map_clean >/dev/null 2>&1
439 "$MAKE" distclean >/dev/null 2>&1
440 "$PERL_PREFIX"/bin/perl Makefile.PL || fatal "$mod: error running Makefile.PL"
441 "$MAKE" || fatal "$mod: error building module"
442 "$PERL_PREFIX"/bin/SP-make-install-make install || fatal "$mod: error installing module"
443 "$MAKE" distclean >/dev/null 2>&1
444 exit 0
445 ) || exit $?
446 done
447 }
448
449 #############################################################################
450 # main
451
452 podusage() {
453 echo
454
455 if [ -e "$PERL_PREFIX/bin/perl" ]; then
456 "$PERL_PREFIX/bin/perl" -MPod::Usage -e \
457 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
458 2>/dev/null && exit
459 fi
460
461 # try whatever perl we can find
462 perl -MPod::Usage -e \
463 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
464 2>/dev/null && exit
465
466 fatal "displaying documentation requires a working perl - try '$0 install' to build one in a safe location"
467 }
468
469 usage() {
470 podusage 0
471 }
472
473 catmkbundle() {
474 {
475 read dummy
476 echo "#!$PERL_PREFIX/bin/perl"
477 cat
478 } <<'MKBUNDLE'
479 #CAT mkbundle
480 MKBUNDLE
481 }
482
483 bundle() {
484 MKBUNDLE="${MKBUNDLE:=$PERL_PREFIX/bin/SP-mkbundle}"
485 catmkbundle >"$MKBUNDLE~" || fatal "$MKBUNDLE~: cannot create"
486 chmod 755 "$MKBUNDLE~" && mv "$MKBUNDLE~" "$MKBUNDLE"
487 CACHE="$STATICPERL/cache"
488 mkdir -p "$CACHE"
489 "$PERL_PREFIX/bin/perl" -- "$MKBUNDLE" --cache "$CACHE" "$@"
490 }
491
492 if [ $# -gt 0 ]; then
493 while [ $# -gt 0 ]; do
494 mkdir -p "$STATICPERL" || fatal "$STATICPERL: cannot create"
495 mkdir -p "$PERL_PREFIX" || fatal "$PERL_PREFIX: cannot create"
496
497 command="${1#--}"; shift
498 case "$command" in
499 version )
500 echo "staticperl version $VERSION"
501 ;;
502 fetch | configure | build | install | clean | realclean | distclean)
503 ( "$command" ) || exit
504 ;;
505 instsrc )
506 ( instsrc "$@" ) || exit
507 exit
508 ;;
509 instcpan )
510 ( instcpan "$@" ) || exit
511 exit
512 ;;
513 perl )
514 ( install ) || exit
515 exec "$PERL_PREFIX/bin/perl" "$@"
516 exit
517 ;;
518 cpan )
519 ( install ) || exit
520 exec "$PERL_PREFIX/bin/cpan" "$@"
521 exit
522 ;;
523 mkbundle )
524 ( install ) || exit
525 bundle "$@"
526 exit
527 ;;
528 mkperl )
529 ( install ) || exit
530 bundle --perl "$@"
531 exit
532 ;;
533 mkapp )
534 ( install ) || exit
535 bundle --app "$@"
536 exit
537 ;;
538 help )
539 podusage 2
540 ;;
541 * )
542 exec 1>&2
543 echo
544 echo "Unknown command: $command"
545 podusage 0
546 ;;
547 esac
548 done
549 else
550 usage
551 fi
552
553 exit 0
554
555 #CAT staticperl.pod
556