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.
31${SRCGEN_JAVA_BINARY} ${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
57${SRCGEN_JAVA_BINARY} ${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}"
75${SRCGEN_JAVA_BINARY} ${SRCGEN_JAVA_ARGS} -cp ${CLASSPATH} com.android.icu4j.srcgen.Icu4jTestsTransform \
76  ${ALL_TEST_INPUT_DIRS} ${TEST_DEST_DIR}
77
78# Apply line-based javadoc patches
79${ANDROID_BUILD_TOP}/external/icu/tools/srcgen/javadoc_patches/apply_patches.sh
80
81# Copy the data files.
82echo Copying test data
83for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do
84  RESOURCES=$(find ${INPUT_DIR} -type f | egrep -v '(\.java|com\.ibm\.icu.*\.dat|/package\.html)' || true )
85  for RESOURCE in ${RESOURCES}; do
86    SOURCE_DIR=$(dirname ${RESOURCE})
87    RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,")
88    RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,')
89    DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR}
90    mkdir -p ${DEST_DIR}
91    cp $RESOURCE ${DEST_DIR}
92  done
93done
94
95echo Repackaging serialized test data
96# Excludes JavaTimeZone.dat files as they depend on sun.util.calendar.ZoneInfo
97for INPUT_DIR in ${ALL_TEST_INPUT_DIRS}; do
98  RESOURCES=$(find ${INPUT_DIR} -type f | egrep '(/com\.ibm\.icu.*\.dat)' | egrep -v "JavaTimeZone.dat" || true )
99  for RESOURCE in ${RESOURCES}; do
100    SOURCE_DIR=$(dirname ${RESOURCE})
101    RELATIVE_SOURCE_DIR=$(echo ${SOURCE_DIR} | sed "s,${INPUT_DIR}/,,")
102    RELATIVE_DEST_DIR=$(echo ${RELATIVE_SOURCE_DIR} | sed 's,com/ibm/icu,android/icu,')
103    SOURCE_NAME=$(basename ${RESOURCE})
104    DEST_NAME=${SOURCE_NAME/com.ibm/android}
105    DEST_DIR=${TEST_DEST_DIR}/${RELATIVE_DEST_DIR}
106    mkdir -p ${DEST_DIR}
107    # A simple textual substitution works even though the file is binary as 'com.ibm' and 'android'
108    # are the same length.
109    sed 's|com[./]ibm|android|g' $RESOURCE > ${DEST_DIR}/${DEST_NAME} 
110  done
111done
112