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