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