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