ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/App-Staticperl/staticperl.sh
Revision: 1.42
Committed: Sun May 1 10:03:29 2011 UTC (13 years, 2 months ago) by root
Content type: application/x-sh
Branch: MAIN
CVS Tags: rel-1_22
Changes since 1.41: +3 -0 lines
Log Message:
1.22

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.22"
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
162 either curl or wget is required for automatic download.
163 curl is tried first, then wget.
164 EOF
165
166 rm -f perl-$PERL_VERSION.tar.$BZ2~ # just to be on the safe side
167 curl -f >perl-$PERL_VERSION.tar.$BZ2~ "$URL" \
168 || wget -O perl-$PERL_VERSION.tar.$BZ2~ "$URL" \
169 || fatal "$URL: unable to download"
170 rm -f perl-$PERL_VERSION.tar.$BZ2
171 mv perl-$PERL_VERSION.tar.$BZ2~ perl-$PERL_VERSION.tar.$BZ2
172 fi
173
174 verblock <<EOF
175 unpacking perl
176 EOF
177
178 mkdir -p unpack
179 rm -rf unpack/perl-$PERL_VERSION
180 $BZIP2 -d <perl-$PERL_VERSION.tar.$BZ2 | ( cd unpack && tar xf - ) \
181 || fatal "perl-$PERL_VERSION.tar.$BZ2: error during unpacking"
182 chmod -R u+w unpack/perl-$PERL_VERSION
183 mv unpack/perl-$PERL_VERSION perl-$PERL_VERSION
184 rmdir -p unpack
185 fi
186 }
187
188 # similar to GNU-sed -i or perl -pi
189 sedreplace() {
190 sed -e "$1" <"$2" > "$2~" || fatal "error while running sed"
191 rm -f "$2"
192 mv "$2~" "$2"
193 }
194
195 configure_failure() {
196 cat <<EOF
197
198
199 ***
200 *** Configure failed - see above for the exact error message(s).
201 ***
202 *** Most commonly, this is because the default PERL_CCFLAGS or PERL_OPTIMIZE
203 *** flags are not supported by your compiler. Less often, this is because
204 *** PERL_LIBS either contains a library not available on your system (such as
205 *** -lcrypt), or because it lacks a required library (e.g. -lsocket or -lnsl).
206 ***
207 *** You can provide your own flags by creating a ~/.staticperlrc file with
208 *** variable assignments. For example (these are the actual values used):
209 ***
210
211 PERL_CC="$PERL_CC"
212 PERL_CCFLAGS="$PERL_CCFLAGS"
213 PERL_OPTIMIZE="$PERL_OPTIMIZE"
214 PERL_LDFLAGS="$PERL_LDFLAGS"
215 PERL_LIBS="$PERL_LIBS"
216
217 EOF
218 exit 1
219 }
220
221 configure() {
222 fetch
223
224 rcd "$STATICPERL/src/perl-$PERL_VERSION"
225
226 [ -e staticstamp.configure ] && return
227
228 verblock <<EOF
229 configuring $STATICPERL/src/perl-$PERL_VERSION
230 EOF
231
232 rm -f "$PERL_PREFIX/staticstamp.install"
233
234 "$MAKE" distclean >/dev/null 2>&1
235
236 sedreplace '/^#define SITELIB/d' config_h.SH
237
238 # I hate them for this
239 grep -q -- -fstack-protector Configure && \
240 sedreplace 's/-fstack-protector/-fno-stack-protector/g' Configure
241
242 # what did that bloke think
243 grep -q -- usedl=.define hints/darwin.sh && \
244 sedreplace '/^usedl=.define.;$/d' hints/darwin.sh
245
246 preconfigure || fatal "preconfigure hook failed"
247
248 # trace configure \
249 sh Configure -Duselargefiles \
250 -Uuse64bitint \
251 -Dusemymalloc=n \
252 -Uusedl \
253 -Uusethreads \
254 -Uuseithreads \
255 -Uusemultiplicity \
256 -Uusesfio \
257 -Uuseshrplib \
258 -Uinstallusrbinperl \
259 -A ccflags=" $PERL_CCFLAGS" \
260 -Dcc="$PERL_CC" \
261 -Doptimize="$PERL_OPTIMIZE" \
262 -Dldflags="$PERL_LDFLAGS" \
263 -Dlibs="$PERL_LIBS" \
264 -Dprefix="$PERL_PREFIX" \
265 -Dbin="$PERL_PREFIX/bin" \
266 -Dprivlib="$PERL_PREFIX/lib" \
267 -Darchlib="$PERL_PREFIX/lib" \
268 -Uusevendorprefix \
269 -Dsitelib="$PERL_PREFIX/lib" \
270 -Dsitearch="$PERL_PREFIX/lib" \
271 -Uman1dir \
272 -Uman3dir \
273 -Usiteman1dir \
274 -Usiteman3dir \
275 -Dpager=/usr/bin/less \
276 -Demail="$EMAIL" \
277 -Dcf_email="$EMAIL" \
278 -Dcf_by="$EMAIL" \
279 $PERL_CONFIGURE \
280 -Duseperlio \
281 -dE || configure_failure
282
283 sedreplace '
284 s/-Wl,--no-gc-sections/-Wl,--gc-sections/g
285 s/ *-fno-stack-protector */ /g
286 ' config.sh
287
288 patchconfig || fatal "patchconfig hook failed"
289
290 sh Configure -S || fatal "Configure -S failed"
291
292 postconfigure || fatal "postconfigure hook failed"
293
294 touch staticstamp.configure
295 }
296
297 write_shellscript() {
298 {
299 echo "#!/bin/sh"
300 echo "STATICPERL=\"$STATICPERL\""
301 echo "PERL_PREFIX=\"$PERL_PREFIX\""
302 echo "MAKE=\"$MAKE\""
303 cat
304 } >"$PERL_PREFIX/bin/$1"
305 chmod 755 "$PERL_PREFIX/bin/$1"
306 }
307
308 build() {
309 configure
310
311 rcd "$STATICPERL/src/perl-$PERL_VERSION"
312
313 verblock <<EOF
314 building $STATICPERL/src/perl-$PERL_VERSION
315 EOF
316
317 rm -f "$PERL_PREFIX/staticstamp.install"
318
319 "$MAKE" || fatal "make: error while building perl"
320
321 postbuild || fatal "postbuild hook failed"
322 }
323
324 install() {
325 if ! [ -e "$PERL_PREFIX/staticstamp.install" ]; then
326 build
327
328 verblock <<EOF
329 installing $STATICPERL/src/perl-$PERL_VERSION
330 to $PERL_PREFIX
331 EOF
332
333 ln -sf "perl/bin/" "$STATICPERL/bin"
334 ln -sf "perl/lib/" "$STATICPERL/lib"
335
336 mkdir "$STATICPERL/patched"
337
338 ln -sf "$PERL_PREFIX" "$STATICPERL/perl" # might get overwritten
339 rm -rf "$PERL_PREFIX" # by this rm -rf
340
341 "$MAKE" install || fatal "make install: error while installing"
342
343 rcd "$PERL_PREFIX"
344
345 # create a "make install" replacement for CPAN
346 write_shellscript SP-make-install-make <<'EOF'
347 #CAT make-install-make.sh
348 EOF
349
350 # create a "patch modules" helper
351 write_shellscript SP-patch-postinstall <<'EOF'
352 #CAT patch-postinstall.sh
353 EOF
354
355 "$PERL_PREFIX/bin/SP-patch-postinstall"
356
357 # help to trick CPAN into avoiding ~/.cpan completely
358 echo 1 >"$PERL_PREFIX/lib/CPAN/MyConfig.pm"
359
360 # we call cpan with -MCPAN::MyConfig in this script, which
361 # is strictly unnecssary as we have to patch CPAN anyway,
362 # so consider it "for good measure".
363 "$PERL_PREFIX"/bin/perl -MCPAN::MyConfig -MCPAN -e '
364 CPAN::Shell->o (conf => urllist => push => "'"$CPAN"'");
365 CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
366 CPAN::Shell->o (conf => q<init>);
367 CPAN::Shell->o (conf => q<cpan_home>, "'"$STATICPERL"'/cpan");
368 CPAN::Shell->o (conf => q<build_dir>, "'"$STATICPERL"'/cpan/build");
369 CPAN::Shell->o (conf => q<prefs_dir>, "'"$STATICPERL"'/cpan/prefs");
370 CPAN::Shell->o (conf => q<histfile> , "'"$STATICPERL"'/cpan/histfile");
371 CPAN::Shell->o (conf => q<keep_source_where>, "'"$STATICPERL"'/cpan/sources");
372 CPAN::Shell->o (conf => q<make_install_make_command>, "'"$PERL_PREFIX"'/bin/SP-make-install-make");
373 CPAN::Shell->o (conf => q<prerequisites_policy>, q<follow>);
374 CPAN::Shell->o (conf => q<build_requires_install_policy>, q<no>);
375 CPAN::Shell->o (conf => q<prefer_installer>, "EUMM");
376 CPAN::Shell->o (conf => q<commit>);
377 ' || fatal "error while initialising CPAN"
378
379 touch "$PERL_PREFIX/staticstamp.install"
380 fi
381
382 if ! [ -e "$PERL_PREFIX/staticstamp.postinstall" ]; then
383 NOCHECK_INSTALL=+
384 instcpan $STATICPERL_MODULES
385 [ $EXTRA_MODULES ] && instcpan $EXTRA_MODULES
386
387 postinstall || fatal "postinstall hook failed"
388
389 touch "$PERL_PREFIX/staticstamp.postinstall"
390 fi
391 }
392
393 #############################################################################
394 # install a module from CPAN
395
396 instcpan() {
397 [ $NOCHECK_INSTALL ] || install
398
399 verblock <<EOF
400 installing modules from CPAN
401 $@
402 EOF
403
404 for mod in "$@"; do
405 "$PERL_PREFIX"/bin/perl -MCPAN::MyConfig -MCPAN -e 'notest install => "'"$mod"'"' \
406 || fatal "$mod: unable to install from CPAN"
407 done
408 rm -rf "$STATICPERL/build"
409 }
410
411 #############################################################################
412 # install a module from unpacked sources
413
414 instsrc() {
415 [ $NOCHECK_INSTALL ] || install
416
417 verblock <<EOF
418 installing modules from source
419 $@
420 EOF
421
422 for mod in "$@"; do
423 echo
424 echo $mod
425 (
426 rcd $mod
427 "$MAKE" -f Makefile.aperl map_clean >/dev/null 2>&1
428 "$MAKE" distclean >/dev/null 2>&1
429 "$PERL_PREFIX"/bin/perl Makefile.PL || fatal "$mod: error running Makefile.PL"
430 "$MAKE" || fatal "$mod: error building module"
431 "$PERL_PREFIX"/bin/SP-make-install-make install || fatal "$mod: error installing module"
432 "$MAKE" distclean >/dev/null 2>&1
433 exit 0
434 ) || exit $?
435 done
436 }
437
438 #############################################################################
439 # main
440
441 podusage() {
442 echo
443
444 if [ -e "$PERL_PREFIX/bin/perl" ]; then
445 "$PERL_PREFIX/bin/perl" -MPod::Usage -e \
446 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
447 2>/dev/null && exit
448 fi
449
450 # try whatever perl we can find
451 perl -MPod::Usage -e \
452 'pod2usage -input => *STDIN, -output => *STDOUT, -verbose => '$1', -exitval => 0, -noperldoc => 1' <"$0" \
453 2>/dev/null && exit
454
455 fatal "displaying documentation requires a working perl - try '$0 install' to build one in a safe location"
456 }
457
458 usage() {
459 podusage 0
460 }
461
462 catmkbundle() {
463 {
464 read dummy
465 echo "#!$PERL_PREFIX/bin/perl"
466 cat
467 } <<'MKBUNDLE'
468 #CAT mkbundle
469 MKBUNDLE
470 }
471
472 bundle() {
473 MKBUNDLE="${MKBUNDLE:=$PERL_PREFIX/bin/SP-mkbundle}"
474 catmkbundle >"$MKBUNDLE~" || fatal "$MKBUNDLE~: cannot create"
475 chmod 755 "$MKBUNDLE~" && mv "$MKBUNDLE~" "$MKBUNDLE"
476 CACHE="$STATICPERL/cache"
477 mkdir -p "$CACHE"
478 "$PERL_PREFIX/bin/perl" -- "$MKBUNDLE" --cache "$CACHE" "$@"
479 }
480
481 if [ $# -gt 0 ]; then
482 while [ $# -gt 0 ]; do
483 mkdir -p "$STATICPERL" || fatal "$STATICPERL: cannot create"
484 mkdir -p "$PERL_PREFIX" || fatal "$PERL_PREFIX: cannot create"
485
486 command="${1#--}"; shift
487 case "$command" in
488 version )
489 echo "staticperl version $VERSION"
490 ;;
491 fetch | configure | build | install | clean | realclean | distclean)
492 ( "$command" ) || exit
493 ;;
494 instsrc )
495 ( instsrc "$@" ) || exit
496 exit
497 ;;
498 instcpan )
499 ( instcpan "$@" ) || exit
500 exit
501 ;;
502 perl )
503 ( install ) || exit
504 exec "$PERL_PREFIX/bin/perl" "$@"
505 exit
506 ;;
507 cpan )
508 ( install ) || exit
509 exec "$PERL_PREFIX/bin/cpan" "$@"
510 exit
511 ;;
512 mkbundle )
513 ( install ) || exit
514 bundle "$@"
515 exit
516 ;;
517 mkperl )
518 ( install ) || exit
519 bundle --perl "$@"
520 exit
521 ;;
522 mkapp )
523 ( install ) || exit
524 bundle --app "$@"
525 exit
526 ;;
527 help )
528 podusage 2
529 ;;
530 * )
531 exec 1>&2
532 echo
533 echo "Unknown command: $command"
534 podusage 0
535 ;;
536 esac
537 done
538 else
539 usage
540 fi
541
542 exit 0
543
544 #CAT staticperl.pod
545