Move logic to specific distcheck
[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+= " CXXFLAGS=-DNDEBUG "
392 BUILD_CONFIGURE_ARG+= " CFLAGS=-DNDEBUG "
393 else
394 if $DEBUG; then
395 BUILD_CONFIGURE_ARG+=' --enable-debug --enable-assert'
396 elif [[ -n "$VCS_CHECKOUT" ]]; then
397 BUILD_CONFIGURE_ARG+=' --enable-assert'
398 fi
399 fi
400
401 if [[ -n "$CONFIGURE_ARG" ]]; then
402 BUILD_CONFIGURE_ARG+=" $CONFIGURE_ARG"
403 fi
404
405 if [[ -n "$PREFIX_ARG" ]]; then
406 BUILD_CONFIGURE_ARG+=" $PREFIX_ARG"
407 fi
408
409 ret=1;
410 # If we are executing on OSX use CLANG, otherwise only use it if we find it in the ENV
411 case $HOST_OS in
412 *-darwin-*)
413 CC=clang CXX=clang++ $top_srcdir/configure $BUILD_CONFIGURE_ARG || die "Cannot execute CC=clang CXX=clang++ configure $BUILD_CONFIGURE_ARG"
414 ret=$?
415 ;;
416 rhel-5*)
417 command_exists 'gcc44' || die "Could not locate gcc44"
418 CC=gcc44 CXX=gcc44 $top_srcdir/configure $BUILD_CONFIGURE_ARG || die "Cannot execute CC=gcc44 CXX=gcc44 configure $BUILD_CONFIGURE_ARG"
419 ret=$?
420 ;;
421 *)
422 $CONFIGURE $BUILD_CONFIGURE_ARG
423 ret=$?
424 ;;
425 esac
426
427 if [ $ret -ne 0 ]; then
428 die "Could not execute $CONFIGURE $BUILD_CONFIGURE_ARG"
429 fi
430
431 if [ ! -f 'Makefile' ]; then
432 die "Programmer error, configure was run but no Makefile existed after $CONFIGURE was run"
433 fi
434 }
435
436 function setup_gdb_command () {
437 GDB_TMPFILE=$(mktemp /tmp/gdb.XXXXXXXXXX)
438 echo 'set logging overwrite on' > $GDB_TMPFILE
439 echo 'set logging on' >> $GDB_TMPFILE
440 echo 'set environment LIBTEST_IN_GDB=1' >> $GDB_TMPFILE
441 echo 'run' >> $GDB_TMPFILE
442 echo 'thread apply all bt' >> $GDB_TMPFILE
443 echo 'quit' >> $GDB_TMPFILE
444 GDB_COMMAND="gdb -f -batch -x $GDB_TMPFILE"
445 }
446
447 function setup_valgrind_command () {
448 VALGRIND_PROGRAM=`type -p valgrind`
449 if [[ -n "$VALGRIND_PROGRAM" ]]; then
450 VALGRIND_COMMAND="$VALGRIND_PROGRAM --error-exitcode=1 --leak-check=yes --malloc-fill=A5 --free-fill=DE --xml=yes --xml-file=\"valgrind-%p.xml\""
451 fi
452 }
453
454 function save_BUILD ()
455 {
456 if [[ -n "$OLD_CONFIGURE" ]]; then
457 die "OLD_CONFIGURE($OLD_CONFIGURE) was set on push, programmer error!"
458 fi
459
460 if [[ -n "$OLD_CONFIGURE_ARG" ]]; then
461 die "OLD_CONFIGURE_ARG($OLD_CONFIGURE_ARG) was set on push, programmer error!"
462 fi
463
464 if [[ -n "$OLD_PREFIX" ]]; then
465 die "OLD_PREFIX($OLD_PREFIX) was set on push, programmer error!"
466 fi
467
468 if [[ -n "$OLD_MAKE" ]]; then
469 die "OLD_MAKE($OLD_MAKE) was set on push, programmer error!"
470 fi
471
472 if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
473 die "OLD_TESTS_ENVIRONMENT($OLD_TESTS_ENVIRONMENT) was set on push, programmer error!"
474 fi
475
476 if [[ -n "$CONFIGURE" ]]; then
477 OLD_CONFIGURE=$CONFIGURE
478 fi
479
480 if [[ -n "$CONFIGURE_ARG" ]]; then
481 OLD_CONFIGURE_ARG=$CONFIGURE_ARG
482 fi
483
484 if [[ -n "$MAKE" ]]; then
485 OLD_MAKE=$MAKE
486 fi
487
488 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
489 OLD_TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT
490 fi
491 }
492
493 function restore_BUILD ()
494 {
495 if [[ -n "$OLD_CONFIGURE" ]]; then
496 CONFIGURE=$OLD_CONFIGURE
497 fi
498
499 if [[ -n "$OLD_CONFIGURE_ARG" ]]; then
500 CONFIGURE_ARG=$OLD_CONFIGURE_ARG
501 fi
502
503 if [[ -n "$OLD_PREFIX" ]]; then
504 PREFIX_ARG=$OLD_PREFIX
505 fi
506
507 if [[ -n "$OLD_MAKE" ]]; then
508 MAKE=$OLD_MAKE
509 fi
510
511 if [[ -n "$OLD_TESTS_ENVIRONMENT" ]]; then
512 TESTS_ENVIRONMENT=$OLD_TESTS_ENVIRONMENT
513 fi
514
515 OLD_CONFIGURE=
516 OLD_CONFIGURE_ARG=
517 OLD_PREFIX=
518 OLD_MAKE=
519 OLD_TESTS_ENVIRONMENT=
520
521 export -n CC CXX
522 }
523
524 function safe_pushd ()
525 {
526 pushd $1 &> /dev/null ;
527
528 if [ -n "$BUILD_DIR" ]; then
529 if $VERBOSE; then
530 echo "BUILD_DIR=$BUILD_DIR"
531 fi
532 fi
533 }
534
535 function safe_popd ()
536 {
537 local directory_to_delete=`pwd`
538 popd &> /dev/null ;
539 if [ $? -eq 0 ]; then
540 if [[ "$top_srcdir" == "$directory_to_delete" ]]; then
541 die "We almost deleted top_srcdir($top_srcdir), programmer error"
542 fi
543
544 rm -r -f "$directory_to_delete"
545 fi
546 }
547
548 function make_valgrind ()
549 {
550 # If the env VALGRIND_COMMAND is set then we assume it is valid
551 local valgrind_was_set=false
552 if [[ -z "$VALGRIND_COMMAND" ]]; then
553 setup_valgrind_command
554 if [[ -n "$VALGRIND_COMMAND" ]]; then
555 valgrind_was_set=true
556 fi
557 else
558 valgrind_was_set=true
559 fi
560
561 # If valgrind_was_set is set to no we bail
562 if ! $valgrind_was_set; then
563 echo 'valgrind was not present'
564 return 1
565 fi
566
567 save_BUILD
568
569 valgrind_run=true
570
571 # If we are required to run configure, do so now
572 run_configure
573
574 # If we don't have a configure, then most likely we will be missing libtool
575 assert_file 'configure'
576 if [[ -f 'libtool' ]]; then
577 TESTS_ENVIRONMENT="./libtool --mode=execute $VALGRIND_COMMAND"
578 else
579 TESTS_ENVIRONMENT="$VALGRIND_COMMAND"
580 fi
581
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 'dist'
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 make_for_continuus_integration ()
860 {
861 # Setup the environment if we are local
862 check_for_jenkins
863
864 # No matter then evironment, we should not have a Makefile at this point
865 assert_no_file 'Makefile'
866
867 # Platforms which require bootstrap should have some setup done before we hit this stage.
868 # If we are building locally, skip this step, unless we are just testing locally.
869 if $BOOTSTRAP_SNAPSHOT; then
870 if $BOOTSTRAP_SNAPSHOT; then
871 assert_file 'configure'
872 fi
873
874 if [ -n "$BOOTSTRAP_SNAPSHOT_CHECK" ]; then
875 assert_file "$BOOTSTRAP_SNAPSHOT_CHECK" 'snapshot check failed'
876 fi
877 else
878 # If we didn't require a snapshot, then we should not have a configure
879 assert_no_file 'configure'
880
881 run_autoreconf
882 fi
883
884 assert_no_file 'Makefile' 'Programmer error, Makefile existed where build state should have been clean'
885
886 case $HOST_OS in
887 *-fedora-*)
888 run_configure
889
890 assert_exec_file 'configure'
891 assert_file 'Makefile'
892
893 make_target 'all'
894
895 # make rpm includes "make distcheck"
896 if [[ -f rpm.am ]]; then
897 make_rpm
898 elif [[ -d rpm ]]; then
899 make_rpm
900 fi
901 ;;
902 *)
903 make_jenkins_default
904 ;;
905 esac
906
907 make_maintainer_clean
908
909 safe_popd
910 }
911
912 # The point to this test is to test bootstrap.sh itself
913 function self_test ()
914 {
915 # We start off with a clean env
916 make_maintainer_clean
917
918 eval "./bootstrap.sh jenkins" || die "failed 'jenkins'"
919 eval "./bootstrap.sh all" || die "failed 'all'"
920 eval "./bootstrap.sh gdb" || die "failed 'gdb'"
921 eval "./bootstrap.sh maintainer-clean" || die "failed 'maintainer-clean'"
922 }
923
924 function make_install_html ()
925 {
926 run_configure_if_required
927 assert_file 'configure'
928
929 make_target 'install-html'
930 }
931
932 function make_gdb ()
933 {
934 save_BUILD
935
936 if command_exists 'gdb'; then
937 run_configure_if_required
938
939 # Set ENV GDB_COMMAND
940 if [[ -z "$GDB_COMMAND" ]]; then
941 setup_gdb_command
942 fi
943
944 # If we don't have a configure, then most likely we will be missing libtool
945 assert_file 'configure'
946 if [[ -f 'libtool' ]]; then
947 TESTS_ENVIRONMENT="./libtool --mode=execute $GDB_COMMAND"
948 else
949 TESTS_ENVIRONMENT="$GDB_COMMAND"
950 fi
951
952 make_target 'check'
953
954 if [ -f 'gdb.txt' ]; then
955 rm 'gdb.txt'
956 fi
957
958 if [ -f '.gdb_history' ]; then
959 rm '.gdb_history'
960 fi
961
962 if $jenkins_build_environment; then
963 make_target 'clean'
964 fi
965 else
966 echo 'gdb was not present'
967 return 1
968 fi
969
970 restore_BUILD
971 }
972
973 # $1 target to compile
974 # $2 to die, or not to die, based on contents
975 function make_target ()
976 {
977 if [ -z "$1" ]; then
978 die "Programmer error, no target provided for make"
979 fi
980
981 if [ ! -f 'Makefile' ]; then
982 die "Programmer error, make was called before configure"
983 run_configure
984 fi
985
986 if [ -n "$TESTS_ENVIRONMENT" ]; then
987 if $VERBOSE; then
988 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
989 fi
990 fi
991
992 if [ -z "$MAKE" ]; then
993 die "MAKE was not set"
994 fi
995
996 # $2 represents error or warn
997 run $MAKE $1
998 ret=$?
999
1000 if [ $ret -ne 0 ]; then
1001 if [ -n "$2" ]; then
1002 warn "Failed to execute $MAKE $1: $ret"
1003 else
1004 die "Failed to execute $MAKE $1: $ret"
1005 fi
1006 fi
1007
1008 return $ret
1009 }
1010
1011 function make_distcheck ()
1012 {
1013 make_target 'distcheck'
1014 }
1015
1016 function make_rpm ()
1017 {
1018 if command_exists 'rpmbuild'; then
1019 if [ -f 'rpm.am' -o -d 'rpm' ]; then
1020 run_configure_if_required
1021 make_target 'rpm'
1022
1023 if $jenkins_build_environment; then
1024 make_target 'clean'
1025 fi
1026
1027 fi
1028 fi
1029 }
1030
1031 function make_maintainer_clean ()
1032 {
1033 run_configure_if_required
1034 make_target 'maintainer-clean' 'no_error'
1035
1036 # Lets make sure we really cleaned up the environment
1037 assert_no_file 'Makefile'
1038 assert_no_file 'configure'
1039 assert_no_directory 'autom4te.cache'
1040 }
1041
1042 function make_check ()
1043 {
1044 make_target 'check'
1045 }
1046
1047 function make_jenkins_default ()
1048 {
1049 run_configure
1050 make_target 'all'
1051 }
1052
1053 function make_default ()
1054 {
1055 run_configure_if_required
1056 make_target 'all'
1057 }
1058
1059 function run_configure_if_required ()
1060 {
1061 run_autoreconf_if_required
1062
1063 if [ ! -f 'Makefile' ]; then
1064 run_configure
1065 fi
1066
1067 assert_file 'Makefile' 'configure did not produce a Makefile'
1068 }
1069
1070 function run_make_maintainer_clean_if_possible ()
1071 {
1072 if [ -f 'Makefile' ]; then
1073 make_maintainer_clean
1074 fi
1075 }
1076
1077 function run_autoreconf_if_required ()
1078 {
1079 if [ ! -x 'configure' ]; then
1080 run_autoreconf
1081 fi
1082
1083 assert_exec_file 'configure'
1084 bash -n configure
1085 }
1086
1087 function run_autoreconf ()
1088 {
1089 if [[ -z "$AUTORECONF" ]]; then
1090 die "Programmer error, tried to call run_autoreconf () but AUTORECONF was not set"
1091 fi
1092
1093 if test $use_libtool = 1; then
1094 assert $BOOTSTRAP_LIBTOOLIZE
1095 run $BOOTSTRAP_LIBTOOLIZE '--copy' '--install' '--force' || die "Cannot execute $BOOTSTRAP_LIBTOOLIZE"
1096 fi
1097
1098 run $AUTORECONF $AUTORECONF_ARGS || die "Cannot execute $AUTORECONF"
1099
1100 eval 'bash -n configure' || die "autoreconf generated a malformed configure"
1101 }
1102
1103 function run ()
1104 {
1105 if $VERBOSE; then
1106 echo "\`$@' $ARGS"
1107 fi
1108
1109 if [ -z "$1" ]; then
1110 return 127;
1111 fi
1112
1113 eval $@ $ARGS
1114 }
1115
1116 function parse_command_line_options ()
1117 {
1118 local SHORTOPTS=':apcmt:dvh'
1119
1120 nassert MAKE_TARGET
1121
1122 while getopts "$SHORTOPTS" opt; do
1123 case $opt in
1124 a) #--autoreconf
1125 AUTORECONF_OPTION=true
1126 MAKE_TARGET='autoreconf'
1127 ;;
1128 p) #--print-env
1129 PRINT_SETUP_OPTION=true
1130 ;;
1131 c) # --configure
1132 CONFIGURE_OPTION=true
1133 MAKE_TARGET='configure'
1134 ;;
1135 m) # maintainer-clean
1136 CLEAN_OPTION=true
1137 MAKE_TARGET='clean_op'
1138 ;;
1139 t) # target
1140 TARGET_OPTION=true
1141 TARGET_OPTION_ARG="$OPTARG"
1142 MAKE_TARGET="$OPTARG"
1143 ;;
1144 d) # debug
1145 DEBUG_OPTION=true
1146 enable_debug
1147 ;;
1148 h) # help
1149 echo "bootstrap.sh [options] optional_target ..."
1150 echo " -a # Just run autoreconf";
1151 echo " -p # Print ENV";
1152 echo " -c # Just run configure";
1153 echo " -m # Just run maintainer-clean";
1154 echo " -t # Make target";
1155 echo " -d # Enable debug";
1156 echo " -h # Show help";
1157 echo " -v # Be more verbose in output";
1158 exit
1159 ;;
1160 v) # verbose
1161 VERBOSE_OPTION=true
1162 VERBOSE=true
1163 ;;
1164 :)
1165 echo "Option -$OPTARG requires an argument." >&2
1166 exit 1
1167 ;;
1168 *)
1169 echo "$0: error - unrecognized option $1" 1>&2
1170 exit 1
1171 ;;
1172 esac
1173 done
1174
1175 shift $((OPTIND-1))
1176
1177 if [ -n "$1" ]; then
1178 MAKE_TARGET="$@"
1179 fi
1180 }
1181
1182 function determine_vcs ()
1183 {
1184 if [[ -d '.git' ]]; then
1185 VCS_CHECKOUT=git
1186 elif [[ -d '.bzr' ]]; then
1187 VCS_CHECKOUT=bzr
1188 elif [[ -d '.svn' ]]; then
1189 VCS_CHECKOUT=svn
1190 elif [[ -d '.hg' ]]; then
1191 VCS_CHECKOUT=hg
1192 else
1193 VCS_CHECKOUT=
1194 fi
1195
1196 if [[ -n "$VCS_CHECKOUT" ]]; then
1197 VERBOSE=true
1198 fi
1199 }
1200
1201 function require_libtoolise ()
1202 {
1203 use_libtool=0
1204 grep '^[ ]*A[CM]_PROG_LIBTOOL' configure.ac >/dev/null \
1205 && use_libtool=1
1206 grep '^[ ]*LT_INIT' configure.ac >/dev/null \
1207 && use_libtool=1
1208 }
1209
1210 function autoreconf_setup ()
1211 {
1212 # Set ENV MAKE in order to override "make"
1213 if [[ -z "$MAKE" ]]; then
1214 if command_exists 'gmake'; then
1215 MAKE=`type -p gmake`
1216 else
1217 if command_exists 'make'; then
1218 MAKE=`type -p make`
1219 fi
1220 fi
1221
1222 if [ "$VCS_CHECKOUT" ]; then
1223 if $DEBUG; then
1224 MAKE="$MAKE --warn-undefined-variables"
1225 fi
1226 fi
1227
1228 if $DEBUG; then
1229 MAKE="$MAKE -d"
1230 fi
1231 fi
1232
1233 if [[ -z "$GNU_BUILD_FLAGS" ]]; then
1234 GNU_BUILD_FLAGS="--install --force"
1235 fi
1236
1237 if $VERBOSE; then
1238 GNU_BUILD_FLAGS="$GNU_BUILD_FLAGS --verbose"
1239 fi
1240
1241 if [ -z "$ACLOCAL_PATH" ]; then
1242 ACLOCAL_PATH="/usr/local/share/aclocal $ACLOCAL_PATH"
1243 fi
1244
1245 if [[ -z "$WARNINGS" ]]; then
1246 if [[ -n "$VCS_CHECKOUT" ]]; then
1247 WARNINGS="all,error"
1248 else
1249 WARNINGS="all"
1250 fi
1251 fi
1252
1253 if test $use_libtool = 1; then
1254 if [[ -n "$LIBTOOLIZE" ]]; then
1255 BOOTSTRAP_LIBTOOLIZE=`type -p $LIBTOOLIZE`
1256
1257 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1258 echo "Couldn't find user supplied libtoolize, it is required"
1259 return 1
1260 fi
1261 else
1262 # If we are using OSX, we first check to see glibtoolize is available
1263 if [[ "$VENDOR_DISTRIBUTION" == "darwin" ]]; then
1264 BOOTSTRAP_LIBTOOLIZE=`type -p glibtoolize`
1265
1266 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1267 echo "Couldn't find glibtoolize, it is required on OSX"
1268 return 1
1269 fi
1270 else
1271 BOOTSTRAP_LIBTOOLIZE=`type -p libtoolize`
1272
1273 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1274 echo "Couldn't find libtoolize, it is required"
1275 return 1
1276 fi
1277 fi
1278 fi
1279
1280 if $VERBOSE; then
1281 LIBTOOLIZE_OPTIONS="--verbose $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
1282 fi
1283
1284 if $DEBUG; then
1285 LIBTOOLIZE_OPTIONS="--debug $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
1286 fi
1287
1288 # Here we set LIBTOOLIZE to true since we are going to invoke it via BOOTSTRAP_LIBTOOLIZE
1289 LIBTOOLIZE=true
1290 fi
1291
1292 # Test the ENV AUTOMAKE if it exists
1293 if [[ -n "$AUTOMAKE" ]]; then
1294 run $AUTOMAKE '--help' &> /dev/null || die "Failed to run AUTOMAKE:$AUTOMAKE"
1295 fi
1296
1297 # Test the ENV AUTOCONF if it exists
1298 if [[ -n "$AUTOCONF" ]]; then
1299 run $AUTOCONF '--help' &> /dev/null || die "Failed to run AUTOCONF:$AUTOCONF"
1300 fi
1301
1302 # Test the ENV AUTOHEADER if it exists
1303 if [[ -n "$AUTOHEADER" ]]; then
1304 run $AUTOHEADER '--help' &> /dev/null || die "Failed to run AUTOHEADER:$AUTOHEADER"
1305 fi
1306
1307 # Test the ENV AUTOM4TE if it exists
1308 if [[ -n "$AUTOM4TE" ]]; then
1309 run $AUTOM4TE '--help' &> /dev/null || die "Failed to run AUTOM4TE:$AUTOM4TE"
1310 fi
1311
1312 # Test the ENV AUTOHEADER if it exists, if not we add one and add --install
1313 if [[ -z "$ACLOCAL" ]]; then
1314 ACLOCAL="aclocal --install"
1315 fi
1316 run $ACLOCAL '--help' &> /dev/null || die "Failed to run ACLOCAL:$ACLOCAL"
1317
1318 if [[ -z "$AUTORECONF" ]]; then
1319 AUTORECONF=`type -p autoreconf`
1320
1321 if [[ -z "$AUTORECONF" ]]; then
1322 die "Couldn't find autoreconf"
1323 fi
1324
1325 if [[ -n "$GNU_BUILD_FLAGS" ]]; then
1326 AUTORECONF_ARGS="$GNU_BUILD_FLAGS"
1327 fi
1328 fi
1329
1330 run $AUTORECONF '--help' &> /dev/null || die "Failed to run AUTORECONF:$AUTORECONF"
1331 }
1332
1333 function print_setup ()
1334 {
1335 saved_debug_status=$DEBUG
1336 if $DEBUG; then
1337 disable_debug
1338 fi
1339
1340 echo '----------------------------------------------'
1341 echo 'BOOTSTRAP ENV'
1342 echo "AUTORECONF=$AUTORECONF"
1343 echo "HOST_OS=$HOST_OS"
1344 echo "VENDOR=$VENDOR"
1345 echo "VENDOR_DISTRIBUTION=$VENDOR_DISTRIBUTION"
1346 echo "VENDOR_RELEASE=$VENDOR_RELEASE"
1347
1348 echo "getopt()"
1349 if $AUTORECONF_OPTION; then
1350 echo "--autoreconf"
1351 fi
1352
1353 if $CLEAN_OPTION; then
1354 echo "--clean"
1355 fi
1356
1357 if $CONFIGURE_OPTION; then
1358 echo "--configure"
1359 fi
1360
1361 if $DEBUG_OPTION; then
1362 echo "--debug"
1363 fi
1364
1365 if $PRINT_SETUP_OPTION; then
1366 echo "--print-env"
1367 fi
1368
1369 if $TARGET_OPTION; then
1370 echo "--target=$TARGET_OPTION_ARG"
1371 fi
1372
1373 if $VERBOSE_OPTION; then
1374 echo "--verbose"
1375 fi
1376
1377 if [[ -n "$MAKE" ]]; then
1378 echo "MAKE=$MAKE"
1379 fi
1380
1381 if [[ -n "$MAKE_TARGET" ]]; then
1382 echo "MAKE_TARGET=$MAKE_TARGET"
1383 fi
1384
1385 if [[ -n "$PREFIX" ]]; then
1386 echo "PREFIX=$PREFIX"
1387 fi
1388
1389 if [[ -n "$TESTS_ENVIRONMENT" ]]; then
1390 echo "TESTS_ENVIRONMENT=$TESTS_ENVIRONMENT"
1391 fi
1392
1393 if [[ -n "$VCS_CHECKOUT" ]]; then
1394 echo "VCS_CHECKOUT=$VCS_CHECKOUT"
1395 fi
1396
1397 if $VERBOSE; then
1398 echo "VERBOSE=true"
1399 fi
1400
1401 if $DEBUG; then
1402 echo "DEBUG=true"
1403 fi
1404
1405 if [[ -n "$WARNINGS" ]]; then
1406 echo "WARNINGS=$WARNINGS"
1407 fi
1408 echo '----------------------------------------------'
1409
1410 if $saved_debug_status; then
1411 enable_debug
1412 fi
1413 }
1414
1415 function make_clean_option ()
1416 {
1417 run_configure_if_required
1418
1419 make_maintainer_clean
1420
1421 if [[ "$VCS_CHECKOUT" == 'git' ]]; then
1422 run "$VCS_CHECKOUT" status --ignored
1423 elif [[ -n "$VCS_CHECKOUT" ]]; then
1424 run "$VCS_CHECKOUT" status
1425 fi
1426 }
1427
1428 function make_for_autoreconf ()
1429 {
1430 if [ -f 'Makefile' ]; then
1431 make_maintainer_clean
1432 fi
1433
1434 run_autoreconf
1435
1436 assert_no_file 'Makefile'
1437 }
1438
1439 function check_make_target()
1440 {
1441 case $1 in
1442 'self')
1443 ;;
1444 'rpm')
1445 ;;
1446 'gdb')
1447 ;;
1448 'clean_op')
1449 ;;
1450 'autoreconf')
1451 ;;
1452 'install-system')
1453 ;;
1454 'configure')
1455 ;;
1456 'distcheck')
1457 ;;
1458 'check')
1459 ;;
1460 'snapshot')
1461 ;;
1462 'mingw')
1463 ;;
1464 'universe')
1465 ;;
1466 'valgrind')
1467 ;;
1468 'jenkins')
1469 ;;
1470 'distclean')
1471 ;;
1472 'maintainer-clean')
1473 ;;
1474 'install')
1475 ;;
1476 'all')
1477 ;;
1478 'make_default')
1479 ;;
1480 'clang')
1481 ;;
1482 'clang-analyzer')
1483 ;;
1484 test-*)
1485 ;;
1486 valgrind-*)
1487 ;;
1488 gdb-*)
1489 ;;
1490 'dist')
1491 ;;
1492 *)
1493 echo "Matched default"
1494 return 1
1495 ;;
1496 esac
1497
1498 return 0
1499 }
1500
1501 function bootstrap ()
1502 {
1503 determine_target_platform
1504
1505 determine_vcs
1506
1507 # Set up whatever we need to do to use autoreconf later
1508 require_libtoolise
1509 if ! autoreconf_setup; then
1510 return 1
1511 fi
1512
1513 if [ -z "$MAKE_TARGET" ]; then
1514 MAKE_TARGET="make_default"
1515 fi
1516
1517 if $PRINT_SETUP_OPTION -o $DEBUG; then
1518 echo
1519 print_setup
1520 echo
1521
1522 # Exit if all we were looking for were the currently used options
1523 if $PRINT_SETUP_OPTION; then
1524 exit
1525 fi
1526 fi
1527
1528 # Use OLD_TESTS_ENVIRONMENT for tracking the state of the variable
1529 local OLD_TESTS_ENVIRONMENT=
1530
1531 # Set ENV PREFIX in order to set --prefix for ./configure
1532 if [[ -n "$PREFIX" ]]; then
1533 PREFIX_ARG="--prefix=$PREFIX"
1534 fi
1535
1536 # We should always have a target by this point
1537 assert MAKE_TARGET
1538
1539 local MAKE_TARGET_ARRAY=($MAKE_TARGET)
1540
1541 for target in "${MAKE_TARGET_ARRAY[@]}"
1542 do
1543 # If we are running inside of Jenkins, we want to only run some of the possible tests
1544 if $jenkins_build_environment; then
1545 check_make_target $target
1546 ret=$?
1547 if [ $ret -ne 0 ]; then
1548 die "Unknown MAKE_TARGET option: $target"
1549 fi
1550 fi
1551
1552 local snapshot_run=false
1553 local valgrind_run=false
1554
1555 case $target in
1556 'self')
1557 self_test
1558 ;;
1559 'gdb')
1560 make_gdb
1561 ;;
1562 'install-html')
1563 make_install_html
1564 ;;
1565 'clean_op')
1566 make_clean_option
1567 ;;
1568 'autoreconf')
1569 make_for_autoreconf
1570 ;;
1571 'install-system')
1572 make_install_system
1573 ;;
1574 'configure')
1575 run_configure
1576 ;;
1577 'make_default')
1578 make_default
1579 ;;
1580 'clang')
1581 if ! check_clang; then
1582 die "clang was not found"
1583 fi
1584
1585 if ! make_for_clang; then
1586 die "Failed to build clang: $?"
1587 fi
1588 ;;
1589 'clang-analyzer')
1590 if ! check_clang_analyzer; then
1591 die "clang-analyzer was not found"
1592 fi
1593 if ! check_clang; then
1594 die "clang was not found"
1595 fi
1596
1597 if ! make_for_clang_analyzer; then
1598 die "Failed to build clang-analyzer: $?"
1599 fi
1600 ;;
1601 'mingw')
1602 if ! check_mingw; then
1603 die "mingw was not found"
1604 fi
1605
1606 if ! make_for_mingw; then
1607 die "Failed to build mingw: $?"
1608 fi
1609 ;;
1610 'snapshot')
1611 make_for_snapshot
1612 snapshot_run=true
1613 ;;
1614 'rpm')
1615 make_rpm
1616 ;;
1617 'darwin_malloc')
1618 make_darwin_malloc
1619 ;;
1620 'valgrind')
1621 make_maintainer_clean
1622 make_valgrind
1623 ;;
1624 'universe')
1625 make_universe
1626 ;;
1627 'jenkins')
1628 make_for_continuus_integration
1629 ;;
1630 *)
1631 run_configure_if_required
1632 make_target "$target"
1633 ;;
1634 esac
1635
1636 if $jenkins_build_environment; then
1637 if ! $snapshot_run; then
1638 run_make_maintainer_clean_if_possible
1639 fi
1640 fi
1641
1642 done
1643 }
1644
1645 function main ()
1646 {
1647 # Variables we export
1648 declare -x VCS_CHECKOUT=
1649
1650 # Variables we control globally
1651 local MAKE_TARGET=
1652 local CONFIGURE=
1653
1654 # Options for getopt
1655 local AUTORECONF_OPTION=false
1656 local CLEAN_OPTION=false
1657 local CONFIGURE_OPTION=false
1658 local DEBUG_OPTION=false
1659 local PRINT_SETUP_OPTION=false
1660 local TARGET_OPTION=false
1661 local TARGET_OPTION_ARG=
1662 local VERBOSE_OPTION=false
1663
1664 local OLD_CONFIGURE=
1665 local OLD_CONFIGURE_ARG=
1666 local OLD_PREFIX=
1667 local OLD_MAKE=
1668 local OLD_TESTS_ENVIRONMENT=
1669
1670 # If we call autoreconf on the platform or not
1671 local AUTORECONF_REBUILD_HOST=false
1672 local AUTORECONF_REBUILD=false
1673
1674 local -r top_srcdir=`pwd`
1675
1676 # Default configure
1677 if [ -z "$CONFIGURE" ]; then
1678 CONFIGURE="$top_srcdir/configure"
1679 fi
1680
1681
1682 # Variables for determine_target_platform () and rebuild_host_os ()
1683 # UNAME_MACHINE_ARCH= uname -m
1684 # VENDOR= apple, redhat, centos, canonical
1685 # VENDOR_RELEASE=
1686 # RHEL{rhel,Tikanga,Santiago}
1687 # Ubuntu{ubuntu,Lucid,Maverick,Natty,Oneiric,Precise,Quantal}
1688 # Fedora{fedora,Verne,Beefy}
1689 # OSX{osx,lion,snow,mountain}
1690 # VENDOR_DISTRIBUTION= darwin,fedora,rhel,ubuntu
1691 # UNAME_KERNEL= Linux, Darwin,...
1692 # UNAME_KERNEL_RELEASE= Linux, Darwin,...
1693 local UNAME_MACHINE_ARCH=unknown
1694 local VENDOR=unknown
1695 local VENDOR_RELEASE=unknown
1696 local VENDOR_DISTRIBUTION=unknown
1697 local UNAME_KERNEL=unknown
1698 local UNAME_KERNEL_RELEASE=unknown
1699 local HOST_OS=
1700
1701 rebuild_host_os no_output
1702
1703 parse_command_line_options $@
1704
1705 # If we are running under Jenkins we predetermine what tests we will run against
1706 # This MAKE_TARGET can be overridden by parse_command_line_options based MAKE_TARGET changes.
1707 # We don't want Jenkins overriding other variables, so we NULL them.
1708 if [ -z "$MAKE_TARGET" ]; then
1709 if $jenkins_build_environment; then
1710 if [[ -n "$label" ]]; then
1711 check_make_target $label
1712 if [ $? -eq 0 ]; then
1713 MAKE_TARGET="$label"
1714 fi
1715 fi
1716
1717 if [ -z "$MAKE_TARGET" ]; then
1718 MAKE_TARGET='jenkins'
1719 fi
1720 fi
1721 fi
1722
1723 bootstrap
1724
1725 jobs -l
1726 wait
1727
1728 exit 0
1729 }
1730
1731 function set_branch ()
1732 {
1733 if [ -z "$BRANCH" ]; then
1734 if [ -z "$CI_PROJECT_TEAM" ]; then
1735 die "Variable CI_PROJECT_TEAM has not been set"
1736 fi
1737 if [ -z "$PROJECT" ]; then
1738 die "Variable PROJECT has not been set"
1739 fi
1740 if [ -z "$BUILD_TAG" ]; then
1741 die "Variable BUILD_TAG has not been set"
1742 fi
1743
1744 BRANCH="lp:~$CI_PROJECT_TEAM/$PROJECT/$BUILD_TAG"
1745 export BRANCH
1746 fi
1747
1748 if [ -z "$BRANCH" ]; then
1749 die "Missing values required to build BRANCH variable."
1750 fi
1751 }
1752
1753 function merge ()
1754 {
1755 if [ -z "$VCS_CHECKOUT" ]; then
1756 die "Merges require VCS_CHECKOUT."
1757 fi
1758
1759 set_branch
1760
1761 if [[ "$VCS_CHECKOUT" == 'bzr' ]]; then
1762 if test -n "$BRANCH_TO_MERGE"; then
1763 bzr merge $BRANCH_TO_MERGE
1764 bzr commit --message="Merge $BRANCH_TO_MERGE Build: $BUILD_TAG" --unchanged
1765 fi
1766
1767 bzr push "$BRANCH"
1768 elif [[ -n "$VCS_CHECKOUT" ]]; then
1769 die "Merge attempt occured, current VCS setup does not support this"
1770 fi
1771 }
1772
1773 function enable_debug ()
1774 {
1775 if ! $DEBUG; then
1776 local caller_loc=`caller`
1777 if [ -n $1 ]; then
1778 echo "$caller_loc Enabling debug: $1"
1779 else
1780 echo "$caller_loc Enabling debug"
1781 fi
1782 set -x
1783 DEBUG=true
1784 fi
1785 }
1786
1787 function usage ()
1788 {
1789 cat << EOF
1790 Usage: $program_name [OPTION]..
1791
1792 Bootstrap this package from the checked-out sources, and optionally walk through CI run.
1793
1794 Options:
1795
1796 EOF
1797 }
1798
1799 function disable_debug ()
1800 {
1801 set +x
1802 DEBUG=true
1803 }
1804
1805 # Script begins here
1806
1807 program_name=$0
1808
1809 env_debug_enabled=false
1810 if [[ -n "$JENKINS_HOME" ]]; then
1811 declare -r jenkins_build_environment=true
1812 else
1813 declare -r jenkins_build_environment=false
1814 fi
1815
1816 export ACLOCAL
1817 export AUTOCONF
1818 export AUTOHEADER
1819 export AUTOM4TE
1820 export AUTOMAKE
1821 export AUTORECONF
1822 export CONFIGURE_ARG
1823 export DEBUG
1824 export GNU_BUILD_FLAGS
1825 export LIBTOOLIZE
1826 export LIBTOOLIZE_OPTIONS
1827 export MAKE
1828 export PREFIX_ARG
1829 export TESTS_ENVIRONMENT
1830 export VERBOSE
1831 export WARNINGS
1832
1833 case $OSTYPE in
1834 darwin*)
1835 export MallocGuardEdges
1836 export MallocErrorAbort
1837 export MallocScribble
1838 ;;
1839 esac
1840
1841 # We check for DEBUG twice, once before we source the config file, and once afterward
1842 env_debug_enabled=false
1843 if [[ -n "$DEBUG" ]]; then
1844 env_debug_enabled=true
1845 enable_debug
1846 print_setup
1847 fi
1848
1849 # Variables which only can be set by .bootstrap
1850 BOOTSTRAP_SNAPSHOT=false
1851 BOOTSTRAP_SNAPSHOT_CHECK=
1852
1853 if [ -f '.bootstrap' ]; then
1854 source '.bootstrap'
1855 fi
1856
1857 if $env_debug_enabled; then
1858 enable_debug
1859 else
1860 if [[ -n "$DEBUG" ]]; then
1861 enable_debug "Enabling DEBUG from '.bootstrap'"
1862 print_setup
1863 fi
1864 fi
1865
1866 # We do this in order to protect the case where DEBUG
1867 if ! $env_debug_enabled; then
1868 DEBUG=false
1869 fi
1870
1871 main $@