1#!/bin/bash
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Common logic for use by repackaging scripts.
17# The following environment variables must be set before including this script:
18#
19#   PROJECT_DIR
20#        the root directory (relative to ${ANDROID_BUILD_TOP}) of the project within which the
21#        repackaging is to be done. e.g. external/conscrypt
22#
23#   MODULE_DIRS
24#        a space separated list of the module directories (relative to the PROJECT_DIR) whose
25#        sources need repackaging. e.g. core common android
26#
27#   SOURCE_DIRS
28#        a space separated list of the source directories (relative to the MODULE_DIRS) that are to
29#        be repackaged. If the ${PROJECT_DIR}/${MODULE_DIR}/${SOURCE_DIR} does not exist then it is
30#        ignored. e.g. src/main/java src/main/test
31#
32#   PACKAGE_TRANSFORMATIONS
33#        a space separated list of the package transformations to apply. Must be in the form
34#        <old package prefix>:<new package prefix>.
35#
36#   UNSUPPORTED_APP_USAGE_CLASS
37#        the fully qualified path to the UnsupportedAppUsage annotation to insert.
38#
39# The following environment variables are optional.
40#
41#   TAB_SIZE
42#        the tab size for formatting any inserted code, e.g. annotations. Defaults to 4.
43#
44# The following environment variables can be used after including this file:
45#   REPACKAGED_DIR
46#        the absolute path to the directory into which the repackaged source has been written.
47#
48# This should be used as follows:
49#
50#if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
51#    echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" >&2
52#    exit 1
53#fi
54#
55# PROJECT_DIR=...
56# MODULE_DIRS=...
57# SOURCE_DIRS=...
58# PACKAGE_TRANSFORMATIONS=...
59# source ${ANDROID_BUILD_TOP}/tools/currysrc/scripts/repackage-common.sh
60# ...any post transformation changes, e.g. to remove unnecessary files.
61
62if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
63    echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" >&2
64    exit 1
65fi
66
67if [[ -z "${PROJECT_DIR}" ]]; then
68  echo "PROJECT_DIR is not set" >&2
69  exit 1
70fi
71
72PROJECT_DIR=${ANDROID_BUILD_TOP}/${PROJECT_DIR}
73
74if [[ ! -d "${PROJECT_DIR}" ]]; then
75  echo "${PROJECT_DIR} does not exist" >&2
76  exit 1
77fi
78
79if [[ -z "${MODULE_DIRS}" ]]; then
80  echo "MODULE_DIRS is not set" >&2
81  exit 1
82fi
83
84if [[ -z "${SOURCE_DIRS}" ]]; then
85  echo "SOURCE_DIRS is not set" >&2
86  exit 1
87fi
88
89if [[ -z "${PACKAGE_TRANSFORMATIONS}" ]]; then
90  echo "PACKAGE_TRANSFORMATIONS is not set" >&2
91  exit 1
92fi
93
94set -e
95
96CLASSPATH=${ANDROID_HOST_OUT}/framework/currysrc.jar
97CHANGE_LOG=$(mktemp --suffix srcgen-change.log)
98
99function get_uncommitted_repackaged_files() {
100  git -C "${PROJECT_DIR}" status -s | cut -c4- | grep "^repackaged/"
101}
102
103cd ${ANDROID_BUILD_TOP}
104build/soong/soong_ui.bash --make-mode currysrc
105
106DEFAULT_CONSTRUCTORS_FILE=${PROJECT_DIR}/srcgen/default-constructors.txt
107CORE_PLATFORM_API_FILE=${PROJECT_DIR}/srcgen/core-platform-api.txt
108INTRA_CORE_API_FILE=${PROJECT_DIR}/srcgen/intra-core-api.txt
109UNSUPPORTED_APP_USAGE_FILE=${PROJECT_DIR}/srcgen/unsupported-app-usage.json
110
111TAB_SIZE=${TAB_SIZE-4}
112
113REPACKAGE_ARGS=""
114SEP=""
115for i in ${PACKAGE_TRANSFORMATIONS}
116do
117  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--package-transformation ${i}"
118  SEP=" "
119done
120
121if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
122  echo "Adding default constructors from ${DEFAULT_CONSTRUCTORS_FILE}"
123  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--default-constructors-file ${DEFAULT_CONSTRUCTORS_FILE}"
124  SEP=" "
125fi
126
127if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
128  echo "Adding CorePlatformApi annotations from ${CORE_PLATFORM_API_FILE}"
129  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--core-platform-api-file ${CORE_PLATFORM_API_FILE}"
130  SEP=" "
131fi
132
133if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
134  echo "Adding IntraCoreApi annotations from ${INTRA_CORE_API_FILE}"
135  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--intra-core-api-file ${INTRA_CORE_API_FILE}"
136  SEP=" "
137fi
138
139if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
140  echo "Adding UnsupportedAppUsage annotations from ${UNSUPPORTED_APP_USAGE_FILE}"
141  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--unsupported-app-usage-file ${UNSUPPORTED_APP_USAGE_FILE}"
142  SEP=" "
143  if [[ -n "${UNSUPPORTED_APP_USAGE_CLASS}" ]]; then
144    REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--unsupported-app-usage-class ${UNSUPPORTED_APP_USAGE_CLASS}"
145  fi
146fi
147
148if [[ -n "${TAB_SIZE}" ]]; then
149  echo "Using tab size of ${TAB_SIZE}"
150  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--tab-size ${TAB_SIZE}"
151  SEP=" "
152fi
153
154function do_transform() {
155  local SRC_IN_DIR=$1
156  local SRC_OUT_DIR=$2
157
158  rm -rf ${SRC_OUT_DIR}
159  mkdir -p ${SRC_OUT_DIR}
160
161  java -cp ${CLASSPATH} com.google.currysrc.aosp.RepackagingTransform \
162       --source-dir ${SRC_IN_DIR} \
163       --target-dir ${SRC_OUT_DIR} \
164       --change-log ${CHANGE_LOG} \
165       ${REPACKAGE_ARGS}
166
167  # Restore TEST_MAPPING files that may have been removed from the source directory
168  (cd $SRC_OUT_DIR; git checkout HEAD $(git status --short | grep -E "^ D .*/TEST_MAPPING$" | cut -c4-))
169}
170
171REPACKAGED_DIR=${PROJECT_DIR}/repackaged
172for i in ${MODULE_DIRS}
173do
174  MODULE_DIR=${PROJECT_DIR}/${i}
175  if [[ ! -d ${MODULE_DIR} ]]; then
176    echo "Module directory ${MODULE_DIR} does not exist" >&2
177    exit 1;
178  fi
179
180  for s in ${SOURCE_DIRS}
181  do
182    IN=${MODULE_DIR}/${s}
183    if [[ -d ${IN} ]]; then
184      OUT=${REPACKAGED_DIR}/${i}/${s}
185      do_transform ${IN} ${OUT}
186    fi
187  done
188done
189
190# Check to ensure that the entries in the change log are correct
191typeset -i ERROR=0
192function checkChangeLog {
193  local IN="$1"
194  local TAG="$2"
195  local MSG="$3"
196  DIFF=$(comm -23 "${IN}" <(grep -P "^\Q$TAG\E:" ${CHANGE_LOG} | cut -f2- -d: | sort -u))
197  if [[ -n "${DIFF}" ]]; then
198    ERROR=1
199    echo -e "\nERROR: ${MSG}" >&2
200    for i in ${DIFF}
201    do
202      echo "  $i" >&2
203    done
204    echo >&2
205  fi
206}
207
208if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
209  # Check to ensure that all the requested default constructors were added.
210  checkChangeLog <(sort -u "${DEFAULT_CONSTRUCTORS_FILE}" | grep -v '^#') "AddDefaultConstructor" \
211      "Default constructors were not added at the following locations from ${DEFAULT_CONSTRUCTORS_FILE}:"
212fi
213
214if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
215  # Check to ensure that all the requested annotations were added.
216  checkChangeLog <(sort -u "${CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
217      "CorePlatformApi annotations were not added at the following locations from ${CORE_PLATFORM_API_FILE}:"
218fi
219
220if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
221  # Check to ensure that all the requested annotations were added.
222  checkChangeLog <(sort -u "${INTRA_CORE_API_FILE}" | grep -v '^#') "@libcore.api.IntraCoreApi" \
223      "IntraCoreApi annotations were not added at the following locations from ${INTRA_CORE_API_FILE}:"
224fi
225
226if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
227  # Check to ensure that all the requested annotations were added.
228  checkChangeLog <(grep @location "${UNSUPPORTED_APP_USAGE_FILE}" | grep -vE "[[:space:]]*//" | cut -f4 -d\" | sort -u) \
229      "@android.compat.annotation.UnsupportedAppUsage" \
230      "UnsupportedAppUsage annotations were not added at the following locations from ${UNSUPPORTED_APP_USAGE_FILE}:"
231fi
232
233if [[ $ERROR = 1 ]]; then
234  echo "Errors found during transformation, see above.\n" >&2
235  exit 1
236fi
237