summaryrefslogtreecommitdiff
path: root/scripts/repackage-common.sh
blob: a11fa5cecd86113a2663b6aa7cdc0937a37e240e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Common logic for use by repackaging scripts.
# The following environment variables must be set before including this script:
#
#   PROJECT_DIR
#        the root directory (relative to ${ANDROID_BUILD_TOP}) of the project within which the
#        repackaging is to be done. e.g. external/conscrypt
#
#   MODULE_DIRS
#        a space separated list of the module directories (relative to the PROJECT_DIR) whose
#        sources need repackaging. e.g. core common android
#
#   SOURCE_DIRS
#        a space separated list of the source directories (relative to the MODULE_DIRS) that are to
#        be repackaged. If the ${PROJECT_DIR}/${MODULE_DIR}/${SOURCE_DIR} does not exist then it is
#        ignored. e.g. src/main/java src/main/test
#
#   PACKAGE_TRANSFORMATIONS
#        a space separated list of the package transformations to apply. Must be in the form
#        <old package prefix>:<new package prefix>.
#
#   UNSUPPORTED_APP_USAGE_CLASS
#        the fully qualified path to the UnsupportedAppUsage annotation to insert.
#
# The following environment variables are optional.
#
#   TAB_SIZE
#        the tab size for formatting any inserted code, e.g. annotations. Defaults to 4.
#
# The following environment variables can be used after including this file:
#   REPACKAGED_DIR
#        the absolute path to the directory into which the repackaged source has been written.
#
# This should be used as follows:
#
#if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
#    echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" >&2
#    exit 1
#fi
#
# PROJECT_DIR=...
# MODULE_DIRS=...
# SOURCE_DIRS=...
# PACKAGE_TRANSFORMATIONS=...
# source ${ANDROID_BUILD_TOP}/tools/currysrc/scripts/repackage-common.sh
# ...any post transformation changes, e.g. to remove unnecessary files.

if [[ -z "${ANDROID_BUILD_TOP}" ]]; then
    echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" >&2
    exit 1
fi

if [[ -z "${PROJECT_DIR}" ]]; then
  echo "PROJECT_DIR is not set" >&2
  exit 1
fi

PROJECT_DIR=${ANDROID_BUILD_TOP}/${PROJECT_DIR}

if [[ ! -d "${PROJECT_DIR}" ]]; then
  echo "${PROJECT_DIR} does not exist" >&2
  exit 1
fi

if [[ -z "${MODULE_DIRS}" ]]; then
  echo "MODULE_DIRS is not set" >&2
  exit 1
fi

if [[ -z "${SOURCE_DIRS}" ]]; then
  echo "SOURCE_DIRS is not set" >&2
  exit 1
fi

if [[ -z "${PACKAGE_TRANSFORMATIONS}" ]]; then
  echo "PACKAGE_TRANSFORMATIONS is not set" >&2
  exit 1
fi

set -e

CLASSPATH=${ANDROID_HOST_OUT}/framework/currysrc.jar
CHANGE_LOG=$(mktemp --suffix srcgen-change.log)

cd ${ANDROID_BUILD_TOP}
build/soong/soong_ui.bash --make-mode currysrc

if [[ -z "${SRCGEN_DIR}" ]]; then
  SRCGEN_DIR=${PROJECT_DIR}/srcgen
fi

DEFAULT_CONSTRUCTORS_FILE=${SRCGEN_DIR}/default-constructors.txt
CORE_PLATFORM_API_FILE=${SRCGEN_DIR}/core-platform-api.txt
STABLE_CORE_PLATFORM_API_FILE=${SRCGEN_DIR}/stable-core-platform-api.txt
INTRA_CORE_API_FILE=${SRCGEN_DIR}/intra-core-api.txt
UNSUPPORTED_APP_USAGE_FILE=${SRCGEN_DIR}/unsupported-app-usage.json

TAB_SIZE=${TAB_SIZE-4}

REPACKAGE_ARGS=""
SEP=""
for i in ${PACKAGE_TRANSFORMATIONS}
do
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--package-transformation ${i}"
  SEP=" "
done

if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
  echo "Adding default constructors from ${DEFAULT_CONSTRUCTORS_FILE}"
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--default-constructors-file ${DEFAULT_CONSTRUCTORS_FILE}"
  SEP=" "
fi

if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
  echo "Adding CorePlatformApi annotations from ${CORE_PLATFORM_API_FILE}"
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--core-platform-api-file ${CORE_PLATFORM_API_FILE}"
  SEP=" "
fi

if [[ -f "${STABLE_CORE_PLATFORM_API_FILE}" ]]; then
  echo "Adding CorePlatformApi(status=STABLE) annotations from ${STABLE_CORE_PLATFORM_API_FILE}"
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--stable-core-platform-api-file ${STABLE_CORE_PLATFORM_API_FILE}"
  SEP=" "
