debian.bbclass 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. python debian_package_name_hook () {
  19. import glob, copy, stat, errno, re, pathlib, subprocess
  20. pkgdest = d.getVar("PKGDEST")
  21. packages = d.getVar('PACKAGES')
  22. so_re = re.compile(r"lib.*\.so")
  23. def socrunch(s):
  24. s = s.lower().replace('_', '-')
  25. m = re.match(r"^(.*)(.)\.so\.(.*)$", s)
  26. if m is None:
  27. return None
  28. if m.group(2) in '0123456789':
  29. bin = '%s%s-%s' % (m.group(1), m.group(2), m.group(3))
  30. else:
  31. bin = m.group(1) + m.group(2) + m.group(3)
  32. dev = m.group(1) + m.group(2)
  33. return (bin, dev)
  34. def isexec(path):
  35. try:
  36. s = os.stat(path)
  37. except (os.error, AttributeError):
  38. return 0
  39. return (s[stat.ST_MODE] & stat.S_IEXEC)
  40. def add_rprovides(pkg, d):
  41. newpkg = d.getVar('PKG:' + pkg)
  42. if newpkg and newpkg != pkg:
  43. provs = (d.getVar('RPROVIDES:' + pkg) or "").split()
  44. if pkg not in provs:
  45. d.appendVar('RPROVIDES:' + pkg, " " + pkg + " (=" + d.getVar("PKGV") + ")")
  46. def auto_libname(packages, orig_pkg):
  47. p = lambda var: pathlib.PurePath(d.getVar(var))
  48. libdirs = (p("base_libdir"), p("libdir"))
  49. bindirs = (p("base_bindir"), p("base_sbindir"), p("bindir"), p("sbindir"))
  50. sonames = []
  51. has_bins = 0
  52. has_libs = 0
  53. for f in pkgfiles[orig_pkg]:
  54. # This is .../packages-split/orig_pkg/
  55. pkgpath = pathlib.PurePath(pkgdest, orig_pkg)
  56. # Strip pkgpath off the full path to a file in the package, re-root
  57. # so it is absolute, and then get the parent directory of the file.
  58. path = pathlib.PurePath("/") / (pathlib.PurePath(f).relative_to(pkgpath).parent)
  59. if path in bindirs:
  60. has_bins = 1
  61. if path in libdirs:
  62. has_libs = 1
  63. if so_re.match(os.path.basename(f)):
  64. try:
  65. cmd = [d.expand("${TARGET_PREFIX}objdump"), "-p", f]
  66. output = subprocess.check_output(cmd).decode("utf-8")
  67. for m in re.finditer(r"\s+SONAME\s+([^\s]+)", output):
  68. if m.group(1) not in sonames:
  69. sonames.append(m.group(1))
  70. except subprocess.CalledProcessError:
  71. pass
  72. bb.debug(1, 'LIBNAMES: pkg %s libs %d bins %d sonames %s' % (orig_pkg, has_libs, has_bins, sonames))
  73. soname = None
  74. if len(sonames) == 1:
  75. soname = sonames[0]
  76. elif len(sonames) > 1:
  77. lead = d.getVar('LEAD_SONAME')
  78. if lead:
  79. r = re.compile(lead)
  80. filtered = []
  81. for s in sonames:
  82. if r.match(s):
  83. filtered.append(s)
  84. if len(filtered) == 1:
  85. soname = filtered[0]
  86. elif len(filtered) > 1:
  87. bb.note("Multiple matches (%s) for LEAD_SONAME '%s'" % (", ".join(filtered), lead))
  88. else:
  89. bb.note("Multiple libraries (%s) found, but LEAD_SONAME '%s' doesn't match any of them" % (", ".join(sonames), lead))
  90. else:
  91. bb.note("Multiple libraries (%s) found and LEAD_SONAME not defined" % ", ".join(sonames))
  92. if has_libs and not has_bins and soname:
  93. soname_result = socrunch(soname)
  94. if soname_result:
  95. (pkgname, devname) = soname_result
  96. for pkg in packages.split():
  97. if (d.getVar('PKG:' + pkg, False) or d.getVar('DEBIAN_NOAUTONAME:' + pkg, False)):
  98. add_rprovides(pkg, d)
  99. continue
  100. debian_pn = d.getVar('DEBIANNAME:' + pkg, False)
  101. if debian_pn:
  102. newpkg = debian_pn
  103. elif pkg == orig_pkg:
  104. newpkg = pkgname
  105. else:
  106. newpkg = pkg.replace(orig_pkg, devname, 1)
  107. mlpre=d.getVar('MLPREFIX')
  108. if mlpre:
  109. if not newpkg.find(mlpre) == 0:
  110. newpkg = mlpre + newpkg
  111. if newpkg != pkg:
  112. bb.note("debian: renaming %s to %s" % (pkg, newpkg))
  113. d.setVar('PKG:' + pkg, newpkg)
  114. add_rprovides(pkg, d)
  115. else:
  116. add_rprovides(orig_pkg, d)
  117. # reversed sort is needed when some package is substring of another
  118. # ie in ncurses we get without reverse sort:
  119. # DEBUG: LIBNAMES: pkgname libtic5 devname libtic pkg ncurses-libtic orig_pkg ncurses-libtic debian_pn None newpkg libtic5
  120. # and later
  121. # DEBUG: LIBNAMES: pkgname libtic5 devname libtic pkg ncurses-libticw orig_pkg ncurses-libtic debian_pn None newpkg libticw
  122. # so we need to handle ncurses-libticw->libticw5 before ncurses-libtic->libtic5
  123. for pkg in sorted((d.getVar('AUTO_LIBNAME_PKGS') or "").split(), reverse=True):
  124. auto_libname(packages, pkg)
  125. }
  126. EXPORT_FUNCTIONS package_name_hook
  127. DEBIAN_NAMES = "1"