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