xref: /aosp_15_r20/external/icu/tools/icuutil.py (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker# Copyright 2017 The Android Open Source Project
2*0e209d39SAndroid Build Coastguard Worker#
3*0e209d39SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*0e209d39SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*0e209d39SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*0e209d39SAndroid Build Coastguard Worker#
7*0e209d39SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*0e209d39SAndroid Build Coastguard Worker#
9*0e209d39SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*0e209d39SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*0e209d39SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*0e209d39SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*0e209d39SAndroid Build Coastguard Worker# limitations under the License.
14*0e209d39SAndroid Build Coastguard Worker
15*0e209d39SAndroid Build Coastguard Worker"""Utility methods associated with ICU source and builds."""
16*0e209d39SAndroid Build Coastguard Worker
17*0e209d39SAndroid Build Coastguard Workerfrom __future__ import print_function
18*0e209d39SAndroid Build Coastguard Worker
19*0e209d39SAndroid Build Coastguard Workerimport filecmp
20*0e209d39SAndroid Build Coastguard Workerimport glob
21*0e209d39SAndroid Build Coastguard Workerimport os
22*0e209d39SAndroid Build Coastguard Workerimport pathlib
23*0e209d39SAndroid Build Coastguard Workerimport shutil
24*0e209d39SAndroid Build Coastguard Workerimport subprocess
25*0e209d39SAndroid Build Coastguard Workerimport sys
26*0e209d39SAndroid Build Coastguard Worker
27*0e209d39SAndroid Build Coastguard Workerimport i18nutil
28*0e209d39SAndroid Build Coastguard Workerimport ziputil
29*0e209d39SAndroid Build Coastguard Worker
30*0e209d39SAndroid Build Coastguard Worker
31*0e209d39SAndroid Build Coastguard Worker# See https://github.com/unicode-org/icu/blob/main/docs/userguide/icu_data/buildtool.md
32*0e209d39SAndroid Build Coastguard Worker# for the documentation.
33*0e209d39SAndroid Build Coastguard WorkerICU_DATA_FILTERS = """{
34*0e209d39SAndroid Build Coastguard Worker  "featureFilters": {
35*0e209d39SAndroid Build Coastguard Worker    "misc": {
36*0e209d39SAndroid Build Coastguard Worker      "excludelist": [
37*0e209d39SAndroid Build Coastguard Worker        "metaZones",
38*0e209d39SAndroid Build Coastguard Worker        "timezoneTypes",
39*0e209d39SAndroid Build Coastguard Worker        "windowsZones",
40*0e209d39SAndroid Build Coastguard Worker        "zoneinfo64"
41*0e209d39SAndroid Build Coastguard Worker      ]
42*0e209d39SAndroid Build Coastguard Worker    },
43*0e209d39SAndroid Build Coastguard Worker    "brkitr_adaboost": {
44*0e209d39SAndroid Build Coastguard Worker      "includelist": [
45*0e209d39SAndroid Build Coastguard Worker        "jaml"
46*0e209d39SAndroid Build Coastguard Worker      ]
47*0e209d39SAndroid Build Coastguard Worker    }
48*0e209d39SAndroid Build Coastguard Worker  }
49*0e209d39SAndroid Build Coastguard Worker}
50*0e209d39SAndroid Build Coastguard Worker"""
51*0e209d39SAndroid Build Coastguard Worker
52*0e209d39SAndroid Build Coastguard WorkerICU_MLDATA_FILTERS = """{
53*0e209d39SAndroid Build Coastguard Worker  "featureFilters": {
54*0e209d39SAndroid Build Coastguard Worker    "brkitr_adaboost": {
55*0e209d39SAndroid Build Coastguard Worker      "includelist": [
56*0e209d39SAndroid Build Coastguard Worker        "jaml"
57*0e209d39SAndroid Build Coastguard Worker      ]
58*0e209d39SAndroid Build Coastguard Worker    }
59*0e209d39SAndroid Build Coastguard Worker  }
60*0e209d39SAndroid Build Coastguard Worker}
61*0e209d39SAndroid Build Coastguard Worker"""
62*0e209d39SAndroid Build Coastguard Worker
63*0e209d39SAndroid Build Coastguard Worker
64*0e209d39SAndroid Build Coastguard Workerdef cldrDir():
65*0e209d39SAndroid Build Coastguard Worker  """Returns the location of CLDR in the Android source tree."""
66*0e209d39SAndroid Build Coastguard Worker  android_build_top = i18nutil.GetAndroidRootOrDie()
67*0e209d39SAndroid Build Coastguard Worker  cldr_dir = os.path.realpath('%s/external/cldr' % android_build_top)
68*0e209d39SAndroid Build Coastguard Worker  i18nutil.CheckDirExists(cldr_dir, 'external/cldr')
69*0e209d39SAndroid Build Coastguard Worker  return cldr_dir
70*0e209d39SAndroid Build Coastguard Worker
71*0e209d39SAndroid Build Coastguard Worker
72*0e209d39SAndroid Build Coastguard Workerdef icuDir():
73*0e209d39SAndroid Build Coastguard Worker  """Returns the location of ICU in the Android source tree."""
74*0e209d39SAndroid Build Coastguard Worker  android_build_top = i18nutil.GetAndroidRootOrDie()
75*0e209d39SAndroid Build Coastguard Worker  icu_dir = os.path.realpath('%s/external/icu' % android_build_top)
76*0e209d39SAndroid Build Coastguard Worker  i18nutil.CheckDirExists(icu_dir, 'external/icu')
77*0e209d39SAndroid Build Coastguard Worker  return icu_dir
78*0e209d39SAndroid Build Coastguard Worker
79*0e209d39SAndroid Build Coastguard Worker
80*0e209d39SAndroid Build Coastguard Workerdef icu4cDir():
81*0e209d39SAndroid Build Coastguard Worker  """Returns the location of ICU4C in the Android source tree."""
82*0e209d39SAndroid Build Coastguard Worker  icu4c_dir = os.path.realpath('%s/icu4c/source' % icuDir())
83*0e209d39SAndroid Build Coastguard Worker  i18nutil.CheckDirExists(icu4c_dir, 'external/icu/icu4c/source')
84*0e209d39SAndroid Build Coastguard Worker  return icu4c_dir
85*0e209d39SAndroid Build Coastguard Worker
86*0e209d39SAndroid Build Coastguard Worker
87*0e209d39SAndroid Build Coastguard Workerdef icu4jDir():
88*0e209d39SAndroid Build Coastguard Worker  """Returns the location of ICU4J in the Android source tree."""
89*0e209d39SAndroid Build Coastguard Worker  icu4j_dir = os.path.realpath('%s/icu4j' % icuDir())
90*0e209d39SAndroid Build Coastguard Worker  i18nutil.CheckDirExists(icu4j_dir, 'external/icu/icu4j')
91*0e209d39SAndroid Build Coastguard Worker  return icu4j_dir
92*0e209d39SAndroid Build Coastguard Worker
93*0e209d39SAndroid Build Coastguard Worker
94*0e209d39SAndroid Build Coastguard Workerdef datFile(icu_build_dir):
95*0e209d39SAndroid Build Coastguard Worker  """Returns the location of the ICU .dat file in the specified ICU build dir."""
96*0e209d39SAndroid Build Coastguard Worker  dat_file_pattern = '%s/data/out/tmp/icudt??l.dat' % icu_build_dir
97*0e209d39SAndroid Build Coastguard Worker  dat_files = glob.glob(dat_file_pattern)
98*0e209d39SAndroid Build Coastguard Worker  if len(dat_files) != 1:
99*0e209d39SAndroid Build Coastguard Worker    print('ERROR: Unexpectedly found %d .dat files (%s). Halting.' % (len(datfiles), datfiles))
100*0e209d39SAndroid Build Coastguard Worker    sys.exit(1)
101*0e209d39SAndroid Build Coastguard Worker  dat_file = dat_files[0]
102*0e209d39SAndroid Build Coastguard Worker  return dat_file
103*0e209d39SAndroid Build Coastguard Worker
104*0e209d39SAndroid Build Coastguard Worker
105*0e209d39SAndroid Build Coastguard Workerdef PrepareIcuBuild(icu_build_dir, data_filters_json=None):
106*0e209d39SAndroid Build Coastguard Worker  """Sets up an ICU build in the specified directory.
107*0e209d39SAndroid Build Coastguard Worker
108*0e209d39SAndroid Build Coastguard Worker  Creates the directory and runs "runConfigureICU Linux"
109*0e209d39SAndroid Build Coastguard Worker  """
110*0e209d39SAndroid Build Coastguard Worker  # Keep track of the original cwd so we can go back to it at the end.
111*0e209d39SAndroid Build Coastguard Worker  original_working_dir = os.getcwd()
112*0e209d39SAndroid Build Coastguard Worker
113*0e209d39SAndroid Build Coastguard Worker  # Create a directory to run 'make' from.
114*0e209d39SAndroid Build Coastguard Worker  if not os.path.exists(icu_build_dir):
115*0e209d39SAndroid Build Coastguard Worker    os.mkdir(icu_build_dir)
116*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu_build_dir)
117*0e209d39SAndroid Build Coastguard Worker
118*0e209d39SAndroid Build Coastguard Worker  # Build the ICU tools.
119*0e209d39SAndroid Build Coastguard Worker  print('Configuring ICU tools...')
120*0e209d39SAndroid Build Coastguard Worker  cmd = ['env']
121*0e209d39SAndroid Build Coastguard Worker  if data_filters_json is not None:
122*0e209d39SAndroid Build Coastguard Worker    json_file_path = os.path.join(icu_build_dir, "icu4c_data_filters.json")
123*0e209d39SAndroid Build Coastguard Worker    print("json path: %s" % json_file_path)
124*0e209d39SAndroid Build Coastguard Worker    writeFileContent(json_file_path, data_filters_json)
125*0e209d39SAndroid Build Coastguard Worker    cmd.append('ICU_DATA_FILTER_FILE=%s' % json_file_path)
126*0e209d39SAndroid Build Coastguard Worker
127*0e209d39SAndroid Build Coastguard Worker  cmd += ['ICU_DATA_BUILDTOOL_OPTS=--include_uni_core_data',
128*0e209d39SAndroid Build Coastguard Worker          '%s/runConfigureICU' % icu4cDir(),
129*0e209d39SAndroid Build Coastguard Worker          'Linux']
130*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(cmd)
131*0e209d39SAndroid Build Coastguard Worker
132*0e209d39SAndroid Build Coastguard Worker  os.chdir(original_working_dir)
133*0e209d39SAndroid Build Coastguard Worker
134*0e209d39SAndroid Build Coastguard Workerdef writeFileContent(file_path, file_content):
135*0e209d39SAndroid Build Coastguard Worker  """Write a string into the file"""
136*0e209d39SAndroid Build Coastguard Worker  with open(file_path, "w") as file:
137*0e209d39SAndroid Build Coastguard Worker    file.write(file_content)
138*0e209d39SAndroid Build Coastguard Worker
139*0e209d39SAndroid Build Coastguard Workerdef MakeTzDataFiles(icu_build_dir, iana_tar_file):
140*0e209d39SAndroid Build Coastguard Worker  """Builds and runs the ICU tools in ${icu_Build_dir}/tools/tzcode.
141*0e209d39SAndroid Build Coastguard Worker
142*0e209d39SAndroid Build Coastguard Worker  The tools are run against the specified IANA tzdata .tar.gz.
143*0e209d39SAndroid Build Coastguard Worker  The resulting zoneinfo64.txt is copied into the src directories.
144*0e209d39SAndroid Build Coastguard Worker  """
145*0e209d39SAndroid Build Coastguard Worker  tzcode_working_dir = '%s/tools/tzcode' % icu_build_dir
146*0e209d39SAndroid Build Coastguard Worker
147*0e209d39SAndroid Build Coastguard Worker  # Fix missing files.
148*0e209d39SAndroid Build Coastguard Worker  # The tz2icu tool only picks up icuregions and icuzones if they are in the CWD
149*0e209d39SAndroid Build Coastguard Worker  for icu_data_file in [ 'icuregions', 'icuzones']:
150*0e209d39SAndroid Build Coastguard Worker    icu_data_file_source = '%s/tools/tzcode/%s' % (icu4cDir(), icu_data_file)
151*0e209d39SAndroid Build Coastguard Worker    icu_data_file_symlink = '%s/%s' % (tzcode_working_dir, icu_data_file)
152*0e209d39SAndroid Build Coastguard Worker    os.symlink(icu_data_file_source, icu_data_file_symlink)
153*0e209d39SAndroid Build Coastguard Worker
154*0e209d39SAndroid Build Coastguard Worker  iana_tar_filename = os.path.basename(iana_tar_file)
155*0e209d39SAndroid Build Coastguard Worker  working_iana_tar_file = '%s/%s' % (tzcode_working_dir, iana_tar_filename)
156*0e209d39SAndroid Build Coastguard Worker  shutil.copyfile(iana_tar_file, working_iana_tar_file)
157*0e209d39SAndroid Build Coastguard Worker
158*0e209d39SAndroid Build Coastguard Worker  print('Making ICU tz data files...')
159*0e209d39SAndroid Build Coastguard Worker  # The Makefile assumes the existence of the bin directory.
160*0e209d39SAndroid Build Coastguard Worker  os.mkdir('%s/bin' % icu_build_dir)
161*0e209d39SAndroid Build Coastguard Worker
162*0e209d39SAndroid Build Coastguard Worker  # -j1 is needed because the build is not parallelizable. http://b/109641429
163*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['make', '-j1', '-C', tzcode_working_dir])
164*0e209d39SAndroid Build Coastguard Worker
165*0e209d39SAndroid Build Coastguard Worker  # Copy the source file to its ultimate destination.
166*0e209d39SAndroid Build Coastguard Worker  zoneinfo_file = '%s/zoneinfo64.txt' % tzcode_working_dir
167*0e209d39SAndroid Build Coastguard Worker  icu_txt_data_dir = '%s/data/misc' % icu4cDir()
168*0e209d39SAndroid Build Coastguard Worker  print('Copying zoneinfo64.txt to %s ...' % icu_txt_data_dir)
169*0e209d39SAndroid Build Coastguard Worker  shutil.copy(zoneinfo_file, icu_txt_data_dir)
170*0e209d39SAndroid Build Coastguard Worker
171*0e209d39SAndroid Build Coastguard Worker
172*0e209d39SAndroid Build Coastguard Workerdef MakeAndCopyIcuDataFiles(icu_build_dir, copy_icu4c_dat_file_only=False):
173*0e209d39SAndroid Build Coastguard Worker  """Builds the ICU .dat and .jar files using the current src data.
174*0e209d39SAndroid Build Coastguard Worker
175*0e209d39SAndroid Build Coastguard Worker  The files are copied back into the expected locations in the src tree.
176*0e209d39SAndroid Build Coastguard Worker
177*0e209d39SAndroid Build Coastguard Worker  This is a low-level method.
178*0e209d39SAndroid Build Coastguard Worker  Please check :func:`GenerateIcuDataFiles()` for caveats.
179*0e209d39SAndroid Build Coastguard Worker  """
180*0e209d39SAndroid Build Coastguard Worker  # Keep track of the original cwd so we can go back to it at the end.
181*0e209d39SAndroid Build Coastguard Worker  original_working_dir = os.getcwd()
182*0e209d39SAndroid Build Coastguard Worker
183*0e209d39SAndroid Build Coastguard Worker  # Regenerate the .dat file.
184*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu_build_dir)
185*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['make', '-j32'])
186*0e209d39SAndroid Build Coastguard Worker
187*0e209d39SAndroid Build Coastguard Worker  # Copy the .dat file to its ultimate destination.
188*0e209d39SAndroid Build Coastguard Worker  icu_dat_data_dir = '%s/stubdata' % icu4cDir()
189*0e209d39SAndroid Build Coastguard Worker  dat_file = datFile(icu_build_dir)
190*0e209d39SAndroid Build Coastguard Worker
191*0e209d39SAndroid Build Coastguard Worker  print('Copying %s to %s ...' % (dat_file, icu_dat_data_dir))
192*0e209d39SAndroid Build Coastguard Worker  shutil.copy(dat_file, icu_dat_data_dir)
193*0e209d39SAndroid Build Coastguard Worker
194*0e209d39SAndroid Build Coastguard Worker  if copy_icu4c_dat_file_only:
195*0e209d39SAndroid Build Coastguard Worker    return
196*0e209d39SAndroid Build Coastguard Worker
197*0e209d39SAndroid Build Coastguard Worker  # Generate the ICU4J .jar files
198*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['make', '-j32', 'icu4j-data'])
199*0e209d39SAndroid Build Coastguard Worker
200*0e209d39SAndroid Build Coastguard Worker  # Generate the test data in icu4c/source/test/testdata/out
201*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['make', '-j32', 'tests'])
202*0e209d39SAndroid Build Coastguard Worker
203*0e209d39SAndroid Build Coastguard Worker  # Copy the ICU4J .jar files to their ultimate destination.
204*0e209d39SAndroid Build Coastguard Worker  CopyIcu4jDataFiles()
205*0e209d39SAndroid Build Coastguard Worker
206*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu4jDir())
207*0e209d39SAndroid Build Coastguard Worker  # os.path.basename(dat_file) is like icudt??l.dat
208*0e209d39SAndroid Build Coastguard Worker  icu4j_data_ver = os.path.basename(dat_file)[:-5] + "b"
209*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['env', 'ICU_DATA_VER=' + icu4j_data_ver, './extract-data-files.sh'])
210*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu_build_dir)
211*0e209d39SAndroid Build Coastguard Worker
212*0e209d39SAndroid Build Coastguard Worker  testdata_out_dir = '%s/test/testdata/out' % icu4cDir()
213*0e209d39SAndroid Build Coastguard Worker  print('Copying test data to %s ' % testdata_out_dir)
214*0e209d39SAndroid Build Coastguard Worker  if os.path.exists(testdata_out_dir):
215*0e209d39SAndroid Build Coastguard Worker    shutil.rmtree(testdata_out_dir)
216*0e209d39SAndroid Build Coastguard Worker  shutil.copytree('test/testdata/out', testdata_out_dir)
217*0e209d39SAndroid Build Coastguard Worker
218*0e209d39SAndroid Build Coastguard Worker  # Switch back to the original working cwd.
219*0e209d39SAndroid Build Coastguard Worker  os.chdir(original_working_dir)
220*0e209d39SAndroid Build Coastguard Worker
221*0e209d39SAndroid Build Coastguard Workerdef CopyIcu4jDataFiles():
222*0e209d39SAndroid Build Coastguard Worker  """Copy the ICU4J .jar files to their ultimate destination"""
223*0e209d39SAndroid Build Coastguard Worker  icu_jar_data_dir = '%s/main/shared/data' % icu4jDir()
224*0e209d39SAndroid Build Coastguard Worker  os.makedirs(icu_jar_data_dir, exist_ok=True)
225*0e209d39SAndroid Build Coastguard Worker  jarfiles = glob.glob('data/out/icu4j/*.jar')
226*0e209d39SAndroid Build Coastguard Worker  if len(jarfiles) != 3:
227*0e209d39SAndroid Build Coastguard Worker    print('ERROR: Unexpectedly found %d .jar files (%s). Halting.' % (len(jarfiles), jarfiles))
228*0e209d39SAndroid Build Coastguard Worker    sys.exit(1)
229*0e209d39SAndroid Build Coastguard Worker  for jarfile in jarfiles:
230*0e209d39SAndroid Build Coastguard Worker    icu_jarfile = os.path.join(icu_jar_data_dir, os.path.basename(jarfile))
231*0e209d39SAndroid Build Coastguard Worker    if ziputil.ZipCompare(jarfile, icu_jarfile):
232*0e209d39SAndroid Build Coastguard Worker      print('Ignoring %s which is identical to %s ...' % (jarfile, icu_jarfile))
233*0e209d39SAndroid Build Coastguard Worker    else:
234*0e209d39SAndroid Build Coastguard Worker      print('Copying %s to %s ...' % (jarfile, icu_jar_data_dir))
235*0e209d39SAndroid Build Coastguard Worker      shutil.copy(jarfile, icu_jar_data_dir)
236*0e209d39SAndroid Build Coastguard Worker
237*0e209d39SAndroid Build Coastguard Workerdef MakeAndCopyIcuTzFiles(icu_build_dir, res_dest_dir):
238*0e209d39SAndroid Build Coastguard Worker  """Makes .res files containing just time zone data.
239*0e209d39SAndroid Build Coastguard Worker
240*0e209d39SAndroid Build Coastguard Worker  They provide time zone data only: some strings like translated
241*0e209d39SAndroid Build Coastguard Worker  time zone names will be missing, but rules will be correct.
242*0e209d39SAndroid Build Coastguard Worker  """
243*0e209d39SAndroid Build Coastguard Worker
244*0e209d39SAndroid Build Coastguard Worker  # Keep track of the original cwd so we can go back to it at the end.
245*0e209d39SAndroid Build Coastguard Worker  original_working_dir = os.getcwd()
246*0e209d39SAndroid Build Coastguard Worker
247*0e209d39SAndroid Build Coastguard Worker  # Regenerate the .res files.
248*0e209d39SAndroid Build Coastguard Worker  os.chdir(icu_build_dir)
249*0e209d39SAndroid Build Coastguard Worker  subprocess.check_call(['make', '-j32'])
250*0e209d39SAndroid Build Coastguard Worker
251*0e209d39SAndroid Build Coastguard Worker  # The list of ICU resources needed for time zone data overlays.
252*0e209d39SAndroid Build Coastguard Worker  tz_res_names = [
253*0e209d39SAndroid Build Coastguard Worker          'metaZones.res',
254*0e209d39SAndroid Build Coastguard Worker          'timezoneTypes.res',
255*0e209d39SAndroid Build Coastguard Worker          'windowsZones.res',
256*0e209d39SAndroid Build Coastguard Worker          'zoneinfo64.res',
257*0e209d39SAndroid Build Coastguard Worker  ]
258*0e209d39SAndroid Build Coastguard Worker
259*0e209d39SAndroid Build Coastguard Worker  dat_file = datFile(icu_build_dir)
260*0e209d39SAndroid Build Coastguard Worker  icu_package_dat = os.path.basename(dat_file)
261*0e209d39SAndroid Build Coastguard Worker  if not icu_package_dat.endswith('.dat'):
262*0e209d39SAndroid Build Coastguard Worker      print('%s does not end with .dat' % icu_package_dat)
263*0e209d39SAndroid Build Coastguard Worker      sys.exit(1)
264*0e209d39SAndroid Build Coastguard Worker  icu_package = icu_package_dat[:-4]
265*0e209d39SAndroid Build Coastguard Worker
266*0e209d39SAndroid Build Coastguard Worker  # Copy all the .res files we need from, e.g. ./data/out/build/icudt55l, to the
267*0e209d39SAndroid Build Coastguard Worker  # destination directory.
268*0e209d39SAndroid Build Coastguard Worker  res_src_dir = '%s/data/out/build/%s' % (icu_build_dir, icu_package)
269*0e209d39SAndroid Build Coastguard Worker  for tz_res_name in tz_res_names:
270*0e209d39SAndroid Build Coastguard Worker    shutil.copy('%s/%s' % (res_src_dir, tz_res_name), res_dest_dir)
271*0e209d39SAndroid Build Coastguard Worker
272*0e209d39SAndroid Build Coastguard Worker  # Switch back to the original working cwd.
273*0e209d39SAndroid Build Coastguard Worker  os.chdir(original_working_dir)
274*0e209d39SAndroid Build Coastguard Worker
275*0e209d39SAndroid Build Coastguard Workerdef GenerateIcuDataFiles():
276*0e209d39SAndroid Build Coastguard Worker  """ There are ICU files generation of which depends on ICU itself.
277*0e209d39SAndroid Build Coastguard Worker  This method repeatedly builds ICU and re-generates these files until they
278*0e209d39SAndroid Build Coastguard Worker  converge, i.e. subsequent builds do not change these files.
279*0e209d39SAndroid Build Coastguard Worker  """
280*0e209d39SAndroid Build Coastguard Worker  last_icu_build_dir = _MakeIcuDataFilesOnce()
281*0e209d39SAndroid Build Coastguard Worker
282*0e209d39SAndroid Build Coastguard Worker  _MakeIcuDataFilesWithoutTimeZoneFiles(last_icu_build_dir)
283*0e209d39SAndroid Build Coastguard Worker
284*0e209d39SAndroid Build Coastguard Workerdef _MakeIcuDataFilesOnce():
285*0e209d39SAndroid Build Coastguard Worker  """Builds ICU and copies .dat and .jar files to expected places.
286*0e209d39SAndroid Build Coastguard Worker  Build is invoked only once. It is unlikely that you need to call
287*0e209d39SAndroid Build Coastguard Worker  this method outside of this script.
288*0e209d39SAndroid Build Coastguard Worker
289*0e209d39SAndroid Build Coastguard Worker  This is a low-level method.
290*0e209d39SAndroid Build Coastguard Worker  Please check :func:`GenerateIcuDataFiles()` for caveats.
291*0e209d39SAndroid Build Coastguard Worker  """
292*0e209d39SAndroid Build Coastguard Worker  i18nutil.SwitchToNewTemporaryDirectory()
293*0e209d39SAndroid Build Coastguard Worker  icu_build_dir = '%s/icu' % os.getcwd()
294*0e209d39SAndroid Build Coastguard Worker
295*0e209d39SAndroid Build Coastguard Worker  PrepareIcuBuild(icu_build_dir, data_filters_json=ICU_MLDATA_FILTERS)
296*0e209d39SAndroid Build Coastguard Worker
297*0e209d39SAndroid Build Coastguard Worker  MakeAndCopyIcuDataFiles(icu_build_dir)
298*0e209d39SAndroid Build Coastguard Worker
299*0e209d39SAndroid Build Coastguard Worker  return icu_build_dir
300*0e209d39SAndroid Build Coastguard Worker
301*0e209d39SAndroid Build Coastguard Workerdef _MakeIcuDataFilesWithoutTimeZoneFiles(icu_build_dir):
302*0e209d39SAndroid Build Coastguard Worker  """
303*0e209d39SAndroid Build Coastguard Worker  Remove the timezone .res files from the .dat file in order to save ~200 KB file size.
304*0e209d39SAndroid Build Coastguard Worker  TODO (b/206956042): Move this to the first build whenhttps://unicode-org.atlassian.net/browse/ICU-21769 is fixed.
305*0e209d39SAndroid Build Coastguard Worker  Now another build is needed to build a new .dat file without the timezone files.
306*0e209d39SAndroid Build Coastguard Worker  """
307*0e209d39SAndroid Build Coastguard Worker  # A manual removal of the .lst file is needed to force GNUmake to rebuild the .lst file
308*0e209d39SAndroid Build Coastguard Worker  list_file_path = pathlib.Path(icu_build_dir, 'data/out/tmp/icudata.lst')
309*0e209d39SAndroid Build Coastguard Worker  list_file_path.unlink(missing_ok=True)
310*0e209d39SAndroid Build Coastguard Worker
311*0e209d39SAndroid Build Coastguard Worker  PrepareIcuBuild(icu_build_dir, data_filters_json=ICU_DATA_FILTERS)
312*0e209d39SAndroid Build Coastguard Worker  # copy_icu4c_dat_file_only is set to true to avoid copying the ICU4J data or other files
313*0e209d39SAndroid Build Coastguard Worker  # because the data files may be incomplete to be consumed for a host tool.
314*0e209d39SAndroid Build Coastguard Worker  # The ICU4J implementation on device doesn't use the ICU4J data files,
315*0e209d39SAndroid Build Coastguard Worker  # e.g. ./icu4j/main/shared/data/icudata.jar
316*0e209d39SAndroid Build Coastguard Worker  MakeAndCopyIcuDataFiles(icu_build_dir, copy_icu4c_dat_file_only=True)
317*0e209d39SAndroid Build Coastguard Worker
318*0e209d39SAndroid Build Coastguard Workerdef CopyLicenseFiles(target_dir):
319*0e209d39SAndroid Build Coastguard Worker  """Copies ICU license files to the target_dir"""
320*0e209d39SAndroid Build Coastguard Worker
321*0e209d39SAndroid Build Coastguard Worker  license_file = '%s/LICENSE' % icuDir()
322*0e209d39SAndroid Build Coastguard Worker  print('Copying %s to %s ...' % (license_file, target_dir))
323*0e209d39SAndroid Build Coastguard Worker  shutil.copy(license_file, target_dir)
324*0e209d39SAndroid Build Coastguard Worker
325