summaryrefslogtreecommitdiff
path: root/bin/tinbuild_internals.sh
blob: d839573cc3cae46899e2ec3c27ba11730df8e963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
#!/usr/bin/env bash
# -*- mode : Shell ; tab-width : 4; indent-tabs-mode : nil -*-
#    Copyright (C) 2011-2012 Norbert Thiebaud, Robinson Tryon
#    License: GPLv3
#

lock_file=/tmp/tinbuild-lockfile
push_opts="-a"

# Do we have timeout? If yes, guard git pull with that - which has a
# tendency to hang forever, when connection is flaky
if which timeout > /dev/null 2>&1 ; then
	# std coreutils - timeout is two hours
	timeout="`which timeout` 2h"
fi

if [ -z "$FLOCK" ] ; then
    if [ -x ${BIN_DIR?}/flock ] ; then
        FLOCK="${BIN_DIR?}/flock"
    else
        FLOCK="$(which flock)"
    fi
fi

do_flock()
{
    if [ "$LOCK" = "1" ] ; then
        if [ -n "${FLOCK}" -a -x "$FLOCK" ] ; then
            [ $V ] && echo "locking... $@"
            ${FLOCK} $@
            [ $V ] && echo "locked. $@"
        else
            echo "no flock implementation, please build it from buildbot/flock or use -e" 1>&2
            exit 1;
        fi
    else
	    true
    fi
}

epoch_from_utc()
{
    local utc="$@"

    date -u '+%s' -d "$utc UTC"
}

epoch_to_utc()
{
    date -u -d @${1}
}

print_date()
{
    date -u '+%Y-%m-%d %H:%M:%S'
}

log_msgs()
{
    echo "[$(print_date) $TINDER_BRANCH]" "$@"
}

die()
{
    echo "[$(print_date) $TINDER_BRANCH] Error:" "$@"
    exit -1;
}

source_build_env()
{
    if test -f ./config.mk ; then
        . ./config.mk
    fi

    if test -f ./Env.Host.sh ; then
        . ./Env.Host.sh
    fi
}

generate_cgit_link()
{
    line="$1"
    repo=$(echo $line | cut -f 1 -d \:)
    sha=$(echo $line | cut -f 2 -d \:)

    echo "<a href='http://cgit.freedesktop.org/libreoffice/${repo}/log/?id=$sha'>$repo</a>"
}

prepare_upload_manifest()
{
    local manifest_file="build_info.txt"

    echo "Build Info" > $manifest_file

    echo "tinderbox: administrator: ${OWNER?}" >> $manifest_file
    echo "tinderbox: buildname: ${TINDER_NAME?}" >> $manifest_file
    echo "tinderbox: tree: ${TINDER_BRANCH?}" >> $manifest_file
    echo "tinderbox: pull time $(cat "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log")" >> $manifest_file
    echo "tinderbox: git sha1s"  >> $manifest_file
    cat "${METADATA_DIR?}/tb_${B}_current-git-heads.log"  >> $manifest_file
    echo ""  >> $manifest_file
    echo "tinderbox: autogen log"  >> $manifest_file
    cat tb_${B}_autogen.log  >> $manifest_file

}

get_commits_since_last_good()
{
    local mode=$1
    local head=
    local repo=
    local sha=

    if [ -f .gitmodules ] ; then
        head=$(head -n1 "${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt")
        repo=$(echo ${head} | cut -d : -f 1)
        sha=$(echo ${head} | cut -d : -f 2)
        if [ "${mode?}" = "people" ] ; then
            git log '--pretty=tformat:%ce' ${sha?}..HEAD
        else
            echo "==== ${repo} ===="
            git log '--pretty=tformat:%h  %s' ${sha?}..HEAD | sed 's/^/  /'
        fi
    else
        if [ -f "${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt" ] ; then
            for head in $(cat "${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt") ; do
                repo=$(echo ${head} | cut -d : -f 1)
                sha=$(echo ${head} | cut -d : -f 2)
                (
                    if [ "${repo?}" != "bootstrap" -a "${repo}" != "core" ] ; then
                        cd clone/${repo?}
                    fi
                    if [ "${mode?}" = "people" ] ; then
                        git log '--pretty=tformat:%ce' ${sha?}..HEAD
                    else
                        echo "==== ${repo} ===="
                        git log '--pretty=tformat:%h  %s' ${sha?}..HEAD | sed 's/^/  /'
                    fi
                )
            done
        fi
    fi
}

get_commits_since_last_bad()
{
    local mode=$1
    local head=
    local repo=
    local sha=

    if [ -f .gitmodules ] ; then
        head=$(head -n1 "${METADATA_DIR?}/tb_${B}_last-failure-git-heads.txt")
        repo=$(echo ${head} | cut -d : -f 1)
        sha=$(echo ${head} | cut -d : -f 2)
        if [ "${mode?}" = "people" ] ; then
            git log '--pretty=tformat:%ce' ${sha?}..HEAD
        else
            echo "==== ${repo} ===="
            git log '--pretty=tformat:%h  %s' ${sha?}..HEAD | sed 's/^/  /'
        fi
    else
        if [ -f tb_${B}_last-failure-git-heads.txt ] ; then
            for head in $(cat "${METADATA_DIR?}/tb_${B}_last-failure-git-heads.txt") ; do
                repo=$(echo ${head} | cut -d : -f 1)
                sha=$(echo ${head} | cut -d : -f 2)
                (
                    if [ "${repo?}" != "bootstrap" -a "${repo}" != "core" ] ; then
                        cd clone/${repo?}
                    fi
                    if [ "${mode?}" = "people" ] ; then
                        git log '--pretty=tformat:%ce' ${sha?}..HEAD
                    else
                        echo "==== ${repo} ===="
                        git log '--pretty=tformat:%h  %s' ${sha?}..HEAD | sed 's/^/  /'
                    fi
                )
            done
        fi
    fi
}

