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