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 -JXint" 39fi 40 41if [ -n "$GEN_DIR" ]; then 42 ANDROID_ICU4J_DIR=${GEN_DIR}/android_icu4j 43 mkdir -p ${ANDROID_ICU4J_DIR} 44fi 45 46ALLOWLIST_API_FILE=${ICU_SRCGEN_DIR}/allowlisted-public-api.txt 47CORE_PLATFORM_API_FILE=${ICU_SRCGEN_DIR}/core-platform-api.txt 48INTRA_CORE_API_FILE=${ICU_SRCGEN_DIR}/intra-core-api.txt 49UNSUPPORTED_APP_USAGE_FILE=${ICU_SRCGEN_DIR}/unsupported-app-usage.json 50 51# Clean out previous generated code / resources. 52DEST_SRC_DIR=${ANDROID_ICU4J_DIR}/src/main/java 53rm -rf ${DEST_SRC_DIR} 54mkdir -p ${DEST_SRC_DIR} 55 56DEST_RESOURCE_DIR=${ANDROID_ICU4J_DIR}/resources 57rm -rf ${DEST_RESOURCE_DIR} 58mkdir -p ${DEST_RESOURCE_DIR} 59 60# Generate the source code needed by Android. 61# Branches used for testing new versions of ICU will have have the ${ALLOWLIST_API_FILE} file 62# that prevents new (stable) APIs being added to the Android public SDK API. The file should 63# not exist on "normal" release branches and master. 64ICU4J_BASE_COMMAND="${SRCGEN_TOOL_BINARY} Icu4jTransform" 65if [ -e "${ALLOWLIST_API_FILE}" ]; then 66 ICU4J_BASE_COMMAND+=" --hide-non-allowlisted-api ${ALLOWLIST_API_FILE}" 67fi 68${ICU4J_BASE_COMMAND} ${INPUT_DIRS} ${DEST_SRC_DIR} ${CORE_PLATFORM_API_FILE} ${INTRA_CORE_API_FILE} ${UNSUPPORTED_APP_USAGE_FILE} 69 70# Copy / transform the resources needed by the android_icu4j code. 71for INPUT_DIR in ${INPUT_DIRS}; do 72 RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|\/package\.html|\/ICUConfig\.properties)' || true ) 73 for RESOURCE in ${RESOURCES}; do 74 SOURCE_DIR=$(dirname ${RESOURCE}) 75 RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,") 76 RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,') 77 DEST_DIR=${DEST_RESOURCE_DIR}/${RELATIVE_DEST_DIR} 78 mkdir -p ${DEST_DIR} 79 cp $RESOURCE ${DEST_DIR} 80 done 81done 82 83# Create the ICUConfig.properties for Android. 84mkdir -p ${ANDROID_ICU4J_DIR}/resources/android/icu 85sed '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 86 87# Clean out previous generated sample code. 88SAMPLE_DEST_DIR=${ANDROID_ICU4J_DIR}/src/samples/java 89rm -rf ${SAMPLE_DEST_DIR} 90mkdir -p ${SAMPLE_DEST_DIR} 91 92echo Processing sample code 93# Create the android_icu4j sample code 94${SRCGEN_TOOL_BINARY} Icu4jBasicTransform ${SAMPLE_INPUT_FILES} ${SAMPLE_DEST_DIR} 95 96# Clean out previous generated test code. 97TEST_DEST_DIR=${ANDROID_ICU4J_DIR}/src/main/tests 98rm -rf ${TEST_DEST_DIR} 99mkdir -p ${TEST_DEST_DIR} 100 101# Create a temporary directory into which the testdata.jar can be unzipped. It must be called src 102# as that is what is used to determine the root of the directory containing all the files to 103# copy and that is used to calculate the relative path to the file that is used for its output path. 104echo Unpacking testdata.jar 105TESTDATA_DIR=$(mktemp -d)/src 106mkdir -p ${TESTDATA_DIR} 107unzip ${ICU4J_DIR}/main/shared/data/testdata.jar com/ibm/icu/* -d ${TESTDATA_DIR} 108 109echo Processing test code 110# Create the android_icu4j test code 111ALL_TEST_INPUT_DIRS="${TEST_INPUT_DIRS} ${TESTDATA_DIR}" 112${SRCGEN_TOOL_BINARY} Icu4jTestsTransform ${ALL_TEST_INPUT_DIRS} ${TEST_DEST_DIR} 113# Apply line-based javadoc patches 114if [ "$APPLY_DOC_PATCH" -eq "1" ]; then 115 ${ANDROID_BUILD_TOP}/external/icu/tools/srcgen/javadoc_patches/apply_patches.sh 116fi 117 118# Copy the data files. 119echo Copying test data 120for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do 121 RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|com\.ibm\.icu.*\.dat|/package\.html)' || true ) 122 for RESOURCE in ${RESOURCES}; do 123 SOURCE_DIR=$(dirname ${RESOURCE}) 124 RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,") 125 RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,') 126 DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR} 127 mkdir -p ${DEST_DIR} 128 cp $RESOURCE ${DEST_DIR} 129 done 130done 131 132echo Repackaging serialized test data 133# Excludes JavaTimeZone.dat files as they depend on sun.util.calendar.ZoneInfo 134for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do 135 RESOURCES=$(find ${INPUT_DIR} -type f | egrep '(/com\.ibm\.icu.*\.dat)' | egrep -v "JavaTimeZone.dat" || true ) 136 for RESOURCE in ${RESOURCES}; do 137 SOURCE_DIR=$(dirname ${RESOURCE}) 138 RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,") 139 RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,') 140 SOURCE_NAME=$(basename ${RESOURCE}) 141 DEST_NAME=${SOURCE_NAME/com.ibm/android} 142 DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR} 143 mkdir -p ${DEST_DIR} 144 # A simple textual substitution works even though the file is binary as 'com.ibm' and 'android' 145 # are the same length. 146 sed 's|com[./]ibm|android|g' $RESOURCE > ${DEST_DIR}/${DEST_NAME} 147 done 148done 149