send_mail_msg()
{
    local to="$1"
    local subject="$2"
    local headers="$3"
    local bcc="$4"
    local log="$5"
    local quiet="-q"
    local smtp_auth=""

    if [ -n "${SMTPUSER}" ] ; then
        smtp_auth="-xu ${SMTPUSER?} -xp ${SMTPPW?}"
    fi

    log_msgs "send mail to ${to?} with subject \"${subject?}\""
    [ $V ] && quiet=""
    if [ -n "${log}" ] ; then
        ${BIN_DIR?}/sendEmail $quiet -f "$OWNER" -s "${SMTPHOST?}" $smtp_auth -t "${to?}" -bcc "${bcc?}" -u "${subject?}" -o "message-header=${headers?}" -a "${log?}"
    elif [ -n "${headers?}" ] ; then
        ${BIN_DIR?}/sendEmail $quiet -f "$OWNER" -s "${SMTPHOST?}" $smtp_auth -t "${to?}" -bcc "${bcc?}" -u "${subject?}" -o "message-header=${headers?}"
    else
        ${BIN_DIR?}/sendEmail $quiet -f "$OWNER" -s "${SMTPHOST?}" $smtp_auth -t "${to?}" -bcc "${bcc?}" -u "${subject?}"
    fi
}

report_to_tinderbox()
{
    [ $V ] && echo "report_to_tinderbox status=$2"
    if [ -z "$SEND_MAIL" -o -z "$TINDER_NAME" ] ; then
        return 0
    fi

    local start_date="$1"
    local status="$2"
    local log="$3"
    local start_line=
    local xtinder="X-Tinder: cookie"
    local subject="tinderbox build start notification"
    local gzlog=
    local message_content=

    start_line="tinderbox: starttime: $(epoch_from_utc ${start_date})"
    message_content="
tinderbox: administrator: ${OWNER?}
tinderbox: buildname: ${TINDER_NAME?}
tinderbox: tree: ${TINDER_BRANCH?}
$start_line
tinderbox: timenow: `date -u '+%s'`
tinderbox: errorparser: unix
tinderbox: status: ${status?}
tinderbox: END
"

    if [ "$log" = "yes" ] ; then
        gzlog="tinder.log.gz"
        (
            echo "$message_content"
            cat "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log"
            for cm in $(cat ${METADATA_DIR?}/tb_${B}_current-git-heads.log) ; do echo "TinderboxPrint: $(generate_cgit_link ${cm})" ; done
            cat tb_${B}_autogen.log tb_${B}_clean.log tb_${B}_build.log tb_${B}_tests.log 2>/dev/null
        ) | gzip -c > "${gzlog}"
        xtinder="X-Tinder: gzookie"
        subject="tinderbox gzipped logfile"
    fi

    if [ "$SEND_MAIL" = "debug" ] ; then
        echo "$message_content" | send_mail_msg "${OWNER}" "${subject?}" "${xtinder?}" '' "${gzlog}"
    elif [ "$SEND_MAIL" = "author" ] ; then
        echo "$message_content" | send_mail_msg "${OWNER}" "${subject?}" "${xtinder?}" '' "${gzlog}"
        if [ -n "${BRANCH_AUTHOR}" ] ; then
            echo "$message_content" | send_mail_msg "${BRANCH_AUTHOR}" "${subject?}" "${xtinder?}" '' "${gzlog}"
        fi
    else
        echo "$message_content" | send_mail_msg "tinderbox@gimli.documentfoundation.org" "${subject?}" "${xtinder?}" '' "${gzlog}"
    fi
}


report_error ()
{
    local_to_mail=
    local tinder1=
    local tinder2=
    local error_kind="$1"
    shift
    local rough_time="$1"
    shift
    local error_log="$1"
    shift

    local last_success=$(cat "${METADATA_DIR?}/tb_${B}_last-success-git-timestamp.txt")
    to_mail=
    if [ "$SEND_MAIL" = "owner" -o "$SEND_MAIL" = "debug" -o "$SEND_MAIL" = "author" ] ; then
        to_mail="${OWNER?}"
    else
        if [ "$SEND_MAIL" = "all" ] ; then
            case "$error_kind" in
                owner) to_mail="${OWNER?}"
                    message="box broken" ;;
                *)
                    if [ -z "$last_success" ] ; then
                        # we need at least one successful build to
                        # be reliable
                        to_mail="${OWNER?}"
                    else
                        to_mail="$(get_committers)"
                    fi
                    message="last success: ${last_success?}" ;;
            esac
        fi
    fi
    if [ -n "$to_mail" ] ; then
        echo "$*" 1>&2
        echo "Last success: ${last_success}" 1>&2
        tinder1="`echo \"Full log available at http://tinderbox.libreoffice.org/$TINDER_BRANCH/status.html\"`"
        tinder2="`echo \"Box name: ${TINDER_NAME?}\"`"

        cat <<EOF | send_mail_msg "$to_mail" "Tinderbox failure, $TINDER_NAME, $TINDER_BRANCH, $message" "" "${OWNER?}" ""
