debian.bbclass 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # Debian package renaming only occurs when a package is built
  7. # We therefore have to make sure we build all runtime packages
  8. # before building the current package to make the packages runtime
  9. # depends are correct
  10. #
  11. # Custom library package names can be defined setting
  12. # DEBIANNAME: + pkgname to the desired name.
  13. #
  14. # Better expressed as ensure all RDEPENDS package before we package
  15. # This means we can't have circular RDEPENDS/RRECOMMENDS
  16. AUTO_LIBNAME_PKGS = "${PACKAGES}"
  17. inherit package
  18. DEBIANRDEP = "do_packagedata"
  19. do_package_write_ipk[deptask] = "${DEBIANRDEP}"
  20. do_package_write_deb[deptask] = "${DEBIANRDEP}"
  21. do_package_write_tar[deptask] = "${DEBIANRDEP}"
  22. do_package_write_rpm[deptask] = "${DEBIANRDEP}"
  23. do_package_write_ipk[rdeptask] = "${DEBIANRDEP}"
  24. do_package_write_deb[rdeptask] = "${DEBIANRDEP}"
  25. do_package_write_tar[rdeptask] = "${DEBIANRDEP}"
  26. do_package_write_rpm[rdeptask] = "${DEBIANRDEP}"
  27. python () {
  28. if not d.getVar("PACKAGES"):
  29. d.setVar("DEBIANRDEP", "")
  30. }
  31. python debian_package_name_hook () {
  32. import glob, copy, stat, errno, re, pathlib, subprocess
  33. pkgdest = d.getVar("PKGDEST")
  34. packages = d.getVar('PACKAGES')
  35. so_re = re.compile(r"lib.*\.so")
  36. def socrunch(s):
  37. s = s.lower().replace('_', '-')
  38. m = re.match(r"^(.*)(.)\.so\.(.*)$", s)
  39. if m is None:
  40. return None
  41. if m.group(2) in '0123456789':
  42. bin = '%s%s-%s' % (m.group(1), m.group(2), m.group(3))
  43. else:
  44. bin = m.group(1) + m.group(2) + m.group(3)
  45. dev = m.group(1) + m.group(2)
  46. return (bin, dev)
  47. def isexec(path):
  48. try:
  49. s = os.stat(path)
  50. except (os.error, AttributeError):
  51. return 0
  52. return (s[stat.ST_MODE] & stat.S_IEXEC)
  53. def add_rprovides(pkg, d):
  54. newpkg = d.getVar('PKG:' + pkg)
  55. if newpkg and newpkg != pkg:
  56. provs = (d.getVar('RPROVIDES:' + pkg) or "").split()
  57. if pkg not in provs:
  58. d.appendVar('RPROVIDES:' + pkg, " " + pkg + " (=" + d.getVar("PKGV") + ")")
  59. def auto_libname(packages, orig_pkg):
  60. p = lambda var: pathlib.PurePath(d.getVar(var))
  61. libdirs = (p("base_libdir"), p("libdir"))
  62. bindirs = (p("base_bindir"), p("base_sbindir"), p("bindir"), p("sbindir"))
  63. sonames = []
  64. has_bins = 0
  65. has_libs = 0
  66. for f in pkgfiles[orig_pkg]:
  67. # This is .../packages-split/orig_pkg/
  68. pkgpath = pathlib.PurePath(pkgdest, orig_pkg)
  69. # Strip pkgpath off the full path to a file in the package, re-root
  70. # so it is absolute, and then get the parent directory of the file.
  71. path = pathlib.PurePath("/") / (pathlib.PurePath(f).relative_to(pkgpath).parent)
  72. if path in bindirs:
  73. has_bins = 1
  74. if path in libdirs:
  75. has_libs = 1
  76. if so_re.match(os.path.basename(f)):
  77. try:
  78. cmd = [d.expand("${TARGET_PREFIX}objdump"), "-p", f]
  79. output = subprocess.check_output(cmd).decode("utf-8")
  80. for m in re.finditer(r"\s+SONAME\s+([^\s]+)", output):
  81. if m.group(1) not in sonames:
  82. sonames.append(m.group(1))
  83. except subprocess.CalledProcessError:
  84. pass
  85. bb.debug(1, 'LIBNAMES: pkg %s libs %d bins %d sonames %s' % (orig_pkg, has_libs, has_bins, sonames))
  86. soname = None
  87. if len(sonames) == 1:
  88. soname = sonames[0]
  89. elif len(sonames) > 1:
  90. lead = d.getVar('LEAD_SONAME')
  91. if lead:
  92. r = re.compile(lead)
  93. filtered = []
  94. for s in sonames:
  95. if r.match(s):
  96. filtered.append(s)
  97. if len(filtered) == 1:
  98. soname = filtered[0]
  99. elif len(filtered) > 1:
  100. bb.note("Multiple matches (%s) for LEAD_SONAME '%s'" % (", ".join(filtered), lead))
  101. else:
  102. bb.note("Multiple libraries (%s) found, but LEAD_SONAME '%s' doesn't match any of them" % (", ".join(sonames), lead))
  103. else:
  104. bb.note("Multiple libraries (%s) found and LEAD_SONAME not defined" % ", ".join(sonames))
  105. if has_libs and not has_bins and soname:
  106. soname_result = socrunch(soname)
  107. if soname_result:
  108. (pkgname, devname) = soname_result
  109. for pkg in packages.split():
  110. if (d.getVar('PKG:' + pkg, False) or d.getVar('DEBIAN_NOAUTONAME:' + pkg, False)):
  111. add_rprovides(pkg, d)
  112. continue
  113. debian_pn = d.getVar('DEBIANNAME:' + pkg, False)
  114. if debian_pn:
  115. newpkg = debian_pn
  116. elif pkg == orig_pkg:
  117. newpkg = pkgname
  118. else:
  119. newpkg = pkg.replace(orig_pkg, devname, 1)
  120. mlpre=d.getVar('MLPREFIX')
  121. if mlpre:
  122. if not newpkg.find(mlpre) == 0:
  123. newpkg = mlpre + newpkg
  124. if newpkg != pkg:
  125. bb.note("debian: renaming %s to %s" % (pkg, newpkg))
  126. d.setVar('PKG:' + pkg, newpkg)
  127. add_rprovides(pkg, d)
  128. else:
  129. add_rprovides(orig_pkg, d)
  130. # reversed sort is needed when some package is substring of another
  131. # ie in ncurses we get without reverse sort:
  132. # DEBUG: LIBNAMES: pkgname libtic5 devname libtic pkg ncurses-libtic orig_pkg ncurses-libtic debian_pn None newpkg libtic5
  133. # and later
  134. # DEBUG: LIBNAMES: pkgname libtic5 devname libtic pkg ncurses-libticw orig_pkg ncurses-libtic debian_pn None newpkg libticw
  135. # so we need to handle ncurses-libticw->libticw5 before ncurses-libtic->libtic5
  136. for pkg in sorted((d.getVar('AUTO_LIBNAME_PKGS') or "").split(), reverse=True):
  137. auto_libname(packages, pkg)
  138. }
  139. EXPORT_FUNCTIONS package_name_hook
  140. DEBIAN_NAMES = "1"