Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 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 | |
| 62 | if [[ -z "${ANDROID_BUILD_TOP}" ]]; then |
| 63 | echo "Missing environment variables. Did you run build/envsetup.sh and lunch?" >&2 |
| 64 | exit 1 |
| 65 | fi |
| 66 | |
| 67 | if [[ -z "${PROJECT_DIR}" ]]; then |
| 68 | echo "PROJECT_DIR is not set" >&2 |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | PROJECT_DIR=${ANDROID_BUILD_TOP}/${PROJECT_DIR} |
| 73 | |
| 74 | if [[ ! -d "${PROJECT_DIR}" ]]; then |
| 75 | echo "${PROJECT_DIR} does not exist" >&2 |
| 76 | exit 1 |
| 77 | fi |
| 78 | |
| 79 | if [[ -z "${MODULE_DIRS}" ]]; then |
| 80 | echo "MODULE_DIRS is not set" >&2 |
| 81 | exit 1 |
| 82 | fi |
| 83 | |
| 84 | if [[ -z "${SOURCE_DIRS}" ]]; then |
| 85 | echo "SOURCE_DIRS is not set" >&2 |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
| 89 | if [[ -z "${PACKAGE_TRANSFORMATIONS}" ]]; then |
| 90 | echo "PACKAGE_TRANSFORMATIONS is not set" >&2 |
| 91 | exit 1 |
| 92 | fi |
| 93 | |
| 94 | set -e |
| 95 | |
| 96 | CLASSPATH=${ANDROID_HOST_OUT}/framework/currysrc.jar |
| 97 | CHANGE_LOG=$(mktemp --suffix srcgen-change.log) |
| 98 | |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 99 | cd ${ANDROID_BUILD_TOP} |
Cody Kesting | c091e06 | 2019-09-16 18:27:30 -0700 | [diff] [blame] | 100 | build/soong/soong_ui.bash --make-mode currysrc |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 101 | |
Daulet Zhanguzin | a9b025d | 2021-01-20 18:32:18 +0000 | [diff] [blame] | 102 | if [[ -z "${SRCGEN_DIR}" ]]; then |
| 103 | SRCGEN_DIR=${PROJECT_DIR}/srcgen |
| 104 | fi |
| 105 | |
| 106 | DEFAULT_CONSTRUCTORS_FILE=${SRCGEN_DIR}/default-constructors.txt |
| 107 | CORE_PLATFORM_API_FILE=${SRCGEN_DIR}/core-platform-api.txt |
| 108 | STABLE_CORE_PLATFORM_API_FILE=${SRCGEN_DIR}/stable-core-platform-api.txt |
| 109 | INTRA_CORE_API_FILE=${SRCGEN_DIR}/intra-core-api.txt |
| 110 | UNSUPPORTED_APP_USAGE_FILE=${SRCGEN_DIR}/unsupported-app-usage.json |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 111 | |
| 112 | TAB_SIZE=${TAB_SIZE-4} |
| 113 | |
| 114 | REPACKAGE_ARGS="" |
| 115 | SEP="" |
| 116 | for i in ${PACKAGE_TRANSFORMATIONS} |
| 117 | do |
| 118 | REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--package-transformation ${i}" |
| 119 | SEP=" " |
| 120 | done |
| 121 | |
| 122 | if [[ -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=" " |
| 126 | fi |
| 127 | |
| 128 | if [[ -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=" " |
| 132 | fi |
| 133 | |
Pete Gillin | 7074dfc | 2020-06-12 13:10:19 +0100 | [diff] [blame] | 134 | if [[ -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=" " |
| 138 | fi |
| 139 | |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 140 | if [[ -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=" " |
| 144 | fi |
| 145 | |
| 146 | if [[ -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 |
| 153 | fi |
| 154 | |
| 155 | if [[ -n "${TAB_SIZE}" ]]; then |
| 156 | echo "Using tab size of ${TAB_SIZE}" |
| 157 | REPACKAGE_ARGS="${REPACKAGE_ARGS}${SEP}--tab-size ${TAB_SIZE}" |
| 158 | SEP=" " |
| 159 | fi |
| 160 | |
| 161 | function 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} |
Paul Duffin | 51931e2 | 2019-09-19 15:32:40 +0100 | [diff] [blame] | 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-)) |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Daulet Zhanguzin | a9b025d | 2021-01-20 18:32:18 +0000 | [diff] [blame] | 178 | if [[ -z "${REPACKAGED_DIR}" ]]; then |
| 179 | REPACKAGED_DIR=${PROJECT_DIR}/repackaged |
| 180 | fi |
| 181 | |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 182 | for i in ${MODULE_DIRS} |
| 183 | do |
| 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 |
| 198 | done |
| 199 | |
| 200 | # Check to ensure that the entries in the change log are correct |
| 201 | typeset -i ERROR=0 |
| 202 | function 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 | |
| 218 | if [[ -f "${DEFAULT_CONSTRUCTORS_FILE}" ]]; then |
| 219 | # Check to ensure that all the requested default constructors were added. |
Adam Vartanian | f42e4e5 | 2019-04-10 11:06:57 +0100 | [diff] [blame] | 220 | checkChangeLog <(sort -u "${DEFAULT_CONSTRUCTORS_FILE}" | grep -v '^#') "AddDefaultConstructor" \ |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 221 | "Default constructors were not added at the following locations from ${DEFAULT_CONSTRUCTORS_FILE}:" |
| 222 | fi |
| 223 | |
| 224 | if [[ -f "${CORE_PLATFORM_API_FILE}" ]]; then |
| 225 | # Check to ensure that all the requested annotations were added. |
Adam Vartanian | f42e4e5 | 2019-04-10 11:06:57 +0100 | [diff] [blame] | 226 | checkChangeLog <(sort -u "${CORE_PLATFORM_API_FILE}" | grep -v '^#') "@libcore.api.CorePlatformApi" \ |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 227 | "CorePlatformApi annotations were not added at the following locations from ${CORE_PLATFORM_API_FILE}:" |
| 228 | fi |
| 229 | |
Pete Gillin | 7074dfc | 2020-06-12 13:10:19 +0100 | [diff] [blame] | 230 | if [[ -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}:" |
| 234 | fi |
| 235 | |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 236 | if [[ -f "${INTRA_CORE_API_FILE}" ]]; then |
| 237 | # Check to ensure that all the requested annotations were added. |
Adam Vartanian | f42e4e5 | 2019-04-10 11:06:57 +0100 | [diff] [blame] | 238 | checkChangeLog <(sort -u "${INTRA_CORE_API_FILE}" | grep -v '^#') "@libcore.api.IntraCoreApi" \ |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 239 | "IntraCoreApi annotations were not added at the following locations from ${INTRA_CORE_API_FILE}:" |
| 240 | fi |
| 241 | |
| 242 | if [[ -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) \ |
Artur Satayev | 0b1b0c9 | 2019-12-10 12:56:41 +0000 | [diff] [blame] | 245 | "@android.compat.annotation.UnsupportedAppUsage" \ |
Paul Duffin | 7a9cffc | 2019-03-05 15:18:12 +0000 | [diff] [blame] | 246 | "UnsupportedAppUsage annotations were not added at the following locations from ${UNSUPPORTED_APP_USAGE_FILE}:" |
| 247 | fi |
| 248 | |
| 249 | if [[ $ERROR = 1 ]]; then |
| 250 | echo "Errors found during transformation, see above.\n" >&2 |
| 251 | exit 1 |
| 252 | fi |