Hi folks,

One of you broke the build of LibreOffice with your commit :-(
Please commit and push a fix ASAP!

${tinder1}

 Tinderbox info:

 ${tinder2}
 Branch: $TINDER_BRANCH
 "starttime": $(epoch_from_utc "$rough_time")
 Machine: `uname -a`
 Configured with: `cat autogen.lastrun`

 Commits since the last success:

 $(get_commits_since_last_good commits)

 The error is:

 $(cat "$error_log")
EOF
    else
        cat $error_log
    fi
}

report_fixed ()
{
    local_to_mail=
    local tinder1=
    local tinder2=
    local mail_tail=
    local success_kind="$1"
    shift
    local rough_time="$1"
    shift

    local previous_success="$(cat tb_${B}_last-success-git-timestamp.txt)"
    local last_failure="$(cat tb_${B}_last-failure-git-timestamp.txt)"
    to_mail=
    if [ "$SEND_MAIL" = "owner" -o "$SEND_MAIL" = "debug" -o "$SEND_MAIL" = "author" ] ; then
        to_mail="${OWNER?}"
    else
        if [ "$SEND_MAIL" = "all" ] ; then
            case "$success_kind" in
                owner) to_mail="${OWNER?}"
                    message="box fixed" ;;
                *)
                    if [ -z "$previous_success" ] ; then
                      # we need at least one successful build to
                      # be reliable
                        to_mail="${OWNER?}"
                    else
                        to_mail="$(get_committers)"
                    fi
                    message="previous success: ${previous_success?}" ;;
            esac
        fi
    fi
    if [ -n "$to_mail" ] ; then
        echo "$*" 1>&2
        echo "Previous success: ${previous_success}" 1>&2
        echo "Last failure: ${last_failure}" 1>&2
        tinder1="`echo \"Full log available at http://tinderbox.libreoffice.org/$TINDER_BRANCH/status.html\"`"
        tinder2="`echo \"Box name: ${TINDER_NAME?}\"`"
        if [ "$*" != "" ]; then
            mail_tail = $'\nAdditional information:\n\n'"$*"
        fi

        cat <<EOF | send_mail_msg "$to_mail" "Tinderbox fixed, $message" "" "${OWNER?}" ""
Hi folks,

The previously reported build failure is fixed. Thanks!

${tinder1}

Tinderbox info:

  ${tinder2}
  Machine: `uname -a`
  Configured with: `cat autogen.lastrun`

Commits since last failure:

$(get_commits_since_last_bad commits)

Commits since the previous success:

$(get_commits_since_last_good commits)
${mail_tail}
EOF
    else
        echo "$*" 1>&2
    fi
}

collect_current_heads()
{
    [ $V ] && echo "collect_current_head"
    if [ -f .gitmodules ] ; then
        echo "core:$(git rev-parse HEAD)" > "${METADATA_DIR?}/tb_${B}_current-git-heads.log"
    else
        ./g -1 rev-parse HEAD > "${METADATA_DIR?}/tb_${B}_current-git-heads.log"
    fi
    print_date > "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log"
}

get_committers()
{
    echo "get_committers: $(get_commits_since_last_good people)" 1>&2
    get_commits_since_last_good people | sort | uniq | tr '\n' ','
}

rotate_logs()
{
    if [ "$retval" = "0" ] ; then
        cp -f "${METADATA_DIR?}/tb_${B}_current-git-heads.log" "${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt" 2>/dev/null
        cp -f "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log" "${METADATA_DIR?}/tb_${B}_last-success-git-timestamp.txt" 2>/dev/null
    elif [ "$retval" != "false_negative" ]; then
        cp -f "${METADATA_DIR?}/tb_${B}_current-git-heads.log" "${METADATA_DIR?}/tb_${B}_last-failure-git-heads.txt" 2>/dev/null
        cp -f "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log" "${METADATA_DIR?}/tb_${B}_last-failure-git-timestamp.txt" 2>/dev/null
    fi
    for f in tb_${B}*.log ; do
        mv -f ${f} prev-${f} 2>/dev/null
    done
    pushd "${METADATA_DIR?}" > /dev/null
    for f in tb_${B}*.log ; do
        mv -f ${f} prev-${f} 2>/dev/null
    done
    popd > /dev/null
}

