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
108STABLE_CORE_PLATFORM_API_FILE=${PROJECT_DIR}/srcgen/stable-core-platform-api.txt
109INTRA_CORE_API_FILE=${PROJECT_DIR}/srcgen/intra-core-api.txt
110UNSUPPORTED_APP_USAGE_FILE=${PROJECT_DIR}/srcgen/unsupported-app-usage.json
111
112TAB_SIZE=${TAB_SIZE-4}
113
114REPACKAGE_ARGS=""
115SEP=""
116for i in ${PACKAGE_TRANSFORMATIONS}
117do
118  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--package-transformation ${i}"
119  SEP=" "
120done
121
122if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
123  echo "Adding default constructors from ${DEFAULT_CONSTRUCTORS_FILE}"
124  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--default-constructors-file ${DEFAULT_CONSTRUCTORS_FILE}"
125  SEP=" "
126fi
127
128if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
129  echo "Adding CorePlatformApi annotations from ${CORE_PLATFORM_API_FILE}"
130  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--core-platform-api-file ${CORE_PLATFORM_API_FILE}"
131  SEP=" "
132fi
133
134if [[ -f "${STABLE_CORE_PLATFORM_API_FILE}" ]]; then
135  echo "Adding CorePlatformApi(status=STABLE) annotations from ${STABLE_CORE_PLATFORM_API_FILE}"
136  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--stable-core-platform-api-file ${STABLE_CORE_PLATFORM_API_FILE}"
137  SEP=" "
138fi
139
140if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
141  echo "Adding IntraCoreApi annotations from ${INTRA_CORE_API_FILE}"
142  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--intra-core-api-file ${INTRA_CORE_API_FILE}"
143  SEP=" "
144fi
145
146if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
147  echo "Adding UnsupportedAppUsage annotations from ${UNSUPPORTED_APP_USAGE_FILE}"
148  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--unsupported-app-usage-file ${UNSUPPORTED_APP_USAGE_FILE}"
149  SEP=" "
150  if [[ -n "${UNSUPPORTED_APP_USAGE_CLASS}" ]]; then
151    REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--unsupported-app-usage-class ${UNSUPPORTED_APP_USAGE_CLASS}"
152  fi
153fi
154
155if [[ -n "${TAB_SIZE}" ]]; then
156  echo "Using tab size of ${TAB_SIZE}"
157  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--tab-size ${TAB_SIZE}"
158  SEP=" "
159fi
160
161function do_transform() {
162  local SRC_IN_DIR=$1
163  local SRC_OUT_DIR=$2
164
165  rm -rf ${SRC_OUT_DIR}
166  mkdir -p ${SRC_OUT_DIR}
167
168  java -cp ${CLASSPATH} com.google.currysrc.aosp.RepackagingTransform \
169       --source-dir ${SRC_IN_DIR} \
170       --target-dir ${SRC_OUT_DIR} \
171       --change-log ${CHANGE_LOG} \
172       ${REPACKAGE_ARGS}
173
174  # Restore TEST_MAPPING files that may have been removed from the source directory
175  (cd $SRC_OUT_DIR; git checkout HEAD $(git status --short | grep -E "^ D .*/TEST_MAPPING$" | cut -c4-))
176}
177
178REPACKAGED_DIR=${PROJECT_DIR}/repackaged
179for i in ${MODULE_DIRS}
180do
181  MODULE_DIR=${PROJECT_DIR}/${i}
182  if [[ ! -d ${MODULE_DIR} ]]; then
183    echo "Module directory ${MODULE_DIR} does not exist" >&2
184    exit 1;
185  fi
186
187  for s in ${SOURCE_DIRS}
188  do
189    IN=${MODULE_DIR}/${s}
190    if [[ -d ${IN} ]]; then
191      OUT=${REPACKAGED_DIR}/${i}/${s}
192      do_transform ${IN} ${OUT}
193    fi
194  done
195done
196
197# Check to ensure that the entries in the change log are correct
198typeset -i ERROR=0
199function checkChangeLog {
200  local IN="$1"
201  local TAG="$2"
202  local MSG="$3"
203  DIFF=$(comm -23 "${IN}" <(grep -P "^\Q$TAG\E:" ${CHANGE_LOG} | cut -f2- -d: | sort -u))
204  if [[ -n "${DIFF}" ]]; then
205    ERROR=1
206    echo -e "\nERROR: ${MSG}" >&2
207    for i in ${DIFF}
208    do
209      echo "  $i" >&2
210    done
211    echo >&2
212  fi
213}
214
215if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
216  # Check to ensure that all the requested default constructors were added.
217  checkChangeLog <(sort -u "${DEFAULT_CONSTRUCTORS_FILE}" | grep -v '^#') "AddDefaultConstructor" \
218      "Default constructors were not added at the following locations from ${DEFAULT_CONSTRUCTORS_FILE}:"
219fi
220
221if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
222  # Check to ensure that all the requested annotations were added.
223  checkChangeLog <(sort -u "${CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
224      "CorePlatformApi annotations were not added at the following locations from ${CORE_PLATFORM_API_FILE}:"
225fi
226
227if [[ -f "${STABLE_CORE_PLATFORM_API_FILE}" ]]; then
228  # Check to ensure that all the requested annotations were added.
229  checkChangeLog <(sort -u "${STABLE_CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
230      "CorePlatformApi annotations were not added at the following locations from ${STABLE_CORE_PLATFORM_API_FILE}:"
231fi
232
233if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
234  # Check to ensure that all the requested annotations were added.
235  checkChangeLog <(sort -u "${INTRA_CORE_API_FILE}" | grep -v '^#') "@libcore.api.IntraCoreApi" \
236      "IntraCoreApi annotations were not added at the following locations from ${INTRA_CORE_API_FILE}:"
237fi
238
239if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
240  # Check to ensure that all the requested annotations were added.
241  checkChangeLog <(grep @location "${UNSUPPORTED_APP_USAGE_FILE}" | grep -vE "[[:space:]]*//" | cut -f4 -d\" | sort -u) \
242      "@android.compat.annotation.UnsupportedAppUsage" \
243      "UnsupportedAppUsage annotations were not added at the following locations from ${UNSUPPORTED_APP_USAGE_FILE}:"
244fi
245
246if [[ $ERROR = 1 ]]; then
247  echo "Errors found during transformation, see above.\n" >&2
248  exit 1
249fi
250