1#!/bin/bash 2 3# Copyright (C) 2015 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# A script for generating the source code of the subset of ICU used by Android in libcore. 18 19# Flag for applying java doc patches or not 20APPLY_DOC_PATCH=1 21 22# Build Options used by Android.bp 23while true; do 24 case "$1" in 25 --no-doc-patch ) APPLY_DOC_PATCH=0; shift ;; 26 --srcgen-tool ) SRCGEN_TOOL="$2"; shift 2;; 27 --gen ) GEN_DIR="$2"; shift 2 ;; 28 -- ) shift; break ;; 29 * ) break ;; 30 esac 31done 32 33if [ -n "$SRCGEN_TOOL" ]; then 34 source $(dirname $BASH_SOURCE)/common.sh --do-not-make 35 SRCGEN_TOOL_BINARY=${SRCGEN_TOOL} 36else 37 source $(dirname $BASH_SOURCE)/common.sh 38 SRCGEN_TOOL_BINARY=${ANDROID_HOST_OUT}/bin/android_icu4j_srcgen_binary 39fi 40 41if [ -n "$GEN_DIR" ]; then 42 ANDROID_ICU4J_DIR=${GEN_DIR}/android_icu4j 43 mkdir -p ${ANDROID_ICU4J_DIR} 44fi 45 46WHITELIST_API_FILE=${ICU_SRCGEN_DIR}/whitelisted-public-api.txt 47CORE_PLATFORM_API_FILE=${ICU_SRCGEN_DIR}/core-platform-api.txt 48UNSUPPORTED_APP_USAGE_FILE=${ICU_SRCGEN_DIR}/unsupported-app-usage.json 49 50# Clean out previous generated code / resources. 51DEST_SRC_DIR=${ANDROID_ICU4J_DIR}/src/main/java 52rm -rf ${DEST_SRC_DIR} 53mkdir -p ${DEST_SRC_DIR} 54 55DEST_RESOURCE_DIR=${ANDROID_ICU4J_DIR}/resources 56rm -rf ${DEST_RESOURCE_DIR} 57mkdir -p ${DEST_RESOURCE_DIR} 58 59# Generate the source code needed by Android. 60# Branches used for testing new versions of ICU will have have the ${WHITELIST_API_FILE} file 61# that prevents new (stable) APIs being added to the Android public SDK API. The file should 62# not exist on "normal" release branches and master. 63ICU4J_BASE_COMMAND="${SRCGEN_TOOL_BINARY} Icu4jTransform" 64if [ -e "${WHITELIST_API_FILE}" ]; then 65 ICU4J_BASE_COMMAND+=" --hide-non-whitelisted-api ${WHITELIST_API_FILE}" 66fi 67${ICU4J_BASE_COMMAND} ${INPUT_DIRS} ${DEST_SRC_DIR} ${CORE_PLATFORM_API_FILE} ${UNSUPPORTED_APP_USAGE_FILE} 68 69# Copy / transform the resources needed by the android_icu4j code. 70for INPUT_DIR in ${INPUT_DIRS}; do 71 RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|\/package\.html|\/ICUConfig\.properties)' || true ) 72 for RESOURCE in ${RESOURCES}; do 73 SOURCE_DIR=$(dirname ${RESOURCE}) 74 RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,") 75 RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,') 76 DEST_DIR=${DEST_RESOURCE_DIR}/${RELATIVE_DEST_DIR} 77 mkdir -p ${DEST_DIR} 78 cp $RESOURCE ${DEST_DIR} 79 done 80done 81 82# Create the ICUConfig.properties for Android. 83mkdir -p ${ANDROID_ICU4J_DIR}/resources/android/icu 84sed 's,com.ibm.icu,android.icu,' ${ANDROID_BUILD_TOP}/external/icu/icu4j/main/classes/core/src/com/ibm/icu/ICUConfig.properties > ${ANDROID_ICU4J_DIR}/resources/android/icu/ICUConfig.properties 85 86# Clean out previous generated sample code. 87SAMPLE_DEST_DIR=${ANDROID_ICU4J_DIR}/src/samples/java 88rm -rf ${SAMPLE_DEST_DIR} 89mkdir -p ${SAMPLE_DEST_DIR} 90 91echo Processing sample code 92# Create the android_icu4j sample code 93${SRCGEN_TOOL_BINARY} Icu4jBasicTransform ${SAMPLE_INPUT_FILES} ${SAMPLE_DEST_DIR} 94 95# Clean out previous generated test code. 96TEST_DEST_DIR=${ANDROID_ICU4J_DIR}/src/main/tests 97rm -rf ${TEST_DEST_DIR} 98mkdir -p ${TEST_DEST_DIR} 99 100# Create a temporary directory into which the testdata.jar can be unzipped. It must be called src 101# as that is what is used to determine the root of the directory containing all the files to 102# copy and that is used to calculate the relative path to the file that is used for its output path. 103echo Unpacking testdata.jar 104TESTDATA_DIR=$(mktemp -d)/src 105mkdir -p ${TESTDATA_DIR} 106unzip ${ICU4J_DIR}/main/shared/data/testdata.jar com/ibm/icu/* -d ${TESTDATA_DIR} 107 108echo Processing test code 109# Create the android_icu4j test code 110ALL_TEST_INPUT_DIRS="${TEST_INPUT_DIRS} ${TESTDATA_DIR}" 111${SRCGEN_TOOL_BINARY} Icu4jTestsTransform ${ALL_TEST_INPUT_DIRS} ${TEST_DEST_DIR} 112# Apply line-based javadoc patches 113if [ "$APPLY_DOC_PATCH" -eq "1" ]; then 114 ${ANDROID_BUILD_TOP}/external/icu/tools/srcgen/javadoc_patches/apply_patches.sh 115fi 116 117# Copy the data files. 118echo Copying test data 119for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do 120 RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|com\.ibm\.icu.*\.dat|/package\.html)' || true ) 121 for RESOURCE in ${RESOURCES}; do 122 SOURCE_DIR=$(dirname ${RESOURCE}) 123 RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,") 124 RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,') 125 DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR} 126 mkdir -p ${DEST_DIR} 127 cp $RESOURCE ${DEST_DIR} 128 done 129done 130 131echo Repackaging serialized test data 132# Excludes JavaTimeZone.dat files as they depend on sun.util.calendar.ZoneInfo 133for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do 134 RESOURCES=$(find ${INPUT_DIR} -type f | egrep '(/com\.ibm\.icu.*\.dat)' | egrep -v "JavaTimeZone.dat" || true ) 135 for RESOURCE in ${RESOURCES}; do 136 SOURCE_DIR=$(dirname ${RESOURCE}) 137 RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,") 138 RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,') 139 SOURCE_NAME=$(basename ${RESOURCE}) 140 DEST_NAME=${SOURCE_NAME/com.ibm/android} 141 DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR} 142 mkdir -p ${DEST_DIR} 143 # A simple textual substitution works even though the file is binary as 'com.ibm' and 'android' 144 # are the same length. 145 sed 's|com[./]ibm|android|g' $RESOURCE > ${DEST_DIR}/${DEST_NAME} 146 done 147done 148