cd8c5d12dd8c78267edffae2b266a124022da944
[awesomized/libmemcached] / bootstrap.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2012-2013 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 warn "$@: command not found"
49
50 #if $DEBUG; then
51 echo ""
52 echo "Stack trace:"
53 local frame=0
54 while caller $frame; do
55 ((frame++));
56 done
57 echo ""
58 #fi
59
60 return 127
61 }
62
63 function error ()
64 {
65 echo "$BASH_SOURCE:$BASH_LINENO: $@" >&2
66 }
67
68 function die ()
69 {
70 echo "$BASH_SOURCE:$BASH_LINENO: $@" >&2
71 exit 1;
72 }
73
74 function warn ()
75 {
76 echo "$BASH_SOURCE:$BASH_LINENO: $@"
77 #echo "$BASH_SOURCE:$BASH_LINENO: $@" >&1
78 }
79
80 function nassert ()
81 {
82 local param_name=\$"$1"
83 local param_value=`eval "expr \"$param_name\" "`
84
85 if [ -n "$param_value" ]; then
86 echo "$bash_source:$bash_lineno: assert($param_name) had value of "$param_value"" >&2
87 exit 1
88 fi
89 }
90
91 function assert ()
92 {
93 local param_name=\$"$1"
94 local param_value=`eval "expr \"$param_name\" "`
95
96 if [ -z "$param_value" ]; then
97 echo "$bash_source:$bash_lineno: assert($param_name)" >&2
98 exit 1
99 fi
100 }
101
102 function assert_file ()
103 {
104 if [ ! -f "$1" ]; then
105 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) does not exist: $2" >&2
106 exit 1;
107 fi
108 }
109
110 function assert_no_file ()
111 {
112 if [ -f "$1" ]; then
113 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) file exists: $2" >&2
114 exit 1;
115 fi
116 }
117
118 function assert_no_directory ()
119 {
120 if [ -d "$1" ]; then
121 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) directory exists: $2" >&2
122 exit 1;
123 fi
124 }
125
126 function assert_exec_file ()
127 {
128 if [ ! -f "$1" ]; then
129 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) does not exist: $2" >&2
130 exit 1;
131 fi
132
133 if [ ! -x "$1" ]; then
134 echo "$BASH_SOURCE:$BASH_LINENO: assert($1) exists but is not executable: $2" >&2
135 exit 1;
136 fi
137 }
138
139 function command_exists ()
140 {
141 type "$1" &> /dev/null ;
142 }
143
144 function rebuild_host_os ()
145 {
146 HOST_OS="${UNAME_MACHINE_ARCH}-${VENDOR}-${VENDOR_DISTRIBUTION}-${VENDOR_RELEASE}-${UNAME_KERNEL}-${UNAME_KERNEL_RELEASE}"
147 if [ -z "$1" ]; then
148 if $VERBOSE; then
149 echo "HOST_OS=$HOST_OS"
150 fi
151 fi
152 }
153
154 # Validate the distribution name, or toss an erro
155 # values: darwin,fedora,rhel,ubuntu,debian,opensuse
156 function set_VENDOR_DISTRIBUTION ()
157 {
158 local dist=`echo "$1" | tr '[A-Z]' '[a-z]'`
159 case "$dist" in
160 darwin)
161 VENDOR_DISTRIBUTION='darwin'
162 ;;
163 fedora)
164 VENDOR_DISTRIBUTION='fedora'
165 ;;
166 rhel)
167 VENDOR_DISTRIBUTION='rhel'
168 ;;
169 debian)
170 VENDOR_DISTRIBUTION='debian'
171 ;;
172 ubuntu)
173 VENDOR_DISTRIBUTION='ubuntu'
174 ;;
175 suse)
176 VENDOR_DISTRIBUTION='opensuse'
177 ;;
178 opensuse)
179 VENDOR_DISTRIBUTION='opensuse'
180 ;;
181 *)
182 die "attempt to set an invalid VENDOR_DISTRIBUTION=$dist"
183 ;;
184 esac
185 }
186
187 # Validate a Vendor's release name/number
188 function set_VENDOR_RELEASE ()
189 {
190 local release=`echo "$1" | tr '[A-Z]' '[a-z]'`
191
192 if $DEBUG; then
193 echo "VENDOR_DISTRIBUTION:$VENDOR_DISTRIBUTION"
194 echo "VENDOR_RELEASE:$release"
195 fi
196
197 case $VENDOR_DISTRIBUTION in
198 darwin)
199 case $release in
200 10.6*)
201 VENDOR_RELEASE='snow_leopard'
202 ;;
203 10.7*)
204 VENDOR_RELEASE='mountain'
205 ;;
206 mountain)
207 VENDOR_RELEASE='mountain'
208 ;;
209 10.8.*)
210 echo "mountain_lion"
211 VENDOR_RELEASE='mountain_lion'
212 ;;
213 *)
214 echo $VENDOR_RELEASE
215 VENDOR_RELEASE='unknown'
216 ;;
217 esac
218 ;;
219 fedora)
220 VENDOR_RELEASE="$release"
221 if [[ "x$VENDOR_RELEASE" == '18' ]]; then
222 VENDOR_RELEASE='sphericalcow'
223 fi
224 ;;
225 rhel)
226 VENDOR_RELEASE="$release"
227 ;;
228 debian)
229 VENDOR_RELEASE="$release"
230 ;;
231 ubuntu)
232 VENDOR_RELEASE="$release"
233 if [[ "x$VENDOR_RELEASE" == 'x12.04' ]]; then
234 VENDOR_RELEASE="precise"
235 elif [[ "x$VENDOR_RELEASE" == 'x12.10' ]]; then
236 VENDOR_RELEASE="quantal"
237 elif [[ "x$VENDOR_RELEASE" == 'x13.04' ]]; then
238 VENDOR_RELEASE="raring"
239 fi
240 ;;
241 opensuse)
242 VENDOR_RELEASE="$release"
243 ;;
244 unknown)
245 die "attempt to set VENDOR_RELEASE without setting VENDOR_DISTRIBUTION"
246 ;;
247 *)
248 die "attempt to set with an invalid VENDOR_DISTRIBUTION=$VENDOR_DISTRIBUTION"
249 ;;
250 esac
251 }
252
253
254 # Valid values are: apple, redhat, centos, canonical, oracle, suse
255 function set_VENDOR ()
256 {
257 local vendor=`echo "$1" | tr '[A-Z]' '[a-z]'`
258
259 case $vendor in
260 apple)
261 VENDOR='apple'
262 ;;
263 redhat)
264 VENDOR='redhat'
265 ;;
266 fedora)
267 VENDOR='redhat'
268 ;;
269 redhat-release-server-*)
270 VENDOR='redhat'
271 ;;
272 enterprise-release-*)
273 VENDOR='oracle'
274 ;;
275 centos)
276 VENDOR='centos'
277 ;;
278 canonical)
279 VENDOR='canonical'
280 ;;
281 ubuntu)
282 VENDOR='canonical'
283 ;;
284 debian)
285 VENDOR='debian'
286 ;;
287 opensuse)
288 VENDOR='suse'
289 ;;
290 suse)
291 VENDOR='suse'
292 ;;
293 *)
294 die "An attempt was made to set an invalid VENDOR=$_vendor"
295 ;;
296 esac
297
298 set_VENDOR_DISTRIBUTION $2
299 set_VENDOR_RELEASE $3
300
301 # Set which vendor/versions we trust for autoreconf
302 case $VENDOR_DISTRIBUTION in
303 fedora)
304 if [[ "x$VENDOR_RELEASE" == 'x18' ]]; then
305 AUTORECONF_REBUILD_HOST=true
306 elif [[ "x$VENDOR_RELEASE" == 'xsphericalcow' ]]; then
307 AUTORECONF_REBUILD_HOST=true
308 elif [[ "x$VENDOR_RELEASE" == 'x19' ]]; then
309 AUTORECONF_REBUILD_HOST=true
310 fi
311 ;;
312 canonical)
313 if [[ "x$VENDOR_RELEASE" == 'xprecise' ]]; then
314 AUTORECONF_REBUILD_HOST=true
315 elif [[ "x$VENDOR_RELEASE" == 'xquantal' ]]; then
316 AUTORECONF_REBUILD_HOST=true
317 fi
318 ;;
319 esac
320
321 }
322
323 function determine_target_platform ()
324 {
325 UNAME_MACHINE_ARCH=`(uname -m) 2>/dev/null` || UNAME_MACHINE_ARCH=unknown
326 UNAME_KERNEL=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown
327 UNAME_KERNEL_RELEASE=`(uname -r) 2>/dev/null` || UNAME_KERNEL_RELEASE=unknown
328
329 if [[ -x '/usr/bin/sw_vers' ]]; then
330 local _VERSION=`/usr/bin/sw_vers -productVersion`
331 set_VENDOR 'apple' 'darwin' $_VERSION
332 elif [[ $(uname) == 'Darwin' ]]; then
333 set_VENDOR 'apple' 'darwin' 'mountain'
334 elif [[ -f '/etc/fedora-release' ]]; then
335 local fedora_version=`cat /etc/fedora-release | awk ' { print $3 } '`
336 set_VENDOR 'redhat' 'fedora' $fedora_version
337 elif [[ -f '/etc/centos-release' ]]; then
338 local centos_version=`cat /etc/centos-release | awk ' { print $7 } '`
339 set_VENDOR 'centos' 'rhel' $centos_version
340 elif [[ -f '/etc/SuSE-release' ]]; then
341 local suse_distribution=`head -1 /etc/SuSE-release | awk ' { print $1 } '`
342 local suse_version=`head -1 /etc/SuSE-release | awk ' { print $2 } '`
343 set_VENDOR 'suse' $suse_distribution $suse_version
344 elif [[ -f '/etc/redhat-release' ]]; then
345 local rhel_version=`cat /etc/redhat-release | awk ' { print $7 } '`
346 local _vendor=`rpm -qf /etc/redhat-release`
347 set_VENDOR $_vendor 'rhel' $rhel_version
348 elif [[ -f '/etc/os-release' ]]; then
349 source '/etc/os-release'
350 set_VENDOR $ID $ID $VERSION_ID
351 elif [[ -x '/usr/bin/lsb_release' ]]; then
352 local _ID=`/usr/bin/lsb_release -s -i`
353 local _VERSION=`/usr/bin/lsb_release -s -r`
354 set_VENDOR $_ID $_ID $_VERSION_ID
355 elif [[ -f '/etc/lsb-release' ]]; then
356 source '/etc/lsb-release'
357 set_VENDOR 'canonical' $DISTRIB_ID $DISTRIB_CODENAME
358 fi
359
360 rebuild_host_os
361 }
362
363 function run_configure ()
364 {
365 # We will run autoreconf if we are required
366 run_autoreconf_if_required
367
368 # We always begin at the root of our build
369 if [ ! popd ]; then
370 die "Programmer error, we entered run_configure with a stacked directory"
371 fi
372
373 if ! command_exists "$CONFIGURE"; then
374 die "$CONFIGURE does not exist"
375 fi
376
377 local BUILD_DIR="$1"
378 if [[ -n "$BUILD_DIR" ]]; then
379 rm -r -f $BUILD_DIR
380 mkdir -p $BUILD_DIR
381 safe_pushd $BUILD_DIR
382 fi
383
384 # Arguments for configure
385 local BUILD_CONFIGURE_ARG=
386
387 # If ENV DEBUG is set we enable both debug and asssert, otherwise we see if this is a VCS checkout and if so enable assert
388 # Set ENV ASSERT in order to enable assert.
389 # If we are doing a valgrind run, we always compile with assert disabled
390 if $valgrind_run; then
391 BUILD_CONFIGURE_ARG+= '--enable-assert=no'
392 else
393 if $DEBUG; then
394 BUILD_CONFIGURE_ARG+=' --enable-debug --enable-assert'
395 elif [[ -n "$VCS_CHECKOUT" ]]; then
396 BUILD_CONFIGURE_ARG+=' --enable-assert'
397 fi
398 fi
399
400 if [[ -n "$CONFIGURE_ARG" ]]; then
401 BUILD_CONFIGURE_ARG+=" $CONFIGURE_ARG"
402 fi
403
404 if [[ -n "$PREFIX_ARG" ]]; then
405 BUILD_CONFIGURE_ARG+=" $PREFIX_ARG"
406 fi
407
408 ret=1;
409 # If we are executing on OSX use CLANG, otherwise only use it if we find it in the ENV
410 case $HOST_OS in
411 *-darwin-*)
412 CC=clang CXX=clang++ $top_srcdir/configure $BUILD_CONFIGURE_ARG || die "Cannot execute CC=clang CXX=clang++ configure $BUILD_CONFIGURE_ARG"
413 ret=$?
414 ;;
415 rhel-5*)
416 command_exists 'gcc44' || die "Could not locate gcc44"
417 CC=gcc44 CXX=gcc44 $top_srcdir/configure $BUILD_CONFIGURE_ARG || die "Cannot execute CC=gcc44 CXX=gcc44 configure $BUILD_CONFIGURE_ARG"
418 ret=$?
419 ;;
420 *)
421 $CONFIGURE $BUILD_CONFIGURE_ARG
422 ret=$?
423 ;;
424 esac
425
426 if [ $ret -ne 0 ]; then
427 die "Could not execute $CONFIGURE $BUILD_CONFIGURE_ARG"
428 fi
429
430 if [ ! -f 'Makefile' ]; then
431 die "Programmer error, configure was run but no Makefile existed after $CONFIGURE was run"
432 fi
433 }
434
435 function setup_gdb_command () {
436 GDB_TMPFILE=$(mktemp /tmp/gdb.XXXXXXXXXX)
437 echo 'set logging overwrite on' > $GDB_TMPFILE
438 echo 'set logging on' >> $GDB_TMPFILE
439 echo 'set environment LIBTEST_IN_GDB=1' >> $GDB_TMPFILE
440 echo 'run' >> $GDB_TMPFILE
441 echo 'thread apply all bt' >> $GDB_TMPFILE
442 echo 'quit' >> $GDB_TMPFILE
443 GDB_COMMAND="gdb -f -batch -x $GDB_TMPFILE"
444 }
445
446 function setup_valgrind_command () {
447 VALGRIND_PROGRAM=`type -p valgrind`
448 if [[ -n "$VALGRIND_PROGRAM" ]]; then
449 VALGRIND_COMMAND="$VALGRIND_PROGRAM --error-exitcode=1 --leak-check=yes --malloc-fill=A5 --free-fill=DE --xml=yes --xml-file=\"valgrind-%p.xml\" --gen-suppressions=yes --log-file=\"valgrind-%p.txt\""
450 fi
451 }
452
453 function save_BUILD ()
454 {
455 if [[ -n "$OLD_CONFIGURE" ]]; then
456 die "OLD_CONFIGURE($OLD_CONFIGURE) was set on push, programmer error!"
457 fi
458
459 if [[ -n "$OLD_CONFIGURE_ARG" ]]; then
460 die "OLD_CONFIGURE_ARG($OLD_CONFIGURE_ARG) was set on push, programmer error!"
461 fi
462
463 if [[ -n "$OLD_PREFIX" ]]; then
464 die "OLD_PREFIX($OLD_PREFIX) was set on push, programmer error!"
465 fi
466
467 if [[ -n "$OLD_MAKE" ]]; then
468 die "OLD_MAKE($OLD_MAKE) was set on push, programmer error!"
469 fi
470
471 if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
472 die "OLD_TESTS_ENVIRONMENT($OLD_TESTS_ENVIRONMENT) was set on push, programmer error!"
473 fi
474
475 if [[ -n "$CONFIGURE" ]]; then
476 OLD_CONFIGURE=$CONFIGURE
477 fi
478
479 if [[ -n "$CONFIGURE_ARG" ]]; then
480 OLD_CONFIGURE_ARG=$CONFIGURE_ARG
481 fi
482
483 if [[ -n "$MAKE" ]]; then
484 OLD_MAKE=$MAKE
485 fi
486
487 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
488 OLD_TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT
489 fi
490 }
491
492 function restore_BUILD ()
493 {
494 if [[ -n "$OLD_CONFIGURE" ]]; then
495 CONFIGURE=$OLD_CONFIGURE
496 fi
497
498 if [[ -n "$OLD_CONFIGURE_ARG" ]]; then
499 CONFIGURE_ARG=$OLD_CONFIGURE_ARG
500 fi
501
502 if [[ -n "$OLD_PREFIX" ]]; then
503 PREFIX_ARG=$OLD_PREFIX
504 fi
505
506 if [[ -n "$OLD_MAKE" ]]; then
507 MAKE=$OLD_MAKE
508 fi
509
510 if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
511 TESTS_ENVIRONMENT=$OLD_TESTS_ENVIRONMENT
512 fi
513
514 OLD_CONFIGURE=
515 OLD_CONFIGURE_ARG=
516 OLD_PREFIX=
517 OLD_MAKE=
518 OLD_TESTS_ENVIRONMENT=
519
520 export -n CC CXX
521 }
522
523 function safe_pushd ()
524 {
525 pushd $1 &> /dev/null ;
526
527 if [ -n "$BUILD_DIR" ]; then
528 if $VERBOSE; then
529 echo "BUILD_DIR=$BUILD_DIR"
530 fi
531 fi
532 }
533
534 function safe_popd ()
535 {
536 local directory_to_delete=`pwd`
537 popd &> /dev/null ;
538 if [ $? -eq 0 ]; then
539 if [[ "$top_srcdir" == "$directory_to_delete" ]]; then
540 die "We almost deleted top_srcdir($top_srcdir), programmer error"
541 fi
542
543 rm -r -f "$directory_to_delete"
544 fi
545 }
546
547 function make_valgrind ()
548 {
549 # If the env VALGRIND_COMMAND is set then we assume it is valid
550 local valgrind_was_set=false
551 if [[ -z "$VALGRIND_COMMAND" ]]; then
552 setup_valgrind_command
553 if [[ -n "$VALGRIND_COMMAND" ]]; then
554 valgrind_was_set=true
555 fi
556 else
557 valgrind_was_set=true
558 fi
559
560 # If valgrind_was_set is set to no we bail
561 if ! $valgrind_was_set; then
562 echo 'valgrind was not present'
563 return 1
564 fi
565
566 save_BUILD
567
568 valgrind_run=true
569
570 # If we are required to run configure, do so now
571 run_configure
572
573 # If we don't have a configure, then most likely we will be missing libtool
574 assert_file 'configure'
575 if [[ -x 'libtool' ]]; then
576 TESTS_ENVIRONMENT="./libtool --mode=execute $VALGRIND_COMMAND"
577 else
578 TESTS_ENVIRONMENT="$VALGRIND_COMMAND"
579 fi
580
581 make_target 'all'
582 make_target 'check'
583 ret=$?
584
585 # If we aren't going to error, we will clean up our environment
586 if [ "$ret" -eq 0 ]; then
587 make 'distclean'
588 fi
589
590 valgrind_run=false
591
592 restore_BUILD
593
594 if [ "$ret" -ne 0 ]; then
595 return 1
596 fi
597 }
598
599 function make_install_system ()
600 {
601 local INSTALL_LOCATION=$(mktemp -d /tmp/XXXXXXXXXX)
602
603 save_BUILD
604 PREFIX_ARG="--prefix=$INSTALL_LOCATION"
605
606 if [ ! -d $INSTALL_LOCATION ] ; then
607 die "ASSERT temp directory not found '$INSTALL_LOCATION'"
608 fi
609
610 run_configure #install_buid_dir
611
612 make_target 'install'
613
614 make_target 'installcheck'
615
616 make_target 'uninstall'
617
618 rm -r -f $INSTALL_LOCATION
619 make 'distclean'
620
621 if [ -f 'Makefile' ]; then
622 die "ASSERT Makefile should not exist"
623 fi
624
625 restore_BUILD
626 safe_popd
627 }
628
629 function make_darwin_malloc ()
630 {
631 run_configure_if_required
632
633 old_MallocGuardEdges=$MallocGuardEdges
634 MallocGuardEdges=1
635 old_MallocErrorAbort=$MallocErrorAbort
636 MallocErrorAbort=1
637 old_MallocScribble=$MallocScribble
638 MallocScribble=1
639
640 make_check
641
642 MallocGuardEdges=$old_MallocGuardEdges
643 MallocErrorAbort=$old_MallocErrorAbort
644 MallocScribble=$old_MallocScribble
645 }
646
647 # This will reset our environment, and make sure built files are available.
648 function make_for_snapshot ()
649 {
650 # Lets make sure we have a clean environment
651 assert_no_file 'Makefile'
652 assert_no_file 'configure'
653 assert_no_directory 'autom4te.cache'
654
655 run_configure
656 make_target 'all'
657 make_target 'distclean'
658
659 # We should have a configure, but no Makefile at the end of this exercise
660 assert_no_file 'Makefile'
661 assert_exec_file 'configure'
662 }
663
664 function check_mingw ()
665 {
666 command_exists 'mingw64-configure'
667 ret=$?
668 if [ "$ret" -ne 0 ]; then
669 return 1
670 fi
671
672 command_exists 'mingw64-make'
673 ret=$?
674 if [ "$ret" -ne 0 ]; then
675 return 1
676 fi
677
678 return 0
679 }
680
681 function check_clang ()
682 {
683 command_exists 'clang'
684 ret=$?
685 if [ "$ret" -ne 0 ]; then
686 return 1
687 fi
688
689 return 0
690 }
691
692 function check_clang_analyzer ()
693 {
694 command_exists 'scan-build'
695 ret=$?
696 if [ "$ret" -ne 0 ]; then
697 return 1
698 fi
699
700 return 0
701 }
702
703 function make_skeleton ()
704 {
705 run_configure
706 ret=$?
707
708 if [ $ret -eq 0 ]; then
709 assert_file 'Makefile'
710
711 make_target 'all' 'warn'
712 ret=$?
713 if [ $ret -ne 0 ]; then
714 warn "$MAKE failed"
715 else
716 if [[ -n "$DISPLAY" ]]; then
717 if command_exists 'wine'; then
718 TESTS_ENVIRONMENT='wine'
719 fi
720 fi
721
722 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
723 make_target 'check' 'warn' || warn "$MAKE check failed"
724 ret=$?
725 fi
726 fi
727
728 if $jenkins_build_environment; then
729 make_target 'clean' 'warn'
730 fi
731 fi
732
733 return $ret
734 }
735
736 function make_for_mingw ()
737 {
738 if ! check_mingw; then
739 return 1
740 fi
741
742 # Make sure it is clean
743 if [ -f Makefile -o -f configure ]; then
744 make_maintainer_clean
745 fi
746
747 run_autoreconf
748
749 save_BUILD
750
751 CONFIGURE='mingw64-configure'
752 MAKE='mingw64-make'
753 CONFIGURE_ARGS='--enable-static --disable-shared'
754
755 make_skeleton
756 ret=$?
757
758 restore_BUILD
759
760 return $ret
761 }
762
763 function make_for_clang ()
764 {
765 if ! check_clang; then
766 return 1
767 fi
768
769 # Make sure it is clean
770 if [ -f Makefile -o -f configure ]; then
771 make_maintainer_clean
772 fi
773
774 run_autoreconf
775
776 save_BUILD
777
778 CC=clang CXX=clang++
779 export CC CXX
780
781 make_skeleton
782 ret=$?
783
784 make_target 'check'
785
786 restore_BUILD
787
788 return $ret
789 }
790
791 function make_for_clang_analyzer ()
792 {
793 if ! check_clang; then
794 return 1
795 fi
796
797 if ! check_clang_analyzer; then
798 die 'clang-analyzer was not found'
799 fi
800
801 # Make sure it is clean
802 if [ -f Makefile -o -f configure ]; then
803 make_maintainer_clean
804 fi
805
806 run_autoreconf
807
808 save_BUILD
809
810 CC=clang CXX=clang++
811 export CC CXX
812 CONFIGURE_ARGS='--enable-debug'
813
814 make_skeleton
815 ret=$?
816
817 make_target 'clean' 'warn'
818
819 scan-build -o clang-html make -j4 -k
820
821 restore_BUILD
822
823 return $ret
824 }
825
826 # If we are locally testing, we should make sure the environment is setup correctly
827 function check_for_jenkins ()
828 {
829 if ! $jenkins_build_environment; then
830 echo "Not inside of jenkins, simulating environment"
831
832 if [ -f 'configure' ]; then
833 make_maintainer_clean
834 fi
835
836 if $BOOTSTRAP_SNAPSHOT; then
837 make_for_snapshot
838 fi
839 fi
840 }
841
842 function make_universe ()
843 {
844 make_for_snapshot
845 make_valgrind
846 make_gdb
847 make_rpm
848 make_for_clang
849 make_for_clang_analyzer
850
851 if [ check_mingw -eq 0 ]; then
852 make_for_mingw
853 fi
854
855 make_distcheck
856 make_install_system
857 }
858
859 function check_snapshot ()
860 {
861 if [ -n "$BOOTSTRAP_SNAPSHOT_CHECK" ]; then
862 assert_file "$BOOTSTRAP_SNAPSHOT_CHECK" 'snapshot check failed'
863 fi
864 }
865
866 function make_for_continuus_integration ()
867 {
868 # Setup the environment if we are local
869 check_for_jenkins
870
871 # No matter then evironment, we should not have a Makefile at this point
872 assert_no_file 'Makefile'
873
874 # Platforms which require bootstrap should have some setup done before we hit this stage.
875 # If we are building locally, skip this step, unless we are just testing locally.
876 if $BOOTSTRAP_SNAPSHOT; then
877 if $BOOTSTRAP_SNAPSHOT; then
878 assert_file 'configure'
879 fi
880
881 check_snapshot
882 else
883 # If we didn't require a snapshot, then we should not have a configure
884 assert_no_file 'configure'
885
886 run_autoreconf
887 fi
888
889 assert_no_file 'Makefile' 'Programmer error, Makefile existed where build state should have been clean'
890
891 case $HOST_OS in
892 *-fedora-*)
893 run_configure
894
895 assert_exec_file 'configure'
896 assert_file 'Makefile'
897
898 make_target 'all'
899
900 # make rpm includes "make distcheck"
901 if [[ -f rpm.am ]]; then
902 make_rpm
903 elif [[ -d rpm ]]; then
904 make_rpm
905 fi
906 ;;
907 *)
908 make_jenkins_default
909 ;;
910 esac
911
912 make_maintainer_clean
913
914 safe_popd
915 }
916
917 # The point to this test is to test bootstrap.sh itself
918 function self_test ()
919 {
920 # We start off with a clean env
921 make_maintainer_clean
922
923 eval "./bootstrap.sh jenkins" || die "failed 'jenkins'"
924 eval "./bootstrap.sh all" || die "failed 'all'"
925 eval "./bootstrap.sh gdb" || die "failed 'gdb'"
926 eval "./bootstrap.sh maintainer-clean" || die "failed 'maintainer-clean'"
927 }
928
929 function make_install_html ()
930 {
931 run_configure_if_required
932 assert_file 'configure'
933
934 make_target 'install-html'
935 }
936
937 function make_gdb ()
938 {
939 save_BUILD
940
941 if command_exists 'gdb'; then
942 run_configure_if_required
943
944 # Set ENV GDB_COMMAND
945 if [[ -z "$GDB_COMMAND" ]]; then
946 setup_gdb_command
947 fi
948
949 # If we don't have a configure, then most likely we will be missing libtool
950 assert_file 'configure'
951 if [[ -f 'libtool' ]]; then
952 TESTS_ENVIRONMENT="./libtool --mode=execute $GDB_COMMAND"
953 else
954 TESTS_ENVIRONMENT="$GDB_COMMAND"
955 fi
956
957 make_target 'check'
958
959 if [ -f 'gdb.txt' ]; then
960 rm 'gdb.txt'
961 fi
962
963 if [ -f '.gdb_history' ]; then
964 rm '.gdb_history'
965 fi
966
967 if $jenkins_build_environment; then
968 make_target 'clean'
969 fi
970 else
971 echo 'gdb was not present'
972 return 1
973 fi
974
975 restore_BUILD
976 }
977
978 # $1 target to compile
979 # $2 to die, or not to die, based on contents
980 function make_target ()
981 {
982 if [ -z "$1" ]; then
983 die "Programmer error, no target provided for make"
984 fi
985
986 if [ ! -f 'Makefile' ]; then
987 die "Programmer error, make was called before configure"
988 run_configure
989 fi
990
991 if [ -n "$TESTS_ENVIRONMENT" ]; then
992 if $VERBOSE; then
993 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
994 fi
995 fi
996
997 if [ -z "$MAKE" ]; then
998 die "MAKE was not set"
999 fi
1000
1001 # $2 represents error or warn
1002 run $MAKE $1
1003 ret=$?
1004
1005 if [ $ret -ne 0 ]; then
1006 if [ -n "$2" ]; then
1007 warn "Failed to execute $MAKE $1: $ret"
1008 else
1009 die "Failed to execute $MAKE $1: $ret"
1010 fi
1011 fi
1012
1013 return $ret
1014 }
1015
1016 function make_distcheck ()
1017 {
1018 make_target 'distcheck'
1019 }
1020
1021 function make_rpm ()
1022 {
1023 if command_exists 'rpmbuild'; then
1024 if [ -f 'rpm.am' -o -d 'rpm' ]; then
1025 run_configure_if_required
1026 make_target 'rpm'
1027
1028 if $jenkins_build_environment; then
1029 make_target 'clean'
1030 fi
1031
1032 fi
1033 fi
1034 }
1035
1036 function make_maintainer_clean ()
1037 {
1038 run_configure_if_required
1039 make_target 'maintainer-clean' 'no_error'
1040
1041 # Lets make sure we really cleaned up the environment
1042 assert_no_file 'Makefile'
1043 assert_no_file 'configure'
1044 assert_no_directory 'autom4te.cache'
1045 }
1046
1047 function make_check ()
1048 {
1049 make_target 'check'
1050 }
1051
1052 function make_jenkins_default ()
1053 {
1054 run_configure
1055 make_target 'all'
1056 }
1057
1058 function make_default ()
1059 {
1060 run_configure_if_required
1061 make_target 'all'
1062 }
1063
1064 function run_configure_if_required ()
1065 {
1066 run_autoreconf_if_required
1067
1068 if [ ! -f 'Makefile' ]; then
1069 run_configure
1070 fi
1071
1072 assert_file 'Makefile' 'configure did not produce a Makefile'
1073 }
1074
1075 function run_make_maintainer_clean_if_possible ()
1076 {
1077 if [ -f 'Makefile' ]; then
1078 make_maintainer_clean
1079 fi
1080 }
1081
1082 function run_autoreconf_if_required ()
1083 {
1084 if [ ! -x 'configure' ]; then
1085 run_autoreconf
1086 fi
1087
1088 assert_exec_file 'configure'
1089 bash -n configure
1090 }
1091
1092 function run_autoreconf ()
1093 {
1094 if [[ -z "$AUTORECONF" ]]; then
1095 die "Programmer error, tried to call run_autoreconf () but AUTORECONF was not set"
1096 fi
1097
1098 if test $use_libtool = 1; then
1099 assert $BOOTSTRAP_LIBTOOLIZE
1100 run $BOOTSTRAP_LIBTOOLIZE '--copy' '--install' '--force' || die "Cannot execute $BOOTSTRAP_LIBTOOLIZE"
1101 fi
1102
1103 run $AUTORECONF $AUTORECONF_ARGS || die "Cannot execute $AUTORECONF"
1104
1105 eval 'bash -n configure' || die "autoreconf generated a malformed configure"
1106 }
1107
1108 function run ()
1109 {
1110 if $VERBOSE; then
1111 echo "\`$@' $ARGS"
1112 fi
1113
1114 if [ -z "$1" ]; then
1115 return 127;
1116 fi
1117
1118 eval $@ $ARGS
1119 }
1120
1121 function parse_command_line_options ()
1122 {
1123 local SHORTOPTS=':apcmt:dvh'
1124
1125 nassert MAKE_TARGET
1126
1127 while getopts "$SHORTOPTS" opt; do
1128 case $opt in
1129 a) #--autoreconf
1130 AUTORECONF_OPTION=true
1131 MAKE_TARGET='autoreconf'
1132 ;;
1133 p) #--print-env
1134 PRINT_SETUP_OPTION=true
1135 ;;
1136 c) # --configure
1137 CONFIGURE_OPTION=true
1138 MAKE_TARGET='configure'
1139 ;;
1140 m) # maintainer-clean
1141 CLEAN_OPTION=true
1142 MAKE_TARGET='clean_op'
1143 ;;
1144 t) # target
1145 TARGET_OPTION=true
1146 TARGET_OPTION_ARG="$OPTARG"
1147 MAKE_TARGET="$OPTARG"
1148 ;;
1149 d) # debug
1150 DEBUG_OPTION=true
1151 enable_debug
1152 ;;
1153 h) # help
1154 echo "bootstrap.sh [options] optional_target ..."
1155 echo " -a # Just run autoreconf";
1156 echo " -p # Print ENV";
1157 echo " -c # Just run configure";
1158 echo " -m # Just run maintainer-clean";
1159 echo " -t # Make target";
1160 echo " -d # Enable debug";
1161 echo " -h # Show help";
1162 echo " -v # Be more verbose in output";
1163 exit
1164 ;;
1165 v) # verbose
1166 VERBOSE_OPTION=true
1167 VERBOSE=true
1168 ;;
1169 :)
1170 echo "Option -$OPTARG requires an argument." >&2
1171 exit 1
1172 ;;
1173 *)
1174 echo "$0: error - unrecognized option $1" 1>&2
1175 exit 1
1176 ;;
1177 esac
1178 done
1179
1180 shift $((OPTIND-1))
1181
1182 if [ -n "$1" ]; then
1183 MAKE_TARGET="$@"
1184 fi
1185 }
1186
1187 function determine_vcs ()
1188 {
1189 if [[ -d '.git' ]]; then
1190 VCS_CHECKOUT=git
1191 elif [[ -d '.bzr' ]]; then
1192 VCS_CHECKOUT=bzr
1193 elif [[ -d '.svn' ]]; then
1194 VCS_CHECKOUT=svn
1195 elif [[ -d '.hg' ]]; then
1196 VCS_CHECKOUT=hg
1197 else
1198 VCS_CHECKOUT=
1199 fi
1200
1201 if [[ -n "$VCS_CHECKOUT" ]]; then
1202 VERBOSE=true
1203 fi
1204 }
1205
1206 function require_libtoolise ()
1207 {
1208 use_libtool=0
1209 grep '^[ ]*A[CM]_PROG_LIBTOOL' configure.ac >/dev/null \
1210 && use_libtool=1
1211 grep '^[ ]*LT_INIT' configure.ac >/dev/null \
1212 && use_libtool=1
1213 }
1214
1215 function autoreconf_setup ()
1216 {
1217 # Set ENV MAKE in order to override "make"
1218 if [[ -z "$MAKE" ]]; then
1219 if command_exists 'gmake'; then
1220 MAKE=`type -p gmake`
1221 else
1222 if command_exists 'make'; then
1223 MAKE=`type -p make`
1224 fi
1225 fi
1226
1227 if [ "$VCS_CHECKOUT" ]; then
1228 if $DEBUG; then
1229 MAKE="$MAKE --warn-undefined-variables"
1230 fi
1231 fi
1232
1233 if $DEBUG; then
1234 MAKE="$MAKE -d"
1235 fi
1236 fi
1237
1238 if [[ -z "$GNU_BUILD_FLAGS" ]]; then
1239 GNU_BUILD_FLAGS="--install --force"
1240 fi
1241
1242 if $VERBOSE; then
1243 GNU_BUILD_FLAGS="$GNU_BUILD_FLAGS --verbose"
1244 fi
1245
1246 if [ -z "$ACLOCAL_PATH" ]; then
1247 ACLOCAL_PATH="/usr/local/share/aclocal $ACLOCAL_PATH"
1248 fi
1249
1250 if [[ -z "$WARNINGS" ]]; then
1251 if [[ -n "$VCS_CHECKOUT" ]]; then
1252 WARNINGS="all,error"
1253 else
1254 WARNINGS="all"
1255 fi
1256 fi
1257
1258 if test $use_libtool = 1; then
1259 if [[ -n "$LIBTOOLIZE" ]]; then
1260 BOOTSTRAP_LIBTOOLIZE=`type -p $LIBTOOLIZE`
1261
1262 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1263 echo "Couldn't find user supplied libtoolize, it is required"
1264 return 1
1265 fi
1266 else
1267 # If we are using OSX, we first check to see glibtoolize is available
1268 if [[ "$VENDOR_DISTRIBUTION" == "darwin" ]]; then
1269 BOOTSTRAP_LIBTOOLIZE=`type -p glibtoolize`
1270
1271 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1272 echo "Couldn't find glibtoolize, it is required on OSX"
1273 return 1
1274 fi
1275 else
1276 BOOTSTRAP_LIBTOOLIZE=`type -p libtoolize`
1277
1278 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1279 echo "Couldn't find libtoolize, it is required"
1280 return 1
1281 fi
1282 fi
1283 fi
1284
1285 if $VERBOSE; then
1286 LIBTOOLIZE_OPTIONS="--verbose $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
1287 fi
1288
1289 if $DEBUG; then
1290 LIBTOOLIZE_OPTIONS="--debug $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
1291 fi
1292
1293 # Here we set LIBTOOLIZE to true since we are going to invoke it via BOOTSTRAP_LIBTOOLIZE
1294 LIBTOOLIZE=true
1295 fi
1296
1297 # Test the ENV AUTOMAKE if it exists
1298 if [[ -n "$AUTOMAKE" ]]; then
1299 run $AUTOMAKE '--help' &> /dev/null || die "Failed to run AUTOMAKE:$AUTOMAKE"
1300 fi
1301
1302 # Test the ENV AUTOCONF if it exists
1303 if [[ -n "$AUTOCONF" ]]; then
1304 run $AUTOCONF '--help' &> /dev/null || die "Failed to run AUTOCONF:$AUTOCONF"
1305 fi
1306
1307 # Test the ENV AUTOHEADER if it exists
1308 if [[ -n "$AUTOHEADER" ]]; then
1309 run $AUTOHEADER '--help' &> /dev/null || die "Failed to run AUTOHEADER:$AUTOHEADER"
1310 fi
1311
1312 # Test the ENV AUTOM4TE if it exists
1313 if [[ -n "$AUTOM4TE" ]]; then
1314 run $AUTOM4TE '--help' &> /dev/null || die "Failed to run AUTOM4TE:$AUTOM4TE"
1315 fi
1316
1317 # Test the ENV AUTOHEADER if it exists, if not we add one and add --install
1318 if [[ -z "$ACLOCAL" ]]; then
1319 ACLOCAL="aclocal --install"
1320 fi
1321 run $ACLOCAL '--help' &> /dev/null || die "Failed to run ACLOCAL:$ACLOCAL"
1322
1323 if [[ -z "$AUTORECONF" ]]; then
1324 AUTORECONF=`type -p autoreconf`
1325
1326 if [[ -z "$AUTORECONF" ]]; then
1327 die "Couldn't find autoreconf"
1328 fi
1329
1330 if [[ -n "$GNU_BUILD_FLAGS" ]]; then
1331 AUTORECONF_ARGS="$GNU_BUILD_FLAGS"
1332 fi
1333 fi
1334
1335 run $AUTORECONF '--help' &> /dev/null || die "Failed to run AUTORECONF:$AUTORECONF"
1336 }
1337
1338 function print_setup ()
1339 {
1340 saved_debug_status=$DEBUG
1341 if $DEBUG; then
1342 disable_debug
1343 fi
1344
1345 echo '----------------------------------------------'
1346 echo 'BOOTSTRAP ENV'
1347 echo "AUTORECONF=$AUTORECONF"
1348 echo "HOST_OS=$HOST_OS"
1349 echo "VENDOR=$VENDOR"
1350 echo "VENDOR_DISTRIBUTION=$VENDOR_DISTRIBUTION"
1351 echo "VENDOR_RELEASE=$VENDOR_RELEASE"
1352
1353 echo "getopt()"
1354 if $AUTORECONF_OPTION; then
1355 echo "--autoreconf"
1356 fi
1357
1358 if $CLEAN_OPTION; then
1359 echo "--clean"
1360 fi
1361
1362 if $CONFIGURE_OPTION; then
1363 echo "--configure"
1364 fi
1365
1366 if $DEBUG_OPTION; then
1367 echo "--debug"
1368 fi
1369
1370 if $PRINT_SETUP_OPTION; then
1371 echo "--print-env"
1372 fi
1373
1374 if $TARGET_OPTION; then
1375 echo "--target=$TARGET_OPTION_ARG"
1376 fi
1377
1378 if $VERBOSE_OPTION; then
1379 echo "--verbose"
1380 fi
1381
1382 if [[ -n "$MAKE" ]]; then
1383 echo "MAKE=$MAKE"
1384 fi
1385
1386 if [[ -n "$MAKE_TARGET" ]]; then
1387 echo "MAKE_TARGET=$MAKE_TARGET"
1388 fi
1389
1390 if [[ -n "$PREFIX" ]]; then
1391 echo "PREFIX=$PREFIX"
1392 fi
1393
1394 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
1395 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
1396 fi
1397
1398 if [[ -n "$VCS_CHECKOUT" ]]; then
1399 echo "VCS_CHECKOUT=$VCS_CHECKOUT"
1400 fi
1401
1402 if $VERBOSE; then
1403 echo "VERBOSE=true"
1404 fi
1405
1406 if $DEBUG; then
1407 echo "DEBUG=true"
1408 fi
1409
1410 if [[ -n "$WARNINGS" ]]; then
1411 echo "WARNINGS=$WARNINGS"
1412 fi
1413 echo '----------------------------------------------'
1414
1415 if $saved_debug_status; then
1416 enable_debug
1417 fi
1418 }
1419
1420 function make_clean_option ()
1421 {
1422 run_configure_if_required
1423
1424 make_maintainer_clean
1425
1426 if [[ "$VCS_CHECKOUT" == 'git' ]]; then
1427 run "$VCS_CHECKOUT" status --ignored
1428 elif [[ -n "$VCS_CHECKOUT" ]]; then
1429 run "$VCS_CHECKOUT" status
1430 fi
1431 }
1432
1433 function make_for_autoreconf ()
1434 {
1435 if [ -f 'Makefile' ]; then
1436 make_maintainer_clean
1437 fi
1438
1439 run_autoreconf
1440
1441 assert_no_file 'Makefile'
1442 }
1443
1444 function check_make_target()
1445 {
1446 case $1 in
1447 'self')
1448 ;;
1449 'rpm')
1450 ;;
1451 'gdb')
1452 ;;
1453 'clean_op')
1454 ;;
1455 'autoreconf')
1456 ;;
1457 'install-system')
1458 ;;
1459 'configure')
1460 ;;
1461 'distcheck')
1462 ;;
1463 'check')
1464 ;;
1465 'snapshot')
1466 ;;
1467 'mingw')
1468 ;;
1469 'universe')
1470 ;;
1471 'valgrind')
1472 ;;
1473 'jenkins')
1474 ;;
1475 'distclean')
1476 ;;
1477 'maintainer-clean')
1478 ;;
1479 'install')
1480 ;;
1481 'all')
1482 ;;
1483 'make_default')
1484 ;;
1485 'clang')
1486 ;;
1487 'clang-analyzer')
1488 ;;
1489 test-*)
1490 ;;
1491 valgrind-*)
1492 ;;
1493 gdb-*)
1494 ;;
1495 'dist')
1496 ;;
1497 *)
1498 echo "Matched default"
1499 return 1
1500 ;;
1501 esac
1502
1503 return 0
1504 }
1505
1506 function bootstrap ()
1507 {
1508 determine_target_platform
1509
1510 determine_vcs
1511
1512 # Set up whatever we need to do to use autoreconf later
1513 require_libtoolise
1514 if ! autoreconf_setup; then
1515 return 1
1516 fi
1517
1518 if [ -z "$MAKE_TARGET" ]; then
1519 MAKE_TARGET="make_default"
1520 fi
1521
1522 if $PRINT_SETUP_OPTION -o $DEBUG; then
1523 echo
1524 print_setup
1525 echo
1526
1527 # Exit if all we were looking for were the currently used options
1528 if $PRINT_SETUP_OPTION; then
1529 exit
1530 fi
1531 fi
1532
1533 # Use OLD_TESTS_ENVIRONMENT for tracking the state of the variable
1534 local OLD_TESTS_ENVIRONMENT=
1535
1536 # Set ENV PREFIX in order to set --prefix for ./configure
1537 if [[ -n "$PREFIX" ]]; then
1538 PREFIX_ARG="--prefix=$PREFIX"
1539 fi
1540
1541 # We should always have a target by this point
1542 assert MAKE_TARGET
1543
1544 local MAKE_TARGET_ARRAY=($MAKE_TARGET)
1545
1546 for target in "${MAKE_TARGET_ARRAY[@]}"
1547 do
1548 # If we are running inside of Jenkins, we want to only run some of the possible tests
1549 if $jenkins_build_environment; then
1550 check_make_target $target
1551 ret=$?
1552 if [ $ret -ne 0 ]; then
1553 die "Unknown MAKE_TARGET option: $target"
1554 fi
1555 fi
1556
1557 local snapshot_run=false
1558 local valgrind_run=false
1559
1560 case $target in
1561 'self')
1562 self_test
1563 ;;
1564 'gdb')
1565 make_gdb
1566 ;;
1567 'install-html')
1568 make_install_html
1569 ;;
1570 'clean_op')
1571 make_clean_option
1572 ;;
1573 'autoreconf')
1574 make_for_autoreconf
1575 ;;
1576 'install-system')
1577 make_install_system
1578 ;;
1579 'configure')
1580 run_configure
1581 ;;
1582 'make_default')
1583 make_default
1584 ;;
1585 'clang')
1586 if ! check_clang; then
1587 die "clang was not found"
1588 fi
1589
1590 if ! make_for_clang; then
1591 die "Failed to build clang: $?"
1592 fi
1593 ;;
1594 'clang-analyzer')
1595 if ! check_clang_analyzer; then
1596 die "clang-analyzer was not found"
1597 fi
1598 if ! check_clang; then
1599 die "clang was not found"
1600 fi
1601
1602 if ! make_for_clang_analyzer; then
1603 die "Failed to build clang-analyzer: $?"
1604 fi
1605 ;;
1606 'mingw')
1607 if ! check_mingw; then
1608 die "mingw was not found"
1609 fi
1610
1611 if ! make_for_mingw; then
1612 die "Failed to build mingw: $?"
1613 fi
1614 ;;
1615 'snapshot')
1616 make_for_snapshot
1617 snapshot_run=true
1618 check_snapshot
1619 ;;
1620 'rpm')
1621 make_rpm
1622 ;;
1623 'darwin_malloc')
1624 make_darwin_malloc
1625 ;;
1626 'valgrind')
1627 make_maintainer_clean
1628 make_valgrind
1629 ;;
1630 'universe')
1631 make_universe
1632 ;;
1633 'jenkins')
1634 make_for_continuus_integration
1635 ;;
1636 *)
1637 run_configure_if_required
1638 make_target "$target"
1639 ;;
1640 esac
1641
1642 if $jenkins_build_environment; then
1643 if ! $snapshot_run; then
1644 run_make_maintainer_clean_if_possible
1645 fi
1646 fi
1647
1648 done
1649 }
1650
1651 function main ()
1652 {
1653 # Variables we export
1654 declare -x VCS_CHECKOUT=
1655
1656 # Variables we control globally
1657 local MAKE_TARGET=
1658 local CONFIGURE=
1659
1660 # Options for getopt
1661 local AUTORECONF_OPTION=false
1662 local CLEAN_OPTION=false
1663 local CONFIGURE_OPTION=false
1664 local DEBUG_OPTION=false
1665 local PRINT_SETUP_OPTION=false
1666 local TARGET_OPTION=false
1667 local TARGET_OPTION_ARG=
1668 local VERBOSE_OPTION=false
1669
1670 local OLD_CONFIGURE=
1671 local OLD_CONFIGURE_ARG=
1672 local OLD_PREFIX=
1673 local OLD_MAKE=
1674 local OLD_TESTS_ENVIRONMENT=
1675
1676 # If we call autoreconf on the platform or not
1677 local AUTORECONF_REBUILD_HOST=false
1678 local AUTORECONF_REBUILD=false
1679
1680 local -r top_srcdir=`pwd`
1681
1682 # Default configure
1683 if [ -z "$CONFIGURE" ]; then
1684 CONFIGURE="$top_srcdir/configure"
1685 fi
1686
1687
1688 # Variables for determine_target_platform () and rebuild_host_os ()
1689 # UNAME_MACHINE_ARCH= uname -m
1690 # VENDOR= apple, redhat, centos, canonical
1691 # VENDOR_RELEASE=
1692 # RHEL{rhel,Tikanga,Santiago}
1693 # Ubuntu{ubuntu,Lucid,Maverick,Natty,Oneiric,Precise,Quantal}
1694 # Fedora{fedora,Verne,Beefy}
1695 # OSX{osx,lion,snow,mountain}
1696 # VENDOR_DISTRIBUTION= darwin,fedora,rhel,ubuntu
1697 # UNAME_KERNEL= Linux, Darwin,...
1698 # UNAME_KERNEL_RELEASE= Linux, Darwin,...
1699 local UNAME_MACHINE_ARCH=unknown
1700 local VENDOR=unknown
1701 local VENDOR_RELEASE=unknown
1702 local VENDOR_DISTRIBUTION=unknown
1703 local UNAME_KERNEL=unknown
1704 local UNAME_KERNEL_RELEASE=unknown
1705 local HOST_OS=
1706
1707 rebuild_host_os no_output
1708
1709 parse_command_line_options $@
1710
1711 # If we are running under Jenkins we predetermine what tests we will run against
1712 # This MAKE_TARGET can be overridden by parse_command_line_options based MAKE_TARGET changes.
1713 # We don't want Jenkins overriding other variables, so we NULL them.
1714 if [ -z "$MAKE_TARGET" ]; then
1715 if $jenkins_build_environment; then
1716 if [[ -n "$label" ]]; then
1717 check_make_target $label
1718 if [ $? -eq 0 ]; then
1719 MAKE_TARGET="$label"
1720 fi
1721 fi
1722 if [[ -n "$LABEL" ]]; then
1723 check_make_target $LABEL
1724 if [ $? -eq 0 ]; then
1725 MAKE_TARGET="$LABEL"
1726 fi
1727 fi
1728
1729 if [ -z "$MAKE_TARGET" ]; then
1730 MAKE_TARGET='jenkins'
1731 fi
1732 fi
1733 fi
1734
1735 bootstrap
1736
1737 jobs -l
1738 wait
1739
1740 exit 0
1741 }
1742
1743 function set_branch ()
1744 {
1745 if [ -z "$BRANCH" ]; then
1746 if [ -z "$CI_PROJECT_TEAM" ]; then
1747 die "Variable CI_PROJECT_TEAM has not been set"
1748 fi
1749 if [ -z "$PROJECT" ]; then
1750 die "Variable PROJECT has not been set"
1751 fi
1752 if [ -z "$BUILD_TAG" ]; then
1753 die "Variable BUILD_TAG has not been set"
1754 fi
1755
1756 BRANCH="lp:~$CI_PROJECT_TEAM/$PROJECT/$BUILD_TAG"
1757 export BRANCH
1758 fi
1759
1760 if [ -z "$BRANCH" ]; then
1761 die "Missing values required to build BRANCH variable."
1762 fi
1763 }
1764
1765 function merge ()
1766 {
1767 if [ -z "$VCS_CHECKOUT" ]; then
1768 die "Merges require VCS_CHECKOUT."
1769 fi
1770
1771 set_branch
1772
1773 if [[ "$VCS_CHECKOUT" == 'bzr' ]]; then
1774 if test -n "$BRANCH_TO_MERGE"; then
1775 bzr merge $BRANCH_TO_MERGE
1776 bzr commit --message="Merge $BRANCH_TO_MERGE Build: $BUILD_TAG" --unchanged
1777 fi
1778
1779 bzr push "$BRANCH"
1780 elif [[ -n "$VCS_CHECKOUT" ]]; then
1781 die "Merge attempt occured, current VCS setup does not support this"
1782 fi
1783 }
1784
1785 function enable_debug ()
1786 {
1787 if ! $DEBUG; then
1788 local caller_loc=`caller`
1789 if [ -n $1 ]; then
1790 echo "$caller_loc Enabling debug: $1"
1791 else
1792 echo "$caller_loc Enabling debug"
1793 fi
1794 set -x
1795 DEBUG=true
1796 fi
1797 }
1798
1799 function usage ()
1800 {
1801 cat << EOF
1802 Usage: $program_name [OPTION]..
1803
1804 Bootstrap this package from the checked-out sources, and optionally walk through CI run.
1805
1806 Options:
1807
1808 EOF
1809 }
1810
1811 function disable_debug ()
1812 {
1813 set +x
1814 DEBUG=true
1815 }
1816
1817 # Script begins here
1818
1819 program_name=$0
1820
1821 env_debug_enabled=false
1822 if [[ -n "$JENKINS_HOME" ]]; then
1823 declare -r jenkins_build_environment=true
1824 else
1825 declare -r jenkins_build_environment=false
1826 fi
1827
1828 export ACLOCAL
1829 export AUTOCONF
1830 export AUTOHEADER
1831 export AUTOM4TE
1832 export AUTOMAKE
1833 export AUTORECONF
1834 export CONFIGURE_ARG
1835 export DEBUG
1836 export GNU_BUILD_FLAGS
1837 export LIBTOOLIZE
1838 export LIBTOOLIZE_OPTIONS
1839 export MAKE
1840 export PREFIX_ARG
1841 export TESTS_ENVIRONMENT
1842 export VERBOSE
1843 export WARNINGS
1844
1845 case $OSTYPE in
1846 darwin*)
1847 export MallocGuardEdges
1848 export MallocErrorAbort
1849 export MallocScribble
1850 ;;
1851 esac
1852
1853 # We check for DEBUG twice, once before we source the config file, and once afterward
1854 env_debug_enabled=false
1855 if [[ -n "$DEBUG" ]]; then
1856 env_debug_enabled=true
1857 enable_debug
1858 print_setup
1859 fi
1860
1861 # Variables which only can be set by .bootstrap
1862 BOOTSTRAP_SNAPSHOT=false
1863 BOOTSTRAP_SNAPSHOT_CHECK=
1864
1865 if [ -f '.bootstrap' ]; then
1866 source '.bootstrap'
1867 fi
1868
1869 if $env_debug_enabled; then
1870 enable_debug
1871 else
1872 if [[ -n "$DEBUG" ]]; then
1873 enable_debug "Enabling DEBUG from '.bootstrap'"
1874 print_setup
1875 fi
1876 fi
1877
1878 # We do this in order to protect the case where DEBUG
1879 if ! $env_debug_enabled; then
1880 DEBUG=false
1881 fi
1882
1883 main $@