blob: 2dc90c44d760d40517496acbaccf6de4937b6234 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# This class knows how to package up [e]glibc. Its shared since prebuild binary toolchains
3# may need packaging and its pointless to duplicate this code.
4#
5# Caller should set GLIBC_INTERNAL_USE_BINARY_LOCALE to one of:
6# "compile" - Use QEMU to generate the binary locale files
7# "precompiled" - The binary locale files are pregenerated and already present
8# "ondevice" - The device will build the locale files upon first boot through the postinst
9
10GLIBC_INTERNAL_USE_BINARY_LOCALE ?= "ondevice"
11
12python __anonymous () {
13 enabled = d.getVar("ENABLE_BINARY_LOCALE_GENERATION", True)
14
15 pn = d.getVar("PN", True)
16 if pn.endswith("-initial"):
17 enabled = False
18
19 if enabled and int(enabled):
20 import re
21
22 target_arch = d.getVar("TARGET_ARCH", True)
23 binary_arches = d.getVar("BINARY_LOCALE_ARCHES", True) or ""
24 use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF", True) or ""
25
26 for regexp in binary_arches.split(" "):
27 r = re.compile(regexp)
28
29 if r.match(target_arch):
30 depends = d.getVar("DEPENDS", True)
31 if use_cross_localedef == "1" :
32 depends = "%s cross-localedef-native" % depends
33 else:
34 depends = "%s qemu-native" % depends
35 d.setVar("DEPENDS", depends)
36 d.setVar("GLIBC_INTERNAL_USE_BINARY_LOCALE", "compile")
37 break
38
39 # try to fix disable charsets/locales/locale-code compile fail
40 if bb.utils.contains('DISTRO_FEATURES', 'libc-charsets', True, False, d) and \
41 bb.utils.contains('DISTRO_FEATURES', 'libc-locales', True, False, d) and \
42 bb.utils.contains('DISTRO_FEATURES', 'libc-locale-code', True, False, d):
43 d.setVar('PACKAGE_NO_GCONV', '0')
44 else:
45 d.setVar('PACKAGE_NO_GCONV', '1')
46}
47
48OVERRIDES_append = ":${TARGET_ARCH}-${TARGET_OS}"
49
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050locale_base_postinst() {
51#!/bin/sh
52
53if [ "x$D" != "x" ]; then
54 exit 1
55fi
56
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057localedef --inputfile=${datadir}/i18n/locales/%s --charmap=%s %s
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058}
59
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060locale_base_postrm() {
61#!/bin/sh
Patrick Williamsc0f7c042017-02-23 20:41:17 -060062localedef --delete-from-archive --inputfile=${datadir}/locales/%s --charmap=%s %s
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063}
64
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065LOCALETREESRC ?= "${PKGD}"
66
67do_prep_locale_tree() {
68 treedir=${WORKDIR}/locale-tree
69 rm -rf $treedir
70 mkdir -p $treedir/${base_bindir} $treedir/${base_libdir} $treedir/${datadir} $treedir/${localedir}
71 tar -cf - -C ${LOCALETREESRC}${datadir} -p i18n | tar -xf - -C $treedir/${datadir}
72 # unzip to avoid parsing errors
73 for i in $treedir/${datadir}/i18n/charmaps/*gz; do
74 gunzip $i
75 done
76 tar -cf - -C ${LOCALETREESRC}${base_libdir} -p . | tar -xf - -C $treedir/${base_libdir}
77 if [ -f ${STAGING_DIR_NATIVE}${prefix_native}/lib/libgcc_s.* ]; then
78 tar -cf - -C ${STAGING_DIR_NATIVE}/${prefix_native}/${base_libdir} -p libgcc_s.* | tar -xf - -C $treedir/${base_libdir}
79 fi
80 install -m 0755 ${LOCALETREESRC}${bindir}/localedef $treedir/${base_bindir}
81}
82
83do_collect_bins_from_locale_tree() {
84 treedir=${WORKDIR}/locale-tree
85
86 parent=$(dirname ${localedir})
87 mkdir -p ${PKGD}/$parent
88 tar -cf - -C $treedir/$parent -p $(basename ${localedir}) | tar -xf - -C ${PKGD}$parent
89}
90
91inherit qemu
92
93python package_do_split_gconvs () {
94 import re
95 if (d.getVar('PACKAGE_NO_GCONV', True) == '1'):
96 bb.note("package requested not splitting gconvs")
97 return
98
99 if not d.getVar('PACKAGES', True):
100 return
101
102 mlprefix = d.getVar("MLPREFIX", True) or ""
103
104 bpn = d.getVar('BPN', True)
105 libdir = d.getVar('libdir', True)
106 if not libdir:
107 bb.error("libdir not defined")
108 return
109 datadir = d.getVar('datadir', True)
110 if not datadir:
111 bb.error("datadir not defined")
112 return
113
114 gconv_libdir = base_path_join(libdir, "gconv")
115 charmap_dir = base_path_join(datadir, "i18n", "charmaps")
116 locales_dir = base_path_join(datadir, "i18n", "locales")
117 binary_locales_dir = d.getVar('localedir', True)
118
119 def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group):
120 deps = []
121 f = open(fn, "rb")
122 c_re = re.compile('^copy "(.*)"')
123 i_re = re.compile('^include "(\w+)".*')
124 for l in f.readlines():
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600125 l = l.decode("latin-1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 m = c_re.match(l) or i_re.match(l)
127 if m:
128 dp = legitimize_package_name('%s%s-gconv-%s' % (mlprefix, bpn, m.group(1)))
129 if not dp in deps:
130 deps.append(dp)
131 f.close()
132 if deps != []:
133 d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
134 if bpn != 'glibc':
135 d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
136
137 do_split_packages(d, gconv_libdir, file_regex='^(.*)\.so$', output_pattern=bpn+'-gconv-%s', \
138 description='gconv module for character set %s', hook=calc_gconv_deps, \
139 extra_depends=bpn+'-gconv')
140
141 def calc_charmap_deps(fn, pkg, file_regex, output_pattern, group):
142 deps = []
143 f = open(fn, "rb")
144 c_re = re.compile('^copy "(.*)"')
145 i_re = re.compile('^include "(\w+)".*')
146 for l in f.readlines():
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147 l = l.decode("latin-1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 m = c_re.match(l) or i_re.match(l)
149 if m:
150 dp = legitimize_package_name('%s%s-charmap-%s' % (mlprefix, bpn, m.group(1)))
151 if not dp in deps:
152 deps.append(dp)
153 f.close()
154 if deps != []:
155 d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
156 if bpn != 'glibc':
157 d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
158
159 do_split_packages(d, charmap_dir, file_regex='^(.*)\.gz$', output_pattern=bpn+'-charmap-%s', \
160 description='character map for %s encoding', hook=calc_charmap_deps, extra_depends='')
161
162 def calc_locale_deps(fn, pkg, file_regex, output_pattern, group):
163 deps = []
164 f = open(fn, "rb")
165 c_re = re.compile('^copy "(.*)"')
166 i_re = re.compile('^include "(\w+)".*')
167 for l in f.readlines():
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600168 l = l.decode("latin-1")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 m = c_re.match(l) or i_re.match(l)
170 if m:
171 dp = legitimize_package_name(mlprefix+bpn+'-localedata-%s' % m.group(1))
172 if not dp in deps:
173 deps.append(dp)
174 f.close()
175 if deps != []:
176 d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
177 if bpn != 'glibc':
178 d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
179
180 do_split_packages(d, locales_dir, file_regex='(.*)', output_pattern=bpn+'-localedata-%s', \
181 description='locale definition for %s', hook=calc_locale_deps, extra_depends='')
182 d.setVar('PACKAGES', d.getVar('PACKAGES', False) + ' ' + d.getVar('MLPREFIX', False) + bpn + '-gconv')
183
184 use_bin = d.getVar("GLIBC_INTERNAL_USE_BINARY_LOCALE", True)
185
186 dot_re = re.compile("(.*)\.(.*)")
187
188 # Read in supported locales and associated encodings
189 supported = {}
190 with open(base_path_join(d.getVar('WORKDIR', True), "SUPPORTED")) as f:
191 for line in f.readlines():
192 try:
193 locale, charset = line.rstrip().split()
194 except ValueError:
195 continue
196 supported[locale] = charset
197
198 # GLIBC_GENERATE_LOCALES var specifies which locales to be generated. empty or "all" means all locales
199 to_generate = d.getVar('GLIBC_GENERATE_LOCALES', True)
200 if not to_generate or to_generate == 'all':
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600201 to_generate = sorted(supported.keys())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 else:
203 to_generate = to_generate.split()
204 for locale in to_generate:
205 if locale not in supported:
206 if '.' in locale:
207 charset = locale.split('.')[1]
208 else:
209 charset = 'UTF-8'
210 bb.warn("Unsupported locale '%s', assuming encoding '%s'" % (locale, charset))
211 supported[locale] = charset
212
213 def output_locale_source(name, pkgname, locale, encoding):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500214 d.setVar('RDEPENDS_%s' % pkgname, '%slocaledef %s-localedata-%s %s-charmap-%s' % \
215 (mlprefix, mlprefix+bpn, legitimize_package_name(locale), mlprefix+bpn, legitimize_package_name(encoding)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216 d.setVar('pkg_postinst_%s' % pkgname, d.getVar('locale_base_postinst', True) \
217 % (locale, encoding, locale))
218 d.setVar('pkg_postrm_%s' % pkgname, d.getVar('locale_base_postrm', True) % \
219 (locale, encoding, locale))
220
221 def output_locale_binary_rdepends(name, pkgname, locale, encoding):
222 m = re.match("(.*)\.(.*)", name)
223 if m:
224 libc_name = "%s.%s" % (m.group(1), m.group(2).lower())
225 else:
226 libc_name = name
227 d.setVar('RDEPENDS_%s' % pkgname, legitimize_package_name('%s-binary-localedata-%s' \
228 % (mlprefix+bpn, libc_name)))
229
230 commands = {}
231
232 def output_locale_binary(name, pkgname, locale, encoding):
233 treedir = base_path_join(d.getVar("WORKDIR", True), "locale-tree")
234 ldlibdir = base_path_join(treedir, d.getVar("base_libdir", True))
235 path = d.getVar("PATH", True)
236 i18npath = base_path_join(treedir, datadir, "i18n")
237 gconvpath = base_path_join(treedir, "iconvdata")
238 outputpath = base_path_join(treedir, binary_locales_dir)
239
240 use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF", True) or "0"
241 if use_cross_localedef == "1":
242 target_arch = d.getVar('TARGET_ARCH', True)
243 locale_arch_options = { \
244 "arm": " --uint32-align=4 --little-endian ", \
245 "armeb": " --uint32-align=4 --big-endian ", \
246 "aarch64": " --uint32-align=4 --little-endian ", \
247 "aarch64_be": " --uint32-align=4 --big-endian ", \
248 "sh4": " --uint32-align=4 --big-endian ", \
249 "powerpc": " --uint32-align=4 --big-endian ", \
250 "powerpc64": " --uint32-align=4 --big-endian ", \
251 "mips": " --uint32-align=4 --big-endian ", \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600252 "mipsisa32r6": " --uint32-align=4 --big-endian ", \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500253 "mips64": " --uint32-align=4 --big-endian ", \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600254 "mipsisa64r6": " --uint32-align=4 --big-endian ", \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 "mipsel": " --uint32-align=4 --little-endian ", \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600256 "mipsisa32r6el": " --uint32-align=4 --little-endian ", \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257 "mips64el":" --uint32-align=4 --little-endian ", \
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600258 "mipsisa64r6el":" --uint32-align=4 --little-endian ", \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 "i586": " --uint32-align=4 --little-endian ", \
260 "i686": " --uint32-align=4 --little-endian ", \
261 "x86_64": " --uint32-align=4 --little-endian " }
262
263 if target_arch in locale_arch_options:
264 localedef_opts = locale_arch_options[target_arch]
265 else:
266 bb.error("locale_arch_options not found for target_arch=" + target_arch)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600267 bb.fatal("unknown arch:" + target_arch + " for locale_arch_options")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600269 localedef_opts += " --force --no-archive --prefix=%s \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270 --inputfile=%s/%s/i18n/locales/%s --charmap=%s %s/%s" \
271 % (treedir, treedir, datadir, locale, encoding, outputpath, name)
272
273 cmd = "PATH=\"%s\" I18NPATH=\"%s\" GCONV_PATH=\"%s\" cross-localedef %s" % \
274 (path, i18npath, gconvpath, localedef_opts)
275 else: # earlier slower qemu way
276 qemu = qemu_target_binary(d)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600277 localedef_opts = "--force --no-archive --prefix=%s \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500278 --inputfile=%s/i18n/locales/%s --charmap=%s %s" \
279 % (treedir, datadir, locale, encoding, name)
280
281 qemu_options = d.getVar('QEMU_OPTIONS', True)
282
283 cmd = "PSEUDO_RELOADED=YES PATH=\"%s\" I18NPATH=\"%s\" %s -L %s \
284 -E LD_LIBRARY_PATH=%s %s %s/bin/localedef %s" % \
285 (path, i18npath, qemu, treedir, ldlibdir, qemu_options, treedir, localedef_opts)
286
287 commands["%s/%s" % (outputpath, name)] = cmd
288
289 bb.note("generating locale %s (%s)" % (locale, encoding))
290
291 def output_locale(name, locale, encoding):
292 pkgname = d.getVar('MLPREFIX', False) + 'locale-base-' + legitimize_package_name(name)
293 d.setVar('ALLOW_EMPTY_%s' % pkgname, '1')
294 d.setVar('PACKAGES', '%s %s' % (pkgname, d.getVar('PACKAGES', True)))
295 rprovides = ' %svirtual-locale-%s' % (mlprefix, legitimize_package_name(name))
296 m = re.match("(.*)_(.*)", name)
297 if m:
298 rprovides += ' %svirtual-locale-%s' % (mlprefix, m.group(1))
299 d.setVar('RPROVIDES_%s' % pkgname, rprovides)
300
301 if use_bin == "compile":
302 output_locale_binary_rdepends(name, pkgname, locale, encoding)
303 output_locale_binary(name, pkgname, locale, encoding)
304 elif use_bin == "precompiled":
305 output_locale_binary_rdepends(name, pkgname, locale, encoding)
306 else:
307 output_locale_source(name, pkgname, locale, encoding)
308
309 if use_bin == "compile":
310 bb.note("preparing tree for binary locale generation")
311 bb.build.exec_func("do_prep_locale_tree", d)
312
313 utf8_only = int(d.getVar('LOCALE_UTF8_ONLY', True) or 0)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500314 utf8_is_default = int(d.getVar('LOCALE_UTF8_IS_DEFAULT', True) or 0)
315
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500316 encodings = {}
317 for locale in to_generate:
318 charset = supported[locale]
319 if utf8_only and charset != 'UTF-8':
320 continue
321
322 m = dot_re.match(locale)
323 if m:
324 base = m.group(1)
325 else:
326 base = locale
327
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500328 # Non-precompiled locales may be renamed so that the default
329 # (non-suffixed) encoding is always UTF-8, i.e., instead of en_US and
330 # en_US.UTF-8, we have en_US and en_US.ISO-8859-1. This implicitly
331 # contradicts SUPPORTED.
332 if use_bin == "precompiled" or not utf8_is_default:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500333 output_locale(locale, base, charset)
334 else:
335 if charset == 'UTF-8':
336 output_locale(base, base, charset)
337 else:
338 output_locale('%s.%s' % (base, charset), base, charset)
339
340 if use_bin == "compile":
341 makefile = base_path_join(d.getVar("WORKDIR", True), "locale-tree", "Makefile")
342 m = open(makefile, "w")
343 m.write("all: %s\n\n" % " ".join(commands.keys()))
344 for cmd in commands:
345 m.write(cmd + ":\n")
346 m.write("\t" + commands[cmd] + "\n\n")
347 m.close()
348 d.setVar("EXTRA_OEMAKE", "-C %s ${PARALLEL_MAKE}" % (os.path.dirname(makefile)))
349 bb.note("Executing binary locale generation makefile")
350 bb.build.exec_func("oe_runmake", d)
351 bb.note("collecting binary locales from locale tree")
352 bb.build.exec_func("do_collect_bins_from_locale_tree", d)
353 do_split_packages(d, binary_locales_dir, file_regex='(.*)', \
354 output_pattern=bpn+'-binary-localedata-%s', \
355 description='binary locale definition for %s', extra_depends='', allow_dirs=True)
356 elif use_bin == "precompiled":
357 do_split_packages(d, binary_locales_dir, file_regex='(.*)', \
358 output_pattern=bpn+'-binary-localedata-%s', \
359 description='binary locale definition for %s', extra_depends='', allow_dirs=True)
360 else:
361 bb.note("generation of binary locales disabled. this may break i18n!")
362
363}
364
365# We want to do this indirection so that we can safely 'return'
366# from the called function even though we're prepending
367python populate_packages_prepend () {
368 bb.build.exec_func('package_do_split_gconvs', d)
369}