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 ", \ |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 271 | "x86_64": " --uint32-align=4 --little-endian ", \ |
| 272 | "loongarch64": " --uint32-align=4 --little-endian " } |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 273 | |
| 274 | if target_arch in locale_arch_options: |
| 275 | localedef_opts = locale_arch_options[target_arch] |
| 276 | else: |
| 277 | bb.error("locale_arch_options not found for target_arch=" + target_arch) |
| 278 | bb.fatal("unknown arch:" + target_arch + " for locale_arch_options") |
| 279 | |
| 280 | localedef_opts += " --force --no-hard-links --no-archive --prefix=%s \ |
| 281 | --inputfile=%s/%s/i18n/locales/%s --charmap=%s %s/%s" \ |
| 282 | % (treedir, treedir, datadir, locale, encoding, outputpath, name) |
| 283 | |
| 284 | cmd = "PATH=\"%s\" I18NPATH=\"%s\" GCONV_PATH=\"%s\" cross-localedef %s" % \ |
| 285 | (path, i18npath, gconvpath, localedef_opts) |
| 286 | else: # earlier slower qemu way |
| 287 | qemu = qemu_target_binary(d) |
| 288 | localedef_opts = "--force --no-hard-links --no-archive --prefix=%s \ |
| 289 | --inputfile=%s/i18n/locales/%s --charmap=%s %s" \ |
| 290 | % (treedir, datadir, locale, encoding, name) |
| 291 | |
| 292 | qemu_options = d.getVar('QEMU_OPTIONS') |
| 293 | |
| 294 | cmd = "PSEUDO_RELOADED=YES PATH=\"%s\" I18NPATH=\"%s\" %s -L %s \ |
| 295 | -E LD_LIBRARY_PATH=%s %s %s${base_bindir}/localedef %s" % \ |
| 296 | (path, i18npath, qemu, treedir, ldlibdir, qemu_options, treedir, localedef_opts) |
| 297 | |
| 298 | commands["%s/%s" % (outputpath, name)] = cmd |
| 299 | |
| 300 | bb.note("generating locale %s (%s)" % (locale, encoding)) |
| 301 | |
| 302 | def output_locale(name, locale, encoding): |
| 303 | pkgname = d.getVar('MLPREFIX', False) + 'locale-base-' + legitimize_package_name(name) |
| 304 | d.setVar('ALLOW_EMPTY:%s' % pkgname, '1') |
| 305 | d.setVar('PACKAGES', '%s %s' % (pkgname, d.getVar('PACKAGES'))) |
| 306 | rprovides = ' %svirtual-locale-%s' % (mlprefix, legitimize_package_name(name)) |
| 307 | m = re.match(r"(.*)_(.*)", name) |
| 308 | if m: |
| 309 | rprovides += ' %svirtual-locale-%s' % (mlprefix, m.group(1)) |
| 310 | d.setVar('RPROVIDES:%s' % pkgname, rprovides) |
| 311 | |
| 312 | if use_bin == "compile": |
| 313 | output_locale_binary_rdepends(name, pkgname, locale, encoding) |
| 314 | output_locale_binary(name, pkgname, locale, encoding) |
| 315 | elif use_bin == "precompiled": |
| 316 | output_locale_binary_rdepends(name, pkgname, locale, encoding) |
| 317 | else: |
| 318 | output_locale_source(name, pkgname, locale, encoding) |
| 319 | |
| 320 | if use_bin == "compile": |
| 321 | bb.note("preparing tree for binary locale generation") |
| 322 | bb.build.exec_func("do_prep_locale_tree", d) |
| 323 | |
| 324 | utf8_only = int(d.getVar('LOCALE_UTF8_ONLY') or 0) |
| 325 | utf8_is_default = int(d.getVar('LOCALE_UTF8_IS_DEFAULT') or 0) |
| 326 | |
| 327 | encodings = {} |
| 328 | for locale in to_generate: |
| 329 | charset = supported[locale] |
| 330 | if utf8_only and charset != 'UTF-8': |
| 331 | continue |
| 332 | |
| 333 | m = dot_re.match(locale) |
| 334 | if m: |
| 335 | base = m.group(1) |
| 336 | else: |
| 337 | base = locale |
| 338 | |
| 339 | # Non-precompiled locales may be renamed so that the default |
| 340 | # (non-suffixed) encoding is always UTF-8, i.e., instead of en_US and |
| 341 | # en_US.UTF-8, we have en_US and en_US.ISO-8859-1. This implicitly |
| 342 | # contradicts SUPPORTED. |
| 343 | if use_bin == "precompiled" or not utf8_is_default: |
| 344 | output_locale(locale, base, charset) |
| 345 | else: |
| 346 | if charset == 'UTF-8': |
| 347 | output_locale(base, base, charset) |
| 348 | else: |
| 349 | output_locale('%s.%s' % (base, charset), base, charset) |
| 350 | |
| 351 | def metapkg_hook(file, pkg, pattern, format, basename): |
| 352 | name = basename.split('/', 1)[0] |
| 353 | metapkg = legitimize_package_name('%s-binary-localedata-%s' % (mlprefix+bpn, name)) |
| 354 | d.appendVar('RDEPENDS:%s' % metapkg, ' ' + pkg) |
| 355 | |
| 356 | if use_bin == "compile": |
| 357 | makefile = oe.path.join(d.getVar("WORKDIR"), "locale-tree", "Makefile") |
| 358 | with open(makefile, "w") as m: |
| 359 | m.write("all: %s\n\n" % " ".join(commands.keys())) |
| 360 | total = len(commands) |
| 361 | for i, (maketarget, makerecipe) in enumerate(commands.items()): |
| 362 | m.write(maketarget + ":\n") |
| 363 | m.write("\t@echo 'Progress %d/%d'\n" % (i, total)) |
| 364 | m.write("\t" + makerecipe + "\n\n") |
| 365 | d.setVar("EXTRA_OEMAKE", "-C %s ${PARALLEL_MAKE}" % (os.path.dirname(makefile))) |
| 366 | d.setVarFlag("oe_runmake", "progress", r"outof:Progress\s(\d+)/(\d+)") |
| 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 | |
| 372 | if use_bin in ('compile', 'precompiled'): |
| 373 | lcsplit = d.getVar('GLIBC_SPLIT_LC_PACKAGES') |
| 374 | if lcsplit and int(lcsplit): |
| 375 | do_split_packages(d, binary_locales_dir, file_regex=r'^(.*/LC_\w+)', \ |
| 376 | output_pattern=bpn+'-binary-localedata-%s', \ |
| 377 | description='binary locale definition for %s', recursive=True, |
| 378 | hook=metapkg_hook, extra_depends='', allow_dirs=True, match_path=True) |
| 379 | else: |
| 380 | do_split_packages(d, binary_locales_dir, file_regex=r'(.*)', \ |
| 381 | output_pattern=bpn+'-binary-localedata-%s', \ |
| 382 | description='binary locale definition for %s', extra_depends='', allow_dirs=True) |
| 383 | else: |
| 384 | bb.note("generation of binary locales disabled. this may break i18n!") |
| 385 | |
| 386 | } |
| 387 | |
| 388 | # We want to do this indirection so that we can safely 'return' |
| 389 | # from the called function even though we're prepending |
| 390 | python populate_packages:prepend () { |
| 391 | bb.build.exec_func('package_do_split_gconvs', d) |
| 392 | } |