blob: 467d5679238bb22e60a8dbc006591ec18ef972fa [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
50do_configure_prepend() {
51 if [ -e ${S}/elf/ldd.bash.in ]; then
52 sed -e "s#@BASH@#/bin/sh#" -i ${S}/elf/ldd.bash.in
53 fi
54}
55
56
57
58# indentation removed on purpose
59locale_base_postinst() {
60#!/bin/sh
61
62if [ "x$D" != "x" ]; then
63 exit 1
64fi
65
66rm -rf ${TMP_LOCALE}
67mkdir -p ${TMP_LOCALE}
68if [ -f ${localedir}/locale-archive ]; then
69 cp ${localedir}/locale-archive ${TMP_LOCALE}/
70fi
71localedef --inputfile=${datadir}/i18n/locales/%s --charmap=%s --prefix=/tmp/locale %s
72mkdir -p ${localedir}/
73mv ${TMP_LOCALE}/locale-archive ${localedir}/
74rm -rf ${TMP_LOCALE}
75}
76
77# indentation removed on purpose
78locale_base_postrm() {
79#!/bin/sh
80
81rm -rf ${TMP_LOCALE}
82mkdir -p ${TMP_LOCALE}
83if [ -f ${localedir}/locale-archive ]; then
84 cp ${localedir}/locale-archive ${TMP_LOCALE}/
85fi
86localedef --delete-from-archive --inputfile=${datadir}/locales/%s --charmap=%s --prefix=/tmp/locale %s
87mv ${TMP_LOCALE}/locale-archive ${localedir}/
88rm -rf ${TMP_LOCALE}
89}
90
91
92TMP_LOCALE="/tmp/locale${localedir}"
93LOCALETREESRC ?= "${PKGD}"
94
95do_prep_locale_tree() {
96 treedir=${WORKDIR}/locale-tree
97 rm -rf $treedir
98 mkdir -p $treedir/${base_bindir} $treedir/${base_libdir} $treedir/${datadir} $treedir/${localedir}
99 tar -cf - -C ${LOCALETREESRC}${datadir} -p i18n | tar -xf - -C $treedir/${datadir}
100 # unzip to avoid parsing errors
101 for i in $treedir/${datadir}/i18n/charmaps/*gz; do
102 gunzip $i
103 done
104 tar -cf - -C ${LOCALETREESRC}${base_libdir} -p . | tar -xf - -C $treedir/${base_libdir}
105 if [ -f ${STAGING_DIR_NATIVE}${prefix_native}/lib/libgcc_s.* ]; then
106 tar -cf - -C ${STAGING_DIR_NATIVE}/${prefix_native}/${base_libdir} -p libgcc_s.* | tar -xf - -C $treedir/${base_libdir}
107 fi
108 install -m 0755 ${LOCALETREESRC}${bindir}/localedef $treedir/${base_bindir}
109}
110
111do_collect_bins_from_locale_tree() {
112 treedir=${WORKDIR}/locale-tree
113
114 parent=$(dirname ${localedir})
115 mkdir -p ${PKGD}/$parent
116 tar -cf - -C $treedir/$parent -p $(basename ${localedir}) | tar -xf - -C ${PKGD}$parent
117}
118
119inherit qemu
120
121python package_do_split_gconvs () {
122 import re
123 if (d.getVar('PACKAGE_NO_GCONV', True) == '1'):
124 bb.note("package requested not splitting gconvs")
125 return
126
127 if not d.getVar('PACKAGES', True):
128 return
129
130 mlprefix = d.getVar("MLPREFIX", True) or ""
131
132 bpn = d.getVar('BPN', True)
133 libdir = d.getVar('libdir', True)
134 if not libdir:
135 bb.error("libdir not defined")
136 return
137 datadir = d.getVar('datadir', True)
138 if not datadir:
139 bb.error("datadir not defined")
140 return
141
142 gconv_libdir = base_path_join(libdir, "gconv")
143 charmap_dir = base_path_join(datadir, "i18n", "charmaps")
144 locales_dir = base_path_join(datadir, "i18n", "locales")
145 binary_locales_dir = d.getVar('localedir', True)
146
147 def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group):
148 deps = []
149 f = open(fn, "rb")
150 c_re = re.compile('^copy "(.*)"')
151 i_re = re.compile('^include "(\w+)".*')
152 for l in f.readlines():
153 m = c_re.match(l) or i_re.match(l)
154 if m:
155 dp = legitimize_package_name('%s%s-gconv-%s' % (mlprefix, bpn, m.group(1)))
156 if not dp in deps:
157 deps.append(dp)
158 f.close()
159 if deps != []:
160 d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
161 if bpn != 'glibc':
162 d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
163
164 do_split_packages(d, gconv_libdir, file_regex='^(.*)\.so$', output_pattern=bpn+'-gconv-%s', \
165 description='gconv module for character set %s', hook=calc_gconv_deps, \
166 extra_depends=bpn+'-gconv')
167
168 def calc_charmap_deps(fn, pkg, file_regex, output_pattern, group):
169 deps = []
170 f = open(fn, "rb")
171 c_re = re.compile('^copy "(.*)"')
172 i_re = re.compile('^include "(\w+)".*')
173 for l in f.readlines():
174 m = c_re.match(l) or i_re.match(l)
175 if m:
176 dp = legitimize_package_name('%s%s-charmap-%s' % (mlprefix, bpn, m.group(1)))
177 if not dp in deps:
178 deps.append(dp)
179 f.close()
180 if deps != []:
181 d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
182 if bpn != 'glibc':
183 d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
184
185 do_split_packages(d, charmap_dir, file_regex='^(.*)\.gz$', output_pattern=bpn+'-charmap-%s', \
186 description='character map for %s encoding', hook=calc_charmap_deps, extra_depends='')
187
188 def calc_locale_deps(fn, pkg, file_regex, output_pattern, group):
189 deps = []
190 f = open(fn, "rb")
191 c_re = re.compile('^copy "(.*)"')
192 i_re = re.compile('^include "(\w+)".*')
193 for l in f.readlines():
194 m = c_re.match(l) or i_re.match(l)
195 if m:
196 dp = legitimize_package_name(mlprefix+bpn+'-localedata-%s' % m.group(1))
197 if not dp in deps:
198 deps.append(dp)
199 f.close()
200 if deps != []:
201 d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
202 if bpn != 'glibc':
203 d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
204
205 do_split_packages(d, locales_dir, file_regex='(.*)', output_pattern=bpn+'-localedata-%s', \
206 description='locale definition for %s', hook=calc_locale_deps, extra_depends='')
207 d.setVar('PACKAGES', d.getVar('PACKAGES', False) + ' ' + d.getVar('MLPREFIX', False) + bpn + '-gconv')
208
209 use_bin = d.getVar("GLIBC_INTERNAL_USE_BINARY_LOCALE", True)
210
211 dot_re = re.compile("(.*)\.(.*)")
212
213 # Read in supported locales and associated encodings
214 supported = {}
215 with open(base_path_join(d.getVar('WORKDIR', True), "SUPPORTED")) as f:
216 for line in f.readlines():
217 try:
218 locale, charset = line.rstrip().split()
219 except ValueError:
220 continue
221 supported[locale] = charset
222
223 # GLIBC_GENERATE_LOCALES var specifies which locales to be generated. empty or "all" means all locales
224 to_generate = d.getVar('GLIBC_GENERATE_LOCALES', True)
225 if not to_generate or to_generate == 'all':
226 to_generate = supported.keys()
227 else:
228 to_generate = to_generate.split()
229 for locale in to_generate:
230 if locale not in supported:
231 if '.' in locale:
232 charset = locale.split('.')[1]
233 else:
234 charset = 'UTF-8'
235 bb.warn("Unsupported locale '%s', assuming encoding '%s'" % (locale, charset))
236 supported[locale] = charset
237
238 def output_locale_source(name, pkgname, locale, encoding):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500239 d.setVar('RDEPENDS_%s' % pkgname, '%slocaledef %s-localedata-%s %s-charmap-%s' % \
240 (mlprefix, mlprefix+bpn, legitimize_package_name(locale), mlprefix+bpn, legitimize_package_name(encoding)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 d.setVar('pkg_postinst_%s' % pkgname, d.getVar('locale_base_postinst', True) \
242 % (locale, encoding, locale))
243 d.setVar('pkg_postrm_%s' % pkgname, d.getVar('locale_base_postrm', True) % \
244 (locale, encoding, locale))
245
246 def output_locale_binary_rdepends(name, pkgname, locale, encoding):
247 m = re.match("(.*)\.(.*)", name)
248 if m:
249 libc_name = "%s.%s" % (m.group(1), m.group(2).lower())
250 else:
251 libc_name = name
252 d.setVar('RDEPENDS_%s' % pkgname, legitimize_package_name('%s-binary-localedata-%s' \
253 % (mlprefix+bpn, libc_name)))
254
255 commands = {}
256
257 def output_locale_binary(name, pkgname, locale, encoding):
258 treedir = base_path_join(d.getVar("WORKDIR", True), "locale-tree")
259 ldlibdir = base_path_join(treedir, d.getVar("base_libdir", True))
260 path = d.getVar("PATH", True)
261 i18npath = base_path_join(treedir, datadir, "i18n")
262 gconvpath = base_path_join(treedir, "iconvdata")
263 outputpath = base_path_join(treedir, binary_locales_dir)
264
265 use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF", True) or "0"
266 if use_cross_localedef == "1":
267 target_arch = d.getVar('TARGET_ARCH', True)
268 locale_arch_options = { \
269 "arm": " --uint32-align=4 --little-endian ", \
270 "armeb": " --uint32-align=4 --big-endian ", \
271 "aarch64": " --uint32-align=4 --little-endian ", \
272 "aarch64_be": " --uint32-align=4 --big-endian ", \
273 "sh4": " --uint32-align=4 --big-endian ", \
274 "powerpc": " --uint32-align=4 --big-endian ", \
275 "powerpc64": " --uint32-align=4 --big-endian ", \
276 "mips": " --uint32-align=4 --big-endian ", \
277 "mips64": " --uint32-align=4 --big-endian ", \
278 "mipsel": " --uint32-align=4 --little-endian ", \
279 "mips64el":" --uint32-align=4 --little-endian ", \
280 "i586": " --uint32-align=4 --little-endian ", \
281 "i686": " --uint32-align=4 --little-endian ", \
282 "x86_64": " --uint32-align=4 --little-endian " }
283
284 if target_arch in locale_arch_options:
285 localedef_opts = locale_arch_options[target_arch]
286 else:
287 bb.error("locale_arch_options not found for target_arch=" + target_arch)
288 raise bb.build.FuncFailed("unknown arch:" + target_arch + " for locale_arch_options")
289
290 localedef_opts += " --force --old-style --no-archive --prefix=%s \
291 --inputfile=%s/%s/i18n/locales/%s --charmap=%s %s/%s" \
292 % (treedir, treedir, datadir, locale, encoding, outputpath, name)
293
294 cmd = "PATH=\"%s\" I18NPATH=\"%s\" GCONV_PATH=\"%s\" cross-localedef %s" % \
295 (path, i18npath, gconvpath, localedef_opts)
296 else: # earlier slower qemu way
297 qemu = qemu_target_binary(d)
298 localedef_opts = "--force --old-style --no-archive --prefix=%s \
299 --inputfile=%s/i18n/locales/%s --charmap=%s %s" \
300 % (treedir, datadir, locale, encoding, name)
301
302 qemu_options = d.getVar('QEMU_OPTIONS', True)
303
304 cmd = "PSEUDO_RELOADED=YES PATH=\"%s\" I18NPATH=\"%s\" %s -L %s \
305 -E LD_LIBRARY_PATH=%s %s %s/bin/localedef %s" % \
306 (path, i18npath, qemu, treedir, ldlibdir, qemu_options, treedir, localedef_opts)
307
308 commands["%s/%s" % (outputpath, name)] = cmd
309
310 bb.note("generating locale %s (%s)" % (locale, encoding))
311
312 def output_locale(name, locale, encoding):
313 pkgname = d.getVar('MLPREFIX', False) + 'locale-base-' + legitimize_package_name(name)
314 d.setVar('ALLOW_EMPTY_%s' % pkgname, '1')
315 d.setVar('PACKAGES', '%s %s' % (pkgname, d.getVar('PACKAGES', True)))
316 rprovides = ' %svirtual-locale-%s' % (mlprefix, legitimize_package_name(name))
317 m = re.match("(.*)_(.*)", name)
318 if m:
319 rprovides += ' %svirtual-locale-%s' % (mlprefix, m.group(1))
320 d.setVar('RPROVIDES_%s' % pkgname, rprovides)
321
322 if use_bin == "compile":
323 output_locale_binary_rdepends(name, pkgname, locale, encoding)
324 output_locale_binary(name, pkgname, locale, encoding)
325 elif use_bin == "precompiled":
326 output_locale_binary_rdepends(name, pkgname, locale, encoding)
327 else:
328 output_locale_source(name, pkgname, locale, encoding)
329
330 if use_bin == "compile":
331 bb.note("preparing tree for binary locale generation")
332 bb.build.exec_func("do_prep_locale_tree", d)
333
334 utf8_only = int(d.getVar('LOCALE_UTF8_ONLY', True) or 0)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500335 utf8_is_default = int(d.getVar('LOCALE_UTF8_IS_DEFAULT', True) or 0)
336
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500337 encodings = {}
338 for locale in to_generate:
339 charset = supported[locale]
340 if utf8_only and charset != 'UTF-8':
341 continue
342
343 m = dot_re.match(locale)
344 if m:
345 base = m.group(1)
346 else:
347 base = locale
348
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500349 # Non-precompiled locales may be renamed so that the default
350 # (non-suffixed) encoding is always UTF-8, i.e., instead of en_US and
351 # en_US.UTF-8, we have en_US and en_US.ISO-8859-1. This implicitly
352 # contradicts SUPPORTED.
353 if use_bin == "precompiled" or not utf8_is_default:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354 output_locale(locale, base, charset)
355 else:
356 if charset == 'UTF-8':
357 output_locale(base, base, charset)
358 else:
359 output_locale('%s.%s' % (base, charset), base, charset)
360
361 if use_bin == "compile":
362 makefile = base_path_join(d.getVar("WORKDIR", True), "locale-tree", "Makefile")
363 m = open(makefile, "w")
364 m.write("all: %s\n\n" % " ".join(commands.keys()))
365 for cmd in commands:
366 m.write(cmd + ":\n")
367 m.write("\t" + commands[cmd] + "\n\n")
368 m.close()
369 d.setVar("EXTRA_OEMAKE", "-C %s ${PARALLEL_MAKE}" % (os.path.dirname(makefile)))
370 bb.note("Executing binary locale generation makefile")
371 bb.build.exec_func("oe_runmake", d)
372 bb.note("collecting binary locales from locale tree")
373 bb.build.exec_func("do_collect_bins_from_locale_tree", d)
374 do_split_packages(d, binary_locales_dir, file_regex='(.*)', \
375 output_pattern=bpn+'-binary-localedata-%s', \
376 description='binary locale definition for %s', extra_depends='', allow_dirs=True)
377 elif use_bin == "precompiled":
378 do_split_packages(d, binary_locales_dir, file_regex='(.*)', \
379 output_pattern=bpn+'-binary-localedata-%s', \
380 description='binary locale definition for %s', extra_depends='', allow_dirs=True)
381 else:
382 bb.note("generation of binary locales disabled. this may break i18n!")
383
384}
385
386# We want to do this indirection so that we can safely 'return'
387# from the called function even though we're prepending
388python populate_packages_prepend () {
389 bb.build.exec_func('package_do_split_gconvs', d)
390}
391