1#!/bin/bash
2set -e
3
4# This script runs frameworks/support/gradlew
5
6# find script
7SCRIPT_DIR="$(cd $(dirname $0) && pwd)"
8
9# resolve directories
10cd "$SCRIPT_DIR/../.."
11if [ "$OUT_DIR" == "" ]; then
12  OUT_DIR="../../out"
13fi
14mkdir -p "$OUT_DIR"
15export OUT_DIR="$(cd $OUT_DIR && pwd)"
16if [ "$DIST_DIR" == "" ]; then
17  DIST_DIR="$OUT_DIR/dist"
18fi
19mkdir -p "$DIST_DIR"
20export DIST_DIR="$DIST_DIR"
21if [ "$CHANGE_INFO" != "" ]; then
22  cp "$CHANGE_INFO" "$DIST_DIR/"
23  if [ "$MANIFEST" == "" ]; then
24    export MANIFEST="$DIST_DIR/manifest_${BUILD_NUMBER}.xml"
25  fi
26fi
27
28# parse arguments
29if [ "$1" == "--diagnose" ]; then
30  DIAGNOSE=true
31  shift
32else
33  DIAGNOSE=false
34fi
35if [ "$1" == "--diagnose-timeout" ]; then
36  shift
37  DIAGNOSE_TIMEOUT_ARG="--timeout $1"
38  shift
39else
40  DIAGNOSE_TIMEOUT_ARG=""
41fi
42
43# record the build start time
44BUILD_START_MARKER="$OUT_DIR/build.sh.start"
45rm -f "$BUILD_START_MARKER"
46touch $BUILD_START_MARKER
47# record the build number
48echo "$BUILD_NUMBER" >> "$OUT_DIR/build_number.log"
49# only keep the last 10 build numbers
50tail -n 10 "$OUT_DIR/build_number.log" > "$OUT_DIR/build_number.log.tail"
51mv "$OUT_DIR/build_number.log.tail" "$OUT_DIR/build_number.log"
52cp "$OUT_DIR/build_number.log" "$DIST_DIR/build_number.log"
53
54# runs a given command and prints its result if it fails
55function run() {
56  echo Running "$*"
57  if eval "$*"; then
58    return 0
59  else
60    # Echo the Gradle command formatted for ease of reading.
61    echo "Gradle command failed:" >&2
62    echo "    $*" >&2
63    return 1
64  fi
65}
66
67BUILD_STATUS=0
68# enable remote build cache unless explicitly disabled
69if [ "$USE_ANDROIDX_REMOTE_BUILD_CACHE" == "" ]; then
70  export USE_ANDROIDX_REMOTE_BUILD_CACHE=gcp
71fi
72
73# list kotlin sessions in case there are several, b/279739438
74function checkForLeftoverKotlinSessions() {
75  KOTLIN_SESSIONS_DIR=$OUT_DIR/gradle-project-cache/kotlin/sessions
76  NUM_KOTLIN_SESSIONS="$(ls $KOTLIN_SESSIONS_DIR 2>/dev/null | wc -l)"
77  if [ "$NUM_KOTLIN_SESSIONS" -gt 0 ]; then
78    echo "Found $NUM_KOTLIN_SESSIONS leftover kotlin sessions in $KOTLIN_SESSIONS_DIR"
79  fi
80}
81checkForLeftoverKotlinSessions
82
83# list java processes to check for any running kotlin daemons, b/282228230
84function listJavaProcesses() {
85  echo "All java processes:"
86  ps -ef | grep /java || true
87}
88listJavaProcesses
89
90# launch a process to monitor for timeouts
91busytown/impl/monitor.sh 3600 busytown/impl/showJavaStacks.sh &
92
93# run the build
94if run ./gradlew --ci "$@"; then
95  echo build passed
96else
97  if [ "$DIAGNOSE" == "true" ]; then
98    # see if diagnose-build-failure.sh can identify the root cauase
99    echo "running diagnose-build-failure.sh, see build.log" >&2
100    # Specify a short timeout in case we're running on a remote server, so we don't take too long.
101    # We probably won't have enough time to fully diagnose the problem given this timeout, but
102    # we might be able to determine whether this problem is reproducible enough for a developer to
103    # more easily investigate further
104    ./development/diagnose-build-failure/diagnose-build-failure.sh $DIAGNOSE_TIMEOUT_ARG "--ci $*" || true
105    scansPrevDir="$DIST_DIR/scans-prev"
106    mkdir -p "$scansPrevDir"
107    # restore any prior build scans into the dist dir
108    cp ../../diagnose-build-failure/prev/dist/scan*.zip "$scansPrevDir/" || true
109  fi
110  BUILD_STATUS=1 # failure
111fi
112
113# check that no unexpected modifications were made to the source repository, such as new cache directories
114DIST_DIR=$DIST_DIR $SCRIPT_DIR/verify_no_caches_in_source_repo.sh $BUILD_START_MARKER
115
116# copy problem report to DIST_DIR so we can see them
117PROBLEM_REPORTS_EXPORTED=$DIST_DIR/problem-reports
118PROBLEM_REPORTS=$OUT_DIR/androidx/build/reports/problems
119if [ -d "$PROBLEM_REPORTS" ]; then
120    rm -rf "$PROBLEM_REPORTS_EXPORTED"
121    cp -r "$PROBLEM_REPORTS" "$PROBLEM_REPORTS_EXPORTED"
122fi
123
124# copy configuration cache reports to DIST_DIR so we can see them b/250893051
125CONFIGURATION_CACHE_REPORTS_EXPORTED=$DIST_DIR/configuration-cache-reports
126CONFIGURATION_CACHE_REPORTS=$OUT_DIR/androidx/build/reports/configuration-cache
127if [ -d "$CONFIGURATION_CACHE_REPORTS" ]; then
128    rm -rf "$CONFIGURATION_CACHE_REPORTS_EXPORTED"
129    cp -r "$CONFIGURATION_CACHE_REPORTS" "$CONFIGURATION_CACHE_REPORTS_EXPORTED"
130fi
131
132# stop Gradle daemon to clean up after ourselves
133./gradlew --stop
134
135exit "$BUILD_STATUS"
136