ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/App-Staticperl/staticperl.sh
Revision: 1.52
Committed: Mon Mar 5 07:14:32 2012 UTC (12 years, 3 months ago) by root
Content type: application/x-sh
Branch: MAIN
CVS Tags: rel-1_41
Changes since 1.51: +1 -1 lines
Log Message:
1.41

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.4 # 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" # -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.41"
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"
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~ >/dev/null 2>&1; } \
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 : > 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 _postinstall() {
336 if ! [ -e "$PERL_PREFIX/staticstamp.postinstall" ]; then
337 NOCHECK_INSTALL=+
338 instcpan $STATICPERL_MODULES
339 [ $EXTRA_MODULES ] && instcpan $EXTRA_MODULES
340
341 postinstall || fatal "postinstall hook failed"
342
343 : > "$PERL_PREFIX/staticstamp.postinstall"
344 fi
345 }
346
347 install() {
348 if ! [ -e "$PERL_PREFIX/staticstamp.install" ]; then
349 build
350
351 verblock <<EOF
352 installing $STATICPERL/src/perl-$PERL_VERSION
353 to $PERL_PREFIX
354 EOF
355
356 ln -sf "perl/bin/" "$STATICPERL/bin"
357 ln -sf "perl/lib/" "$STATICPERL/lib"
358
359 mkdir "$STATICPERL/patched"
360
361 ln -sf "$PERL_PREFIX" "$STATICPERL/perl" # might get overwritten
362 rm -rf "$PERL_PREFIX" # by this rm -rf
363
364 "$MAKE" install || fatal "make install: error while installing"
365
366 rcd "$PERL_PREFIX"
367
368 # create a "make install" replacement for CPAN
369 write_shellscript SP-make-install-make <<'EOF'
370 #CAT make-install-make.sh
371 EOF
372
373 # create a "patch modules" helper
374 write_shellscript SP-patch-postinstall <<'EOF'
375 #CAT patch-postinstall.sh
376 EOF
377
378 "$PERL_PREFIX/bin/SP-patch-postinstall"
379
380 # help to trick CPAN into avoiding ~/.cpan completely
381 echo 1 >"$PERL_PREFIX/lib/CPAN/MyConfig.pm"
382
383 # we call cpan with -MCPAN::MyConfig in this script, which
384 # is strictly unnecssary as we have to patch CPAN anyway,
385 # so consider it "for good measure".
386 "$PERL_PREFIX"/bin/perl -MCPAN::MyConfig -MCPAN -e '
387 CPAN::Shell->o (conf => urllist => push => "'"$CPAN"'");
388 CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
389 CPAN::Shell->o (conf => q<init>);
390 CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
391 CPAN::Shell->o (conf => q<build_dir>, "'"$STATICPERL"'/cpan/build");
392 CPAN::Shell->o (conf => q<prefs_dir>, "'"$STATICPERL"'/cpan/prefs");
393 CPAN::Shell->o (conf => q<histfile> , "'"$STATICPERL"'/cpan/histfile");
394 CPAN::Shell->o (conf => q<keep_source_where>, "'"$STATICPERL"'/cpan/sources");
395 CPAN::Shell->o (conf => q<make_install_make_command>, "'"$PERL_PREFIX"'/bin/SP-make-install-make");
396 CPAN::Shell->o (conf => q<prerequisites_policy>, q<follow>);
397 CPAN::Shell->o (conf => q<build_requires_install_policy>, q<no>);
398 CPAN::Shell->o (conf => q<prefer_installer>, "EUMM");
399 CPAN::Shell->o (conf => q<commit>);
400 ' || fatal "error while initialising CPAN"
401
402 : > "$PERL_PREFIX/staticstamp.install"
403 fi
404
405 _postinstall
406 }
407
408 import() {
409 IMPORT="$1"
410
411 rcd "$STATICPERL"
412
413 if ! [ -e "$PERL_PREFIX/staticstamp.install" ]; then
414 verblock <<EOF
415 import perl from $IMPORT to $STATICPERL
416 EOF
417
418 rm -rf bin cpan lib patched perl src
419 mkdir -m 755 perl perl/bin
420 ln -s perl/bin/ bin
421 ln -s "$IMPORT" perl/bin/
422
423 echo "$IMPORT" > "$PERL_PREFIX/.import"
424
425 : > "$PERL_PREFIX/staticstamp.install"
426 fi
427
428 _postinstall
429 }
430
431 #############################################################################
432 # install a module from CPAN
433
434 instcpan() {
435 [ $NOCHECK_INSTALL ] || install
436
437 verblock <<EOF
438 installing modules from CPAN
439 $@
440 EOF
441
442 MYCONFIG=
443 [ -e "$PERL_PREFIX/.import" ] || MYCONFIG=-MCPAN::MyConfig
444
445 "$PERL_PREFIX"/bin/perl $MYCONFIG -MCPAN -e 'notest (install => $_) for @ARGV' -- "$@" | tee "$STATICPERL/instcpan.log"
446
447 if grep -q " -- NOT OK\$" "$STATICPERL/instcpan.log"; then
448 fatal "failure while installing modules from CPAN ($@)"
449 fi
450 rm -f "$STATICPERL/instcpan.log"
451 }
452
453 #############################################################################
454 # install a module from unpacked sources
455
456 instsrc() {
457 [ $NOCHECK_INSTALL ] || install
458
459 verblock <<EOF
460 installing modules from source
461 $@
462 EOF
463
464 for mod in "$@"; do
465 echo
466 echo $mod
467 (
468 rcd $mod
469 "$MAKE" -f Makefile.aperl map_clean >/dev/null 2>&1
470 "$MAKE" distclean >/dev/null 2>&1
471 "$PERL_PREFIX"/bin/perl Makefile.PL || fatal "$mod: error running Makefile.PL"
472 "$MAKE" || fatal "$mod: error building module"
473 "$PERL_PREFIX"/bin/SP-make-install-make install || fatal "$mod: error installing module"
474 "$MAKE" distclean >/dev/null 2>&1
475 exit 0
476 ) || exit $?
477 done
478 }
479
480 #############################################################################
481 # main
482
483 podusage() {
484 echo
485
486 if [ -e "$PERL_PREFIX/bin/perl" ]; then
487 "$PERL_PREFIX/bin/perl" -MPod::Usage -e \
488 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
489 2>/dev/null && exit
490 fi
491
492 # try whatever perl we can find
493 perl -MPod::Usage -e \
494 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
495 2>/dev/null && exit
496
497 fatal "displaying documentation requires a working perl - try '$0 install' to build one in a safe location"
498 }
499
500 usage() {
501 podusage 0
502 }
503
504 catmkbundle() {
505 {
506 read dummy
507 echo "#!$PERL_PREFIX/bin/perl"
508 cat
509 } <<'MKBUNDLE'
510 #CAT mkbundle
511 MKBUNDLE
512 }
513
514 bundle() {
515 MKBUNDLE="${MKBUNDLE:=$PERL_PREFIX/bin/SP-mkbundle}"
516 catmkbundle >"$MKBUNDLE~" || fatal "$MKBUNDLE~: cannot create"
517 chmod 755 "$MKBUNDLE~" && mv "$MKBUNDLE~" "$MKBUNDLE"
518 CACHE="$STATICPERL/cache"
519 mkdir -p "$CACHE"
520 "$PERL_PREFIX/bin/perl" -- "$MKBUNDLE" --cache "$CACHE" "$@"
521 }
522
523 if [ $# -gt 0 ]; then
524 while [ $# -gt 0 ]; do
525 mkdir -p "$STATICPERL" || fatal "$STATICPERL: cannot create"
526 mkdir -p "$PERL_PREFIX" || fatal "$PERL_PREFIX: cannot create"
527
528 command="${1#--}"; shift
529 case "$command" in
530 version )
531 echo "staticperl version $VERSION"
532 ;;
533 fetch | configure | build | install | clean | realclean | distclean )
534 ( "$command" ) || exit
535 ;;
536 import )
537 ( import "$1" ) || exit
538 shift
539 ;;
540 instsrc )
541 ( instsrc "$@" ) || exit
542 exit
543 ;;
544 instcpan )
545 ( instcpan "$@" ) || exit
546 exit
547 ;;
548 perl )
549 ( install ) || exit
550 exec "$PERL_PREFIX/bin/perl" "$@"
551 exit
552 ;;
553 cpan )
554 ( install ) || exit
555 PERL="$PERL_PREFIX/bin/perl"
556 export PERL
557 exec "$PERL_PREFIX/bin/cpan" "$@"
558 exit
559 ;;
560 mkbundle )
561 ( install ) || exit
562 bundle "$@"
563 exit
564 ;;
565 mkperl )
566 ( install ) || exit
567 bundle --perl "$@"
568 exit
569 ;;
570 mkapp )
571 ( install ) || exit
572 bundle --app "$@"
573 exit
574 ;;
575 help )
576 podusage 2
577 ;;
578 * )
579 exec 1>&2
580 echo
581 echo "Unknown command: $command"
582 podusage 0
583 ;;
584 esac
585 done
586 else
587 usage
588 fi
589
590 exit 0
591
592 #CAT staticperl.pod
593