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