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