fi

if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
  echo "Adding IntraCoreApi annotations from ${INTRA_CORE_API_FILE}"
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--intra-core-api-file ${INTRA_CORE_API_FILE}"
  SEP=" "
fi

if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
  echo "Adding UnsupportedAppUsage annotations from ${UNSUPPORTED_APP_USAGE_FILE}"
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--unsupported-app-usage-file ${UNSUPPORTED_APP_USAGE_FILE}"
  SEP=" "
  if [[ -n "${UNSUPPORTED_APP_USAGE_CLASS}" ]]; then
    REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--unsupported-app-usage-class ${UNSUPPORTED_APP_USAGE_CLASS}"
  fi
fi

if [[ -n "${TAB_SIZE}" ]]; then
  echo "Using tab size of ${TAB_SIZE}"
  REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--tab-size ${TAB_SIZE}"
  SEP=" "
fi

function do_transform() {
  local SRC_IN_DIR=$1
  local SRC_OUT_DIR=$2

  rm -rf ${SRC_OUT_DIR}
  mkdir -p ${SRC_OUT_DIR}

  java -cp ${CLASSPATH} com.google.currysrc.aosp.RepackagingTransform \
       --source-dir ${SRC_IN_DIR} \
       --target-dir ${SRC_OUT_DIR} \
       --change-log ${CHANGE_LOG} \
       ${REPACKAGE_ARGS}

  # Restore TEST_MAPPING files that may have been removed from the source directory
  (cd $SRC_OUT_DIR; git checkout HEAD $(git status --short | grep -E "^ D .*/TEST_MAPPING$" | cut -c4-))
}

if [[ -z "${REPACKAGED_DIR}" ]]; then
  REPACKAGED_DIR=${PROJECT_DIR}/repackaged
fi

for i in ${MODULE_DIRS}
do
  MODULE_DIR=${PROJECT_DIR}/${i}
  if [[ ! -d ${MODULE_DIR} ]]; then
    echo "Module directory ${MODULE_DIR} does not exist" >&2
    exit 1;
  fi

  for s in ${SOURCE_DIRS}
  do
    IN=${MODULE_DIR}/${s}
    if [[ -d ${IN} ]]; then
      OUT=${REPACKAGED_DIR}/${i}/${s}
      do_transform ${IN} ${OUT}
    fi
  done
done

# Check to ensure that the entries in the change log are correct
typeset -i ERROR=0
function checkChangeLog {
  local IN="$1"
  local TAG="$2"
  local MSG="$3"
  DIFF=$(comm -23 "${IN}" <(grep -P "^\Q$TAG\E:" ${CHANGE_LOG} | cut -f2- -d: | sort -u))
  if [[ -n "${DIFF}" ]]; then
    ERROR=1
    echo -e "\nERROR: ${MSG}" >&2
    for i in ${DIFF}
    do
      echo "  $i" >&2
    done
    echo >&2
  fi
}

if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then
  # Check to ensure that all the requested default constructors were added.
  checkChangeLog <(sort -u "${DEFAULT_CONSTRUCTORS_FILE}" | grep -v '^#') "AddDefaultConstructor" \
      "Default constructors were not added at the following locations from ${DEFAULT_CONSTRUCTORS_FILE}:"
fi

if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then
  # Check to ensure that all the requested annotations were added.
  checkChangeLog <(sort -u "${CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
      "CorePlatformApi annotations were not added at the following locations from ${CORE_PLATFORM_API_FILE}:"
fi

if [[ -f "${STABLE_CORE_PLATFORM_API_FILE}" ]]; then
  # Check to ensure that all the requested annotations were added.
  checkChangeLog <(sort -u "${STABLE_CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \
      "CorePlatformApi annotations were not added at the following locations from ${STABLE_CORE_PLATFORM_API_FILE}:"
fi

if [[ -f "${INTRA_CORE_API_FILE}" ]]; then
  # Check to ensure that all the requested annotations were added.
  checkChangeLog <(sort -u "${INTRA_CORE_API_FILE}" | grep -v '^#') "@libcore.api.IntraCoreApi" \
      "IntraCoreApi annotations were not added at the following locations from ${INTRA_CORE_API_FILE}:"
fi

if [[ -f "${UNSUPPORTED_APP_USAGE_FILE}" ]]; then
  # Check to ensure that all the requested annotations were added.
  checkChangeLog <(grep @location "${UNSUPPORTED_APP_USAGE_FILE}" | grep -vE "[[:space:]]*//" | cut -f4 -d\" | sort -u) \
      "@android.compat.annotation.UnsupportedAppUsage" \
      "UnsupportedAppUsage annotations were not added at the following locations from ${UNSUPPORTED_APP_USAGE_FILE}:"
fi

if [[ $ERROR = 1 ]]; then
  echo "Errors found during transformation, see above.\n" >&2
  exit 1
fi