check_for_commit()
{
    [ $V ] && echo "pulling from the repos"
    err_msgs="$( $timeout ./g pull -r 2>&1)"
    if [ "$?" -ne "0" ] ; then
        printf "git repo broken - error is:\n\n$err_msgs" > error_log.log
        report_error owner "$(print_date)" error_log.log
        IS_NEW_COMMIT="error"
    else
        collect_current_heads

        if [ "$(cat "${METADATA_DIR?}/tb_${B}_current-git-heads.log")" != "$(cat "${METADATA_DIR?}/prev-tb_${B}_current-git-heads.log")" ] ; then
            IS_NEW_COMMIT="yes"
        else
            IS_NEW_COMMIT="no"
        fi
    fi
    [ $V ] && echo "pulling from the repos -> new commit : ${IS_NEW_COMMIT?}"
}

check_for_gerrit()
{
    local result
    local has_task

    [ $V ] && echo "try to get a task for gerrit buildbot"

    IS_NEW_GERRIT="error"
    GERRIT_TASK_TICKET=""
    GERRIT_TASK_BRANCH=""
    GERRIT_TASK_REF=""
    result=$(ssh ${GERRIT_HOST?} buildbot get -p core --id ${TINDER_ID?} -a ${GERRIT_PLATFORM?} --format BASH)
    [ $V ] && echo "get task result:${result}"

    has_task=$(echo "$result" | grep "^GERRIT_TASK_")
    if [ -n "${has_task}" ] ; then
        eval "${result}"
        if [ -z "$GERRIT_TASK_TICKET" -o -z "$GERRIT_TASK_REF" ] ; then
            IS_NEW_GERRIT="no"
            [ $V ] && echo "no task from gerrit buildbot"
        else
            IS_NEW_GERRIT="yes"
            [ $V ] && echo "got task TASK_TICKET=$GERRIT_TASK_TICKET TASK_REF=$GERRIT_TASK_REF"
        fi
    else
        #FIXME normal "no task" detection
        IS_NEW_GERRIT="no"
        [ $V ] && echo "no task from gerrit buildbot"
    fi

}

determine_make()
{
    ## Determine how GNU make is called on the system
    for _g in make gmake gnumake; do
        $_g --version 2> /dev/null | grep -q GNU
        if test $? -eq 0;  then
            MAKE=$_g
            break
        fi
    done
}

find_dev_install_location()
{
    find . -name opt -type d
}


position_bibisect_branch()
{
    pushd ${ARTIFACTDIR?} > /dev/null
    git checkout -q ${B?}
    if [ "$?" -ne "0" ] ; then
        echo "Error could not position the bibisect repository to the branch $B" 1>&2
        exit 1;
    fi
    popd > /dev/null
}

deliver_lo_to_bibisect()
{
    # copy the content of lo proper to bibisect
    # this is  separate function so it can easily be overriden
    cp -fR ${optdir?} ${ARTIFACTDIR?}/

}

bibisect_post()
{
    pushd ${ARTIFACTDIR?} > /dev/null
    if [ "${BIBISECT_GC}" = "Y" ] ; then
        git gc
    fi
    if [ "${BIBISECT_PUSH}" = "Y" ] ; then
        git push
    fi
    popd > /dev/null
}

deliver_to_bibisect()
{
    local cc=""
    local oc=""

    [ $V ] && echo "deliver_to_bibisect()"

    if [ -n ${optdir} ] ; then
        # verify that someone did not screw-up bibisect repo
        # while we were running
        if [ "${PUSH_TO_BIBISECT_REPO}" != "0" ] ; then
            # note: this function will exit if something is wrong

            # avoid delivering the same build twice to bibisect
            cc=$(git rev-list -1 HEAD)
            if [ -f  ${ARTIFACTDIR?}/commit.hash ] ; then
                oc="$(cat ${ARTIFACTDIR?}/commit.hash)"
            fi
            if [ "${cc}" != "${oc}" ] ; then
                deliver_lo_to_bibisect

                git log -1 --pretty=format:"source-hash-%H%n%n" $BUILDCOMMIT > ${ARTIFACTDIR?}/commitmsg
                git log -1 --pretty=fuller $BUILDCOMMIT >> ${ARTIFACTDIR?}/commitmsg

                [ $V ] && echo "Bibisect: Include interesting logs/other data"
                # Include the autogen log.
                cp tb_${B?}_autogen.log ${ARTIFACTDIR?}

                # Include the build, test logs.
                cp tb_${B?}_build.log ${ARTIFACTDIR?}

                # Make it easy to grab the commit id.
                git rev-list -1 HEAD > ${ARTIFACTDIR?}/commit.hash

                # Commit build to the local repo and push to the remote.
                [ $V ] && echo "Bibisect: Committing to local bibisect repo"
                pushd "${ARTIFACTDIR?}" >/dev/null
                git add -A
                git commit -q --file=commitmsg
                popd > /dev/null

                bibisect_post

            fi
        fi
    fi
    [ $V ] && echo "unlock ${lock_file?}.bibisect"
    [ $V ] && echo "unlock ${lock_file?}.bibisect"
}

