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