debian.bbclass 5.9 KB

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