ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/staticperl.sh
Revision: 1.40
Committed: Sun May 1 07:56:26 2011 UTC (13 years, 2 months ago) by root
Content type: application/x-sh
Branch: MAIN
Changes since 1.39: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

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