Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 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 | |
| 10 | GLIBC_INTERNAL_USE_BINARY_LOCALE ?= "ondevice" |
| 11 | |
| 12 | python __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 | |
| 48 | OVERRIDES_append = ":${TARGET_ARCH}-${TARGET_OS}" |
| 49 | |
| 50 | do_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 |
| 59 | locale_base_postinst() { |
| 60 | #!/bin/sh |
| 61 | |
| 62 | if [ "x$D" != "x" ]; then |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | rm -rf ${TMP_LOCALE} |
| 67 | mkdir -p ${TMP_LOCALE} |
| 68 | if [ -f ${localedir}/locale-archive ]; then |
| 69 | cp ${localedir}/locale-archive ${TMP_LOCALE}/ |
| 70 | fi |
| 71 | localedef --inputfile=${datadir}/i18n/locales/%s --charmap=%s --prefix=/tmp/locale %s |
| 72 | mkdir -p ${localedir}/ |
| 73 | mv ${TMP_LOCALE}/locale-archive ${localedir}/ |
| 74 | rm -rf ${TMP_LOCALE} |
| 75 | } |
| 76 | |
| 77 | # indentation removed on purpose |
| 78 | locale_base_postrm() { |
| 79 | #!/bin/sh |
| 80 | |
| 81 | rm -rf ${TMP_LOCALE} |
| 82 | mkdir -p ${TMP_LOCALE} |
| 83 | if [ -f ${localedir}/locale-archive ]; then |
| 84 | cp ${localedir}/locale-archive ${TMP_LOCALE}/ |
| 85 | fi |
| 86 | localedef --delete-from-archive --inputfile=${datadir}/locales/%s --charmap=%s --prefix=/tmp/locale %s |
| 87 | mv ${TMP_LOCALE}/locale-archive ${localedir}/ |
| 88 | rm -rf ${TMP_LOCALE} |
| 89 | } |
| 90 | |
| 91 | |
| 92 | TMP_LOCALE="/tmp/locale${localedir}" |
| 93 | LOCALETREESRC ?= "${PKGD}" |
| 94 | |
| 95 | do_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 | |
| 111 | do_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 | |
| 119 | inherit qemu |
| 120 | |
| 121 | python 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame^] | 239 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | 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) |
| 335 | encodings = {} |
| 336 | for locale in to_generate: |
| 337 | charset = supported[locale] |
| 338 | if utf8_only and charset != 'UTF-8': |
| 339 | continue |
| 340 | |
| 341 | m = dot_re.match(locale) |
| 342 | if m: |
| 343 | base = m.group(1) |
| 344 | else: |
| 345 | base = locale |
| 346 | |
| 347 | # Precompiled locales are kept as is, obeying SUPPORTED, while |
| 348 | # others are adjusted, ensuring that the non-suffixed locales |
| 349 | # are utf-8, while the suffixed are not. |
| 350 | if use_bin == "precompiled": |
| 351 | output_locale(locale, base, charset) |
| 352 | else: |
| 353 | if charset == 'UTF-8': |
| 354 | output_locale(base, base, charset) |
| 355 | else: |
| 356 | output_locale('%s.%s' % (base, charset), base, charset) |
| 357 | |
| 358 | if use_bin == "compile": |
| 359 | makefile = base_path_join(d.getVar("WORKDIR", True), "locale-tree", "Makefile") |
| 360 | m = open(makefile, "w") |
| 361 | m.write("all: %s\n\n" % " ".join(commands.keys())) |
| 362 | for cmd in commands: |
| 363 | m.write(cmd + ":\n") |
| 364 | m.write("\t" + commands[cmd] + "\n\n") |
| 365 | m.close() |
| 366 | d.setVar("EXTRA_OEMAKE", "-C %s ${PARALLEL_MAKE}" % (os.path.dirname(makefile))) |
| 367 | bb.note("Executing binary locale generation makefile") |
| 368 | bb.build.exec_func("oe_runmake", d) |
| 369 | bb.note("collecting binary locales from locale tree") |
| 370 | bb.build.exec_func("do_collect_bins_from_locale_tree", d) |
| 371 | do_split_packages(d, binary_locales_dir, file_regex='(.*)', \ |
| 372 | output_pattern=bpn+'-binary-localedata-%s', \ |
| 373 | description='binary locale definition for %s', extra_depends='', allow_dirs=True) |
| 374 | elif use_bin == "precompiled": |
| 375 | do_split_packages(d, binary_locales_dir, file_regex='(.*)', \ |
| 376 | output_pattern=bpn+'-binary-localedata-%s', \ |
| 377 | description='binary locale definition for %s', extra_depends='', allow_dirs=True) |
| 378 | else: |
| 379 | bb.note("generation of binary locales disabled. this may break i18n!") |
| 380 | |
| 381 | } |
| 382 | |
| 383 | # We want to do this indirection so that we can safely 'return' |
| 384 | # from the called function even though we're prepending |
| 385 | python populate_packages_prepend () { |
| 386 | bb.build.exec_func('package_do_split_gconvs', d) |
| 387 | } |
| 388 | |