Fix for OSX
[m6w6/libmemcached] / bootstrap.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2012 Brian Aker
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 #
18 # * The names of its contributors may not be used to endorse or
19 # promote products derived from this software without specific prior
20 # written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34
35 # Environment Variables that will influence the build:
36 # AUTOMAKE
37 # AUTORECONF
38 # LIBTOOLIZE
39 # MAKE
40 # PREFIX
41 # TESTS_ENVIRONMENT
42 # VERBOSE
43 # WARNINGS
44 #
45
46 command_not_found_handle ()
47 {
48 echo "Command not found: '$@'"
49 exit 127
50 }
51
52 function die ()
53 {
54 echo "$BASH_SOURCE:$BASH_LINENO: $@" >&2
55 exit 1;
56 }
57
58 function nassert ()
59 {
60 local param_name=\$"$1"
61 local param_value=`eval "expr \"$param_name\" "`
62
63 if [ -n "$param_value" ]; then
64 echo "$bash_source:$bash_lineno: assert($param_name) had value of "$param_value"" >&2
65 exit 1
66 fi
67 }
68
69 function assert ()
70 {
71 local param_name=\$"$1"
72 local param_value=`eval "expr \"$param_name\" "`
73
74 if [ -z "$param_value" ]; then
75 echo "$bash_source:$bash_lineno: assert($param_name)" >&2
76 exit 1
77 fi
78 }
79
80 assert_file ()
81 {
82 if [ ! -f "$1" ]; then
83 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) does not exist: $2" >&2
84 exit 1;
85 fi
86 }
87
88 assert_no_file ()
89 {
90 if [ -f "$1" ]; then
91 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) file exists: $2" >&2
92 exit 1;
93 fi
94 }
95
96 assert_exec_file ()
97 {
98 if [ ! -f "$1" ]; then
99 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) does not exist: $2" >&2
100 exit 1;
101 fi
102
103 if [ ! -x "$1" ]; then
104 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) exists but is not executable: $2" >&2
105 exit 1;
106 fi
107 }
108
109 command_exists ()
110 {
111 type "$1" &> /dev/null ;
112 }
113
114 rebuild_host_os ()
115 {
116 HOST_OS="${UNAME_MACHINE_ARCH}-${VENDOR}-${VENDOR_DISTRIBUTION}-${VENDOR_RELEASE}-${UNAME_KERNEL}-${UNAME_KERNEL_RELEASE}"
117 if [ -z "$1" ]; then
118 if $VERBOSE; then
119 echo "HOST_OS=$HOST_OS"
120 fi
121 fi
122 }
123
124 # Valid values are: darwin,fedora,rhel,ubuntu
125 set_VENDOR_DISTRIBUTION ()
126 {
127 local dist=`echo "$1" | tr '[A-Z]' '[a-z]'`
128 case "$dist" in
129 darwin)
130 VENDOR_DISTRIBUTION='darwin'
131 ;;
132 fedora)
133 VENDOR_DISTRIBUTION='fedora'
134 ;;
135 rhel)
136 VENDOR_DISTRIBUTION='rhel'
137 ;;
138 ubuntu)
139 VENDOR_DISTRIBUTION='ubuntu'
140 ;;
141 opensuse)
142 VENDOR_DISTRIBUTION='opensuse'
143 ;;
144 *)
145 die "attempt to set an invalid VENDOR_DISTRIBUTION=$dist"
146 ;;
147 esac
148 }
149
150 set_VENDOR_RELEASE ()
151 {
152 local release=`echo "$1" | tr '[A-Z]' '[a-z]'`
153 case "$VENDOR_DISTRIBUTION" in
154 darwin)
155 VENDOR_RELEASE='mountain'
156 ;;
157 fedora)
158 VENDOR_RELEASE="$release"
159 ;;
160 rhel)
161 VENDOR_RELEASE="$release"
162 ;;
163 ubuntu)
164 VENDOR_RELEASE="$release"
165 ;;
166 opensuse)
167 VENDOR_RELEASE="$release"
168 ;;
169 unknown)
170 die "attempt to set VENDOR_RELEASE without setting VENDOR_DISTRIBUTION"
171 ;;
172 *)
173 die "attempt to set with an invalid VENDOR_DISTRIBUTION=$VENDOR_DISTRIBUTION"
174 ;;
175 esac
176 }
177
178
179 # Valid values are: apple, redhat, centos, canonical
180 set_VENDOR ()
181 {
182 local vendor=`echo "$1" | tr '[A-Z]' '[a-z]'`
183
184 case $vendor in
185 apple)
186 VENDOR='apple'
187 ;;
188 redhat)
189 VENDOR='redhat'
190 ;;
191 centos)
192 VENDOR='centos'
193 ;;
194 canonical)
195 VENDOR='canonical'
196 ;;
197 suse)
198 VENDOR='suse'
199 ;;
200 *)
201 die "An attempt was made to set an invalid VENDOR=$_vendor"
202 ;;
203 esac
204
205 set_VENDOR_DISTRIBUTION $2
206 set_VENDOR_RELEASE $3
207 }
208
209 determine_target_platform ()
210 {
211 UNAME_MACHINE_ARCH=`(uname -m) 2>/dev/null` || UNAME_MACHINE_ARCH=unknown
212 UNAME_KERNEL=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown
213 UNAME_KERNEL_RELEASE=`(uname -r) 2>/dev/null` || UNAME_KERNEL_RELEASE=unknown
214
215 if [[ $(uname) == 'Darwin' ]]; then
216 set_VENDOR 'apple' 'darwin' 'mountain'
217 elif [[ -f '/etc/fedora-release' ]]; then
218 local fedora_version=`cat /etc/fedora-release | awk ' { print $3 } '`
219 set_VENDOR 'redhat' 'fedora' $fedora_version
220 if [[ "x$VENDOR_RELEASE" == 'x17' ]]; then
221 AUTORECONF_REBUILD_HOST=true
222 fi
223 elif [[ -f '/etc/centos-release' ]]; then
224 local centos_version=`cat /etc/centos-release | awk ' { print $7 } '`
225 set_VENDOR 'centos' 'rhel' $centos_version
226 elif [[ -f '/etc/SuSE-release' ]]; then
227 local suse_distribution=`head -1 /etc/SuSE-release | awk ' { print $1 } '`
228 local suse_version=`head -1 /etc/SuSE-release | awk ' { print $2 } '`
229 set_VENDOR 'suse' $suse_distribution $suse_version
230 elif [[ -f '/etc/redhat-release' ]]; then
231 local rhel_version=`cat /etc/redhat-release | awk ' { print $7 } '`
232 set_VENDOR 'redhat' 'rhel' $rhel_version
233 elif [[ -f '/etc/lsb-release' ]]; then
234 local debian_DISTRIB_ID=`cat /etc/lsb-release | grep DISTRIB_ID | awk -F= ' { print $2 } '`
235 local debian_version=`cat /etc/lsb-release | grep DISTRIB_CODENAME | awk -F= ' { print $2 } '`
236 set_VENDOR 'canonical' $debian_DISTRIB_ID $debian_version
237 if [[ "x$VENDOR_RELEASE" == 'xprecise' ]]; then
238 AUTORECONF_REBUILD_HOST=true
239 fi
240 fi
241
242 rebuild_host_os
243 }
244
245 run_configure ()
246 {
247 # We will run autoreconf if we are required
248 run_autoreconf_if_required
249
250 # We always begin at the root of our build
251 if [ ! popd ]; then
252 die "Programmer error, we entered run_configure with a stacked directory"
253 fi
254
255 local BUILD_DIR="$1"
256 if [[ -n "$BUILD_DIR" ]]; then
257 rm -r -f $BUILD_DIR
258 mkdir -p $BUILD_DIR
259 safe_pushd $BUILD_DIR
260 fi
261
262 # Arguments for configure
263 local CONFIGURE_ARG=
264
265 # Set ENV DEBUG in order to enable debugging
266 if $DEBUG; then
267 CONFIGURE_ARG='--enable-debug'
268 fi
269
270 # Set ENV ASSERT in order to enable assert
271 if [[ -n "$ASSERT" ]]; then
272 local ASSERT_ARG=
273 ASSERT_ARG='--enable-assert'
274 CONFIGURE_ARG="$ASSERT_ARG $CONFIGURE_ARG"
275 fi
276
277 # If we are executing on OSX use CLANG, otherwise only use it if we find it in the ENV
278 case $HOST_OS in
279 *-darwin-*)
280 CC=clang CXX=clang++ $top_srcdir/configure $CONFIGURE_ARG || die "Cannot execute CC=clang CXX=clang++ configure $CONFIGURE_ARG $PREFIX_ARG"
281 ;;
282 rhel-5*)
283 command_exists gcc44 || die "Could not locate gcc44"
284 CC=gcc44 CXX=gcc44 $top_srcdir/configure $CONFIGURE_ARG $PREFIX_ARG || die "Cannot execute CC=gcc44 CXX=gcc44 configure $CONFIGURE_ARG $PREFIX_ARG"
285 ;;
286 *)
287 $top_srcdir/configure $CONFIGURE_ARG $PREFIX_ARG || die "Cannot execute configure $CONFIGURE_ARG $PREFIX_ARG"
288 ;;
289 esac
290
291 if [ ! -f 'Makefile' ]; then
292 die "Programmer error, configure was run but no Makefile existed afterward"
293 fi
294 }
295
296 setup_gdb_command () {
297 GDB_TMPFILE=$(mktemp /tmp/gdb.XXXXXXXXXX)
298 echo 'set logging overwrite on' > $GDB_TMPFILE
299 echo 'set logging on' >> $GDB_TMPFILE
300 echo 'set environment LIBTEST_IN_GDB=1' >> $GDB_TMPFILE
301 echo 'run' >> $GDB_TMPFILE
302 echo 'thread apply all bt' >> $GDB_TMPFILE
303 echo 'quit' >> $GDB_TMPFILE
304 GDB_COMMAND="gdb -f -batch -x $GDB_TMPFILE"
305 }
306
307 setup_valgrind_command () {
308 VALGRIND_PROGRAM=`type -p valgrind`
309 if [[ -n "$VALGRIND_PROGRAM" ]]; then
310 VALGRIND_COMMAND="$VALGRIND_PROGRAM --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE"
311 fi
312 }
313
314 push_PREFIX_ARG ()
315 {
316 if [[ -n "$PREFIX_ARG" ]]; then
317 OLD_PREFIX_ARG=$PREFIX_ARG
318 PREFIX_ARG=
319 fi
320
321 if [[ -n "$1" ]]; then
322 PREFIX_ARG="--prefix=$1"
323 fi
324 }
325
326 pop_PREFIX_ARG ()
327 {
328 if [[ -n "$OLD_PREFIX_ARG" ]]; then
329 PREFIX_ARG=$OLD_TESTS_ENVIRONMENT
330 OLD_PREFIX_ARG=
331 else
332 PREFIX_ARG=
333 fi
334 }
335
336 push_TESTS_ENVIRONMENT ()
337 {
338 if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
339 die "OLD_TESTS_ENVIRONMENT was set on push, programmer error!"
340 fi
341
342 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
343 OLD_TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT
344 TESTS_ENVIRONMENT=
345 fi
346 }
347
348 pop_TESTS_ENVIRONMENT ()
349 {
350 TESTS_ENVIRONMENT=
351 if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
352 TESTS_ENVIRONMENT=$OLD_TESTS_ENVIRONMENT
353 OLD_TESTS_ENVIRONMENT=
354 fi
355 }
356
357 function safe_pushd ()
358 {
359 pushd $1 &> /dev/null ;
360
361 if $VERBOSE -a test -n "$BUILD_DIR"; then
362 echo "BUILD_DIR=$BUILD_DIR"
363 fi
364 }
365
366 function safe_popd ()
367 {
368 local directory_to_delete=`pwd`
369 popd &> /dev/null ;
370 if [ $? -eq 0 ]; then
371 if [[ "$top_srcdir" == "$directory_to_delete" ]]; then
372 die "We almost deleted top_srcdir($top_srcdir), programmer error"
373 fi
374
375 rm -r -f "$directory_to_delete"
376 fi
377 }
378
379 function make_valgrind ()
380 {
381 if [[ "$VENDOR_DISTRIBUTION" == 'darwin' ]]; then
382 make_darwin_malloc
383 return
384 fi
385
386 # If the env VALGRIND_COMMAND is set then we assume it is valid
387 local valgrind_was_set=false
388 if [[ -z "$VALGRIND_COMMAND" ]]; then
389 setup_valgrind_command
390 if [[ -n "$VALGRIND_COMMAND" ]]; then
391 valgrind_was_set=true
392 fi
393 else
394 valgrind_was_set=true
395 fi
396
397 # If valgrind_was_set is set to no we bail
398 if ! $valgrind_was_set; then
399 echo 'valgrind was not present'
400 return 1
401 fi
402
403 # If we are required to run configure, do so now
404 run_configure_if_required
405
406 push_TESTS_ENVIRONMENT
407
408 # If we don't have a configure, then most likely we will be missing libtool
409 assert_file 'configure'
410 if [[ -f 'libtool' ]]; then
411 TESTS_ENVIRONMENT="./libtool --mode=execute $VALGRIND_COMMAND"
412 else
413 TESTS_ENVIRONMENT="$VALGRIND_COMMAND"
414 fi
415
416 make_target 'check' || return 1
417
418 pop_TESTS_ENVIRONMENT
419 }
420
421 function make_install_system ()
422 {
423 local INSTALL_LOCATION=$(mktemp -d /tmp/XXXXXXXXXX)
424 push_PREFIX_ARG $INSTALL_LOCATION
425
426 if [ ! -d $INSTALL_LOCATION ] ; then
427 die "ASSERT temp directory not found '$INSTALL_LOCATION'"
428 fi
429
430 run_configure #install_buid_dir
431
432 push_TESTS_ENVIRONMENT
433
434 make_target 'install'
435
436 make_target 'installcheck'
437
438 make_target 'uninstall'
439
440 pop_TESTS_ENVIRONMENT
441 pop_PREFIX_ARG
442
443 rm -r -f $INSTALL_LOCATION
444 make 'distclean'
445
446 if [ -f 'Makefile' ]; then
447 die "ASSERT Makefile should not exist"
448 fi
449
450 safe_popd
451 }
452
453 function make_darwin_malloc ()
454 {
455 run_configure_if_required
456
457 old_MallocGuardEdges=$MallocGuardEdges
458 MallocGuardEdges=1
459 old_MallocErrorAbort=$MallocErrorAbort
460 MallocErrorAbort=1
461 old_MallocScribble=$MallocScribble
462 MallocScribble=1
463
464 make_check
465
466 MallocGuardEdges=$old_MallocGuardEdges
467 MallocErrorAbort=$old_MallocErrorAbort
468 MallocScribble=$old_MallocScribble
469 }
470
471 function snapshot_check ()
472 {
473 if [ -n "$BOOTSTRAP_SNAPSHOT_CHECK" ]; then
474 assert_file "$BOOTSTRAP_SNAPSHOT_CHECK" 'snapshot check failed'
475 fi
476 }
477
478 # This will reset our environment, and make sure built files are available.
479 function make_for_snapshot ()
480 {
481 # Make sure it is clean
482 make_maintainer_clean
483
484 run_configure
485 make_target 'dist'
486 make_target 'distclean'
487
488 # We should have a configure, but no Makefile at the end of this exercise
489 assert_no_file 'Makefile'
490 assert_exec_file 'configure'
491
492 snapshot_check
493 }
494
495 function make_for_mingw32 ()
496 {
497 # Make sure it is clean
498 if [ -f Makefile -o -f configure ]; then
499 make_maintainer_clean
500 fi
501 assert_no_file 'Makefile'
502
503 if command_exists mingw32-configure; then
504 run_autoreconf
505
506 mingw32-configure || die 'mingw32-configure failed'
507 assert_file 'Makefile'
508
509 if command_exists mingw32-make; then
510 mingw32-make || die 'mingw32-make failed'
511 fi
512 fi
513 }
514
515 # If we are locally testing, we should make sure the environment is setup correctly
516 function check_for_jenkins ()
517 {
518 if ! $jenkins_build_environment; then
519 echo "Not inside of jenkins"
520
521 if [ -f 'configure' ]; then
522 make_maintainer_clean
523 fi
524
525 if $BOOTSTRAP_SNAPSHOT; then
526 make_for_snapshot
527 fi
528 fi
529 }
530
531 function make_universe ()
532 {
533 make_for_snapshot
534 make_valgrind
535 make_gdb
536 make_rpm
537 make_for_mingw32
538 make_distcheck
539 make_install_system
540 }
541
542 function make_for_continuus_integration ()
543 {
544 # Setup the environment if we are local
545 check_for_jenkins
546
547 # No matter then evironment, we should not have a Makefile at this point
548 assert_no_file 'Makefile'
549
550 # Platforms which require bootstrap should have some setup done before we hit this stage.
551 # If we are building locally, skip this step, unless we are just testing locally.
552 if $BOOTSTRAP_SNAPSHOT; then
553 snapshot_check
554 else
555 # If we didn't require a snapshot, then we should not have a configure
556 assert_no_file 'configure'
557
558 run_autoreconf
559 fi
560
561 assert_no_file 'Makefile' 'Programmer error, Makefile existed where build state should have been clean'
562
563 case $HOST_OS in
564 *-fedora-*)
565 run_configure
566
567 assert_exec_file 'configure'
568 assert_file 'Makefile'
569
570 # make rpm includes "make distcheck"
571 if [[ -f rpm.am ]]; then
572 make_rpm
573 elif [[ -d rpm ]]; then
574 make_rpm
575 else
576 make_distcheck
577 fi
578
579 assert_exec_file 'configure'
580 assert_file 'Makefile'
581
582 make_install_system
583 ;;
584 *-precise-*)
585 run_configure
586
587 assert_exec_file 'configure'
588 assert_file 'Makefile'
589
590 make_distcheck
591
592 assert_exec_file 'configure'
593 assert_file 'Makefile'
594
595 make_valgrind
596
597 assert_exec_file 'configure'
598 assert_file 'Makefile'
599
600 make_install_system
601 ;;
602 *)
603 make_jenkins_default
604 ;;
605 esac
606
607 make_maintainer_clean
608
609 safe_popd
610 }
611
612 # The point to this test is to test bootstrap.sh itself
613 function self_test ()
614 {
615 # We start off with a clean env
616 make_maintainer_clean
617
618 eval "./bootstrap.sh jenkins" || die "failed 'jenkins'"
619 eval "./bootstrap.sh all" || die "failed 'all'"
620 eval "./bootstrap.sh gdb" || die "failed 'gdb'"
621 eval "./bootstrap.sh maintainer-clean" || die "failed 'maintainer-clean'"
622 }
623
624 function make_gdb ()
625 {
626 run_configure_if_required
627
628 if command_exists gdb; then
629
630 push_TESTS_ENVIRONMENT
631
632 # Set ENV GDB_COMMAND
633 if [[ -z "$GDB_COMMAND" ]]; then
634 setup_gdb_command
635 fi
636
637 # If we don't have a configure, then most likely we will be missing libtool
638 assert_file 'configure'
639 if [[ -f 'libtool' ]]; then
640 TESTS_ENVIRONMENT="./libtool --mode=execute $GDB_COMMAND"
641 else
642 TESTS_ENVIRONMENT="$GDB_COMMAND"
643 fi
644
645 make_target check
646
647 if [ -f 'gdb.txt' ]; then
648 rm 'gdb.txt'
649 fi
650
651 pop_TESTS_ENVIRONMENT
652
653 if [ -f '.gdb_history' ]; then
654 rm '.gdb_history'
655 fi
656 else
657 echo 'gdb was not present'
658 return 1
659 fi
660 }
661
662 # $1 target to compile
663 # $2 to die, or not to die, based on contents
664 function make_target ()
665 {
666 if [[ -z "$1" ]]; then
667 die "Programmer error, no target provided for make"
668 fi
669
670 if [ ! -f 'Makefile' ]; then
671 die "Programmer error, make was called before configure"
672 run_configure
673 fi
674
675 if test -n "$TESTS_ENVIRONMENT" -a $VERBOSE; then
676 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
677 fi
678
679 if [[ -z "$MAKE" ]]; then
680 die "MAKE was not set"
681 fi
682
683 if [[ -n "$2" ]]; then
684 run $MAKE $1 || return 1
685 else
686 run $MAKE $1 || die "Cannot execute $MAKE $1"
687 fi
688 }
689
690 function make_distcheck ()
691 {
692 make_target 'distcheck'
693 }
694
695 function make_rpm ()
696 {
697 if [ -f 'rpm.am' -o -d 'rpm' ]; then
698 run_configure_if_required
699 make_target 'rpm'
700 fi
701 }
702
703 function make_maintainer_clean ()
704 {
705 run_configure_if_required
706 make_target 'maintainer-clean' 'no_error'
707 }
708
709 function make_check ()
710 {
711 make_target 'check'
712 }
713
714 function make_jenkins_default ()
715 {
716 run_configure
717 make_target 'all'
718 }
719
720 function make_default ()
721 {
722 run_configure_if_required
723 make_target 'all'
724 }
725
726 function run_configure_if_required ()
727 {
728 run_autoreconf_if_required
729
730 if [ ! -f 'Makefile' ]; then
731 run_configure
732 fi
733
734 assert_file 'Makefile' 'configure did not produce a Makefile'
735 }
736
737 function run_autoreconf_if_required ()
738 {
739 if [ ! -x 'configure' ]; then
740 run_autoreconf
741 fi
742
743 assert_exec_file 'configure'
744 }
745
746 function run_autoreconf ()
747 {
748 if [[ -z "$AUTORECONF" ]]; then
749 die "Programmer error, tried to call run_autoreconf () but AUTORECONF was not set"
750 fi
751
752 run $AUTORECONF || die "Cannot execute $AUTORECONF"
753
754 eval 'bash -n configure' || die "autoreconf generated a malformed configure"
755 }
756
757 function run ()
758 {
759 if $VERBOSE; then
760 echo "\`$@' $ARGS"
761 fi
762
763 eval $@ $ARGS
764 }
765
766 parse_command_line_options ()
767 {
768 local SHORTOPTS=':apcmt:dvh'
769
770 nassert MAKE_TARGET
771
772 while getopts "$SHORTOPTS" opt; do
773 case $opt in
774 a) #--autoreconf
775 AUTORECONF_OPTION=true
776 MAKE_TARGET='autoreconf'
777 ;;
778 p) #--print-env
779 PRINT_SETUP_OPTION=true
780 ;;
781 c) # --configure
782 CONFIGURE_OPTION=true
783 MAKE_TARGET='configure'
784 ;;
785 m) # maintainer-clean
786 CLEAN_OPTION=true
787 MAKE_TARGET='clean_op'
788 ;;
789 t) # target
790 TARGET_OPTION=true
791 TARGET_OPTION_ARG="$OPTARG"
792 MAKE_TARGET="$OPTARG"
793 ;;
794 d) # debug
795 DEBUG_OPTION=true
796 enable_debug
797 ;;
798 h) # help
799 echo "bootstrap.sh [options] optional_target ..."
800 exit
801 ;;
802 v) # verbose
803 VERBOSE_OPTION=true
804 VERBOSE=true
805 ;;
806 :)
807 echo "Option -$OPTARG requires an argument." >&2
808 exit 1
809 ;;
810 *)
811 echo "$0: error - unrecognized option $1" 1>&2
812 exit 1
813 ;;
814 esac
815 done
816
817 shift $((OPTIND-1))
818
819 if [ -n "$1" ]; then
820 MAKE_TARGET="$@"
821 fi
822 }
823
824 determine_vcs ()
825 {
826 if [[ -d '.git' ]]; then
827 VCS_CHECKOUT=git
828 elif [[ -d '.bzr' ]]; then
829 VCS_CHECKOUT=bzr
830 elif [[ -d '.svn' ]]; then
831 VCS_CHECKOUT=svn
832 elif [[ -d '.hg' ]]; then
833 VCS_CHECKOUT=hg
834 fi
835
836 if [[ -n "$VCS_CHECKOUT" ]]; then
837 VERBOSE=true
838 fi
839 }
840
841 autoreconf_setup ()
842 {
843 # Set ENV MAKE in order to override "make"
844 if [[ -z "$MAKE" ]]; then
845 if command_exists gmake; then
846 MAKE=`type -p gmake`
847 else
848 if command_exists make; then
849 MAKE=`type -p make`
850 fi
851 fi
852
853 if [ "$VCS_CHECKOUT" ]; then
854 if $DEBUG; then
855 MAKE="$MAKE --warn-undefined-variables"
856 fi
857 fi
858
859 if $DEBUG; then
860 MAKE="$MAKE -d"
861 fi
862 fi
863
864 if [[ -z "$GNU_BUILD_FLAGS" ]]; then
865 GNU_BUILD_FLAGS="--install --force"
866 fi
867
868 if $VERBOSE; then
869 GNU_BUILD_FLAGS="$GNU_BUILD_FLAGS --verbose"
870 fi
871
872 if [[ -z "$WARNINGS" ]]; then
873 if [[ -n "$VCS_CHECKOUT" ]]; then
874 WARNINGS="all,error"
875 else
876 WARNINGS="all"
877 fi
878 fi
879
880 if [[ -z "$LIBTOOLIZE" ]]; then
881 # If we are using OSX, we first check to see glibtoolize is available
882 if [[ "$VENDOR_DISTRIBUTION" == "darwin" ]]; then
883 LIBTOOLIZE=`type -p glibtoolize`
884
885 if [[ -z "$LIBTOOLIZE" ]]; then
886 echo "Couldn't find glibtoolize, it is required on OSX"
887 fi
888 fi
889 fi
890
891 # Test the ENV AUTOMAKE if it exists
892 if [[ -n "$AUTOMAKE" ]]; then
893 run $AUTOMAKE '--help' &> /dev/null || die "Failed to run AUTOMAKE:$AUTOMAKE"
894 fi
895
896 # Test the ENV AUTOCONF if it exists
897 if [[ -n "$AUTOCONF" ]]; then
898 run $AUTOCONF '--help' &> /dev/null || die "Failed to run AUTOCONF:$AUTOCONF"
899 fi
900
901 # Test the ENV AUTOHEADER if it exists
902 if [[ -n "$AUTOHEADER" ]]; then
903 run $AUTOHEADER '--help' &> /dev/null || die "Failed to run AUTOHEADER:$AUTOHEADER"
904 fi
905
906 # Test the ENV AUTOM4TE if it exists
907 if [[ -n "$AUTOM4TE" ]]; then
908 run $AUTOM4TE '--help' &> /dev/null || die "Failed to run AUTOM4TE:$AUTOM4TE"
909 fi
910
911 if [[ -z "$AUTORECONF" ]]; then
912 AUTORECONF=`type -p autoreconf`
913
914 if [[ -z "$AUTORECONF" ]]; then
915 die "Couldn't find autoreconf"
916 fi
917
918 if [[ -n "$GNU_BUILD_FLAGS" ]]; then
919 AUTORECONF="$AUTORECONF $GNU_BUILD_FLAGS"
920 fi
921 fi
922
923 run $AUTORECONF '--help' &> /dev/null || die "Failed to run AUTORECONF:$AUTORECONF"
924 }
925
926 print_setup ()
927 {
928 saved_debug_status=$DEBUG
929 if $DEBUG; then
930 disable_debug
931 fi
932
933 echo '----------------------------------------------'
934 echo 'BOOTSTRAP ENV'
935 echo "AUTORECONF=$AUTORECONF"
936 echo "HOST_OS=$HOST_OS"
937
938 echo "getopt()"
939 if $AUTORECONF_OPTION; then
940 echo "--autoreconf"
941 fi
942
943 if $CLEAN_OPTION; then
944 echo "--clean"
945 fi
946
947 if $CONFIGURE_OPTION; then
948 echo "--configure"
949 fi
950
951 if $DEBUG_OPTION; then
952 echo "--debug"
953 fi
954
955 if $PRINT_SETUP_OPTION; then
956 echo "--print-env"
957 fi
958
959 if $TARGET_OPTION; then
960 echo "--target=$TARGET_OPTION_ARG"
961 fi
962
963 if $VERBOSE_OPTION; then
964 echo "--verbose"
965 fi
966
967 if [[ -n "$MAKE" ]]; then
968 echo "MAKE=$MAKE"
969 fi
970
971 if [[ -n "$MAKE_TARGET" ]]; then
972 echo "MAKE_TARGET=$MAKE_TARGET"
973 fi
974
975 if [[ -n "$PREFIX" ]]; then
976 echo "PREFIX=$PREFIX"
977 fi
978
979 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
980 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
981 fi
982
983 if [[ -n "$VCS_CHECKOUT" ]]; then
984 echo "VCS_CHECKOUT=$VCS_CHECKOUT"
985 fi
986
987 if $VERBOSE; then
988 echo "VERBOSE=true"
989 fi
990
991 if $DEBUG; then
992 echo "DEBUG=true"
993 fi
994
995 if [[ -n "$WARNINGS" ]]; then
996 echo "WARNINGS=$WARNINGS"
997 fi
998 echo '----------------------------------------------'
999
1000 if $saved_debug_status; then
1001 enable_debug
1002 fi
1003 }
1004
1005 make_clean_option ()
1006 {
1007 run_configure_if_required
1008
1009 make_maintainer_clean
1010
1011 if [[ "$VCS_CHECKOUT" == 'git' ]]; then
1012 run "$VCS_CHECKOUT" status --ignored
1013 elif [[ -n "$VCS_CHECKOUT" ]]; then
1014 run "$VCS_CHECKOUT" status
1015 fi
1016 }
1017
1018 make_for_autoreconf ()
1019 {
1020 if [ -f 'Makefile' ]; then
1021 make_maintainer_clean
1022 fi
1023
1024 run_autoreconf
1025
1026 assert_no_file 'Makefile'
1027 }
1028
1029 check_make_target()
1030 {
1031 case $1 in
1032 'self')
1033 ;;
1034 'gdb')
1035 ;;
1036 'clean_op')
1037 ;;
1038 'autoreconf')
1039 ;;
1040 'install-system')
1041 ;;
1042 'configure')
1043 ;;
1044 'distcheck')
1045 ;;
1046 'check')
1047 ;;
1048 'snapshot')
1049 ;;
1050 'mingw')
1051 ;;
1052 'universe')
1053 ;;
1054 'valgrind')
1055 ;;
1056 'jenkins')
1057 ;;
1058 'distclean')
1059 ;;
1060 'maintainer-clean')
1061 ;;
1062 'install')
1063 ;;
1064 'all')
1065 ;;
1066 'test-*')
1067 ;;
1068 'valgrind-*')
1069 ;;
1070 'gdb-*')
1071 ;;
1072 'dist')
1073 ;;
1074 *)
1075 die "Unknown MAKE_TARGET option: $1"
1076 ;;
1077 esac
1078 }
1079
1080 function bootstrap ()
1081 {
1082 determine_target_platform
1083
1084 determine_vcs
1085
1086 # Set up whatever we need to do to use autoreconf later
1087 autoreconf_setup
1088
1089 if [ -z "$MAKE_TARGET" ]; then
1090 MAKE_TARGET="make_default"
1091 fi
1092
1093 if $PRINT_SETUP_OPTION -o $DEBUG; then
1094 echo
1095 print_setup
1096 echo
1097
1098 # Exit if all we were looking for were the currently used options
1099 if $PRINT_SETUP_OPTION; then
1100 exit
1101 fi
1102 fi
1103
1104 # Use OLD_TESTS_ENVIRONMENT for tracking the state of the variable
1105 local OLD_TESTS_ENVIRONMENT=
1106
1107 # Set ENV PREFIX in order to set --prefix for ./configure
1108 if [[ -n "$PREFIX" ]]; then
1109 push_PREFIX_ARG $PREFIX
1110 fi
1111
1112 # We should always have a target by this point
1113 assert MAKE_TARGET
1114
1115 local MAKE_TARGET_ARRAY=($MAKE_TARGET)
1116
1117 for target in "${MAKE_TARGET_ARRAY[@]}"
1118 do
1119 # If we are running inside of Jenkins, we want to only run some of the possible tests
1120 if $jenkins_build_environment; then
1121 check_make_target $target
1122 fi
1123
1124 case $target in
1125 'self')
1126 self_test
1127 ;;
1128 'gdb')
1129 make_gdb
1130 ;;
1131 'clean_op')
1132 make_clean_option
1133 ;;
1134 'autoreconf')
1135 make_for_autoreconf
1136 ;;
1137 'install-system')
1138 make_install_system
1139 ;;
1140 'configure')
1141 run_configure
1142 ;;
1143 'default')
1144 make
1145 run_configure
1146 ;;
1147 'mingw')
1148 make_for_mingw32
1149 ;;
1150 'snapshot')
1151 make_for_snapshot
1152 ;;
1153 'rpm')
1154 make_rpm
1155 ;;
1156 'valgrind')
1157 make_valgrind
1158 ;;
1159 'universe')
1160 make_universe
1161 ;;
1162 'jenkins')
1163 make_for_continuus_integration
1164 ;;
1165 *)
1166 run_configure_if_required
1167 make_target "$target"
1168 ;;
1169 esac
1170 done
1171 }
1172
1173 main ()
1174 {
1175 # Variables we export
1176 declare -x VCS_CHECKOUT=
1177
1178 # Variables we control globally
1179 local MAKE_TARGET=
1180
1181 # Options for getopt
1182 local AUTORECONF_OPTION=false
1183 local CLEAN_OPTION=false
1184 local CONFIGURE_OPTION=false
1185 local DEBUG_OPTION=false
1186 local PRINT_SETUP_OPTION=false
1187 local TARGET_OPTION=false
1188 local TARGET_OPTION_ARG=
1189 local VERBOSE_OPTION=false
1190
1191 # If we call autoreconf on the platform or not
1192 local AUTORECONF_REBUILD_HOST=false
1193 local AUTORECONF_REBUILD=false
1194
1195 local -r top_srcdir=`pwd`
1196
1197 # Variables for determine_target_platform () and rebuild_host_os ()
1198 # UNAME_MACHINE_ARCH= uname -m
1199 # VENDOR= apple, redhat, centos, canonical
1200 # VENDOR_RELEASE=
1201 # RHEL{rhel,Tikanga,Santiago}
1202 # Ubuntu{ubuntu,Lucid,Maverick,Natty,Oneiric,Precise,Quantal}
1203 # Fedora{fedora,Verne,Beefy}
1204 # OSX{osx,lion,snow,mountain}
1205 # VENDOR_DISTRIBUTION= darwin,fedora,rhel,ubuntu
1206 # UNAME_KERNEL= Linux, Darwin,...
1207 # UNAME_KERNEL_RELEASE= Linux, Darwin,...
1208 local UNAME_MACHINE_ARCH=unknown
1209 local VENDOR=unknown
1210 local VENDOR_RELEASE=unknown
1211 local VENDOR_DISTRIBUTION=unknown
1212 local UNAME_KERNEL=unknown
1213 local UNAME_KERNEL_RELEASE=unknown
1214 local HOST_OS=
1215
1216 rebuild_host_os no_output
1217
1218 parse_command_line_options $@
1219
1220 # If we are running under Jenkins we predetermine what tests we will run against
1221 # This MAKE_TARGET can be overridden by parse_command_line_options based MAKE_TARGET changes.
1222 # We don't want Jenkins overriding other variables, so we NULL them.
1223 if [ -z "$MAKE_TARGET" -a $jenkins_build_environment ]; then
1224 MAKE_TARGET='jenkins'
1225 fi
1226
1227 bootstrap
1228
1229 jobs -l
1230 wait
1231
1232 exit 0
1233 }
1234
1235 enable_debug ()
1236 {
1237 if ! $DEBUG; then
1238 local caller_loc=`caller`
1239 if [ -n $1 ]; then
1240 echo "$caller_loc Enabling debug: $1"
1241 else
1242 echo "$caller_loc Enabling debug"
1243 fi
1244 set -x
1245 DEBUG=true
1246 fi
1247 }
1248
1249 disable_debug ()
1250 {
1251 set +x
1252 DEBUG=true
1253 }
1254
1255 # Script begins here
1256
1257 env_debug_enabled=false
1258 if [[ -n "$JENKINS_HOME" ]]; then
1259 declare -r jenkins_build_environment=true
1260 else
1261 declare -r jenkins_build_environment=false
1262 fi
1263
1264 export AUTOCONF
1265 export AUTOHEADER
1266 export AUTOM4TE
1267 export AUTOMAKE
1268 export AUTORECONF
1269 export DEBUG
1270 export GNU_BUILD_FLAGS
1271 export MAKE
1272 export TESTS_ENVIRONMENT
1273 export VERBOSE
1274 export WARNINGS
1275
1276 case $OSTYPE in
1277 darwin*)
1278 export MallocGuardEdges
1279 export MallocErrorAbort
1280 export MallocScribble
1281 ;;
1282 esac
1283
1284 # We check for DEBUG twice, once before we source the config file, and once afterward
1285 env_debug_enabled=false
1286 if [[ -n "$DEBUG" ]]; then
1287 env_debug_enabled=true
1288 enable_debug
1289 print_setup
1290 fi
1291
1292 # Variables which only can be set by .bootstrap
1293 BOOTSTRAP_SNAPSHOT=false
1294 BOOTSTRAP_SNAPSHOT_CHECK=
1295
1296 if [ -f '.bootstrap' ]; then
1297 source '.bootstrap'
1298 fi
1299
1300 if $env_debug_enabled; then
1301 enable_debug
1302 else
1303 if [[ -n "$DEBUG" ]]; then
1304 enable_debug "Enabling DEBUG from '.bootstrap'"
1305 print_setup
1306 fi
1307 fi
1308
1309 # We do this in order to protect the case where DEBUG
1310 if ! $env_debug_enabled; then
1311 DEBUG=false
1312 fi
1313
1314 main $@