push_bibisect()
{
    local curr_day=
    local last_day_upload=

    if [ $PUSH_TO_BIBISECT_REPO != "0" -a -n "${optdir}" ] ; then

        [ $V ] && echo "Push: bibisec builds enabled"
        curr_day=$(date -u '+%Y%j')
        last_day_upload="$(cat "${METADATA_DIR?}/tb_${B}_last-bibisect-day.txt" 2>/dev/null)"
        if [ -z "$last_day_upload" ] ; then
            last_day_upload=0
        fi
        [ $V ] && echo "bibisect curr_day=$curr_day"
        [ $V ] && echo "bibisect last_day_upload=$last_day_upload"

        # If it has been less than a day since we pushed the last build
        # (based on calendar date), skip the rest of the push phase.
        if [ $last_day_upload -ge $curr_day ] ; then
            return 0;
        fi
        [ $V ] && echo "Record bibisect"
        deliver_to_bibisect

        echo "$curr_day" > "${METADATA_DIR?}/tb_${B}_last-bibisect-day.txt"

    fi
}

push_nightly()
{
    local curr_day=
    local last_day_upload

    # Push build up to the project server (if enabled).
    if [ "$PUSH_NIGHTLIES" = "1" ] ; then
        [ $V ] && echo "Push: Nightly builds enabled"
        curr_day=$(date -u '+%Y%j')
        last_day_upload="$(cat "${METADATA_DIR?}/tb_${B}_last-upload-day.txt" 2>/dev/null)"
        if [ -z "$last_day_upload" ] ; then
            last_day_upload=0
        fi
        [ $V ] && echo "curr_day=$curr_day"
        [ $V ] && echo "last_day_upload=$last_day_upload"

        # If it has been less than a day since we pushed the last build
        # (based on calendar date), skip the rest of the push phase.
        if [ $last_day_upload -ge $curr_day ] ; then
            return 0;
        fi
        [ $V ] && echo "Push Nightly builds"
        prepare_upload_manifest
        if [ "$SYMBOLS_DIR" ] ; then
            ${BIN_DIR?}/push_nightlies.sh $push_opts -t "$(cat "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log")" -n "$TINDER_NAME" -l "$BANDWIDTH" -p "$SYMBOLS_DIR"/${B}
        else
            ${BIN_DIR?}/push_nightlies.sh $push_opts -t "$(cat "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log")" -n "$TINDER_NAME" -l "$BANDWIDTH"
        fi
        # If we had a failure in pushing the build up, return
        # immediately (making sure we do not mark this build as the
        # last uploaded daily build).
        if [ "$?" != "0" ] ; then
            return 0;
        fi
        echo "$curr_day" > "${METADATA_DIR?}/tb_${B}_last-upload-day.txt"
    fi

}

report_gerrit()
{
local log_type="$1"
local status="failed"

    [ $V ] && echo "report to gerrit retval=${retval} log_type=${log_type}"
    if [ "$log_type" = "short"  -a "${retval?}" = "0" ] ; then
        gzlog="tinder.log.gz"
        (
            echo "gerrit_task_ticket:$GERRIT_TASK_TICKET"
            echo "gerrit_task_branch:$GERRIT_TASK_BRANCH"
            echo "gerrit task_ref:$GERRIT_TASK_REF"
            echo ""
            echo "Build: OK"
            echo ""
            cat tb_${B}_autogen.log 2>/dev/null
        ) | gzip -c > "${gzlog}"
    else
        gzlog="tinder.log.gz"
        (
            echo "gerrit_task_ticket:$GERRIT_TASK_TICKET"
            echo "gerrit_task_branch:$GERRIT_TASK_BRANCH"
            echo "gerrit task_ref:$GERRIT_TASK_REF"
            echo ""
            if [ "${retval?}" = "0" ] ; then
                echo "Build: OK"
            else
                echo "Build: FAIL"
            fi
            echo ""
            cat tb_${B}_autogen.log tb_${B}_clean.log tb_${B}_build.log tb_${B}_tests.log 2>/dev/null
        ) | gzip -c > "${gzlog}"
    fi

    if [ "${retval?}" = "0" ] ; then
	status="success"
    fi
    log_msgs "Report Success for gerrit ref '$GERRIT_TASK_TICKET'."
    cat "${gzlog}" | ssh ${GERRIT_HOST?} buildbot put --id ${TINDER_ID?} --ticket "${GERRIT_TASK_TICKET}" --status $status --log -

}

fetch_gerrit()
{
    GERRIT_PREV_B=`git branch | grep '^\*' | sed 's/^..//' | sed 's/\//_/g'`
    [ $V ] && echo "fetching gerrit path from ssh://${GERRIT_HOST?}/core ${GERRIT_TASK_REF}"
    git fetch -q ssh://${GERRIT_HOST?}/core ${GERRIT_TASK_REF}
    if [ "$?" != "0" ] ; then
        retval="3"
    else
        git checkout -q FETCH_HEAD || die "fatal error checking out gerrit ref"
        git submodule -q update
        [ $V ] && echo "fetched gerrit path from ssh://${GERRIT_HOST?}/core ${GERRIT_TASK_REF}"
        retval="0"
    fi
}

