0383c8fa7ec1f3746a6bb8755d68433ada7b0a26
[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 # LOG_COMPILER
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_LOG_COMPILER" ]]; then
490 die "OLD_LOG_COMPILER($OLD_LOG_COMPILER) 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 "$LOG_COMPILER" ]]; then
506 OLD_LOG_COMPILER=$LOG_COMPILER
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_LOG_COMPILER" ]]; then
529 LOG_COMPILER=$OLD_LOG_COMPILER
530 fi
531
532 OLD_CONFIGURE=
533 OLD_CONFIGURE_ARG=
534 OLD_PREFIX=
535 OLD_MAKE=
536 OLD_LOG_COMPILER=
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 LOG_COMPILER="./libtool --mode=execute $VALGRIND_COMMAND"
571 else
572 LOG_COMPILER="$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 LOG_COMPILER='wine'
707 fi
708 fi
709
710 if [[ -n "$LOG_COMPILER" ]]; 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 LOG_COMPILER="./libtool --mode=execute $GDB_COMMAND"
939 else
940 LOG_COMPILER="$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 "$LOG_COMPILER" ]; then
978 if $verbose; then
979 echo "LOG_COMPILER=$LOG_COMPILER"
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 run_configure_if_required
1014 make_target 'dist-rpm'
1015
1016 if $jenkins_build_environment; then
1017 mkdir artifacts
1018 mv *.tar.gz *.rpm artifacts
1019
1020 make_target 'maintainer-clean'
1021 mv artifacts/* .
1022 rmdir artifacts
1023 fi
1024
1025 fi
1026 fi
1027 }
1028
1029 make_maintainer_clean ()
1030 {
1031 run_configure_if_required
1032 make_target 'maintainer-clean' 'no_error'
1033
1034 # Lets make sure we really cleaned up the environment
1035 assert_no_file 'Makefile'
1036 assert_no_file 'configure'
1037 assert_no_directory 'autom4te.cache'
1038 }
1039
1040 make_distclean ()
1041 {
1042 run_configure_if_required
1043 make_target 'distclean' 'no_error'
1044
1045 # Lets make sure we really cleaned up the environment
1046 assert_no_file 'Makefile'
1047 assert_file 'configure'
1048 }
1049
1050 make_check ()
1051 {
1052 make_target 'check'
1053 }
1054
1055 make_jenkins_default ()
1056 {
1057 run_configure
1058 make_target 'all'
1059 }
1060
1061 make_default ()
1062 {
1063 run_configure_if_required
1064 make_target 'all'
1065 }
1066
1067 run_configure_if_required ()
1068 {
1069 run_autoreconf_if_required
1070
1071 if [ ! -f 'Makefile' ]; then
1072 run_configure
1073 fi
1074
1075 assert_file 'Makefile' 'configure did not produce a Makefile'
1076 }
1077
1078 run_make_maintainer_clean_if_possible ()
1079 {
1080 if [ -f 'Makefile' ]; then
1081 make_maintainer_clean
1082 fi
1083 }
1084
1085 run_autoreconf_if_required ()
1086 {
1087 if [ ! -x 'configure' ]; then
1088 run_autoreconf
1089 fi
1090
1091 assert_exec_file 'configure'
1092 bash -n configure
1093 }
1094
1095 run_autoreconf ()
1096 {
1097 if [[ -z "$AUTORECONF" ]]; then
1098 die "Programmer error, tried to call run_autoreconf () but AUTORECONF was not set"
1099 fi
1100
1101 if $use_libtool; then
1102 assert $BOOTSTRAP_LIBTOOLIZE
1103 if $jenkins_build_environment; then
1104 run "$BOOTSTRAP_LIBTOOLIZE" '--copy' '--install' || die "Cannot execute $BOOTSTRAP_LIBTOOLIZE"
1105 else
1106 run "$BOOTSTRAP_LIBTOOLIZE" '--copy' '--install' '--force' || die "Cannot execute $BOOTSTRAP_LIBTOOLIZE"
1107 fi
1108 fi
1109
1110 run "$AUTORECONF" "$AUTORECONF_ARGS" || die "Cannot execute $AUTORECONF"
1111
1112 eval 'bash -n configure' || die "autoreconf generated a malformed configure"
1113 }
1114
1115 run ()
1116 {
1117 if $verbose; then
1118 echo "\`$@' $ARGS"
1119 fi
1120
1121 if [ -z "$1" ]; then
1122 return 127;
1123 fi
1124
1125 eval "$@" "$ARGS"
1126 }
1127
1128 parse_command_line_options ()
1129 {
1130 local SHORTOPTS=':apcmt:dvh'
1131
1132 nassert OPT_TARGET
1133
1134 while getopts "$SHORTOPTS" opt; do
1135 case $opt in
1136 a) #--autoreconf
1137 AUTORECONF_OPTION=true
1138 OPT_TARGET+='autoreconf'
1139 ;;
1140 p) #--print-env
1141 print_setup_opt=true
1142 ;;
1143 c) # --configure
1144 CONFIGURE_OPTION=true
1145 OPT_TARGET+='configure'
1146 ;;
1147 m) # maintainer-clean
1148 CLEAN_OPTION=true
1149 ;;
1150 o) # target
1151 CONFIGURE_ARG="$OPTARG"
1152 ;;
1153 t) # target
1154 TARGET_OPTION=true
1155 TARGET_OPTION_ARG="$OPTARG"
1156 OPT_TARGET+="$OPTARG"
1157 ;;
1158 d) # debug
1159 opt_debug=true
1160 enable_debug
1161 ;;
1162 h) # help
1163 echo "bootstrap.sh [options] optional_target ..."
1164 echo " -a # Just run autoreconf";
1165 echo " -p # Print ENV";
1166 echo " -c # Just run configure";
1167 echo " -m # Just run maintainer-clean";
1168 echo " -o # Specify configure arguments";
1169 echo " -t # Make target";
1170 echo " -d # Enable debug";
1171 echo " -h # Show help";
1172 echo " -v # Be more verbose in output";
1173 exit
1174 ;;
1175 v) # verbose
1176 opt_verbose=true
1177 verbose=true
1178 ;;
1179 :)
1180 echo "Option -$OPTARG requires an argument." >&2
1181 exit 1
1182 ;;
1183 *)
1184 echo "$0: error - unrecognized option $1" 1>&2
1185 exit 1
1186 ;;
1187 esac
1188 done
1189
1190 shift $((OPTIND-1))
1191
1192 if [ -n "$1" ]; then
1193 OPT_TARGET="$@"
1194 fi
1195 }
1196
1197 determine_vcs ()
1198 {
1199 if [[ -d '.git' ]]; then
1200 VCS_CHECKOUT=git
1201 elif [[ -d '.bzr' ]]; then
1202 VCS_CHECKOUT=bzr
1203 elif [[ -d '.svn' ]]; then
1204 VCS_CHECKOUT=svn
1205 elif [[ -d '.hg' ]]; then
1206 VCS_CHECKOUT=hg
1207 else
1208 VCS_CHECKOUT=
1209 fi
1210
1211 if [[ -n "$VCS_CHECKOUT" ]]; then
1212 verbose=true
1213 fi
1214 }
1215
1216 require_libtoolise ()
1217 {
1218 grep '^[ ]*A[CM]_PROG_LIBTOOL' configure.ac >/dev/null \
1219 && use_libtool=true
1220 grep '^[ ]*LT_INIT' configure.ac >/dev/null \
1221 && use_libtool=true
1222 }
1223
1224 autoreconf_setup ()
1225 {
1226 # Set ENV MAKE in order to override "make"
1227 if [[ -z "$MAKE" ]]; then
1228 if command_exists 'gmake'; then
1229 MAKE="$(type -p gmake)"
1230 else
1231 if command_exists 'make'; then
1232 MAKE="$(type -p make)"
1233 fi
1234 fi
1235
1236 if [ "$VCS_CHECKOUT" ]; then
1237 if $debug; then
1238 MAKE="$MAKE --warn-undefined-variables"
1239 fi
1240 fi
1241
1242 if $debug; then
1243 MAKE="$MAKE -d"
1244 fi
1245 fi
1246
1247 if [[ -z "$GNU_BUILD_FLAGS" ]]; then
1248 if $jenkins_build_environment; then
1249 GNU_BUILD_FLAGS="--install"
1250 else
1251 GNU_BUILD_FLAGS="--install --force"
1252 fi
1253 fi
1254
1255 if $verbose; then
1256 GNU_BUILD_FLAGS="$GNU_BUILD_FLAGS --verbose"
1257 fi
1258
1259 if [ -z "$ACLOCAL_PATH" ]; then
1260 ACLOCAL_PATH="/usr/local/share/aclocal $ACLOCAL_PATH"
1261 fi
1262
1263 if [[ -z "$WARNINGS" ]]; then
1264 if [[ -n "$VCS_CHECKOUT" ]]; then
1265 WARNINGS="all,error"
1266 else
1267 WARNINGS="all"
1268 fi
1269 fi
1270
1271 if $use_libtool; then
1272 if [[ -n "$LIBTOOLIZE" ]]; then
1273 BOOTSTRAP_LIBTOOLIZE="$(type -p $LIBTOOLIZE)"
1274
1275 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1276 echo "Couldn't find user supplied libtoolize, it is required"
1277 return 1
1278 fi
1279 else
1280 # If we are using OSX, we first check to see glibtoolize is available
1281 if [[ "$VENDOR_DISTRIBUTION" == "darwin" ]]; then
1282 BOOTSTRAP_LIBTOOLIZE="$(type -p glibtoolize)"
1283
1284 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1285 echo "Couldn't find glibtoolize, it is required on OSX"
1286 return 1
1287 fi
1288 else
1289 BOOTSTRAP_LIBTOOLIZE="$(type -p libtoolize)"
1290
1291 if [[ -z "$BOOTSTRAP_LIBTOOLIZE" ]]; then
1292 echo "Couldn't find libtoolize, it is required"
1293 return 1
1294 fi
1295 fi
1296 fi
1297
1298 if $verbose; then
1299 LIBTOOLIZE_OPTIONS="--verbose $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
1300 fi
1301
1302 if $debug; then
1303 LIBTOOLIZE_OPTIONS="--debug $BOOTSTRAP_LIBTOOLIZE_OPTIONS"
1304 fi
1305
1306 # Here we set LIBTOOLIZE to true since we are going to invoke it via BOOTSTRAP_LIBTOOLIZE
1307 LIBTOOLIZE=true
1308 fi
1309
1310 # Test the ENV AUTOMAKE if it exists
1311 if [[ -n "$AUTOMAKE" ]]; then
1312 run "$AUTOMAKE" '--help' &> /dev/null || die "Failed to run AUTOMAKE:$AUTOMAKE"
1313 fi
1314
1315 # Test the ENV AUTOCONF if it exists
1316 if [[ -n "$AUTOCONF" ]]; then
1317 run "$AUTOCONF" '--help' &> /dev/null || die "Failed to run AUTOCONF:$AUTOCONF"
1318 fi
1319
1320 # Test the ENV AUTOHEADER if it exists
1321 if [[ -n "$AUTOHEADER" ]]; then
1322 run "$AUTOHEADER" '--help' &> /dev/null || die "Failed to run AUTOHEADER:$AUTOHEADER"
1323 fi
1324
1325 # Test the ENV AUTOM4TE if it exists
1326 if [[ -n "$AUTOM4TE" ]]; then
1327 run "$AUTOM4TE" '--help' &> /dev/null || die "Failed to run AUTOM4TE:$AUTOM4TE"
1328 fi
1329
1330 # Test the ENV AUTOHEADER if it exists, if not we add one and add --install
1331 if [[ -z "$ACLOCAL" ]]; then
1332 ACLOCAL="aclocal --install"
1333 fi
1334 run "$ACLOCAL" '--help' &> /dev/null || die "Failed to run ACLOCAL:$ACLOCAL"
1335
1336 if [[ -z "$AUTORECONF" ]]; then
1337 AUTORECONF="$(type -p autoreconf)"
1338
1339 if [[ -z "$AUTORECONF" ]]; then
1340 die "Couldn't find autoreconf"
1341 fi
1342
1343 if [[ -n "$GNU_BUILD_FLAGS" ]]; then
1344 AUTORECONF_ARGS="$GNU_BUILD_FLAGS"
1345 fi
1346 fi
1347
1348 run "$AUTORECONF" '--help' &> /dev/null || die "Failed to run AUTORECONF:$AUTORECONF"
1349 }
1350
1351 print_setup ()
1352 {
1353 local saved_debug_status=$debug
1354 if $debug; then
1355 disable_debug
1356 fi
1357
1358 echo '----------------------------------------------'
1359 echo 'BOOTSTRAP ENV'
1360 echo "AUTORECONF=$AUTORECONF"
1361 echo "HOST_OS=$HOST_OS"
1362 echo "VENDOR=$VENDOR"
1363 echo "VENDOR_DISTRIBUTION=$VENDOR_DISTRIBUTION"
1364 echo "VENDOR_RELEASE=$VENDOR_RELEASE"
1365
1366 echo "getopt()"
1367 if $AUTORECONF_OPTION; then
1368 echo "--autoreconf"
1369 fi
1370
1371 if $CLEAN_OPTION; then
1372 echo "--clean"
1373 fi
1374
1375 if $CONFIGURE_OPTION; then
1376 echo "--configure"
1377 fi
1378
1379 if $opt_debug; then
1380 echo "--debug"
1381 fi
1382
1383 if $print_setup_opt; then
1384 echo "--print-env"
1385 fi
1386
1387 if $TARGET_OPTION; then
1388 echo "--target=$TARGET_OPTION_ARG"
1389 fi
1390
1391 if $opt_verbose; then
1392 echo "--verbose"
1393 fi
1394
1395 if [[ -n "$MAKE" ]]; then
1396 echo "MAKE=$MAKE"
1397 fi
1398
1399 if [[ -n "$BOOTSTRAP_TARGET" ]]; then
1400 echo "BOOTSTRAP_TARGET=$BOOTSTRAP_TARGET"
1401 fi
1402
1403 if [[ -n "$PREFIX" ]]; then
1404 echo "PREFIX=$PREFIX"
1405 fi
1406
1407 if [[ -n "$LOG_COMPILER" ]]; then
1408 echo "LOG_COMPILER=$LOG_COMPILER"
1409 fi
1410
1411 if [[ -n "$VCS_CHECKOUT" ]]; then
1412 echo "VCS_CHECKOUT=$VCS_CHECKOUT"
1413 fi
1414
1415 if $debug; then
1416 echo "debug=true"
1417 fi
1418
1419 if [[ -n "$WARNINGS" ]]; then
1420 echo "WARNINGS=$WARNINGS"
1421 fi
1422
1423 if $saved_debug_status; then
1424 echo "DEBUG=true"
1425 fi
1426
1427 echo '----------------------------------------------'
1428
1429 if $saved_debug_status; then
1430 enable_debug
1431 fi
1432 }
1433
1434 make_clean_option ()
1435 {
1436 run_configure_if_required
1437
1438 make_maintainer_clean
1439
1440 if [[ "$VCS_CHECKOUT" == 'git' ]]; then
1441 run "$VCS_CHECKOUT" status --ignored
1442 elif [[ -n "$VCS_CHECKOUT" ]]; then
1443 run "$VCS_CHECKOUT" status
1444 fi
1445 }
1446
1447 make_for_autoreconf ()
1448 {
1449 if [ -f 'Makefile' ]; then
1450 make_maintainer_clean
1451 fi
1452
1453 run_autoreconf
1454
1455 assert_no_file 'Makefile'
1456 }
1457
1458 check_make_target()
1459 {
1460 local ret=0
1461 case $1 in
1462 'self')
1463 ;;
1464 'rpm')
1465 ;;
1466 'gdb')
1467 ;;
1468 'clean_op')
1469 ;;
1470 'autoreconf')
1471 ;;
1472 'install-system')
1473 ;;
1474 'configure')
1475 ;;
1476 'distcheck')
1477 ;;
1478 'check')
1479 ;;
1480 'snapshot')
1481 ;;
1482 'mingw')
1483 ;;
1484 'universe')
1485 ;;
1486 'valgrind')
1487 ;;
1488 'jenkins')
1489 ;;
1490 'distclean')
1491 ;;
1492 'maintainer-clean')
1493 ;;
1494 'install')
1495 ;;
1496 'all')
1497 ;;
1498 'make_default')
1499 ;;
1500 'clang')
1501 ;;
1502 'clang-analyzer')
1503 ;;
1504 test-*)
1505 ;;
1506 valgrind-*)
1507 ;;
1508 gdb-*)
1509 ;;
1510 'dist')
1511 ;;
1512 *)
1513 echo "Matched default"
1514 ret=1
1515 ;;
1516 esac
1517
1518 return $ret
1519 }
1520
1521 execute_job ()
1522 {
1523 # We should always have a target by this point
1524 assert BOOTSTRAP_TARGET
1525
1526 determine_target_platform
1527
1528 determine_vcs
1529
1530 # Set up whatever we need to do to use autoreconf later
1531 use_libtool=false
1532 require_libtoolise
1533 if ! autoreconf_setup; then
1534 return 1
1535 fi
1536
1537 if $print_setup_opt -o $debug; then
1538 echo
1539 print_setup
1540 echo
1541
1542 # Exit if all we were looking for were the currently used options
1543 if $print_setup_opt; then
1544 exit
1545 fi
1546 fi
1547
1548 # Use OLD_LOG_COMPILER for tracking the state of the variable
1549 local OLD_LOG_COMPILER=
1550
1551 # Set ENV PREFIX in order to set --prefix for ./configure
1552 if [[ -n "$PREFIX" ]]; then
1553 PREFIX_ARG="--prefix=$PREFIX"
1554 fi
1555
1556 if $CLEAN_OPTION; then
1557 make_maintainer_clean
1558 fi
1559
1560 local BOOTSTRAP_TARGET_ARRAY
1561 BOOTSTRAP_TARGET_ARRAY=( $BOOTSTRAP_TARGET )
1562
1563 for target in "${BOOTSTRAP_TARGET_ARRAY[@]}"
1564 do
1565 # If we are running inside of Jenkins, we want to only run some of the possible tests
1566 if $jenkins_build_environment; then
1567 check_make_target $target
1568 ret=$?
1569 if [ $ret -ne 0 ]; then
1570 warn "Unknown BOOTSTRAP_TARGET option: $target"
1571 target="jenkins"
1572 fi
1573 fi
1574
1575 if $jenkins_build_environment; then
1576 use_banner $target
1577 fi
1578
1579 local snapshot_run=false
1580 local valgrind_run=false
1581
1582 case $target in
1583 'self')
1584 self_test
1585 ;;
1586 'gdb')
1587 make_gdb
1588 ;;
1589 'install-html')
1590 make_install_html
1591 ;;
1592 'clean_op')
1593 make_clean_option
1594 ;;
1595 'autoreconf')
1596 make_for_autoreconf
1597 ;;
1598 'install-system')
1599 make_install_system
1600 ;;
1601 'configure')
1602 run_configure
1603 ;;
1604 'make_default')
1605 make_default
1606 ;;
1607 'clang')
1608 make_distclean
1609 if ! check_clang; then
1610 die "clang was not found"
1611 fi
1612
1613 if ! make_for_clang; then
1614 die "Failed to build clang: $?"
1615 fi
1616 ;;
1617 'clang-analyzer')
1618 make_distclean
1619 if ! check_clang_analyzer; then
1620 die "clang-analyzer was not found"
1621 fi
1622 if ! check_clang; then
1623 die "clang was not found"
1624 fi
1625
1626 if ! make_for_clang_analyzer; then
1627 die "Failed to build clang-analyzer: $?"
1628 fi
1629 ;;
1630 'mingw')
1631 make_distclean
1632 if ! make_for_mingw; then
1633 die "Failed to build mingw: $?"
1634 fi
1635 ;;
1636 'snapshot')
1637 make_for_snapshot
1638 snapshot_run=true
1639 check_snapshot
1640 ;;
1641 'rpm')
1642 make_rpm
1643 ;;
1644 'darwin_malloc')
1645 make_darwin_malloc
1646 ;;
1647 'valgrind')
1648 make_valgrind
1649 ;;
1650 'universe')
1651 make_universe
1652 ;;
1653 'jenkins')
1654 make_for_continuus_integration
1655 ;;
1656 *)
1657 run_configure_if_required
1658 make_target "$target"
1659 ;;
1660 esac
1661 done
1662 }
1663
1664 main ()
1665 {
1666 # Are we running inside of Jenkins?
1667 if [[ -n "$JENKINS_HOME" ]]; then
1668 declare -r jenkins_build_environment=true
1669 else
1670 declare -r jenkins_build_environment=false
1671 fi
1672
1673 # Variables we export
1674 declare -x VCS_CHECKOUT=
1675
1676 # Variables we control globally
1677 local -a BOOTSTRAP_TARGET=
1678 local CONFIGURE=
1679 local use_libtool=false
1680 local verbose=false
1681
1682 #getop variables
1683 local opt_debug=false
1684 local opt_verbose=false
1685
1686 if [[ -n "$VERBOSE" ]]; then
1687 verbose=true
1688 fi
1689
1690 # Options for getopt
1691 local AUTORECONF_OPTION=false
1692 local CLEAN_OPTION=false
1693 local CONFIGURE_OPTION=false
1694 local print_setup_opt=false
1695 local TARGET_OPTION=false
1696 local TARGET_OPTION_ARG=
1697
1698 local OLD_CONFIGURE=
1699 local OLD_CONFIGURE_ARG=
1700 local OLD_PREFIX=
1701 local OLD_MAKE=
1702 local OLD_LOG_COMPILER=
1703
1704 # If we call autoreconf on the platform or not
1705 local AUTORECONF_REBUILD_HOST=false
1706 local AUTORECONF_REBUILD=false
1707
1708 local -r top_srcdir="$(pwd)"
1709
1710 # Default configure
1711 if [ -z "$CONFIGURE" ]; then
1712 CONFIGURE="$top_srcdir/configure"
1713 fi
1714
1715
1716 # Variables for determine_target_platform () and rebuild_host_os ()
1717 # UNAME_MACHINE_ARCH= uname -m
1718 # VENDOR= apple, redhat, centos, canonical
1719 # VENDOR_RELEASE=
1720 # RHEL{rhel,Tikanga,Santiago}
1721 # Ubuntu{ubuntu,Lucid,Maverick,Natty,Oneiric,Precise,Quantal}
1722 # Fedora{fedora,Verne,Beefy}
1723 # OSX{osx,lion,snow,mountain}
1724 # VENDOR_DISTRIBUTION= darwin,fedora,rhel,ubuntu
1725 # UNAME_KERNEL= Linux, Darwin,...
1726 # UNAME_KERNEL_RELEASE= Linux, Darwin,...
1727 local UNAME_MACHINE_ARCH=unknown
1728 local VENDOR=unknown
1729 local VENDOR_RELEASE=unknown
1730 local VENDOR_DISTRIBUTION=unknown
1731 local UNAME_KERNEL=unknown
1732 local UNAME_KERNEL_RELEASE=unknown
1733 local HOST_OS=
1734
1735 rebuild_host_os no_output
1736
1737 local OPT_TARGET=
1738 parse_command_line_options "$@"
1739
1740 nassert BOOTSTRAP_TARGET
1741
1742 if [ -n "$OPT_TARGET" ]; then
1743 BOOTSTRAP_TARGET="$OPT_TARGET"
1744 fi
1745
1746 if [ -z "$BOOTSTRAP_TARGET" ]; then
1747 BOOTSTRAP_TARGET="make_default"
1748 fi
1749
1750 # We should always have a target by this point
1751 assert BOOTSTRAP_TARGET
1752
1753 execute_job
1754 local ret=$?
1755
1756 jobs -l
1757 wait
1758
1759 exit $ret
1760 }
1761
1762 set_branch ()
1763 {
1764 if [ -z "$BRANCH" ]; then
1765 if [ -z "$CI_PROJECT_TEAM" ]; then
1766 die "Variable CI_PROJECT_TEAM has not been set"
1767 fi
1768 if [ -z "$PROJECT" ]; then
1769 die "Variable PROJECT has not been set"
1770 fi
1771 if [ -z "$BUILD_TAG" ]; then
1772 die "Variable BUILD_TAG has not been set"
1773 fi
1774
1775 BRANCH="lp:~$CI_PROJECT_TEAM/$PROJECT/$BUILD_TAG"
1776 export BRANCH
1777 fi
1778
1779 if [ -z "$BRANCH" ]; then
1780 die "Missing values required to build BRANCH variable."
1781 fi
1782 }
1783
1784 merge ()
1785 {
1786 if [ -z "$VCS_CHECKOUT" ]; then
1787 die "Merges require VCS_CHECKOUT."
1788 fi
1789
1790 set_branch
1791
1792 if [[ "$VCS_CHECKOUT" == 'bzr' ]]; then
1793 if test -n "$BRANCH_TO_MERGE"; then
1794 bzr merge $BRANCH_TO_MERGE
1795 bzr commit --message="Merge $BRANCH_TO_MERGE Build: $BUILD_TAG" --unchanged
1796 fi
1797
1798 bzr push "$BRANCH"
1799 elif [[ -n "$VCS_CHECKOUT" ]]; then
1800 die "Merge attempt occured, current VCS setup does not support this"
1801 fi
1802 }
1803
1804 enable_debug ()
1805 {
1806 if ! $debug; then
1807 local caller_loc="$(caller)"
1808 if [[ -n "$1" ]]; then
1809 echo "$caller_loc Enabling debug: $1"
1810 else
1811 echo "$caller_loc Enabling debug"
1812 fi
1813 set -x
1814 debug=true
1815 fi
1816 }
1817
1818 usage ()
1819 {
1820 cat << EOF
1821 Usage: $program_name [OPTION]..
1822
1823 Bootstrap this package from the checked-out sources, and optionally walk through CI run.
1824
1825 Options:
1826
1827 EOF
1828 }
1829
1830 disable_debug ()
1831 {
1832 set +x
1833 debug=false
1834 }
1835
1836 check_shell ()
1837 {
1838 if [ -x '/usr/local/bin/shellcheck' ]; then
1839 /usr/local/bin/shellcheck "$1"
1840 local ret=$?
1841
1842 if [ "$ret" -ne 0 ]; then
1843 die "$1 failed shellcheck"
1844 fi
1845 fi
1846 }
1847
1848 bootstrap ()
1849 {
1850 check_shell 'bootstrap.sh'
1851 local env_debug_enabled=false
1852 local debug=false
1853
1854 export ACLOCAL
1855 export AUTOCONF
1856 export AUTOHEADER
1857 export AUTOM4TE
1858 export AUTOMAKE
1859 export AUTORECONF
1860 export CONFIGURE_ARG
1861 export DEBUG
1862 export GNU_BUILD_FLAGS
1863 export LIBTOOLIZE
1864 export LIBTOOLIZE_OPTIONS
1865 export MAKE
1866 export PREFIX_ARG
1867 export LOG_COMPILER
1868 export VERBOSE
1869 export WARNINGS
1870
1871 case $OSTYPE in
1872 darwin*)
1873 export MallocGuardEdges
1874 export MallocErrorAbort
1875 export MallocScribble
1876 ;;
1877 esac
1878
1879 # We check for DEBUG twice, once before we source the config file, and once afterward
1880 if [[ -n "$DEBUG" ]]; then
1881 env_debug_enabled=true
1882 fi
1883
1884 # Variables which only can be set by .bootstrap
1885 BOOTSTRAP_SNAPSHOT=false
1886 BOOTSTRAP_SNAPSHOT_CHECK=
1887
1888 if [ -f '.bootstrap' ]; then
1889 source '.bootstrap'
1890 fi
1891
1892 # 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
1893 if $env_debug_enabled; then
1894 enable_debug
1895 elif [[ -n "$DEBUG" ]]; then
1896 enable_debug "Enabling DEBUG from '.bootstrap'"
1897 fi
1898
1899 if $env_debug_enabled; then
1900 print_setup
1901 fi
1902
1903
1904 main "$@"
1905 }
1906
1907 # Script begins here
1908 declare -r program_name="$0"
1909 bootstrap "$@"