Style cleanup.
[awesomized/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 [ -n "$BUILD_DIR" ]; then
362 if $VERBOSE; then
363 echo "BUILD_DIR=$BUILD_DIR"
364 fi
365 fi
366 }
367
368 function safe_popd ()
369 {
370 local directory_to_delete=`pwd`
371 popd &> /dev/null ;
372 if [ $? -eq 0 ]; then
373 if [[ "$top_srcdir" == "$directory_to_delete" ]]; then
374 die "We almost deleted top_srcdir($top_srcdir), programmer error"
375 fi
376
377 rm -r -f "$directory_to_delete"
378 fi
379 }
380
381 function make_valgrind ()
382 {
383 if [[ "$VENDOR_DISTRIBUTION" == 'darwin' ]]; then
384 make_darwin_malloc
385 return
386 fi
387
388 # If the env VALGRIND_COMMAND is set then we assume it is valid
389 local valgrind_was_set=false
390 if [[ -z "$VALGRIND_COMMAND" ]]; then
391 setup_valgrind_command
392 if [[ -n "$VALGRIND_COMMAND" ]]; then
393 valgrind_was_set=true
394 fi
395 else
396 valgrind_was_set=true
397 fi
398
399 # If valgrind_was_set is set to no we bail
400 if ! $valgrind_was_set; then
401 echo 'valgrind was not present'
402 return 1
403 fi
404
405 # If we are required to run configure, do so now
406 run_configure_if_required
407
408 push_TESTS_ENVIRONMENT
409
410 # If we don't have a configure, then most likely we will be missing libtool
411 assert_file 'configure'
412 if [[ -f 'libtool' ]]; then
413 TESTS_ENVIRONMENT="./libtool --mode=execute $VALGRIND_COMMAND"
414 else
415 TESTS_ENVIRONMENT="$VALGRIND_COMMAND"
416 fi
417
418 make_target 'check' || return 1
419
420 pop_TESTS_ENVIRONMENT
421 }
422
423 function make_install_system ()
424 {
425 local INSTALL_LOCATION=$(mktemp -d /tmp/XXXXXXXXXX)
426 push_PREFIX_ARG $INSTALL_LOCATION
427
428 if [ ! -d $INSTALL_LOCATION ] ; then
429 die "ASSERT temp directory not found '$INSTALL_LOCATION'"
430 fi
431
432 run_configure #install_buid_dir
433
434 push_TESTS_ENVIRONMENT
435
436 make_target 'install'
437
438 make_target 'installcheck'
439
440 make_target 'uninstall'
441
442 pop_TESTS_ENVIRONMENT
443 pop_PREFIX_ARG
444
445 rm -r -f $INSTALL_LOCATION
446 make 'distclean'
447
448 if [ -f 'Makefile' ]; then
449 die "ASSERT Makefile should not exist"
450 fi
451
452 safe_popd
453 }
454
455 function make_darwin_malloc ()
456 {
457 run_configure_if_required
458
459 old_MallocGuardEdges=$MallocGuardEdges
460 MallocGuardEdges=1
461 old_MallocErrorAbort=$MallocErrorAbort
462 MallocErrorAbort=1
463 old_MallocScribble=$MallocScribble
464 MallocScribble=1
465
466 make_check
467
468 MallocGuardEdges=$old_MallocGuardEdges
469 MallocErrorAbort=$old_MallocErrorAbort
470 MallocScribble=$old_MallocScribble
471 }
472
473 function snapshot_check ()
474 {
475 if [ ! -f "$BOOTSTRAP_SNAPSHOT_CHECK" ]; then
476 make_for_snapshot
477 fi
478
479 if [ -n "$BOOTSTRAP_SNAPSHOT_CHECK" ]; then
480 assert_file "$BOOTSTRAP_SNAPSHOT_CHECK" 'snapshot check failed'
481 fi
482 }
483
484 # This will reset our environment, and make sure built files are available.
485 function make_for_snapshot ()
486 {
487 # Make sure it is clean
488 make_maintainer_clean
489
490 run_configure
491 make_target 'dist'
492 make_target 'distclean'
493
494 # We should have a configure, but no Makefile at the end of this exercise
495 assert_no_file 'Makefile'
496 assert_exec_file 'configure'
497
498 snapshot_check
499 }
500
501 function make_for_mingw32 ()
502 {
503 # Make sure it is clean
504 if [ -f Makefile -o -f configure ]; then
505 make_maintainer_clean
506 fi
507 assert_no_file 'Makefile'
508
509 if command_exists mingw32-configure; then
510 run_autoreconf
511
512 mingw32-configure || die 'mingw32-configure failed'
513 assert_file 'Makefile'
514
515 if command_exists mingw32-make; then
516 mingw32-make || die 'mingw32-make failed'
517 fi
518 fi
519 }
520
521 # If we are locally testing, we should make sure the environment is setup correctly
522 function check_for_jenkins ()
523 {
524 if ! $jenkins_build_environment; then
525 echo "Not inside of jenkins"
526
527 if [ -f 'configure' ]; then
528 make_maintainer_clean
529 fi
530
531 if $BOOTSTRAP_SNAPSHOT; then
532 make_for_snapshot
533 fi
534 fi
535 }
536
537 function make_universe ()
538 {
539 make_for_snapshot
540 make_valgrind
541 make_gdb
542 make_rpm
543 make_for_mingw32
544 make_distcheck
545 make_install_system
546 }
547
548 function make_for_continuus_integration ()
549 {
550 # Setup the environment if we are local
551 check_for_jenkins
552
553 # No matter then evironment, we should not have a Makefile at this point
554 assert_no_file 'Makefile'
555
556 # Platforms which require bootstrap should have some setup done before we hit this stage.
557 # If we are building locally, skip this step, unless we are just testing locally.
558 if $BOOTSTRAP_SNAPSHOT; then
559 snapshot_check
560 else
561 # If we didn't require a snapshot, then we should not have a configure
562 assert_no_file 'configure'
563
564 run_autoreconf
565 fi
566
567 assert_no_file 'Makefile' 'Programmer error, Makefile existed where build state should have been clean'
568
569 case $HOST_OS in
570 *-fedora-*)
571 run_configure
572
573 assert_exec_file 'configure'
574 assert_file 'Makefile'
575
576 make_target 'all'
577
578 # make rpm includes "make distcheck"
579 if [[ -f rpm.am ]]; then
580 make_rpm
581 elif [[ -d rpm ]]; then
582 make_rpm
583 else
584 make_distcheck
585 fi
586
587 assert_exec_file 'configure'
588 assert_file 'Makefile'
589
590 make_install_system
591 ;;
592 *-precise-*)
593 run_configure
594
595 assert_exec_file 'configure'
596 assert_file 'Makefile'
597
598 make_target 'all'
599
600 make_distcheck
601
602 assert_exec_file 'configure'
603 assert_file 'Makefile'
604
605 make_valgrind
606
607 assert_exec_file 'configure'
608 assert_file 'Makefile'
609
610 make_install_system
611 ;;
612 *)
613 make_jenkins_default
614 ;;
615 esac
616
617 make_maintainer_clean
618
619 safe_popd
620 }
621
622 # The point to this test is to test bootstrap.sh itself
623 function self_test ()
624 {
625 # We start off with a clean env
626 make_maintainer_clean
627
628 eval "./bootstrap.sh jenkins" || die "failed 'jenkins'"
629 eval "./bootstrap.sh all" || die "failed 'all'"
630 eval "./bootstrap.sh gdb" || die "failed 'gdb'"
631 eval "./bootstrap.sh maintainer-clean" || die "failed 'maintainer-clean'"
632 }
633
634 function make_gdb ()
635 {
636 run_configure_if_required
637
638 if command_exists gdb; then
639
640 push_TESTS_ENVIRONMENT
641
642 # Set ENV GDB_COMMAND
643 if [[ -z "$GDB_COMMAND" ]]; then
644 setup_gdb_command
645 fi
646
647 # If we don't have a configure, then most likely we will be missing libtool
648 assert_file 'configure'
649 if [[ -f 'libtool' ]]; then
650 TESTS_ENVIRONMENT="./libtool --mode=execute $GDB_COMMAND"
651 else
652 TESTS_ENVIRONMENT="$GDB_COMMAND"
653 fi
654
655 make_target check
656
657 if [ -f 'gdb.txt' ]; then
658 rm 'gdb.txt'
659 fi
660
661 pop_TESTS_ENVIRONMENT
662
663 if [ -f '.gdb_history' ]; then
664 rm '.gdb_history'
665 fi
666 else
667 echo 'gdb was not present'
668 return 1
669 fi
670 }
671
672 # $1 target to compile
673 # $2 to die, or not to die, based on contents
674 function make_target ()
675 {
676 if [ -z "$1" ]; then
677 die "Programmer error, no target provided for make"
678 fi
679
680 if [ ! -f 'Makefile' ]; then
681 die "Programmer error, make was called before configure"
682 run_configure
683 fi
684
685 if [ -n "$TESTS_ENVIRONMENT" ]; then
686 if $VERBOSE; then
687 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
688 fi
689 fi
690
691 if [ -z "$MAKE" ]; then
692 die "MAKE was not set"
693 fi
694
695 if [ -n "$2" ]; then
696 run $MAKE $1 || return 1
697 else
698 run $MAKE $1 || die "Cannot execute $MAKE $1"
699 fi
700 }
701
702 function make_distcheck ()
703 {
704 make_target 'distcheck'
705 }
706
707 function make_rpm ()
708 {
709 if [ -f 'rpm.am' -o -d 'rpm' ]; then
710 run_configure_if_required
711 make_target 'rpm'
712 fi
713 }
714
715 function make_maintainer_clean ()
716 {
717 run_configure_if_required
718 make_target 'maintainer-clean' 'no_error'
719 }
720
721 function make_check ()
722 {
723 make_target 'check'
724 }
725
726 function make_jenkins_default ()
727 {
728 run_configure
729 make_target 'all'
730 }
731
732 function make_default ()
733 {
734 run_configure_if_required
735 make_target 'all'
736 }
737
738 function run_configure_if_required ()
739 {
740 run_autoreconf_if_required
741
742 if [ ! -f 'Makefile' ]; then
743 run_configure
744 fi
745
746 assert_file 'Makefile' 'configure did not produce a Makefile'
747 }
748
749 function run_autoreconf_if_required ()
750 {
751 if [ ! -x 'configure' ]; then
752 run_autoreconf
753 fi
754
755 assert_exec_file 'configure'
756 }
757
758 function run_autoreconf ()
759 {
760 if [[ -z "$AUTORECONF" ]]; then
761 die "Programmer error, tried to call run_autoreconf () but AUTORECONF was not set"
762 fi
763
764 if test $use_libtool = 1; then
765 assert $BOOTSTRAP_LIBTOOLIZE
766 run $BOOTSTRAP_LIBTOOLIZE '--copy' '--install' '--force' || die "Cannot execute $BOOTSTRAP_LIBTOOLIZE"
767 fi
768
769 run $AUTORECONF || die "Cannot execute $AUTORECONF"
770
771 eval 'bash -n configure' || die "autoreconf generated a malformed configure"
772 }
773
774 function run ()
775 {
776 if $VERBOSE; then
777 echo "\`$@' $ARGS"
778 fi
779
780 eval $@ $ARGS
781 }
782
783 parse_command_line_options ()
784 {
785 local SHORTOPTS=':apcmt:dvh'
786
787 nassert MAKE_TARGET
788
789 while getopts "$SHORTOPTS" opt; do
790 case $opt in
791 a) #--autoreconf
792 AUTORECONF_OPTION=true
793 MAKE_TARGET='autoreconf'
794 ;;
795 p) #--print-env
796 PRINT_SETUP_OPTION=true
797 ;;
798 c) # --configure
799 CONFIGURE_OPTION=true
800 MAKE_TARGET='configure'
801 ;;
802 m) # maintainer-clean
803 CLEAN_OPTION=true
804 MAKE_TARGET='clean_op'
805 ;;
806 t) # target
807 TARGET_OPTION=true
808 TARGET_OPTION_ARG="$OPTARG"
809 MAKE_TARGET="$OPTARG"
810 ;;
811 d) # debug
812 DEBUG_OPTION=true
813 enable_debug
814 ;;
815 h) # help
816 echo "bootstrap.sh [options] optional_target ..."
817 exit
818 ;;
819 v) # verbose
820 VERBOSE_OPTION=true
821 VERBOSE=true
822 ;;
823 :)
824 echo "Option -$OPTARG requires an argument." >&2
825 exit 1
826 ;;
827 *)
828 echo "$0: error - unrecognized option $1" 1>&2
829 exit 1
830 ;;
831 esac
832 done
833
834 shift $((OPTIND-1))
835
836 if [ -n "$1" ]; then
837 MAKE_TARGET="$@"
838 fi
839 }
840
841 determine_vcs ()
842 {
843 if [[ -d '.git' ]]; then
844 VCS_CHECKOUT=git
845 elif [[ -d '.bzr' ]]; then
846 VCS_CHECKOUT=bzr
847 elif [[ -d '.svn' ]]; then
848 VCS_CHECKOUT=svn
849 elif [[ -d '.hg' ]]; then
850 VCS_CHECKOUT=hg
851 fi
852
853 if [[ -n "$VCS_CHECKOUT" ]]; then
854 VERBOSE=true
855 fi
856 }
857
858 function require_libtoolise ()
859 {
860 use_libtool=0
861 grep '^[ ]*A[CM]_PROG_LIBTOOL' configure.ac >/dev/null \
862 && use_libtool=1
863 grep '^[ ]*LT_INIT' configure.ac >/dev/null \
864 && use_libtool=1
865 }
866
867 function autoreconf_setup ()
868 {
869 # Set ENV MAKE in order to override "make"
870 if [[ -z "$MAKE" ]]; then
871 if command_exists gmake; then
872 MAKE=`type -p gmake`
873 else
874 if command_exists make; then
875 MAKE=`type -p make`
876 fi
877 fi
878
879 if [ "$VCS_CHECKOUT" ]; then
880 if $DEBUG; then
881 MAKE="$MAKE --warn-undefined-variables"
882 fi
883 fi
884
885 if $DEBUG; then
886 MAKE="$MAKE -d"
887 fi
888 fi
889
890 if [[ -z "$GNU_BUILD_FLAGS" ]]; then
891 GNU_BUILD_FLAGS="--install --force"
892 fi
893
894 if $VERBOSE; then
895 GNU_BUILD_FLAGS="$GNU_BUILD_FLAGS --verbose"
896 fi
897
898 if [ -z "$ACLOCAL_PATH" ]; then
899 ACLOCAL_PATH="/usr/local/share/aclocal $ACLOCAL_PATH"
900 fi
901
902 if [[ -z "$WARNINGS" ]]; then
903 if [[ -n "$VCS_CHECKOUT" ]]; then
904 WARNINGS="all,error"
905 else
906 WARNINGS="all"
907 fi
908 fi
909
910 if test $use_libtool = 1; then
911 if [[ -n "$LIBTOOLIZE" ]]; then
912 BOOTSTRAP_LIBTOOLIZE=`type -p $LIBTOOLIZE`
913
914 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
915 echo "Couldn't find user supplied libtoolize, it is required"
916 fi
917 else
918 # If we are using OSX, we first check to see glibtoolize is available
919 if [[ "$VENDOR_DISTRIBUTION" == "darwin" ]]; then
920 BOOTSTRAP_LIBTOOLIZE=`type -p glibtoolize`
921
922 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
923 echo "Couldn't find glibtoolize, it is required on OSX"
924 fi
925 else
926 BOOTSTRAP_LIBTOOLIZE=`type -p libtoolize`
927
928 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
929 echo "Couldn't find libtoolize, it is required"
930 fi
931 fi
932 fi
933 if $VERBOSE; then
934 LIBTOOLIZE_OPTIONS="--verbose $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
935 fi
936 if $DEBUG; then
937 LIBTOOLIZE_OPTIONS="--debug $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
938 fi
939 LIBTOOLIZE=true
940 fi
941
942 # Test the ENV AUTOMAKE if it exists
943 if [[ -n "$AUTOMAKE" ]]; then
944 run $AUTOMAKE '--help' &> /dev/null || die "Failed to run AUTOMAKE:$AUTOMAKE"
945 fi
946
947 # Test the ENV AUTOCONF if it exists
948 if [[ -n "$AUTOCONF" ]]; then
949 run $AUTOCONF '--help' &> /dev/null || die "Failed to run AUTOCONF:$AUTOCONF"
950 fi
951
952 # Test the ENV AUTOHEADER if it exists
953 if [[ -n "$AUTOHEADER" ]]; then
954 run $AUTOHEADER '--help' &> /dev/null || die "Failed to run AUTOHEADER:$AUTOHEADER"
955 fi
956
957 # Test the ENV AUTOM4TE if it exists
958 if [[ -n "$AUTOM4TE" ]]; then
959 run $AUTOM4TE '--help' &> /dev/null || die "Failed to run AUTOM4TE:$AUTOM4TE"
960 fi
961
962 # Test the ENV AUTOHEADER if it exists, if not we add one and add --install
963 if [[ -z "$ACLOCAL" ]]; then
964 ACLOCAL="aclocal --install"
965 fi
966 run $ACLOCAL '--help' &> /dev/null || die "Failed to run ACLOCAL:$ACLOCAL"
967
968 if [[ -z "$AUTORECONF" ]]; then
969 AUTORECONF=`type -p autoreconf`
970
971 if [[ -z "$AUTORECONF" ]]; then
972 die "Couldn't find autoreconf"
973 fi
974
975 if [[ -n "$GNU_BUILD_FLAGS" ]]; then
976 AUTORECONF="$AUTORECONF $GNU_BUILD_FLAGS"
977 fi
978 fi
979
980 run $AUTORECONF '--help' &> /dev/null || die "Failed to run AUTORECONF:$AUTORECONF"
981 }
982
983 print_setup ()
984 {
985 saved_debug_status=$DEBUG
986 if $DEBUG; then
987 disable_debug
988 fi
989
990 echo '----------------------------------------------'
991 echo 'BOOTSTRAP ENV'
992 echo "AUTORECONF=$AUTORECONF"
993 echo "HOST_OS=$HOST_OS"
994
995 echo "getopt()"
996 if $AUTORECONF_OPTION; then
997 echo "--autoreconf"
998 fi
999
1000 if $CLEAN_OPTION; then
1001 echo "--clean"
1002 fi
1003
1004 if $CONFIGURE_OPTION; then
1005 echo "--configure"
1006 fi
1007
1008 if $DEBUG_OPTION; then
1009 echo "--debug"
1010 fi
1011
1012 if $PRINT_SETUP_OPTION; then
1013 echo "--print-env"
1014 fi
1015
1016 if $TARGET_OPTION; then
1017 echo "--target=$TARGET_OPTION_ARG"
1018 fi
1019
1020 if $VERBOSE_OPTION; then
1021 echo "--verbose"
1022 fi
1023
1024 if [[ -n "$MAKE" ]]; then
1025 echo "MAKE=$MAKE"
1026 fi
1027
1028 if [[ -n "$MAKE_TARGET" ]]; then
1029 echo "MAKE_TARGET=$MAKE_TARGET"
1030 fi
1031
1032 if [[ -n "$PREFIX" ]]; then
1033 echo "PREFIX=$PREFIX"
1034 fi
1035
1036 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
1037 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
1038 fi
1039
1040 if [[ -n "$VCS_CHECKOUT" ]]; then
1041 echo "VCS_CHECKOUT=$VCS_CHECKOUT"
1042 fi
1043
1044 if $VERBOSE; then
1045 echo "VERBOSE=true"
1046 fi
1047
1048 if $DEBUG; then
1049 echo "DEBUG=true"
1050 fi
1051
1052 if [[ -n "$WARNINGS" ]]; then
1053 echo "WARNINGS=$WARNINGS"
1054 fi
1055 echo '----------------------------------------------'
1056
1057 if $saved_debug_status; then
1058 enable_debug
1059 fi
1060 }
1061
1062 make_clean_option ()
1063 {
1064 run_configure_if_required
1065
1066 make_maintainer_clean
1067
1068 if [[ "$VCS_CHECKOUT" == 'git' ]]; then
1069 run "$VCS_CHECKOUT" status --ignored
1070 elif [[ -n "$VCS_CHECKOUT" ]]; then
1071 run "$VCS_CHECKOUT" status
1072 fi
1073 }
1074
1075 make_for_autoreconf ()
1076 {
1077 if [ -f 'Makefile' ]; then
1078 make_maintainer_clean
1079 fi
1080
1081 run_autoreconf
1082
1083 assert_no_file 'Makefile'
1084 }
1085
1086 check_make_target()
1087 {
1088 case $1 in
1089 'self')
1090 ;;
1091 'rpm')
1092 ;;
1093 'gdb')
1094 ;;
1095 'clean_op')
1096 ;;
1097 'autoreconf')
1098 ;;
1099 'install-system')
1100 ;;
1101 'configure')
1102 ;;
1103 'distcheck')
1104 ;;
1105 'check')
1106 ;;
1107 'snapshot')
1108 ;;
1109 'mingw')
1110 ;;
1111 'universe')
1112 ;;
1113 'valgrind')
1114 ;;
1115 'jenkins')
1116 ;;
1117 'distclean')
1118 ;;
1119 'maintainer-clean')
1120 ;;
1121 'install')
1122 ;;
1123 'all')
1124 ;;
1125 'make_default')
1126 ;;
1127 'test-*')
1128 ;;
1129 'valgrind-*')
1130 ;;
1131 'gdb-*')
1132 ;;
1133 'dist')
1134 ;;
1135 *)
1136 die "Unknown MAKE_TARGET option: $1"
1137 ;;
1138 esac
1139 }
1140
1141 function bootstrap ()
1142 {
1143 determine_target_platform
1144
1145 determine_vcs
1146
1147 # Set up whatever we need to do to use autoreconf later
1148 require_libtoolise
1149 autoreconf_setup
1150
1151 if [ -z "$MAKE_TARGET" ]; then
1152 MAKE_TARGET="make_default"
1153 fi
1154
1155 if $PRINT_SETUP_OPTION -o $DEBUG; then
1156 echo
1157 print_setup
1158 echo
1159
1160 # Exit if all we were looking for were the currently used options
1161 if $PRINT_SETUP_OPTION; then
1162 exit
1163 fi
1164 fi
1165
1166 # Use OLD_TESTS_ENVIRONMENT for tracking the state of the variable
1167 local OLD_TESTS_ENVIRONMENT=
1168
1169 # Set ENV PREFIX in order to set --prefix for ./configure
1170 if [[ -n "$PREFIX" ]]; then
1171 push_PREFIX_ARG $PREFIX
1172 fi
1173
1174 # We should always have a target by this point
1175 assert MAKE_TARGET
1176
1177 local MAKE_TARGET_ARRAY=($MAKE_TARGET)
1178
1179 for target in "${MAKE_TARGET_ARRAY[@]}"
1180 do
1181 # If we are running inside of Jenkins, we want to only run some of the possible tests
1182 if $jenkins_build_environment; then
1183 check_make_target $target
1184 fi
1185
1186 case $target in
1187 'self')
1188 self_test
1189 ;;
1190 'gdb')
1191 make_gdb
1192 ;;
1193 'clean_op')
1194 make_clean_option
1195 ;;
1196 'autoreconf')
1197 make_for_autoreconf
1198 ;;
1199 'install-system')
1200 make_install_system
1201 ;;
1202 'configure')
1203 run_configure
1204 ;;
1205 'make_default')
1206 make_default
1207 ;;
1208 'mingw')
1209 make_for_mingw32
1210 ;;
1211 'snapshot')
1212 make_for_snapshot
1213 ;;
1214 'rpm')
1215 make_rpm
1216 ;;
1217 'valgrind')
1218 make_valgrind
1219 ;;
1220 'universe')
1221 make_universe
1222 ;;
1223 'jenkins')
1224 make_for_continuus_integration
1225 ;;
1226 *)
1227 run_configure_if_required
1228 make_target "$target"
1229 ;;
1230 esac
1231 done
1232 }
1233
1234 main ()
1235 {
1236 # Variables we export
1237 declare -x VCS_CHECKOUT=
1238
1239 # Variables we control globally
1240 local MAKE_TARGET=
1241
1242 # Options for getopt
1243 local AUTORECONF_OPTION=false
1244 local CLEAN_OPTION=false
1245 local CONFIGURE_OPTION=false
1246 local DEBUG_OPTION=false
1247 local PRINT_SETUP_OPTION=false
1248 local TARGET_OPTION=false
1249 local TARGET_OPTION_ARG=
1250 local VERBOSE_OPTION=false
1251
1252 # If we call autoreconf on the platform or not
1253 local AUTORECONF_REBUILD_HOST=false
1254 local AUTORECONF_REBUILD=false
1255
1256 local -r top_srcdir=`pwd`
1257
1258 # Variables for determine_target_platform () and rebuild_host_os ()
1259 # UNAME_MACHINE_ARCH= uname -m
1260 # VENDOR= apple, redhat, centos, canonical
1261 # VENDOR_RELEASE=
1262 # RHEL{rhel,Tikanga,Santiago}
1263 # Ubuntu{ubuntu,Lucid,Maverick,Natty,Oneiric,Precise,Quantal}
1264 # Fedora{fedora,Verne,Beefy}
1265 # OSX{osx,lion,snow,mountain}
1266 # VENDOR_DISTRIBUTION= darwin,fedora,rhel,ubuntu
1267 # UNAME_KERNEL= Linux, Darwin,...
1268 # UNAME_KERNEL_RELEASE= Linux, Darwin,...
1269 local UNAME_MACHINE_ARCH=unknown
1270 local VENDOR=unknown
1271 local VENDOR_RELEASE=unknown
1272 local VENDOR_DISTRIBUTION=unknown
1273 local UNAME_KERNEL=unknown
1274 local UNAME_KERNEL_RELEASE=unknown
1275 local HOST_OS=
1276
1277 rebuild_host_os no_output
1278
1279 parse_command_line_options $@
1280
1281 # If we are running under Jenkins we predetermine what tests we will run against
1282 # This MAKE_TARGET can be overridden by parse_command_line_options based MAKE_TARGET changes.
1283 # We don't want Jenkins overriding other variables, so we NULL them.
1284 if [ -z "$MAKE_TARGET" ]; then
1285 if $jenkins_build_environment; then
1286 MAKE_TARGET='jenkins'
1287 fi
1288 fi
1289
1290 bootstrap
1291
1292 jobs -l
1293 wait
1294
1295 exit 0
1296 }
1297
1298 function set_branch ()
1299 {
1300 if [ -z "$BRANCH" ]; then
1301 if [ -z "$CI_PROJECT_TEAM" ]; then
1302 die "Variable CI_PROJECT_TEAM has not been set"
1303 fi
1304 if [ -z "$PROJECT" ]; then
1305 die "Variable PROJECT has not been set"
1306 fi
1307 if [ -z "$BUILD_TAG" ]; then
1308 die "Variable BUILD_TAG has not been set"
1309 fi
1310
1311 BRANCH="lp:~$CI_PROJECT_TEAM/$PROJECT/$BUILD_TAG"
1312 export BRANCH
1313 fi
1314
1315 if [ -z "$BRANCH" ]; then
1316 die "Missing values required to build BRANCH variable."
1317 fi
1318 }
1319
1320 function merge ()
1321 {
1322 if [ -z "$VCS_CHECKOUT" ]; then
1323 die "Merges require VCS_CHECKOUT."
1324 fi
1325
1326 set_branch
1327
1328 if [[ "$VCS_CHECKOUT" == 'bzr' ]]; then
1329 if test -n "$BRANCH_TO_MERGE"; then
1330 bzr merge $BRANCH_TO_MERGE
1331 bzr commit --message="Merge $BRANCH_TO_MERGE Build: $BUILD_TAG" --unchanged
1332 fi
1333
1334 bzr push "$BRANCH"
1335 elif [[ -n "$VCS_CHECKOUT" ]]; then
1336 die "Merge attempt occured, current VCS setup does not support this"
1337 fi
1338 }
1339
1340 enable_debug ()
1341 {
1342 if ! $DEBUG; then
1343 local caller_loc=`caller`
1344 if [ -n $1 ]; then
1345 echo "$caller_loc Enabling debug: $1"
1346 else
1347 echo "$caller_loc Enabling debug"
1348 fi
1349 set -x
1350 DEBUG=true
1351 fi
1352 }
1353
1354 function usage ()
1355 {
1356 cat << EOF
1357 Usage: $program_name [OPTION]..
1358
1359 Bootstrap this package from the checked-out sources, and optionally walk through CI run.
1360
1361 Options:
1362
1363 EOF
1364 }
1365
1366 disable_debug ()
1367 {
1368 set +x
1369 DEBUG=true
1370 }
1371
1372 # Script begins here
1373
1374 program_name=$0
1375
1376 env_debug_enabled=false
1377 if [[ -n "$JENKINS_HOME" ]]; then
1378 declare -r jenkins_build_environment=true
1379 else
1380 declare -r jenkins_build_environment=false
1381 fi
1382
1383 export ACLOCAL
1384 export AUTOCONF
1385 export AUTOHEADER
1386 export AUTOM4TE
1387 export AUTOMAKE
1388 export AUTORECONF
1389 export DEBUG
1390 export GNU_BUILD_FLAGS
1391 export LIBTOOLIZE
1392 export LIBTOOLIZE_OPTIONS
1393 export MAKE
1394 export TESTS_ENVIRONMENT
1395 export VERBOSE
1396 export WARNINGS
1397
1398 case $OSTYPE in
1399 darwin*)
1400 export MallocGuardEdges
1401 export MallocErrorAbort
1402 export MallocScribble
1403 ;;
1404 esac
1405
1406 # We check for DEBUG twice, once before we source the config file, and once afterward
1407 env_debug_enabled=false
1408 if [[ -n "$DEBUG" ]]; then
1409 env_debug_enabled=true
1410 enable_debug
1411 print_setup
1412 fi
1413
1414 # Variables which only can be set by .bootstrap
1415 BOOTSTRAP_SNAPSHOT=false
1416 BOOTSTRAP_SNAPSHOT_CHECK=
1417
1418 if [ -f '.bootstrap' ]; then
1419 source '.bootstrap'
1420 fi
1421
1422 if $env_debug_enabled; then
1423 enable_debug
1424 else
1425 if [[ -n "$DEBUG" ]]; then
1426 enable_debug "Enabling DEBUG from '.bootstrap'"
1427 print_setup
1428 fi
1429 fi
1430
1431 # We do this in order to protect the case where DEBUG
1432 if ! $env_debug_enabled; then
1433 DEBUG=false
1434 fi
1435
1436 main $@