run_primer()
{
    if [ "$SEND_MAIL" != "owner" ] ; then
        SEND_MAIL=""  # we don't want to notify the tinderbox
    fi
    # if we want to upload after a prime, we really want to upload, not just once a day
    if [ "$PUSH_NIGHTLIES" = "1" ] ; then
        rm -f "${METADATA_DIR?}/tb_${B?}_last-upload-day.txt"
    fi


    log_msgs "Starting primer for branch '$TINDER_BRANCH'."
    (
        do_flock -x 200

        collect_current_heads
        retval="0"
        if [ "$DO_NOT_CLEAN" = "1" ] ; then
            PHASE_LIST="autogen make test push"
        else
            PHASE_LIST="autogen clean make test push"
        fi
        do_build "tb"

        rotate_logs
        if [ "$retval" = "0" ] ; then
            exit 0
        else
            exit 1
        fi
    ) 200>${lock_file?}
    retval=$?
    [ $V ] && echo "unlock ${lock_file?}"
    if [ "${retval?}" = "0" ] ; then
        log_msgs "Primer for branch '$TINDER_BRANCH' Successful."
    else
        log_msgs "Primer for branch '$TINDER_BRANCH' Failed."
    fi
    return ${retval?}
}

run_gerrit_patch()
{
    if [ "$PUSH_NIGHTLIES" = "1" ] ; then
        echo "Warning: pushing build is not supported with gerrit build" 1>&2
        PUSH_NIGHTLIES=0
    fi
    if [ "$SEND_MAIL" != "owner" ] ; then
        SEND_MAIL=""  # we don't want to notify the tinderbox
    fi

    log_msgs "Starting build for gerrit ref '$GERRIT_REF'."
    (
        do_flock -x 200

        GERRIT_PREV_B=`git branch | grep '^\*' | sed 's/^..//' | sed 's/\//_/g'`
        [ $V ] && echo "git fetch ssh://${GERRIT_HOST?}/core $GERRIT_REF"
        git fetch ssh://${GERRIT_HOST?}/core $GERRIT_REF && git checkout FETCH_HEAD || die "Error setting up the ref";

        echo "ssh ${GERRIT_HOST?} gerrit review --project core --force-message -m \"Starting Build on ${TINDER_NAME}\" $(git rev-parse HEAD)"
        ssh ${GERRIT_HOST?} gerrit review --project core --force-message -m \"Starting Build on ${TINDER_NAME}\" $(git rev-parse HEAD) || die "error reviewing in"

        retval="0"
        PHASE_LIST="autogen clean make test push"
        do_build "gerrit"

        if [ "${retval}" = "0" ] ; then
            echo "ssh ${GERRIT_HOST?} gerrit review --project core --force-message -m \"Successful build of $(git rev-parse HEAD) on tinderbox: $TINDER_NAME\" --verified +1 $(git rev-parse HEAD)"
            ssh ${GERRIT_HOST?} gerrit review --project core --force-message -m \"Successful build of $(git rev-parse HEAD) on tinderbox: $TINDER_NAME\" --verified +1 $(git rev-parse HEAD)
        else
            echo "ssh ${GERRIT_HOST?} gerrit review --project core --force-message -m \"Failed build of $(git rev-parse HEAD) on tinderbox: $TINDER_NAME\" --verified -1 $(git rev-parse HEAD)"
            ssh ${GERRIT_HOST?} gerrit review --project core --force-message -m \"Failed build of $(git rev-parse HEAD) on tinderbox: $TINDER_NAME\" --verified -1 $(git rev-parse HEAD) || die "error reviewing out"

        fi
        if [ -n "$GERRIT_PREV_B" ] ; then
            git checkout "$GERRIT_PREV_B"
            git submodule update
        fi
        if [ "$retval" = "0" ] ; then
            exit 0
        else
            exit 1
        fi
    ) 200>${lock_file?}
    retval=$?
    [ $V ] && echo "unlock ${lock_file?}"
    return ${retval?}
}

run_gerrit_loop()
{
    # main tinderbox loop
    while true; do
        if [ -f tb_${B}_stop ] ; then
            break
        fi
        (
            do_flock -x 200

            check_for_gerrit

            if [ "${IS_NEW_GERRIT}" = "yes" ] ; then

                log_msgs "Starting build for gerrit ref '$GERRIT_TASK_REF'."
                fetch_gerrit

                if [ "$retval" = "0" ] ; then

                    PHASE_LIST="autogen clean make test push"
                    do_build "gerrit"

                    report_gerrit
                fi
                if [ -n "$GERRIT_PREV_B" ] ; then
                    git checkout "$GERRIT_PREV_B"
                    git submodule update
                fi
                if [ "$retval" = "0" ] ; then
                    exit 0
                elif [ "$retval" = "3" ] ; then
                    exit 3
                elif [ "$retval" = "-1" ] ; then
                    exit -1
                else
                    exit 1
                fi
            else
                exit 3
            fi
        ) 200>${lock_file?}
        retval=$?
        [ $V ] && echo "unlock ${lock_file?}"
        if [ -f tb_${B}_stop -o "${retval?}" = "-1" ] ; then
            break
        fi
        if [ "$retval" = "3" ] ; then
            [ $V ] && echo "sleep 60"
            sleep 60
            retval="0"
        fi
    done
}



