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
17source $(dirname $BASH_SOURCE)/common.sh
18
19# A script for generating the source code of the subset of ICU used by Android in libcore.
20
21# Clean out previous generated code / resources.
22DEST_SRC_DIR=${ANDROID_ICU4J_DIR}/src/main/java
23rm -rf ${DEST_SRC_DIR}
24mkdir -p ${DEST_SRC_DIR}
25
26DEST_RESOURCE_DIR=${ANDROID_ICU4J_DIR}/resources
27rm -rf ${DEST_RESOURCE_DIR}
28mkdir -p ${DEST_RESOURCE_DIR}
29
30# Generate the source code needed by Android.
31java ${SRCGEN_JAVA_ARGS} -cp ${CLASSPATH} com.android.icu4j.srcgen.Icu4jTransform ${INPUT_DIRS} ${DEST_SRC_DIR}
32
33# Copy / transform the resources needed by the android_icu4j code.
34for INPUT_DIR in ${INPUT_DIRS}; do
35  RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|\/package\.html|\/ICUConfig\.properties)' || true )
36  for RESOURCE in ${RESOURCES}; do
37    SOURCE_DIR=$(dirname ${RESOURCE})
38    RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,")
39    RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,')
40    DEST_DIR=${DEST_RESOURCE_DIR}/${RELATIVE_DEST_DIR}
41    mkdir -p ${DEST_DIR}
42    cp $RESOURCE ${DEST_DIR}
43  done
44done
45
46# Create the ICUConfig.properties for Android.
47mkdir -p ${ANDROID_ICU4J_DIR}/resources/android/icu
48sed '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
49
50# Clean out previous generated sample code.
51SAMPLE_DEST_DIR=${ANDROID_ICU4J_DIR}/src/samples/java
52rm -rf ${SAMPLE_DEST_DIR}
53mkdir -p ${SAMPLE_DEST_DIR}
54
55echo Processing sample code
56# Create the android_icu4j sample code
57java ${SRCGEN_JAVA_ARGS} -cp ${CLASSPATH} com.android.icu4j.srcgen.Icu4jBasicTransform ${SAMPLE_INPUT_FILES} ${SAMPLE_DEST_DIR}
58
59# Clean out previous generated test code.
60TEST_DEST_DIR=${ANDROID_ICU4J_DIR}/src/main/tests
61rm -rf ${TEST_DEST_DIR}
62mkdir -p ${TEST_DEST_DIR}
63
64# Create a temporary directory into which the testdata.jar can be unzipped. It must be called src
65# as that is what is used to determine the root of the directory containing all the files to
66# copy and that is used to calculate the relative path to the file that is used for its output path.
67echo Unpacking testdata.jar
68TESTDATA_DIR=$(mktemp -d)/src
69mkdir -p ${TESTDATA_DIR}
70unzip ${ICU4J_DIR}/main/shared/data/testdata.jar com/ibm/icu/* -d ${TESTDATA_DIR}
71
72echo Processing test code
73# Create the android_icu4j test code
74ALL_TEST_INPUT_DIRS="${TEST_INPUT_DIRS} ${TESTDATA_DIR}"
75java ${SRCGEN_JAVA_ARGS} -cp ${CLASSPATH} com.android.icu4j.srcgen.Icu4jTestsTransform \
76  ${ALL_TEST_INPUT_DIRS} ${TEST_DEST_DIR}
77
78# Copy the data files.
79echo Copying test data
80for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do
81  RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|com\.ibm\.icu.*\.dat|/package\.html)' || true )
82  for RESOURCE in ${RESOURCES}; do
83    SOURCE_DIR=$(dirname ${RESOURCE})
84    RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,")
85    RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,')
86    DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR}
87    mkdir -p ${DEST_DIR}
88    cp $RESOURCE ${DEST_DIR}
89  done
90done
91
92echo Repackaging serialized test data
93# Excludes JavaTimeZone.dat files as they depend on sun.util.calendar.ZoneInfo
94for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do
95  RESOURCES=$(find ${INPUT_DIR} -type f | egrep '(/com\.ibm\.icu.*\.dat)' | egrep -v "JavaTimeZone.dat" || true )
96  for RESOURCE in ${RESOURCES}; do
97    SOURCE_DIR=$(dirname ${RESOURCE})
98    RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,")
99    RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,')
100    SOURCE_NAME=$(basename ${RESOURCE})
101    DEST_NAME=${SOURCE_NAME/com.ibm/android}
102    DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR}
103    mkdir -p ${DEST_DIR}
104    # A simple textual substitution works even though the file is binary as 'com.ibm' and 'android'
105    # are the same length.
106    sed 's|com[./]ibm|android|g' $RESOURCE > ${DEST_DIR}/${DEST_NAME}
107  done
108done
109