1#!/bin/bash
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19usage() {
20  echo "usage: $0 <command> <arguments> [options]"
21  echo
22  echo "Executes <command> <arguments> and then runs build_log_simplifier.py against its output"
23  echo
24  echo "-Pandroidx.validateNoUnrecognizedMessages"
25  echo "  Run build_log_simplifier.py --validate on success to confirm that the build generated no unrecognized messages"
26  exit 1
27}
28
29if [[ "$1" == "" ]]; then
30  usage
31fi
32
33validateNoUnrecognizedMessagesOnSuccess=false
34validateArgument="-Pandroidx.validateNoUnrecognizedMessages"
35if [[ " ${@} " =~ " $validateArgument " ]]; then
36  validateNoUnrecognizedMessagesOnSuccess=true
37fi
38if [[ " ${@} " =~ " ${validateArgument}=false " ]]; then
39  validateNoUnrecognizedMessagesOnSuccess=false
40fi
41printTimestamps=false
42timestampsArgument="-Pandroidx.printTimestamps"
43if [[ " ${@} " =~ " ${timestampsArgument} " ]]; then
44  printTimestamps=true
45fi
46
47# identify some filepaths
48SCRIPT_PATH="$(cd $(dirname $0) && pwd)"
49CHECKOUT="$(cd "$SCRIPT_PATH/../../.." && pwd)"
50if [ -n "$DIST_DIR" ]; then
51  LOG_DIR="$DIST_DIR/logs"
52else
53  LOG_DIR="$CHECKOUT/out/dist/logs"
54fi
55
56mkdir -p "$LOG_DIR"
57logFile="$LOG_DIR/gradle.log"
58
59# Move any preexisting $logFile to make room for a new one
60# After moving $logFile several times it eventually gets deleted
61function rotateLogs() {
62  iPlus1="10"
63  for i in $(seq 9 -1 1); do
64    mv "$LOG_DIR/gradle.${i}.log" "$LOG_DIR/gradle.${iPlus1}.log" 2>/dev/null || true
65    iPlus1=$i
66  done
67  mv $logFile "$LOG_DIR/gradle.1.log" 2>/dev/null || true
68}
69rotateLogs
70
71# Save OUT_DIR and some other variables into the log file so build_log_simplifier.py can
72# identify them later
73echo "OUT_DIR=$OUT_DIR" | tee -a $logFile
74if [ "$DIST_DIR" == "" ]; then
75  DIST_DIR="$OUT_DIR/dist"
76fi
77echo "DIST_DIR=$DIST_DIR" | tee -a $logFile
78echo "CHECKOUT=$CHECKOUT" | tee -a $logFile
79if [ "$GRADLE_USER_HOME" == "" ]; then
80  GRADLE_USER_HOME="$(cd && pwd)/.gradle"
81fi
82echo "GRADLE_USER_HOME=$GRADLE_USER_HOME" | tee -a $logFile
83programName="$1"
84shift
85
86if [ "$printTimestamps" == "true" ]; then
87  # this program adds a timestamp to each line of input, and echos the result
88  timestampPrinter="$SCRIPT_PATH/ts.py"
89else
90  # this program just echos its input
91  timestampPrinter=cat
92fi
93
94if "$programName" "$@" > >(tee -a "$logFile" | "$timestampPrinter") 2>&1; then
95  if [ "$validateNoUnrecognizedMessagesOnSuccess" == "true" ]; then
96    if $SCRIPT_PATH/build_log_simplifier.py --validate $logFile >&2; then
97      echo No unrecognized messages found in build log
98    else
99      exit 1
100    fi
101  fi
102else
103  echo >&2
104  echo "############################################################################" >&2
105  echo "Attempting to locate the relevant error messages via build_log_simplifier.py" >&2
106  echo "############################################################################" >&2
107  echo >&2
108  # Try to identify the most relevant lines of output, and put them at the bottom of the
109  # output where they will also be placed into the build failure email.
110  # TODO: We may be able to stop cleaning up Gradle's output after Gradle can do this on its own:
111  # https://github.com/gradle/gradle/issues/1005
112  # and https://github.com/gradle/gradle/issues/13090
113  summaryLog="$LOG_DIR/error_summary.log"
114  $SCRIPT_PATH/build_log_simplifier.py $logFile | tee "$summaryLog" >&2
115  exit 1
116fi
117