run_tb_gerrit_loop()
{
    local priority="${1:-fair}"
    local next_priority="$priority"
    local retry_count

    if [ "${priority?}" = "fair" ] ; then
        next_priority="tb"
        if [ ! -f "${METADATA_DIR?}/tb_${B}_next_priority" ] ; then
            echo -n "{next_priority}" > "${METADATA_DIR?}/tb_${B}_next_priority"
        fi
    else
        echo -n "{next_priority}" > "${METADATA_DIR?}/tb_${B}_next_priority"
    fi
    while true; do

        if [ -f tb_${B?}_stop ] ; then
            break
        fi
        (
            do_flock -x 200
            build_type=""
            next_priority=$(cat "${METADATA_DIR?}/tb_${B}_next_priority")
            if [ "${next_priority?}" = "tb" ] ; then
                check_for_commit
                if [ "${IS_NEW_COMMIT?}" = "yes" ] ; then
                    build_type="tb"
                    if [ "${priority?}" = "fair" ] ; then
                        next_priority="gerrit"
                    fi
                else
                    check_for_gerrit
                    if [ "${IS_NEW_GERRIT?}" = "yes" ] ; then
                        build_type="gerrit"
                        if [ "${priority?}" = "fair" ] ; then
                            next_priority="tb"
                        fi
                    fi
                fi
            else
                check_for_gerrit
                if [ "${IS_NEW_GERRIT?}" = "yes" ] ; then
                    build_type="gerrit"
                    if [ "${priority?}" = "fair" ] ; then
                        next_priority="tb"
                    fi
                else
                    check_for_commit
                    if [ "${IS_NEW_COMMIT?}" = "yes" ] ; then
                        build_type="tb"
                        if [ "${priority?}" = "fair" ] ; then
                            next_priority="gerrit"
                        fi
                    fi
                fi
            fi
            echo -n "${next_priority}" > "${METADATA_DIR?}/tb_${B}_next_priority"
            if [ "${build_type}" = "tb" ] ; then

                last_checkout_date="$(cat "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log")"

                report_to_tinderbox "${last_checkout_date?}" "building" "no"

                previous_build_status="${build_status}"
                build_status="build_failed"
                retval=0
                retry_count=3

                if [ "$DO_NOT_CLEAN" = "1" ] ; then
                    PHASE_LIST="autogen make test push"
                else
                    PHASE_LIST="autogen clean make test push"
                fi

                while [ "${PHASE_LIST}" != "" ] ; do

                    do_build "tb"

                    if [ "$retval" = "0" ] ; then
                        build_status="success"
                        report_to_tinderbox "$last_checkout_date" "success" "yes"
                        if [ "${previous_build_status}" = "build_failed" ]; then
                            report_fixed committer "$last_checkout_date"
                        fi
                    elif [ "$retval" = "false_negative" ] ; then
                        report_to_tinderbox "${last_checkout_date?}" "fold" "no"
                        log_msgs "False negative build, skip reporting"
                    # false negative does not need a full clean build, let's just redo make and after
                        retry_count=$((retry_count - 1))
                        if [ "$retry_count" = "0" ] ; then
                            PHASE_LIST=
                        else
                            PHASE_LIST="make test push"
                            retval=0
                        fi
                    else
                        printf "${report_msgs?}:\n\n" > report_error.log
                        echo "======" >> report_error.log
                        if [ "${report_log?}" == "tb_${B}_build.log" ] ; then
                            tail -n1000 ${report_log?} >> report_error.log
                        else
                            cat ${report_log?} >> report_error.log
                        fi
                        report_error committer "$last_checkout_date" report_error.log
                        report_to_tinderbox "${last_checkout_date?}" "build_failed" "yes"
                    fi
                done

                rotate_logs

                if [ "$retval" = "0" ] ; then
                    exit 0
                elif [ "$retval" = "false_negative" ] ; then
                    exit 2
                else
                    exit 1
                fi
            elif [ "${build_type?}" = "gerrit" ] ; then

                log_msgs "Starting build for gerrit ref '$GERRIT_TASK_REF'."
                fetch_gerrit

                if [ "${retval?}" = "0" ] ; then

                    PHASE_LIST="autogen clean make test push"
                    retry_count=3
                    while [ "${PHASE_LIST}" != "" ] ; do
                        do_build "gerrit"
                        if [ "${retval?}" = "false_negative" ] ; then
                            report_to_tinderbox "${last_checkout_date?}" "fold" "no"
                            log_msgs "False negative build, skip reporting"
                    # false negative does not need a full clean build, let's just redo make and after
                            retry_count=$((retry_count - 1))
                            if [ "${retry_count?}" = "0" ] ; then
                                PHASE_LIST=
                            else
                                PHASE_LIST="make test push"
                                retval=0
                            fi
                        else
                            report_gerrit
                        fi
                    done
                fi
                if [ -n "$GERRIT_PREV_B" ] ; then
                    git checkout -q "$GERRIT_PREV_B"
                    git submodule -q update
                fi
                if [ "${retval?}" = "0" ] ; then
                    exit 0
                elif [ "${retval?}" = "3" ] ; then
                    exit 3
                elif [ "$retval" = "-1" ] ; then
                    exit -1
                else
                    exit 1
                fi
            else
                exit 3
            fi
        ) 200>${lock_file?}

        ret="$?"
        if [ -f tb_${B}_stop -o "${ret?}" = "-1" ] ; then
            break
        fi
        if [ "${ret?}" == "2" ] ; then
            [ $V ] && echo "sleep 60"
            sleep 60
            retval="false_negative"
        elif [ "$ret" == "3" ] ; then
            [ $V ] && echo "sleep 60"
            sleep 60
            retval="0"
        else
            log_msgs "Waiting ${PAUSE_SECONDS?} seconds."
            sleep ${PAUSE_SECONDS?}
            retval="0"
        fi
    done

    if [ -f tb_${B}_stop ] ; then
        log_msgs "Stoped by request"
        rm tb_${B}_stop
    fi

}

