1#!/bin/bash
2set -e
3
4function usage() {
5  echo "Confirms that no unexpected, generated files exist in the source repository"
6  echo
7  echo "Usage: $0 <timestamp file>"
8  echo
9  echo "<timestamp file>: any file newer than this one will be considered an error unless it is already exempted"
10  return 1
11}
12
13# parse arguments
14# a file whose timestamp is the oldest acceptable timestamp for source files
15COMPARE_TO_FILE="$1"
16if [ "$COMPARE_TO_FILE" == "" ]; then
17  usage
18fi
19
20# get script path
21SCRIPT_DIR="$(cd $(dirname $0) && pwd)"
22SOURCE_DIR="$(cd $SCRIPT_DIR/../.. && pwd)"
23
24# puts a copy of src at dest (even if dest's parent dir doesn't exist yet)
25function copy() {
26  src="$1"
27  dest="$2"
28
29  mkdir -p "$(dirname $dest)"
30  cp -r "$src" "$dest"
31}
32
33# confirm that no files in the source repo were unexpectedly created (other than known exemptions)
34function checkForGeneratedFilesInSourceRepo() {
35
36  # Regexes for paths that are still expected to be generated and that we have to allow
37  # If you need add or remove an exemption here, update cleanBuild.sh too
38  EXEMPT_PATHS=".gradle placeholder/.gradle buildSrc/.gradle local.properties reports build .*.attach_pid.*"
39  # put "./" in front of each path to match the output from 'find'
40  EXEMPT_PATHS="$(echo " $EXEMPT_PATHS" | sed 's| | ./|g')"
41  # build a `find` argument for skipping descending into the exempt paths
42  EXEMPTIONS_ARGUMENT="$(echo $EXEMPT_PATHS | sed 's/ /\n/g' | sed 's|\(.*\)|-path \1 -prune -o|g' | xargs echo)"
43
44  # Search for files that were created or updated more recently than the build start.
45  # Unfortunately we can't also include directories because the `test` task seems to update
46  # the modification time in several projects
47  GENERATED_FILES="$(cd $SOURCE_DIR && find . $EXEMPTIONS_ARGUMENT -newer $COMPARE_TO_FILE -type f)"
48  UNEXPECTED_GENERATED_FILES=""
49  for f in $GENERATED_FILES; do
50    exempt=false
51    for exemption in $EXEMPT_PATHS; do
52      if echo "$f" | grep "^${exemption}$" >/dev/null 2>/dev/null; then
53        exempt=true
54        break
55      fi
56    done
57    if [ "$exempt" == "false" ]; then
58      UNEXPECTED_GENERATED_FILES="$UNEXPECTED_GENERATED_FILES $f"
59    fi
60  done
61  if [ "$UNEXPECTED_GENERATED_FILES" != "" ]; then
62    echo >&2
63    echo "Unexpectedly found these files generated or modified by the build:
64
65${UNEXPECTED_GENERATED_FILES}
66
67Generated files should go in OUT_DIR instead because that is where developers expect to find them
68(to make it easier to diagnose build problems: inspect or delete these files)" >&2
69
70    # copy these new files into DIST_DIR in case anyone wants to inspect them
71    COPY_TO=$DIST_DIR/new_files
72    for f in $UNEXPECTED_GENERATED_FILES; do
73      copy "$SOURCE_DIR/$f" "$COPY_TO/$f"
74    done
75
76    # b/331622149 temporarily also copy $OUT_DIR/androidx/room/integration-tests
77    if echo $UNEXPECTED_GENERATED_FILES | grep room.*core >/dev/null; then
78      ALSO_COPY=androidx/room/integration-tests/room-testapp-multiplatform/build
79      copy $OUT_DIR/$ALSO_COPY $COPY_TO/out/$ALSO_COPY
80    fi
81
82    echo >&2
83    echo Copied these generated files into $COPY_TO >&2
84    exit 1
85  fi
86}
87checkForGeneratedFilesInSourceRepo
88