libc-package.bbclass 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. GLIBC_INTERNAL_USE_BINARY_LOCALE ?= "ondevice"
  10. python __anonymous () {
  11. enabled = d.getVar("ENABLE_BINARY_LOCALE_GENERATION", True)
  12. pn = d.getVar("PN", True)
  13. if pn.endswith("-initial"):
  14. enabled = False
  15. if enabled and int(enabled):
  16. import re
  17. target_arch = d.getVar("TARGET_ARCH", True)
  18. binary_arches = d.getVar("BINARY_LOCALE_ARCHES", True) or ""
  19. use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF", True) or ""
  20. for regexp in binary_arches.split(" "):
  21. r = re.compile(regexp)
  22. if r.match(target_arch):
  23. depends = d.getVar("DEPENDS", True)
  24. if use_cross_localedef == "1" :
  25. depends = "%s cross-localedef-native" % depends
  26. else:
  27. depends = "%s qemu-native" % depends
  28. d.setVar("DEPENDS", depends)
  29. d.setVar("GLIBC_INTERNAL_USE_BINARY_LOCALE", "compile")
  30. break
  31. distro_features = (d.getVar('DISTRO_FEATURES', True) or '').split()
  32. # try to fix disable charsets/locales/locale-code compile fail
  33. if 'libc-charsets' in distro_features and 'libc-locales' in distro_features and 'libc-locale-code' in distro_features:
  34. d.setVar('PACKAGE_NO_GCONV', '0')
  35. else:
  36. d.setVar('PACKAGE_NO_GCONV', '1')
  37. }
  38. OVERRIDES_append = ":${TARGET_ARCH}-${TARGET_OS}"
  39. do_configure_prepend() {
  40. if [ -e ${S}/elf/ldd.bash.in ]; then
  41. sed -e "s#@BASH@#/bin/sh#" -i ${S}/elf/ldd.bash.in
  42. fi
  43. }
  44. # indentation removed on purpose
  45. locale_base_postinst() {
  46. #!/bin/sh
  47. if [ "x$D" != "x" ]; then
  48. exit 1
  49. fi
  50. rm -rf ${TMP_LOCALE}
  51. mkdir -p ${TMP_LOCALE}
  52. if [ -f ${libdir}/locale/locale-archive ]; then
  53. cp ${libdir}/locale/locale-archive ${TMP_LOCALE}/
  54. fi
  55. localedef --inputfile=${datadir}/i18n/locales/%s --charmap=%s --prefix=/tmp/locale %s
  56. mkdir -p ${libdir}/locale/
  57. mv ${TMP_LOCALE}/locale-archive ${libdir}/locale/
  58. rm -rf ${TMP_LOCALE}
  59. }
  60. # indentation removed on purpose
  61. locale_base_postrm() {
  62. #!/bin/sh
  63. rm -rf ${TMP_LOCALE}
  64. mkdir -p ${TMP_LOCALE}
  65. if [ -f ${libdir}/locale/locale-archive ]; then
  66. cp ${libdir}/locale/locale-archive ${TMP_LOCALE}/
  67. fi
  68. localedef --delete-from-archive --inputfile=${datadir}/locales/%s --charmap=%s --prefix=/tmp/locale %s
  69. mv ${TMP_LOCALE}/locale-archive ${libdir}/locale/
  70. rm -rf ${TMP_LOCALE}
  71. }
  72. TMP_LOCALE="/tmp/locale${libdir}/locale"
  73. LOCALETREESRC ?= "${PKGD}"
  74. do_prep_locale_tree() {
  75. treedir=${WORKDIR}/locale-tree
  76. rm -rf $treedir
  77. mkdir -p $treedir/${base_bindir} $treedir/${base_libdir} $treedir/${datadir} $treedir/${libdir}/locale
  78. tar -cf - -C ${LOCALETREESRC}${datadir} -ps i18n | tar -xf - -C $treedir/${datadir}
  79. # unzip to avoid parsing errors
  80. for i in $treedir/${datadir}/i18n/charmaps/*gz; do
  81. gunzip $i
  82. done
  83. tar -cf - -C ${LOCALETREESRC}${base_libdir} -ps . | tar -xf - -C $treedir/${base_libdir}
  84. if [ -f ${STAGING_DIR_NATIVE}${prefix_native}/lib/libgcc_s.* ]; then
  85. tar -cf - -C ${STAGING_DIR_NATIVE}/${prefix_native}/${base_libdir} -ps libgcc_s.* | tar -xf - -C $treedir/${base_libdir}
  86. fi
  87. install -m 0755 ${LOCALETREESRC}${bindir}/localedef $treedir/${base_bindir}
  88. }
  89. do_collect_bins_from_locale_tree() {
  90. treedir=${WORKDIR}/locale-tree
  91. mkdir -p ${PKGD}${libdir}
  92. tar -cf - -C $treedir/${libdir} -ps locale | tar -xf - -C ${PKGD}${libdir}
  93. }
  94. inherit qemu
  95. python package_do_split_gconvs () {
  96. import re
  97. if (d.getVar('PACKAGE_NO_GCONV', True) == '1'):
  98. bb.note("package requested not splitting gconvs")
  99. return
  100. if not d.getVar('PACKAGES', True):
  101. return
  102. mlprefix = d.getVar("MLPREFIX", True) or ""
  103. bpn = d.getVar('BPN', True)
  104. libdir = d.getVar('libdir', True)
  105. if not libdir:
  106. bb.error("libdir not defined")
  107. return
  108. datadir = d.getVar('datadir', True)
  109. if not datadir:
  110. bb.error("datadir not defined")
  111. return
  112. gconv_libdir = base_path_join(libdir, "gconv")
  113. charmap_dir = base_path_join(datadir, "i18n", "charmaps")
  114. locales_dir = base_path_join(datadir, "i18n", "locales")
  115. binary_locales_dir = base_path_join(libdir, "locale")
  116. def calc_gconv_deps(fn, pkg, file_regex, output_pattern, group):
  117. deps = []
  118. f = open(fn, "r")
  119. c_re = re.compile('^copy "(.*)"')
  120. i_re = re.compile('^include "(\w+)".*')
  121. for l in f.readlines():
  122. m = c_re.match(l) or i_re.match(l)
  123. if m:
  124. dp = legitimize_package_name('%s%s-gconv-%s' % (mlprefix, bpn, m.group(1)))
  125. if not dp in deps:
  126. deps.append(dp)
  127. f.close()
  128. if deps != []:
  129. d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
  130. if bpn != 'glibc':
  131. d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
  132. do_split_packages(d, gconv_libdir, file_regex='^(.*)\.so$', output_pattern=bpn+'-gconv-%s', \
  133. description='gconv module for character set %s', hook=calc_gconv_deps, \
  134. extra_depends=bpn+'-gconv')
  135. def calc_charmap_deps(fn, pkg, file_regex, output_pattern, group):
  136. deps = []
  137. f = open(fn, "r")
  138. c_re = re.compile('^copy "(.*)"')
  139. i_re = re.compile('^include "(\w+)".*')
  140. for l in f.readlines():
  141. m = c_re.match(l) or i_re.match(l)
  142. if m:
  143. dp = legitimize_package_name('%s%s-charmap-%s' % (mlprefix, bpn, m.group(1)))
  144. if not dp in deps:
  145. deps.append(dp)
  146. f.close()
  147. if deps != []:
  148. d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
  149. if bpn != 'glibc':
  150. d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
  151. do_split_packages(d, charmap_dir, file_regex='^(.*)\.gz$', output_pattern=bpn+'-charmap-%s', \
  152. description='character map for %s encoding', hook=calc_charmap_deps, extra_depends='')
  153. def calc_locale_deps(fn, pkg, file_regex, output_pattern, group):
  154. deps = []
  155. f = open(fn, "r")
  156. c_re = re.compile('^copy "(.*)"')
  157. i_re = re.compile('^include "(\w+)".*')
  158. for l in f.readlines():
  159. m = c_re.match(l) or i_re.match(l)
  160. if m:
  161. dp = legitimize_package_name(mlprefix+bpn+'-localedata-%s' % m.group(1))
  162. if not dp in deps:
  163. deps.append(dp)
  164. f.close()
  165. if deps != []:
  166. d.setVar('RDEPENDS_%s' % pkg, " ".join(deps))
  167. if bpn != 'glibc':
  168. d.setVar('RPROVIDES_%s' % pkg, pkg.replace(bpn, 'glibc'))
  169. do_split_packages(d, locales_dir, file_regex='(.*)', output_pattern=bpn+'-localedata-%s', \
  170. description='locale definition for %s', hook=calc_locale_deps, extra_depends='')
  171. d.setVar('PACKAGES', d.getVar('PACKAGES') + ' ' + d.getVar('MLPREFIX') + bpn + '-gconv')
  172. use_bin = d.getVar("GLIBC_INTERNAL_USE_BINARY_LOCALE", True)
  173. dot_re = re.compile("(.*)\.(.*)")
  174. # Read in supported locales and associated encodings
  175. supported = {}
  176. with open(base_path_join(d.getVar('WORKDIR', True), "SUPPORTED")) as f:
  177. for line in f.readlines():
  178. try:
  179. locale, charset = line.rstrip().split()
  180. except ValueError:
  181. continue
  182. supported[locale] = charset
  183. # GLIBC_GENERATE_LOCALES var specifies which locales to be generated. empty or "all" means all locales
  184. to_generate = d.getVar('GLIBC_GENERATE_LOCALES', True)
  185. if not to_generate or to_generate == 'all':
  186. to_generate = supported.keys()
  187. else:
  188. to_generate = to_generate.split()
  189. for locale in to_generate:
  190. if locale not in supported:
  191. if '.' in locale:
  192. charset = locale.split('.')[1]
  193. else:
  194. charset = 'UTF-8'
  195. bb.warn("Unsupported locale '%s', assuming encoding '%s'" % (locale, charset))
  196. supported[locale] = charset
  197. def output_locale_source(name, pkgname, locale, encoding):
  198. d.setVar('RDEPENDS_%s' % pkgname, 'localedef %s-localedata-%s %s-charmap-%s' % \
  199. (mlprefix+bpn, legitimize_package_name(locale), mlprefix+bpn, legitimize_package_name(encoding)))
  200. d.setVar('pkg_postinst_%s' % pkgname, d.getVar('locale_base_postinst', True) \
  201. % (locale, encoding, locale))
  202. d.setVar('pkg_postrm_%s' % pkgname, d.getVar('locale_base_postrm', True) % \
  203. (locale, encoding, locale))
  204. def output_locale_binary_rdepends(name, pkgname, locale, encoding):
  205. m = re.match("(.*)\.(.*)", name)
  206. if m:
  207. libc_name = "%s.%s" % (m.group(1), m.group(2).lower().replace("-",""))
  208. else:
  209. libc_name = name
  210. d.setVar('RDEPENDS_%s' % pkgname, legitimize_package_name('%s-binary-localedata-%s' \
  211. % (mlprefix+bpn, libc_name)))
  212. commands = {}
  213. def output_locale_binary(name, pkgname, locale, encoding):
  214. treedir = base_path_join(d.getVar("WORKDIR", True), "locale-tree")
  215. ldlibdir = base_path_join(treedir, d.getVar("base_libdir", True))
  216. path = d.getVar("PATH", True)
  217. i18npath = base_path_join(treedir, datadir, "i18n")
  218. gconvpath = base_path_join(treedir, "iconvdata")
  219. outputpath = base_path_join(treedir, libdir, "locale")
  220. use_cross_localedef = d.getVar("LOCALE_GENERATION_WITH_CROSS-LOCALEDEF", True) or "0"
  221. if use_cross_localedef == "1":
  222. target_arch = d.getVar('TARGET_ARCH', True)
  223. locale_arch_options = { \
  224. "arm": " --uint32-align=4 --little-endian ", \
  225. "sh4": " --uint32-align=4 --big-endian ", \
  226. "powerpc": " --uint32-align=4 --big-endian ", \
  227. "powerpc64": " --uint32-align=4 --big-endian ", \
  228. "mips": " --uint32-align=4 --big-endian ", \
  229. "mips64": " --uint32-align=4 --big-endian ", \
  230. "mipsel": " --uint32-align=4 --little-endian ", \
  231. "mips64el":" --uint32-align=4 --little-endian ", \
  232. "i586": " --uint32-align=4 --little-endian ", \
  233. "i686": " --uint32-align=4 --little-endian ", \
  234. "x86_64": " --uint32-align=4 --little-endian " }
  235. if target_arch in locale_arch_options:
  236. localedef_opts = locale_arch_options[target_arch]
  237. else:
  238. bb.error("locale_arch_options not found for target_arch=" + target_arch)
  239. raise bb.build.FuncFailed("unknown arch:" + target_arch + " for locale_arch_options")
  240. localedef_opts += " --force --old-style --no-archive --prefix=%s \
  241. --inputfile=%s/%s/i18n/locales/%s --charmap=%s %s/%s" \
  242. % (treedir, treedir, datadir, locale, encoding, outputpath, name)
  243. cmd = "PATH=\"%s\" I18NPATH=\"%s\" GCONV_PATH=\"%s\" cross-localedef %s" % \
  244. (path, i18npath, gconvpath, localedef_opts)
  245. else: # earlier slower qemu way
  246. qemu = qemu_target_binary(d)
  247. localedef_opts = "--force --old-style --no-archive --prefix=%s \
  248. --inputfile=%s/i18n/locales/%s --charmap=%s %s" \
  249. % (treedir, datadir, locale, encoding, name)
  250. qemu_options = d.getVar("QEMU_OPTIONS_%s" % d.getVar('PACKAGE_ARCH', True), True)
  251. if not qemu_options:
  252. qemu_options = d.getVar('QEMU_OPTIONS', True)
  253. cmd = "PSEUDO_RELOADED=YES PATH=\"%s\" I18NPATH=\"%s\" %s -L %s \
  254. -E LD_LIBRARY_PATH=%s %s %s/bin/localedef %s" % \
  255. (path, i18npath, qemu, treedir, ldlibdir, qemu_options, treedir, localedef_opts)
  256. commands["%s/%s" % (outputpath, name)] = cmd
  257. bb.note("generating locale %s (%s)" % (locale, encoding))
  258. def output_locale(name, locale, encoding):
  259. pkgname = d.getVar('MLPREFIX') + 'locale-base-' + legitimize_package_name(name)
  260. d.setVar('ALLOW_EMPTY_%s' % pkgname, '1')
  261. d.setVar('PACKAGES', '%s %s' % (pkgname, d.getVar('PACKAGES', True)))
  262. rprovides = ' %svirtual-locale-%s' % (mlprefix, legitimize_package_name(name))
  263. m = re.match("(.*)_(.*)", name)
  264. if m:
  265. rprovides += ' %svirtual-locale-%s' % (mlprefix, m.group(1))
  266. d.setVar('RPROVIDES_%s' % pkgname, rprovides)
  267. if use_bin == "compile":
  268. output_locale_binary_rdepends(name, pkgname, locale, encoding)
  269. output_locale_binary(name, pkgname, locale, encoding)
  270. elif use_bin == "precompiled":
  271. output_locale_binary_rdepends(name, pkgname, locale, encoding)
  272. else:
  273. output_locale_source(name, pkgname, locale, encoding)
  274. if use_bin == "compile":
  275. bb.note("preparing tree for binary locale generation")
  276. bb.build.exec_func("do_prep_locale_tree", d)
  277. utf8_only = int(d.getVar('LOCALE_UTF8_ONLY', True) or 0)
  278. encodings = {}
  279. for locale in to_generate:
  280. charset = supported[locale]
  281. if utf8_only and charset != 'UTF-8':
  282. continue
  283. m = dot_re.match(locale)
  284. if m:
  285. base = m.group(1)
  286. else:
  287. base = locale
  288. # Precompiled locales are kept as is, obeying SUPPORTED, while
  289. # others are adjusted, ensuring that the non-suffixed locales
  290. # are utf-8, while the suffixed are not.
  291. if use_bin == "precompiled":
  292. output_locale(locale, base, charset)
  293. else:
  294. if charset == 'UTF-8':
  295. output_locale(base, base, charset)
  296. else:
  297. output_locale('%s.%s' % (base, charset), base, charset)
  298. if use_bin == "compile":
  299. makefile = base_path_join(d.getVar("WORKDIR", True), "locale-tree", "Makefile")
  300. m = open(makefile, "w")
  301. m.write("all: %s\n\n" % " ".join(commands.keys()))
  302. for cmd in commands:
  303. m.write(cmd + ":\n")
  304. m.write("\t" + commands[cmd] + "\n\n")
  305. m.close()
  306. d.setVar("B", os.path.dirname(makefile))
  307. d.setVar("EXTRA_OEMAKE", "${PARALLEL_MAKE}")
  308. bb.note("Executing binary locale generation makefile")
  309. bb.build.exec_func("oe_runmake", d)
  310. bb.note("collecting binary locales from locale tree")
  311. bb.build.exec_func("do_collect_bins_from_locale_tree", d)
  312. do_split_packages(d, binary_locales_dir, file_regex='(.*)', \
  313. output_pattern=bpn+'-binary-localedata-%s', \
  314. description='binary locale definition for %s', extra_depends='', allow_dirs=True)
  315. elif use_bin == "precompiled":
  316. do_split_packages(d, binary_locales_dir, file_regex='(.*)', \
  317. output_pattern=bpn+'-binary-localedata-%s', \
  318. description='binary locale definition for %s', extra_depends='', allow_dirs=True)
  319. else:
  320. bb.note("generation of binary locales disabled. this may break i18n!")
  321. }
  322. # We want to do this indirection so that we can safely 'return'
  323. # from the called function even though we're prepending
  324. python populate_packages_prepend () {
  325. bb.build.exec_func('package_do_split_gconvs', d)
  326. }