run_tb_loop()
{
    if [ ! -f "${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt" ] ; then
        echo "You need a valid baseline. run once with -z or make sure you have a valid ${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt" 1>&2
        exit 1;
    else
        if [ "$FORCE_REBUILD" = "1" ] ; then
            retval="false_negative" # to force a rebuild the first time around
        else
            retval=0
        fi
        cp "${METADATA_DIR?}/tb_${B}_last-success-git-heads.txt" "${METADATA_DIR?}/tb_${B}_current-git-heads.log"
        cp "${METADATA_DIR?}/tb_${B}_last-success-git-timestamp.txt" "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log"
        rotate_logs
    fi

    # main tinderbox loop
    while true; do

        if [ -f tb_${B}_stop ] ; then
            break
        fi
        (
            do_flock -x 200

            if [ "$retval" != "false_negative" ] ; then
                check_for_commit
            else
                collect_current_heads
                IS_NEW_COMMIT="yes"
            fi
            if [ "${IS_NEW_COMMIT?}" = "yes" ] ; then

                last_checkout_date="$(cat "${METADATA_DIR?}/tb_${B}_current-git-timestamp.log")"

                report_to_tinderbox "${last_checkout_date?}" "building" "no"

                previous_build_status="${build_status}"
                build_status="build_failed"
                retval=0
                retry_count=3

                if [ "$DO_NOT_CLEAN" = "1" ] ; then
                    PHASE_LIST="autogen make test push"
                else
                    PHASE_LIST="autogen clean make test push"
                fi

                while [ "${PHASE_LIST}" != "" ] ; do

                    do_build "tb"

                    if [ "$retval" = "0" ] ; then
                        build_status="success"
                        report_to_tinderbox "$last_checkout_date" "success" "yes"
                        if [ "${previous_build_status}" = "build_failed" ]; then
                            report_fixed committer "$last_checkout_date"
                        fi
                    elif [ "$retval" = "false_negative" ] ; then
                        report_to_tinderbox "${last_checkout_date?}" "fold" "no"
                        log_msgs "False negative build, skip reporting"
                    # false negative does not need a full clean build, let's just redo make and after
                        retry_count=$((retry_count - 1))
                        if [ "$retry_count" = "0" ] ; then
                            PHASE_LIST=
                        else
                            PHASE_LIST="make test push"
                            retval=0
                        fi
                    else
                        printf "${report_msgs?}:\n\n" > report_error.log
                        echo "======" >> report_error.log
                        if [ "${report_log?}" == "tb_${B}_build.log" ] ; then
                            tail -n1000 ${report_log?} >> report_error.log
                        else
                            cat ${report_log?} >> report_error.log
                        fi
                        report_error committer "$last_checkout_date" report_error.log
                        report_to_tinderbox "${last_checkout_date?}" "build_failed" "yes"
                    fi
                done

                rotate_logs

                if [ "$retval" = "0" ] ; then
                    exit 0
                elif [ "$retval" = "false_negative" ] ; then
                    exit 2
                else
                    exit 1
                fi
            elif [ "${IS_NEW_COMMIT?}" = "error" ] ; then
                exit 1
            else
                exit 3
            fi

        ) 200>${lock_file?}
        ret="$?"
        [ $V ] && echo "unlock ${lock_file?}"

        if [ -f tb_${B}_stop ] ; then
            break
        fi
        if [ "$ret" == "2" ] ; then
            [ $V ] && echo "sleep 60"
            sleep 60
            retval="false_negative"
        elif [ "$ret" == "3" ] ; then
            [ $V ] && echo "sleep 60"
            sleep 60
            retval="0"
        else
            log_msgs "Waiting ${PAUSE_SECONDS?} seconds."
            sleep ${PAUSE_SECONDS?}
            retval="0"
        fi
    done

    if [ -f tb_${B}_stop ] ; then
        log_msgs "Stoped by request"
        rm tb_${B}_stop
    fi

}

################
# ATTENTION:
# Nothing below this point can be overriden at the platform-level
# so you should probably add code above this point
# unless you have a darn good reason not to

# source the platform specific override

mo="$(uname -o 2>/dev/null)"
ms="$(uname -s 2>/dev/null)"
if [ -n "${mo}" -a -f "${BIN_DIR?}/tinbuild_internals_${mo}.sh" ] ; then
    source "${BIN_DIR?}/tinbuild_internals_${mo}.sh"
else
    if [ -n "${ms}" -a -f "${BIN_DIR?}/tinbuild_internals_${ms}.sh" ] ; then
        source "${BIN_DIR?}/tinbuild_internals_${ms}.sh"
    fi
fi
unset mo
unset ms


determine_make


source ${BIN_DIR?}/tinbuild_phases.sh