• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
99cd ${ANDROID_BUILD_TOP}
100build/soong/soong_ui.bash --make-mode currysrc
101
102if [[ -z "${SRCGEN_DIR}" ]]; then
103  SRCGEN_DIR=${PROJECT_DIR}/srcgen
104fi
105
106DEFAULT_CONSTRUCTORS_FILE=${SRCGEN_DIR}/default-constructors.txt
107CORE_PLATFORM_API_FILE=${SRCGEN_DIR}/core-platform-api.txt
108STABLE_CORE_PLATFORM_API_FILE=${SRCGEN_DIR}/stable-core-platform-api.txt
109INTRA_CORE_API_FILE=${SRCGEN_DIR}/intra-core-api.txt
110UNSUPPORTED_APP_USAGE_FILE=${SRCGEN_DIR}/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
178if [[ -z "${REPACKAGED_DIR}" ]]; then
179  REPACKAGED_DIR=${PROJECT_DIR}/repackaged
180fi
181
182for i in ${MODULE_DIRS}
183do
184  MODULE_DIR=${PROJECT_DIR}/${i}
185  if [[ ! -d ${MODULE_DIR} ]]; then
186    echo "Module directory ${MODULE_DIR} does not exist" >&2
187    exit 1;
188  fi
189
190  for s in ${SOURCE_DIRS}
191  do
192    IN=${MODULE_DIR}/${s}
193    if [[ -d ${IN} ]]; then
194      OUT=${REPACKAGED_DIR}/${i}/${s}
195      do_transform ${IN} ${OUT}
196    fi
197  done
198done
199
200# Check to ensure that the entries in the change log are correct
201typeset -i ERROR=0
202function checkChangeLog {
203  local IN="$1"
204  local TAG="$2"
205  local MSG="$3"
206  DIFF=$(comm -23 "${IN}" <(grep -P "^\Q$TAG\E:" ${CHANGE_LOG} | cut -f2- -d: | sort -u))
207  if [[ -n "${DIFF}" ]]; then
208    ERROR=1
209    echo -e "\nERROR: ${MSG}" >&2
210    for i in ${DIFF}
211    do
212      echo "  $i" >&2
213    done
214    echo >&2
215  fi
216}
217
218if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
219  # Check to ensure that all the requested default constructors were added.
220  checkChangeLog <(sort -u "${DEFAULT_CONSTRUCTORS_FILE}" | grep -v '^#') "AddDefaultConstructor" \
221      "Default constructors were not added at the following locations from ${DEFAULT_CONSTRUCTORS_FILE}:"
222fi
223
224if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
225  # Check to ensure that all the requested annotations were added.
226  checkChangeLog <(sort -u "${CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
227      "CorePlatformApi annotations were not added at the following locations from ${CORE_PLATFORM_API_FILE}:"
228fi
229
230if [[ -f "${STABLE_CORE_PLATFORM_API_FILE}" ]]; then
231  # Check to ensure that all the requested annotations were added.
232  checkChangeLog <(sort -u "${STABLE_CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
233      "CorePlatformApi annotations were not added at the following locations from ${STABLE_CORE_PLATFORM_API_FILE}:"
234fi
235
236if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
237  # Check to ensure that all the requested annotations were added.
238  checkChangeLog <(sort -u "${INTRA_CORE_API_FILE}" | grep -v '^#') "@libcore.api.IntraCoreApi" \
239      "IntraCoreApi annotations were not added at the following locations from ${INTRA_CORE_API_FILE}:"
240fi
241
242if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
243  # Check to ensure that all the requested annotations were added.
244  checkChangeLog <(grep @location "${UNSUPPORTED_APP_USAGE_FILE}" | grep -vE "[[:space:]]*//" | cut -f4 -d\" | sort -u) \
245      "@android.compat.annotation.UnsupportedAppUsage" \
246      "UnsupportedAppUsage annotations were not added at the following locations from ${UNSUPPORTED_APP_USAGE_FILE}:"
247fi
248
249if [[ $ERROR = 1 ]]; then
250  echo "Errors found during transformation, see above.\n" >&2
251  exit 1
252fi
253