• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2from __future__ import print_function
3import optparse
4import os
5import re
6import sys
7import shutil
8
9parser = optparse.OptionParser()
10
11parser.add_option('--icu-small',
12    action='store',
13    dest='icusmall',
14    default='deps/icu-small',
15    help='path to target ICU directory to shrink. Will be deleted.')
16
17parser.add_option('--icu-src',
18    action='store',
19    dest='icusrc',
20    default='deps/icu',
21    help='path to source ICU directory.')
22
23parser.add_option('--icutmp',
24    action='store',
25    dest='icutmp',
26    default='out/Release/obj/gen/icutmp',
27    help='path to icutmp dir.')
28
29
30(options, args) = parser.parse_args()
31
32if os.path.isdir(options.icusmall):
33    print('Deleting existing icusmall %s' % (options.icusmall))
34    shutil.rmtree(options.icusmall)
35
36if not os.path.isdir(options.icusrc):
37    print('Missing source ICU dir --icusrc=%s' % (options.icusrc))
38    sys.exit(1)
39
40
41
42ignore_regex = re.compile('^.*\.(vcxproj|filters|nrm|icu|dat|xml|txt|ac|guess|m4|in|sub|py|mak)$')
43
44def icu_ignore(dir, files):
45    subdir = dir[len(options.icusrc)+1::]
46    ign = []
47    if len(subdir) == 0:
48        # remove all files at root level
49        ign = ign + files
50        # except...
51        ign.remove('source')
52        if 'LICENSE' in ign:
53            ign.remove('LICENSE')
54            # license.html will be removed (it's obviated by LICENSE)
55        elif 'license.html' in ign:
56            ign.remove('license.html')
57    elif subdir == 'source':
58        ign = ign + ['layout','samples','test','extra','config','layoutex','allinone','data']
59        ign = ign + ['runConfigureICU','install-sh','mkinstalldirs','configure']
60        ign = ign + ['io']
61    elif subdir == 'source/tools':
62        ign = ign + ['tzcode','ctestfw','gensprep','gennorm2','gendict','icuswap',
63        'genbrk','gencfu','gencolusb','genren','memcheck','makeconv','gencnval','icuinfo','gentest']
64    ign = ign + ['.DS_Store', 'Makefile', 'Makefile.in']
65
66    for file in files:
67        if ignore_regex.match(file):
68            ign = ign + [file]
69
70    # print '>%s< [%s]' % (subdir, ign)
71    return ign
72
73# copied from configure
74def icu_info(icu_full_path):
75    uvernum_h = os.path.join(icu_full_path, 'source/common/unicode/uvernum.h')
76    if not os.path.isfile(uvernum_h):
77        print(' Error: could not load %s - is ICU installed?' % uvernum_h)
78        sys.exit(1)
79    icu_ver_major = None
80    matchVerExp = r'^\s*#define\s+U_ICU_VERSION_SHORT\s+"([^"]*)".*'
81    match_version = re.compile(matchVerExp)
82    for line in open(uvernum_h).readlines():
83        m = match_version.match(line)
84        if m:
85            icu_ver_major = m.group(1)
86    if not icu_ver_major:
87        print(' Could not read U_ICU_VERSION_SHORT version from %s' % uvernum_h)
88        sys.exit(1)
89    icu_endianness = sys.byteorder[0]  # TODO(srl295): EBCDIC should be 'e'
90    return (icu_ver_major, icu_endianness)
91
92(icu_ver_major, icu_endianness) = icu_info(options.icusrc)
93print("icudt%s%s" % (icu_ver_major, icu_endianness))
94
95src_datafile = os.path.join(options.icutmp, "icusmdt%s.dat" % (icu_ver_major))
96dst_datafile = os.path.join(options.icusmall, "source","data","in", "icudt%s%s.dat" % (icu_ver_major, icu_endianness))
97
98if not os.path.isfile(src_datafile):
99    print("Could not find source datafile %s - did you build small-icu node?" % src_datafile)
100    sys.exit(1)
101else:
102    print("will use small datafile %s" % (src_datafile))
103print('%s --> %s' % (options.icusrc, options.icusmall))
104shutil.copytree(options.icusrc, options.icusmall, ignore=icu_ignore)
105print('%s --> %s' % (src_datafile, dst_datafile))
106
107# now, make the data dir (since we ignored it)
108os.mkdir(os.path.join(os.path.join(options.icusmall, "source", "data")))
109os.mkdir(os.path.join(os.path.join(options.icusmall, "source", "data", "in")))
110
111# OK, now copy the data file
112shutil.copy(src_datafile, dst_datafile)
113
114# Now, print a short notice
115readme_name = os.path.join(options.icusmall, "README-SMALL-ICU.txt" )
116
117fi = open(readme_name, 'wb')
118print("Small ICU sources - auto generated by shrink-icu-src.py", file=fi)
119print("", file=fi)
120print("This directory contains the ICU subset used by --with-intl=small-icu (the default)", file=fi)
121print("It is a strict subset of ICU %s source files with the following exception(s):" % (icu_ver_major), file=fi)
122print("* %s : Reduced-size data file" % (dst_datafile), file=fi)
123print("", file=fi)
124print("", file=fi)
125print("To rebuild this directory, see ../../tools/icu/README.md", file=fi)
126print("", file=fi)
127fi.close()
128