1#!/bin/bash
2set -e
3
4echo "Starting $0 at $(date)"
5
6cd "$(dirname $0)"
7
8CHECKOUT_DIR="$(cd ../../.. && pwd)"
9OUT_DIR="$CHECKOUT_DIR/out"
10if [ "$DIST_DIR" == "" ]; then
11  DIST_DIR="$OUT_DIR/dist"
12fi
13if [ "$MANIFEST" == "" -a "$CHANGE_INFO" != "" ]; then
14  export MANIFEST="$DIST_DIR/manifest_${BUILD_NUMBER}.xml"
15fi
16# move OUT_DIR and DIST_DIR into subdirectories so that if anything deletes them, it doesn't interfere with any files generated by buildbot code
17export OUT_DIR="$OUT_DIR/incremental"
18
19# Given a file containing a date as text, echos which week number it is
20# Examples: input "2024-01-01" should give output "0", input "2024-01-07" should give output "1", input "2024-01-14" should give output "2"
21function getWeekNumber() {
22  text="$1"
23  dayOfYearWithPrecedingZeros="$(date --date="$text" +"%j")"
24  dayOfYear="$(echo $dayOfYearWithPrecedingZeros | sed 's/^0*//')"
25  if [ "$dayOfYear" == "" ]; then
26    # There is an error that we will catch later
27    echo
28  else
29    echo "$(($dayOfYear / 7))"
30  fi
31}
32
33function deleteOldOutDir() {
34  # file telling when the out dir was created
35  createdAtFile=$OUT_DIR/created_at.txt
36  # file telling when the out dir was last updated
37  updatedAtFile=$OUT_DIR/updated_at.txt
38  now="$(date)"
39
40  # if this directory was created a long time ago, delete it
41  if [ -e "$createdAtFile" ]; then
42    createdAt="$(cat "$createdAtFile")"
43    # out dir knows when it was created
44    createdWeekNumber="$(getWeekNumber "$createdAt" || true)"
45    if [ "$createdWeekNumber" == "" ]; then
46      echo "Failed to parse $createdAtFile with text $createdAt" >&2
47      rm -f "$createdAtFile"
48      exit 1
49    fi
50    updatedWeekNumber="$(getWeekNumber "$now")"
51
52    if [ "$createdWeekNumber" != "$updatedWeekNumber" ]; then
53      echo "Deleting $OUT_DIR because it was created at $createdAt week $createdWeekNumber whereas now is $now week $updatedWeekNumber"
54      rm -rf "$OUT_DIR"
55    fi
56  fi
57  mkdir -p "$OUT_DIR"
58
59  # record that this directory was updated
60  echo "$now" > "$updatedAtFile"
61
62  # if we haven't recorded when this directory was created, do that too
63  if [ ! -e "$createdAtFile" ]; then
64    cp "$updatedAtFile" "$createdAtFile"
65  fi
66}
67deleteOldOutDir
68
69mkdir -p "$OUT_DIR"
70export DIST_DIR="$DIST_DIR/incremental"
71mkdir -p "$DIST_DIR"
72
73# Before we start the build, remove temporary directory contents, needs to match gradlew TMPDIR
74rm -fr "$OUT_DIR/tmp"
75
76if echo "$BUILD_NUMBER" | grep "P" >/dev/null; then
77  PRESUBMIT=true
78else
79  PRESUBMIT=false
80fi
81
82export USE_ANDROIDX_REMOTE_BUILD_CACHE=gcp
83
84# If we encounter a failure in postsubmit, we try a few things to determine if the failure is
85# reproducible
86DIAGNOSE_ARG=""
87if [ "$PRESUBMIT" == "false" ]; then
88  if [ "$BUILD_NUMBER" == "" ]; then
89    # This is a local build so we can diagnose without a timeout. The user can cancel it when they're satisfied.
90    DIAGNOSE_ARG="--diagnose"
91  else
92    # This is running on the build server so we should not spend long trying to diagnose it
93    DIAGNOSE_ARG="--diagnose --diagnose-timeout 600"
94  fi
95fi
96
97EXIT_VALUE=0
98
99# Validate translation exports, if present
100if ! impl/check_translations.sh; then
101  echo check_translations failed
102  EXIT_VALUE=1
103else
104    # Run Gradle
105    if impl/build.sh $DIAGNOSE_ARG buildOnServer createAllArchives checkExternalLicenses listTaskOutputs exportSboms \
106        "$@"; then
107    echo build succeeded
108    EXIT_VALUE=0
109    else
110    echo build failed
111    EXIT_VALUE=1
112    fi
113
114    # Parse performance profile reports (generated with the --profile option) and re-export the metrics in an easily machine-readable format for tracking
115    impl/parse_profile_data.sh
116fi
117
118echo "Completing $0 at $(date) with exit value $EXIT_VALUE"
119
120exit "$EXIT_VALUE"
121