Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | # |
| 8 | # This class knows how to package up [e]glibc. Its shared since prebuild binary toolchains |
| 9 | # may need packaging and its pointless to duplicate this code. |
| 10 | # |
| 11 | # Caller should set GLIBC_INTERNAL_USE_BINARY_LOCALE to one of: |
| 12 | # "compile" - Use QEMU to generate the binary locale files |
| 13 | # "precompiled" - The binary locale files are pregenerated and already present |
| 14 | # "ondevice" - The device will build the locale files upon first boot through the postinst |
| 15 | |
| 16 | GLIBC_INTERNAL_USE_BINARY_LOCALE ?= "ondevice" |
| 17 | |
| 18 | GLIBC_SPLIT_LC_PACKAGES ?= "0" |
| 19 | |
| 20 | python __anonymous () { |
| 21 | enabled = d.getVar("ENABLE_BINARY_LOCALE_GENERATION") |
| 22 | |
| 23 | pn = d.getVar("PN") |
| 24 | if pn.endswith("-initial"): |
| 25 | enabled = False |
| 26 | |
| 27 | if enabled and int(enabled): |
| 28 | import re |
| 29 | |
| 30 | target_arch = d.getVar("TARGET_ARCH") |
| 31 | binary_arches = d.getVar("BINARY_LOCALE_ARCHES") or "" |
| 32 | use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF") or "" |
| 33 | |
| 34 | for regexp in binary_arches.split(" "): |
| 35 | r = re.compile(regexp) |
| 36 | |
| 37 | if r.match(target_arch): |
| 38 | depends = d.getVar("DEPENDS") |
| 39 | if use_cross_localedef == "1" : |
| 40 | depends = "%s cross-localedef-native" % depends |
| 41 | else: |
| 42 | depends = "%s qemu-native" % depends |
| 43 | d.setVar("DEPENDS", depends) |
| 44 | d.setVar("GLIBC_INTERNAL_USE_BINARY_LOCALE", "compile") |
| 45 | break |
| 46 | } |
| 47 | |
| 48 | # try to fix disable charsets/locales/locale-code compile fail |
| 49 | PACKAGE_NO_GCONV ?= "0" |
| 50 | |
| 51 | OVERRIDES:append = ":${TARGET_ARCH}-${TARGET_OS}" |
| 52 | |
| 53 | locale_base_postinst_ontarget() { |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame^] | 54 | mkdir ${libdir}/locale |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 55 | localedef --inputfile=${datadir}/i18n/locales/%s --charmap=%s %s |
| 56 | } |
| 57 | |
| 58 | locale_base_postrm() { |
| 59 | #!/bin/sh |
| 60 | localedef --delete-from-archive --inputfile=${datadir}/locales/%s --charmap=%s %s |
| 61 | } |
| 62 | |
| 63 | LOCALETREESRC ?= "${PKGD}" |
| 64 | |
| 65 | do_prep_locale_tree() { |
| 66 | treedir=${WORKDIR}/locale-tree |
| 67 | rm -rf $treedir |
| 68 | mkdir -p $treedir/${base_bindir} $treedir/${base_libdir} $treedir/${datadir} $treedir/${localedir} |
| 69 | tar -cf - -C ${LOCALETREESRC}${datadir} -p i18n | tar -xf - -C $treedir/${datadir} |
| 70 | # unzip to avoid parsing errors |
| 71 | for i in $treedir/${datadir}/i18n/charmaps/*gz; do |
| 72 | gunzip $i |
| 73 | done |
| 74 | # The extract pattern "./l*.so*" is carefully selected so that it will |
| 75 | # match ld*.so and lib*.so*, but not any files in the gconv directory |
| 76 | # (if it exists). This makes sure we only unpack the files we need. |
| 77 | # This is important in case usrmerge is set in DISTRO_FEATURES, which |
| 78 | # means ${base_libdir} == ${libdir}. |
| 79 | tar -cf - -C ${LOCALETREESRC}${base_libdir} -p . | tar -xf - -C $treedir/${base_libdir} --wildcards './l*.so*' |
| 80 | if [ -f ${STAGING_LIBDIR_NATIVE}/libgcc_s.* ]; then |
| 81 | tar -cf - -C ${STAGING_LIBDIR_NATIVE} -p libgcc_s.* | tar -xf - -C $treedir/${base_libdir} |
| 82 | fi |
| 83 | install -m 0755 ${LOCALETREESRC}${bindir}/localedef $treedir/${base_bindir} |
| 84 | } |
| 85 | |
| 86 | do_collect_bins_from_locale_tree() { |
| 87 | treedir=${WORKDIR}/locale-tree |
| 88 | |
| 89 | parent=$(dirname ${localedir}) |
| 90 | mkdir -p ${PKGD}/$parent |
| 91 | tar -cf - -C $treedir/$parent -p $(basename ${localedir}) | tar -xf - -C ${PKGD}$parent |
| 92 | |
| 93 | # Finalize tree by chaning all duplicate files into hard links |
| 94 | cross-localedef-hardlink -c -v ${WORKDIR}/locale-tree |
| 95 | } |
| 96 | |
| 97 | inherit qemu |
| 98 | |
| 99 | python package_do_split_gconvs () { |
| 100 | import re |
| 101 | if (d.getVar('PACKAGE_NO_GCONV') == '1'): |
| 102 | bb.note("package requested not splitting gconvs") |
| 103 | return |
| 104 | |
| 105 | if not d.getVar('PACKAGES'): |
| 106 | return |
| 107 | |
| 108 | mlprefix = d.getVar("MLPREFIX") or "" |
| 109 | |
| 110 | bpn = d.getVar('BPN') |
| 111 | libdir = d.getVar('libdir') |
| 112 | if not libdir: |
| 113 | bb.error("libdir not defined") |
| 114 | return |
| 115 | datadir = d.getVar('datadir') |
| 116 | if not datadir: |
| 117 | bb.error("datadir not defined") |
| 118 | return |
| 119 | |
| 120 | gconv_libdir = oe.path.join(libdir, "gconv") |
| 121 | charmap_dir = oe.path.join(datadir, "i18n", "charmaps") |
| 122 | locales_dir = oe.path.join(datadir, "i18n", "locales") |
| 123 | binary_locales_dir = d.getVar('localedir') |
| 124 | |
| 125 | def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group): |
| 126 | deps = [] |
| 127 | f = open(fn, "rb") |
| 128 | c_re = re.compile(r'^copy "(.*)"') |
| 129 | i_re = re.compile(r'^include "(\w+)".*') |
| 130 | for l in f.readlines(): |
| 131 | l = l.decode("latin-1") |
| 132 | m = c_re.match(l) or i_re.match(l) |
| 133 | if m: |
| 134 | dp = legitimize_package_name('%s%s-gconv-%s' % (mlprefix, bpn, m.group(1))) |
| 135 | if not dp in deps: |
| 136 | deps.append(dp) |
| 137 | f.close() |
| 138 | if deps != []: |
| 139 | d.setVar('RDEPENDS:%s' % pkg, " ".join(deps)) |
| 140 | if bpn != 'glibc': |
| 141 | d.setVar('RPROVIDES:%s' % pkg, pkg.replace(bpn, 'glibc')) |
| 142 | |
| 143 | do_split_packages(d, gconv_libdir, file_regex=r'^(.*)\.so$', output_pattern=bpn+'-gconv-%s', \ |
| 144 | description='gconv module for character set %s', hook=calc_gconv_deps, \ |
| 145 | extra_depends=bpn+'-gconv') |
| 146 | |
| 147 | def calc_charmap_deps(fn, pkg, file_regex, output_pattern, group): |
| 148 | deps = [] |
| 149 | f = open(fn, "rb") |
| 150 | c_re = re.compile(r'^copy "(.*)"') |
| 151 | i_re = re.compile(r'^include "(\w+)".*') |
| 152 | for l in f.readlines(): |
| 153 | l = l.decode("latin-1") |
| 154 | m = c_re.match(l) or i_re.match(l) |
| 155 | if m: |
| 156 | dp = legitimize_package_name('%s%s-charmap-%s' % (mlprefix, bpn, m.group(1))) |
| 157 | if not dp in deps: |
| 158 | deps.append(dp) |
| 159 | f.close() |
| 160 | if deps != []: |
| 161 | d.setVar('RDEPENDS:%s' % pkg, " ".join(deps)) |
| 162 | if bpn != 'glibc': |
| 163 | d.setVar('RPROVIDES:%s' % pkg, pkg.replace(bpn, 'glibc')) |
| 164 | |
| 165 | do_split_packages(d, charmap_dir, file_regex=r'^(.*)\.gz$', output_pattern=bpn+'-charmap-%s', \ |
| 166 | description='character map for %s encoding', hook=calc_charmap_deps, extra_depends='') |
| 167 | |
| 168 | def calc_locale_deps(fn, pkg, file_regex, output_pattern, group): |
| 169 | deps = [] |
| 170 | f = open(fn, "rb") |
| 171 | c_re = re.compile(r'^copy "(.*)"') |
| 172 | i_re = re.compile(r'^include "(\w+)".*') |
| 173 | for l in f.readlines(): |
| 174 | l = l.decode("latin-1") |
| 175 | m = c_re.match(l) or i_re.match(l) |
| 176 | if m: |
| 177 | dp = legitimize_package_name(mlprefix+bpn+'-localedata-%s' % m.group(1)) |
| 178 | if not dp in deps: |
| 179 | deps.append(dp) |
| 180 | f.close() |
| 181 | if deps != []: |
| 182 | d.setVar('RDEPENDS:%s' % pkg, " ".join(deps)) |
| 183 | if bpn != 'glibc': |
| 184 | d.setVar('RPROVIDES:%s' % pkg, pkg.replace(bpn, 'glibc')) |
| 185 | |
| 186 | do_split_packages(d, locales_dir, file_regex=r'(.*)', output_pattern=bpn+'-localedata-%s', \ |
| 187 | description='locale definition for %s', hook=calc_locale_deps, extra_depends='') |
| 188 | d.setVar('PACKAGES', d.getVar('PACKAGES', False) + ' ' + d.getVar('MLPREFIX', False) + bpn + '-gconv') |
| 189 | |
| 190 | use_bin = d.getVar("GLIBC_INTERNAL_USE_BINARY_LOCALE") |
| 191 | |
| 192 | dot_re = re.compile(r"(.*)\.(.*)") |
| 193 | |
| 194 | # Read in supported locales and associated encodings |
| 195 | supported = {} |
| 196 | with open(oe.path.join(d.getVar('WORKDIR'), "SUPPORTED")) as f: |
| 197 | for line in f.readlines(): |
| 198 | try: |
| 199 | locale, charset = line.rstrip().split() |
| 200 | except ValueError: |
| 201 | continue |
| 202 | supported[locale] = charset |
| 203 | |
| 204 | # GLIBC_GENERATE_LOCALES var specifies which locales to be generated. empty or "all" means all locales |
| 205 | to_generate = d.getVar('GLIBC_GENERATE_LOCALES') |
| 206 | if not to_generate or to_generate == 'all': |
| 207 | to_generate = sorted(supported.keys()) |
| 208 | else: |
| 209 | to_generate = to_generate.split() |
| 210 | for locale in to_generate: |
| 211 | if locale not in supported: |
| 212 | if '.' in locale: |
| 213 | charset = locale.split('.')[1] |
| 214 | else: |
| 215 | charset = 'UTF-8' |
| 216 | bb.warn("Unsupported locale '%s', assuming encoding '%s'" % (locale, charset)) |
| 217 | supported[locale] = charset |
| 218 | |
| 219 | def output_locale_source(name, pkgname, locale, encoding): |
| 220 | d.setVar('RDEPENDS:%s' % pkgname, '%slocaledef %s-localedata-%s %s-charmap-%s' % \ |
| 221 | (mlprefix, mlprefix+bpn, legitimize_package_name(locale), mlprefix+bpn, legitimize_package_name(encoding))) |
| 222 | d.setVar('pkg_postinst_ontarget:%s' % pkgname, d.getVar('locale_base_postinst_ontarget') \ |
| 223 | % (locale, encoding, locale)) |
| 224 | d.setVar('pkg_postrm:%s' % pkgname, d.getVar('locale_base_postrm') % \ |
| 225 | (locale, encoding, locale)) |
| 226 | |
| 227 | def output_locale_binary_rdepends(name, pkgname, locale, encoding): |
| 228 | dep = legitimize_package_name('%s-binary-localedata-%s' % (bpn, name)) |
| 229 | lcsplit = d.getVar('GLIBC_SPLIT_LC_PACKAGES') |
| 230 | if lcsplit and int(lcsplit): |
| 231 | d.appendVar('PACKAGES', ' ' + dep) |
| 232 | d.setVar('ALLOW_EMPTY:%s' % dep, '1') |
| 233 | d.setVar('RDEPENDS:%s' % pkgname, mlprefix + dep) |
| 234 | |
| 235 | commands = {} |
| 236 | |
| 237 | def output_locale_binary(name, pkgname, locale, encoding): |
| 238 | treedir = oe.path.join(d.getVar("WORKDIR"), "locale-tree") |
| 239 | ldlibdir = oe.path.join(treedir, d.getVar("base_libdir")) |
| 240 | path = d.getVar("PATH") |
| 241 | i18npath = oe.path.join(treedir, datadir, "i18n") |
| 242 | gconvpath = oe.path.join(treedir, "iconvdata") |
| 243 | outputpath = oe.path.join(treedir, binary_locales_dir) |
| 244 | |
| 245 | use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF") or "0" |
| 246 | if use_cross_localedef == "1": |
| 247 | target_arch = d.getVar('TARGET_ARCH') |
| 248 | locale_arch_options = { \ |
| 249 | "arc": " --uint32-align=4 --little-endian ", \ |
| 250 | "arceb": " --uint32-align=4 --big-endian ", \ |
| 251 | "arm": " --uint32-align=4 --little-endian ", \ |
| 252 | "armeb": " --uint32-align=4 --big-endian ", \ |
| 253 | "aarch64": " --uint32-align=4 --little-endian ", \ |
| 254 | "aarch64_be": " --uint32-align=4 --big-endian ", \ |
| 255 | "sh4": " --uint32-align=4 --big-endian ", \ |
| 256 | "powerpc": " --uint32-align=4 --big-endian ", \ |
| 257 | "powerpc64": " --uint32-align=4 --big-endian ", \ |
| 258 | "powerpc64le": " --uint32-align=4 --little-endian ", \ |
| 259 | "mips": " --uint32-align=4 --big-endian ", \ |
| 260 | "mipsisa32r6": " --uint32-align=4 --big-endian ", \ |
| 261 | "mips64": " --uint32-align=4 --big-endian ", \ |
| 262 | "mipsisa64r6": " --uint32-align=4 --big-endian ", \ |
| 263 | "mipsel": " --uint32-align=4 --little-endian ", \ |
| 264 | "mipsisa32r6el": " --uint32-align=4 --little-endian ", \ |
| 265 | "mips64el":" --uint32-align=4 --little-endian ", \ |
| 266 | "mipsisa64r6el":" --uint32-align=4 --little-endian ", \ |
| 267 | "riscv64": " --uint32-align=4 --little-endian ", \ |
| 268 | "riscv32": " --uint32-align=4 --little-endian ", \ |
| 269 | "i586": " --uint32-align=4 --little-endian ", \ |
| 270 | "i686": " --uint32-align=4 --little-endian ", \ |
| 271 | "x86_64": " --uint32-align=4 --little-endian " } |
| 272 | |
| 273 | if target_arch in locale_arch_options: |
| 274 | localedef_opts = locale_arch_options[target_arch] |
| 275 | else: |
| 276 | bb.error("locale_arch_options not found for target_arch=" + target_arch) |
| 277 | bb.fatal("unknown arch:" + target_arch + " for locale_arch_options") |
| 278 | |
| 279 | localedef_opts += " --force --no-hard-links --no-archive --prefix=%s \ |
| 280 | --inputfile=%s/%s/i18n/locales/%s --charmap=%s %s/%s" \ |
| 281 | % (treedir, treedir, datadir, locale, encoding, outputpath, name) |
| 282 | |
| 283 | cmd = "PATH=\"%s\" I18NPATH=\"%s\" GCONV_PATH=\"%s\" cross-localedef %s" % \ |
| 284 | (path, i18npath, gconvpath, localedef_opts) |
| 285 | else: # earlier slower qemu way |
| 286 | qemu = qemu_target_binary(d) |
| 287 | localedef_opts = "--force --no-hard-links --no-archive --prefix=%s \ |
| 288 | --inputfile=%s/i18n/locales/%s --charmap=%s %s" \ |
| 289 | % (treedir, datadir, locale, encoding, name) |
| 290 | |
| 291 | qemu_options = d.getVar('QEMU_OPTIONS') |
| 292 | |
| 293 | cmd = "PSEUDO_RELOADED=YES PATH=\"%s\" I18NPATH=\"%s\" %s -L %s \ |
| 294 | -E LD_LIBRARY_PATH=%s %s %s${base_bindir}/localedef %s" % \ |
| 295 | (path, i18npath, qemu, treedir, ldlibdir, qemu_options, treedir, localedef_opts) |
| 296 | |
| 297 | commands["%s/%s" % (outputpath, name)] = cmd |
| 298 | |
| 299 | bb.note("generating locale %s (%s)" % (locale, encoding)) |
| 300 | |
| 301 | def output_locale(name, locale, encoding): |
| 302 | pkgname = d.getVar('MLPREFIX', False) + 'locale-base-' + legitimize_package_name(name) |
| 303 | d.setVar('ALLOW_EMPTY:%s' % pkgname, '1') |
| 304 | d.setVar('PACKAGES', '%s %s' % (pkgname, d.getVar('PACKAGES'))) |
| 305 | rprovides = ' %svirtual-locale-%s' % (mlprefix, legitimize_package_name(name)) |
| 306 | m = re.match(r"(.*)_(.*)", name) |
| 307 | if m: |
| 308 | rprovides += ' %svirtual-locale-%s' % (mlprefix, m.group(1)) |
| 309 | d.setVar('RPROVIDES:%s' % pkgname, rprovides) |
| 310 | |
| 311 | if use_bin == "compile": |
| 312 | output_locale_binary_rdepends(name, pkgname, locale, encoding) |
| 313 | output_locale_binary(name, pkgname, locale, encoding) |
| 314 | elif use_bin == "precompiled": |
| 315 | output_locale_binary_rdepends(name, pkgname, locale, encoding) |
| 316 | else: |
| 317 | output_locale_source(name, pkgname, locale, encoding) |
| 318 | |
| 319 | if use_bin == "compile": |
| 320 | bb.note("preparing tree for binary locale generation") |
| 321 | bb.build.exec_func("do_prep_locale_tree", d) |
| 322 | |
| 323 | utf8_only = int(d.getVar('LOCALE_UTF8_ONLY') or 0) |
| 324 | utf8_is_default = int(d.getVar('LOCALE_UTF8_IS_DEFAULT') or 0) |
| 325 | |
| 326 | encodings = {} |
| 327 | for locale in to_generate: |
| 328 | charset = supported[locale] |
| 329 | if utf8_only and charset != 'UTF-8': |
| 330 | continue |
| 331 | |
| 332 | m = dot_re.match(locale) |
| 333 | if m: |
| 334 | base = m.group(1) |
| 335 | else: |
| 336 | base = locale |
| 337 | |
| 338 | # Non-precompiled locales may be renamed so that the default |
| 339 | # (non-suffixed) encoding is always UTF-8, i.e., instead of en_US and |
| 340 | # en_US.UTF-8, we have en_US and en_US.ISO-8859-1. This implicitly |
| 341 | # contradicts SUPPORTED. |
| 342 | if use_bin == "precompiled" or not utf8_is_default: |
| 343 | output_locale(locale, base, charset) |
| 344 | else: |
| 345 | if charset == 'UTF-8': |
| 346 | output_locale(base, base, charset) |
| 347 | else: |
| 348 | output_locale('%s.%s' % (base, charset), base, charset) |
| 349 | |
| 350 | def metapkg_hook(file, pkg, pattern, format, basename): |
| 351 | name = basename.split('/', 1)[0] |
| 352 | metapkg = legitimize_package_name('%s-binary-localedata-%s' % (mlprefix+bpn, name)) |
| 353 | d.appendVar('RDEPENDS:%s' % metapkg, ' ' + pkg) |
| 354 | |
| 355 | if use_bin == "compile": |
| 356 | makefile = oe.path.join(d.getVar("WORKDIR"), "locale-tree", "Makefile") |
| 357 | with open(makefile, "w") as m: |
| 358 | m.write("all: %s\n\n" % " ".join(commands.keys())) |
| 359 | total = len(commands) |
| 360 | for i, (maketarget, makerecipe) in enumerate(commands.items()): |
| 361 | m.write(maketarget + ":\n") |
| 362 | m.write("\t@echo 'Progress %d/%d'\n" % (i, total)) |
| 363 | m.write("\t" + makerecipe + "\n\n") |
| 364 | d.setVar("EXTRA_OEMAKE", "-C %s ${PARALLEL_MAKE}" % (os.path.dirname(makefile))) |
| 365 | d.setVarFlag("oe_runmake", "progress", r"outof:Progress\s(\d+)/(\d+)") |
| 366 | bb.note("Executing binary locale generation makefile") |
| 367 | bb.build.exec_func("oe_runmake", d) |
| 368 | bb.note("collecting binary locales from locale tree") |
| 369 | bb.build.exec_func("do_collect_bins_from_locale_tree", d) |
| 370 | |
| 371 | if use_bin in ('compile', 'precompiled'): |
| 372 | lcsplit = d.getVar('GLIBC_SPLIT_LC_PACKAGES') |
| 373 | if lcsplit and int(lcsplit): |
| 374 | do_split_packages(d, binary_locales_dir, file_regex=r'^(.*/LC_\w+)', \ |
| 375 | output_pattern=bpn+'-binary-localedata-%s', \ |
| 376 | description='binary locale definition for %s', recursive=True, |
| 377 | hook=metapkg_hook, extra_depends='', allow_dirs=True, match_path=True) |
| 378 | else: |
| 379 | do_split_packages(d, binary_locales_dir, file_regex=r'(.*)', \ |
| 380 | output_pattern=bpn+'-binary-localedata-%s', \ |
| 381 | description='binary locale definition for %s', extra_depends='', allow_dirs=True) |
| 382 | else: |
| 383 | bb.note("generation of binary locales disabled. this may break i18n!") |
| 384 | |
| 385 | } |
| 386 | |
| 387 | # We want to do this indirection so that we can safely 'return' |
| 388 | # from the called function even though we're prepending |
| 389 | python populate_packages:prepend () { |
| 390 | bb.build.exec_func('package_do_split_gconvs', d) |
| 391 | } |